]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
add a outside area for borders and focus rects of 3 pixels to a wxWindowDC's clip...
[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, wxID_ANY, 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, wxID_ANY, 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 }
3693
3694
3695 wxGridWindow::~wxGridWindow()
3696 {
3697 }
3698
3699
3700 void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3701 {
3702 wxPaintDC dc( this );
3703 m_owner->PrepareDC( dc );
3704 wxRegion reg = GetUpdateRegion();
3705 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3706 m_owner->DrawGridCellArea( dc , DirtyCells);
3707 #if WXGRID_DRAW_LINES
3708 m_owner->DrawAllGridLines( dc, reg );
3709 #endif
3710 m_owner->DrawGridSpace( dc );
3711 m_owner->DrawHighlight( dc , DirtyCells );
3712 }
3713
3714
3715 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3716 {
3717 wxWindow::ScrollWindow( dx, dy, rect );
3718 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3719 m_colLabelWin->ScrollWindow( dx, 0, rect );
3720 }
3721
3722
3723 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3724 {
3725 m_owner->ProcessGridCellMouseEvent( event );
3726 }
3727
3728 void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3729 {
3730 m_owner->GetEventHandler()->ProcessEvent(event);
3731 }
3732
3733 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3734 // cursor must be in the cell edit control to get key events
3735 //
3736 void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3737 {
3738 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3739 }
3740
3741 void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3742 {
3743 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3744 }
3745
3746 void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
3747 {
3748 }
3749
3750 void wxGridWindow::OnFocus(wxFocusEvent& event)
3751 {
3752 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3753 event.Skip();
3754 }
3755
3756 //////////////////////////////////////////////////////////////////////
3757
3758 // Internal Helper function for computing row or column from some
3759 // (unscrolled) coordinate value, using either
3760 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3761 // of m_rowBottoms/m_ColRights to speed up the search!
3762
3763 // Internal helper macros for simpler use of that function
3764
3765 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
3766 const wxArrayInt& BorderArray, int nMax,
3767 bool clipToMinMax);
3768
3769 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3770 m_minAcceptableColWidth, \
3771 m_colRights, m_numCols, true)
3772 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3773 m_minAcceptableRowHeight, \
3774 m_rowBottoms, m_numRows, true)
3775 /////////////////////////////////////////////////////////////////////
3776
3777 #if wxUSE_EXTENDED_RTTI
3778 WX_DEFINE_FLAGS( wxGridStyle )
3779
3780 wxBEGIN_FLAGS( wxGridStyle )
3781 // new style border flags, we put them first to
3782 // use them for streaming out
3783 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
3784 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
3785 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
3786 wxFLAGS_MEMBER(wxBORDER_RAISED)
3787 wxFLAGS_MEMBER(wxBORDER_STATIC)
3788 wxFLAGS_MEMBER(wxBORDER_NONE)
3789
3790 // old style border flags
3791 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
3792 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
3793 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
3794 wxFLAGS_MEMBER(wxRAISED_BORDER)
3795 wxFLAGS_MEMBER(wxSTATIC_BORDER)
3796 wxFLAGS_MEMBER(wxBORDER)
3797
3798 // standard window styles
3799 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
3800 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
3801 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
3802 wxFLAGS_MEMBER(wxWANTS_CHARS)
3803 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3804 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
3805 wxFLAGS_MEMBER(wxVSCROLL)
3806 wxFLAGS_MEMBER(wxHSCROLL)
3807
3808 wxEND_FLAGS( wxGridStyle )
3809
3810 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
3811
3812 wxBEGIN_PROPERTIES_TABLE(wxGrid)
3813 wxHIDE_PROPERTY( Children )
3814 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3815 wxEND_PROPERTIES_TABLE()
3816
3817 wxBEGIN_HANDLERS_TABLE(wxGrid)
3818 wxEND_HANDLERS_TABLE()
3819
3820 wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
3821
3822 /*
3823 TODO : Expose more information of a list's layout etc. via appropriate objects (à la NotebookPageInfo)
3824 */
3825 #else
3826 IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3827 #endif
3828
3829 BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
3830 EVT_PAINT( wxGrid::OnPaint )
3831 EVT_SIZE( wxGrid::OnSize )
3832 EVT_KEY_DOWN( wxGrid::OnKeyDown )
3833 EVT_KEY_UP( wxGrid::OnKeyUp )
3834 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
3835 END_EVENT_TABLE()
3836
3837 wxGrid::wxGrid()
3838 {
3839 // in order to make sure that a size event is not
3840 // trigerred in a unfinished state
3841 m_cornerLabelWin = NULL ;
3842 m_rowLabelWin = NULL ;
3843 m_colLabelWin = NULL ;
3844 m_gridWin = NULL ;
3845 }
3846
3847 wxGrid::wxGrid( wxWindow *parent,
3848 wxWindowID id,
3849 const wxPoint& pos,
3850 const wxSize& size,
3851 long style,
3852 const wxString& name )
3853 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
3854 m_colMinWidths(GRID_HASH_SIZE),
3855 m_rowMinHeights(GRID_HASH_SIZE)
3856 {
3857 Create();
3858 }
3859
3860 bool wxGrid::Create(wxWindow *parent, wxWindowID id,
3861 const wxPoint& pos, const wxSize& size,
3862 long style, const wxString& name)
3863 {
3864 if (!wxScrolledWindow::Create(parent, id, pos, size,
3865 style | wxWANTS_CHARS , name))
3866 return false;
3867
3868 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE) ;
3869 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE) ;
3870
3871 Create() ;
3872
3873
3874 return true;
3875 }
3876
3877
3878 wxGrid::~wxGrid()
3879 {
3880 // Must do this or ~wxScrollHelper will pop the wrong event handler
3881 SetTargetWindow(this);
3882 ClearAttrCache();
3883 wxSafeDecRef(m_defaultCellAttr);
3884
3885 #ifdef DEBUG_ATTR_CACHE
3886 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3887 wxPrintf(_T("wxGrid attribute cache statistics: "
3888 "total: %u, hits: %u (%u%%)\n"),
3889 total, gs_nAttrCacheHits,
3890 total ? (gs_nAttrCacheHits*100) / total : 0);
3891 #endif
3892
3893 if (m_ownTable)
3894 delete m_table;
3895
3896 delete m_typeRegistry;
3897 delete m_selection;
3898 }
3899
3900
3901 //
3902 // ----- internal init and update functions
3903 //
3904
3905 // NOTE: If using the default visual attributes works everywhere then this can
3906 // be removed as well as the #else cases below.
3907 #define _USE_VISATTR 0
3908
3909 #if _USE_VISATTR
3910 #include "wx/listbox.h"
3911 #endif
3912
3913 void wxGrid::Create()
3914 {
3915 m_created = false; // set to true by CreateGrid
3916
3917 m_table = (wxGridTableBase *) NULL;
3918 m_ownTable = false;
3919
3920 m_cellEditCtrlEnabled = false;
3921
3922 m_defaultCellAttr = new wxGridCellAttr();
3923
3924 // Set default cell attributes
3925 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
3926 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
3927 m_defaultCellAttr->SetFont(GetFont());
3928 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
3929 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3930 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
3931
3932 #if _USE_VISATTR
3933 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
3934 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
3935
3936 m_defaultCellAttr->SetTextColour(gva.colFg);
3937 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
3938
3939 #else
3940 m_defaultCellAttr->SetTextColour(
3941 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3942 m_defaultCellAttr->SetBackgroundColour(
3943 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3944 #endif
3945
3946 m_numRows = 0;
3947 m_numCols = 0;
3948 m_currentCellCoords = wxGridNoCellCoords;
3949
3950 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3951 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3952
3953 // create the type registry
3954 m_typeRegistry = new wxGridTypeRegistry;
3955 m_selection = NULL;
3956
3957 // subwindow components that make up the wxGrid
3958 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3959 wxID_ANY,
3960 wxDefaultPosition,
3961 wxDefaultSize );
3962
3963 m_rowLabelWin = new wxGridRowLabelWindow( this,
3964 wxID_ANY,
3965 wxDefaultPosition,
3966 wxDefaultSize );
3967
3968 m_colLabelWin = new wxGridColLabelWindow( this,
3969 wxID_ANY,
3970 wxDefaultPosition,
3971 wxDefaultSize );
3972
3973 m_gridWin = new wxGridWindow( this,
3974 m_rowLabelWin,
3975 m_colLabelWin,
3976 wxID_ANY,
3977 wxDefaultPosition,
3978 wxDefaultSize );
3979
3980 SetTargetWindow( m_gridWin );
3981
3982 #if _USE_VISATTR
3983 wxColour gfg = gva.colFg;
3984 wxColour gbg = gva.colBg;
3985 wxColour lfg = lva.colFg;
3986 wxColour lbg = lva.colBg;
3987 #else
3988 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
3989 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
3990 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
3991 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
3992 #endif
3993 m_cornerLabelWin->SetOwnForegroundColour(lfg);
3994 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
3995 m_rowLabelWin->SetOwnForegroundColour(lfg);
3996 m_rowLabelWin->SetOwnBackgroundColour(lbg);
3997 m_colLabelWin->SetOwnForegroundColour(lfg);
3998 m_colLabelWin->SetOwnBackgroundColour(lbg);
3999
4000 m_gridWin->SetOwnForegroundColour(gfg);
4001 m_gridWin->SetOwnBackgroundColour(gbg);
4002
4003 Init();
4004 }
4005
4006
4007 bool wxGrid::CreateGrid( int numRows, int numCols,
4008 wxGrid::wxGridSelectionModes selmode )
4009 {
4010 wxCHECK_MSG( !m_created,
4011 false,
4012 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4013
4014 m_numRows = numRows;
4015 m_numCols = numCols;
4016
4017 m_table = new wxGridStringTable( m_numRows, m_numCols );
4018 m_table->SetView( this );
4019 m_ownTable = true;
4020 m_selection = new wxGridSelection( this, selmode );
4021
4022 CalcDimensions();
4023
4024 m_created = true;
4025
4026 return m_created;
4027 }
4028
4029 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
4030 {
4031 wxCHECK_RET( m_created,
4032 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4033
4034 m_selection->SetSelectionMode( selmode );
4035 }
4036
4037 wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4038 {
4039 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
4040 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4041
4042 return m_selection->GetSelectionMode();
4043 }
4044
4045 bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
4046 wxGrid::wxGridSelectionModes selmode )
4047 {
4048 if ( m_created )
4049 {
4050 // stop all processing
4051 m_created = false;
4052
4053 if (m_ownTable)
4054 {
4055 wxGridTableBase *t=m_table;
4056 m_table=0;
4057 delete t;
4058 }
4059 delete m_selection;
4060
4061 m_table=0;
4062 m_selection=0;
4063 m_numRows=0;
4064 m_numCols=0;
4065 }
4066 if (table)
4067 {
4068 m_numRows = table->GetNumberRows();
4069 m_numCols = table->GetNumberCols();
4070
4071 m_table = table;
4072 m_table->SetView( this );
4073 if (takeOwnership)
4074 m_ownTable = true;
4075 m_selection = new wxGridSelection( this, selmode );
4076
4077 CalcDimensions();
4078
4079 m_created = true;
4080 }
4081
4082 return m_created;
4083 }
4084
4085
4086 void wxGrid::Init()
4087 {
4088 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4089 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4090
4091 if ( m_rowLabelWin )
4092 {
4093 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4094 }
4095 else
4096 {
4097 m_labelBackgroundColour = wxColour( _T("WHITE") );
4098 }
4099
4100 m_labelTextColour = wxColour( _T("BLACK") );
4101
4102 // init attr cache
4103 m_attrCache.row = -1;
4104 m_attrCache.col = -1;
4105 m_attrCache.attr = NULL;
4106
4107 // TODO: something better than this ?
4108 //
4109 m_labelFont = this->GetFont();
4110 m_labelFont.SetWeight( wxBOLD );
4111
4112 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4113 m_rowLabelVertAlign = wxALIGN_CENTRE;
4114
4115 m_colLabelHorizAlign = wxALIGN_CENTRE;
4116 m_colLabelVertAlign = wxALIGN_CENTRE;
4117 m_colLabelTextOrientation = wxHORIZONTAL;
4118
4119 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
4120 m_defaultRowHeight = m_gridWin->GetCharHeight();
4121
4122 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4123 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4124
4125 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4126 m_defaultRowHeight += 8;
4127 #else
4128 m_defaultRowHeight += 4;
4129 #endif
4130
4131 m_gridLineColour = wxColour( 192,192,192 );
4132 m_gridLinesEnabled = true;
4133 m_cellHighlightColour = *wxBLACK;
4134 m_cellHighlightPenWidth = 2;
4135 m_cellHighlightROPenWidth = 1;
4136
4137 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
4138 m_winCapture = (wxWindow *)NULL;
4139 m_canDragRowSize = true;
4140 m_canDragColSize = true;
4141 m_canDragGridSize = true;
4142 m_dragLastPos = -1;
4143 m_dragRowOrCol = -1;
4144 m_isDragging = false;
4145 m_startDragPos = wxDefaultPosition;
4146
4147 m_waitForSlowClick = false;
4148
4149 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4150 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4151
4152 m_currentCellCoords = wxGridNoCellCoords;
4153
4154 m_selectingTopLeft = wxGridNoCellCoords;
4155 m_selectingBottomRight = wxGridNoCellCoords;
4156 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4157 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4158
4159 m_editable = true; // default for whole grid
4160
4161 m_inOnKeyDown = false;
4162 m_batchCount = 0;
4163
4164 m_extraWidth =
4165 m_extraHeight = 0;
4166 }
4167
4168 // ----------------------------------------------------------------------------
4169 // the idea is to call these functions only when necessary because they create
4170 // quite big arrays which eat memory mostly unnecessary - in particular, if
4171 // default widths/heights are used for all rows/columns, we may not use these
4172 // arrays at all
4173 //
4174 // with some extra code, it should be possible to only store the
4175 // widths/heights different from default ones but this will be done later...
4176 // ----------------------------------------------------------------------------
4177
4178 void wxGrid::InitRowHeights()
4179 {
4180 m_rowHeights.Empty();
4181 m_rowBottoms.Empty();
4182
4183 m_rowHeights.Alloc( m_numRows );
4184 m_rowBottoms.Alloc( m_numRows );
4185
4186 int rowBottom = 0;
4187
4188 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4189
4190 for ( int i = 0; i < m_numRows; i++ )
4191 {
4192 rowBottom += m_defaultRowHeight;
4193 m_rowBottoms.Add( rowBottom );
4194 }
4195 }
4196
4197 void wxGrid::InitColWidths()
4198 {
4199 m_colWidths.Empty();
4200 m_colRights.Empty();
4201
4202 m_colWidths.Alloc( m_numCols );
4203 m_colRights.Alloc( m_numCols );
4204 int colRight = 0;
4205
4206 m_colWidths.Add( m_defaultColWidth, m_numCols );
4207
4208 for ( int i = 0; i < m_numCols; i++ )
4209 {
4210 colRight += m_defaultColWidth;
4211 m_colRights.Add( colRight );
4212 }
4213 }
4214
4215 int wxGrid::GetColWidth(int col) const
4216 {
4217 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4218 }
4219
4220 int wxGrid::GetColLeft(int col) const
4221 {
4222 return m_colRights.IsEmpty() ? col * m_defaultColWidth
4223 : m_colRights[col] - m_colWidths[col];
4224 }
4225
4226 int wxGrid::GetColRight(int col) const
4227 {
4228 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
4229 : m_colRights[col];
4230 }
4231
4232 int wxGrid::GetRowHeight(int row) const
4233 {
4234 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4235 }
4236
4237 int wxGrid::GetRowTop(int row) const
4238 {
4239 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4240 : m_rowBottoms[row] - m_rowHeights[row];
4241 }
4242
4243 int wxGrid::GetRowBottom(int row) const
4244 {
4245 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4246 : m_rowBottoms[row];
4247 }
4248
4249 void wxGrid::CalcDimensions()
4250 {
4251 int cw, ch;
4252 GetClientSize( &cw, &ch );
4253
4254 if ( m_rowLabelWin->IsShown() )
4255 cw -= m_rowLabelWidth;
4256 if ( m_colLabelWin->IsShown() )
4257 ch -= m_colLabelHeight;
4258
4259 // grid total size
4260 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
4261 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
4262
4263 // take into account editor if shown
4264 if( IsCellEditControlShown() )
4265 {
4266 int w2, h2;
4267 int r = m_currentCellCoords.GetRow();
4268 int c = m_currentCellCoords.GetCol();
4269 int x = GetColLeft(c);
4270 int y = GetRowTop(r);
4271
4272 // how big is the editor
4273 wxGridCellAttr* attr = GetCellAttr(r, c);
4274 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4275 editor->GetControl()->GetSize(&w2, &h2);
4276 w2 += x;
4277 h2 += y;
4278 if( w2 > w ) w = w2;
4279 if( h2 > h ) h = h2;
4280 editor->DecRef();
4281 attr->DecRef();
4282 }
4283
4284 // preserve (more or less) the previous position
4285 int x, y;
4286 GetViewStart( &x, &y );
4287
4288 // ensure the position is valid for the new scroll ranges
4289 if ( x >= w )
4290 x = wxMax( w - 1, 0 );
4291 if ( y >= h )
4292 y = wxMax( h - 1, 0 );
4293
4294 // do set scrollbar parameters
4295 SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
4296 GetScrollX(w), GetScrollY(h), x, y,
4297 GetBatchCount() != 0);
4298
4299 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4300 // still must reposition the children
4301 CalcWindowSizes();
4302 }
4303
4304
4305 void wxGrid::CalcWindowSizes()
4306 {
4307 // escape if the window is has not been fully created yet
4308
4309 if ( m_cornerLabelWin == NULL )
4310 return ;
4311
4312 int cw, ch;
4313 GetClientSize( &cw, &ch );
4314
4315 if ( m_cornerLabelWin->IsShown() )
4316 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4317
4318 if ( m_colLabelWin->IsShown() )
4319 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
4320
4321 if ( m_rowLabelWin->IsShown() )
4322 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
4323
4324 if ( m_gridWin->IsShown() )
4325 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
4326 }
4327
4328
4329 // this is called when the grid table sends a message to say that it
4330 // has been redimensioned
4331 //
4332 bool wxGrid::Redimension( wxGridTableMessage& msg )
4333 {
4334 int i;
4335 bool result = false;
4336
4337 // Clear the attribute cache as the attribute might refer to a different
4338 // cell than stored in the cache after adding/removing rows/columns.
4339 ClearAttrCache();
4340 // By the same reasoning, the editor should be dismissed if columns are
4341 // added or removed. And for consistency, it should IMHO always be
4342 // removed, not only if the cell "underneath" it actually changes.
4343 // For now, I intentionally do not save the editor's content as the
4344 // cell it might want to save that stuff to might no longer exist.
4345 HideCellEditControl();
4346 #if 0
4347 // if we were using the default widths/heights so far, we must change them
4348 // now
4349 if ( m_colWidths.IsEmpty() )
4350 {
4351 InitColWidths();
4352 }
4353
4354 if ( m_rowHeights.IsEmpty() )
4355 {
4356 InitRowHeights();
4357 }
4358 #endif
4359
4360 switch ( msg.GetId() )
4361 {
4362 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4363 {
4364 size_t pos = msg.GetCommandInt();
4365 int numRows = msg.GetCommandInt2();
4366
4367 m_numRows += numRows;
4368
4369 if ( !m_rowHeights.IsEmpty() )
4370 {
4371 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4372 m_rowBottoms.Insert( 0, pos, numRows );
4373
4374 int bottom = 0;
4375 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
4376
4377 for ( i = pos; i < m_numRows; i++ )
4378 {
4379 bottom += m_rowHeights[i];
4380 m_rowBottoms[i] = bottom;
4381 }
4382 }
4383 if ( m_currentCellCoords == wxGridNoCellCoords )
4384 {
4385 // if we have just inserted cols into an empty grid the current
4386 // cell will be undefined...
4387 //
4388 SetCurrentCell( 0, 0 );
4389 }
4390
4391 if ( m_selection )
4392 m_selection->UpdateRows( pos, numRows );
4393 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4394 if (attrProvider)
4395 attrProvider->UpdateAttrRows( pos, numRows );
4396
4397 if ( !GetBatchCount() )
4398 {
4399 CalcDimensions();
4400 m_rowLabelWin->Refresh();
4401 }
4402 }
4403 result = true;
4404 break;
4405
4406 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4407 {
4408 int numRows = msg.GetCommandInt();
4409 int oldNumRows = m_numRows;
4410 m_numRows += numRows;
4411
4412 if ( !m_rowHeights.IsEmpty() )
4413 {
4414 m_rowHeights.Add( m_defaultRowHeight, numRows );
4415 m_rowBottoms.Add( 0, numRows );
4416
4417 int bottom = 0;
4418 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
4419
4420 for ( i = oldNumRows; i < m_numRows; i++ )
4421 {
4422 bottom += m_rowHeights[i];
4423 m_rowBottoms[i] = bottom;
4424 }
4425 }
4426 if ( m_currentCellCoords == wxGridNoCellCoords )
4427 {
4428 // if we have just inserted cols into an empty grid the current
4429 // cell will be undefined...
4430 //
4431 SetCurrentCell( 0, 0 );
4432 }
4433 if ( !GetBatchCount() )
4434 {
4435 CalcDimensions();
4436 m_rowLabelWin->Refresh();
4437 }
4438 }
4439 result = true;
4440 break;
4441
4442 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4443 {
4444 size_t pos = msg.GetCommandInt();
4445 int numRows = msg.GetCommandInt2();
4446 m_numRows -= numRows;
4447
4448 if ( !m_rowHeights.IsEmpty() )
4449 {
4450 m_rowHeights.RemoveAt( pos, numRows );
4451 m_rowBottoms.RemoveAt( pos, numRows );
4452
4453 int h = 0;
4454 for ( i = 0; i < m_numRows; i++ )
4455 {
4456 h += m_rowHeights[i];
4457 m_rowBottoms[i] = h;
4458 }
4459 }
4460 if ( !m_numRows )
4461 {
4462 m_currentCellCoords = wxGridNoCellCoords;
4463 }
4464 else
4465 {
4466 if ( m_currentCellCoords.GetRow() >= m_numRows )
4467 m_currentCellCoords.Set( 0, 0 );
4468 }
4469
4470 if ( m_selection )
4471 m_selection->UpdateRows( pos, -((int)numRows) );
4472 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4473 if (attrProvider) {
4474 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4475 // ifdef'd out following patch from Paul Gammans
4476 #if 0
4477 // No need to touch column attributes, unless we
4478 // removed _all_ rows, in this case, we remove
4479 // all column attributes.
4480 // I hate to do this here, but the
4481 // needed data is not available inside UpdateAttrRows.
4482 if ( !GetNumberRows() )
4483 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4484 #endif
4485 }
4486 if ( !GetBatchCount() )
4487 {
4488 CalcDimensions();
4489 m_rowLabelWin->Refresh();
4490 }
4491 }
4492 result = true;
4493 break;
4494
4495 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4496 {
4497 size_t pos = msg.GetCommandInt();
4498 int numCols = msg.GetCommandInt2();
4499 m_numCols += numCols;
4500
4501 if ( !m_colWidths.IsEmpty() )
4502 {
4503 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4504 m_colRights.Insert( 0, pos, numCols );
4505
4506 int right = 0;
4507 if ( pos > 0 ) right = m_colRights[pos-1];
4508
4509 for ( i = pos; i < m_numCols; i++ )
4510 {
4511 right += m_colWidths[i];
4512 m_colRights[i] = right;
4513 }
4514 }
4515 if ( m_currentCellCoords == wxGridNoCellCoords )
4516 {
4517 // if we have just inserted cols into an empty grid the current
4518 // cell will be undefined...
4519 //
4520 SetCurrentCell( 0, 0 );
4521 }
4522
4523 if ( m_selection )
4524 m_selection->UpdateCols( pos, numCols );
4525 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4526 if (attrProvider)
4527 attrProvider->UpdateAttrCols( pos, numCols );
4528 if ( !GetBatchCount() )
4529 {
4530 CalcDimensions();
4531 m_colLabelWin->Refresh();
4532 }
4533
4534 }
4535 result = true;
4536 break;
4537
4538 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4539 {
4540 int numCols = msg.GetCommandInt();
4541 int oldNumCols = m_numCols;
4542 m_numCols += numCols;
4543 if ( !m_colWidths.IsEmpty() )
4544 {
4545 m_colWidths.Add( m_defaultColWidth, numCols );
4546 m_colRights.Add( 0, numCols );
4547
4548 int right = 0;
4549 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
4550
4551 for ( i = oldNumCols; i < m_numCols; i++ )
4552 {
4553 right += m_colWidths[i];
4554 m_colRights[i] = right;
4555 }
4556 }
4557 if ( m_currentCellCoords == wxGridNoCellCoords )
4558 {
4559 // if we have just inserted cols into an empty grid the current
4560 // cell will be undefined...
4561 //
4562 SetCurrentCell( 0, 0 );
4563 }
4564 if ( !GetBatchCount() )
4565 {
4566 CalcDimensions();
4567 m_colLabelWin->Refresh();
4568 }
4569 }
4570 result = true;
4571 break;
4572
4573 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4574 {
4575 size_t pos = msg.GetCommandInt();
4576 int numCols = msg.GetCommandInt2();
4577 m_numCols -= numCols;
4578
4579 if ( !m_colWidths.IsEmpty() )
4580 {
4581 m_colWidths.RemoveAt( pos, numCols );
4582 m_colRights.RemoveAt( pos, numCols );
4583
4584 int w = 0;
4585 for ( i = 0; i < m_numCols; i++ )
4586 {
4587 w += m_colWidths[i];
4588 m_colRights[i] = w;
4589 }
4590 }
4591 if ( !m_numCols )
4592 {
4593 m_currentCellCoords = wxGridNoCellCoords;
4594 }
4595 else
4596 {
4597 if ( m_currentCellCoords.GetCol() >= m_numCols )
4598 m_currentCellCoords.Set( 0, 0 );
4599 }
4600
4601 if ( m_selection )
4602 m_selection->UpdateCols( pos, -((int)numCols) );
4603 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4604 if (attrProvider) {
4605 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
4606 // ifdef'd out following patch from Paul Gammans
4607 #if 0
4608 // No need to touch row attributes, unless we
4609 // removed _all_ columns, in this case, we remove
4610 // all row attributes.
4611 // I hate to do this here, but the
4612 // needed data is not available inside UpdateAttrCols.
4613 if ( !GetNumberCols() )
4614 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
4615 #endif
4616 }
4617 if ( !GetBatchCount() )
4618 {
4619 CalcDimensions();
4620 m_colLabelWin->Refresh();
4621 }
4622 }
4623 result = true;
4624 break;
4625 }
4626
4627 if (result && !GetBatchCount() )
4628 m_gridWin->Refresh();
4629 return result;
4630 }
4631
4632
4633 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
4634 {
4635 wxRegionIterator iter( reg );
4636 wxRect r;
4637
4638 wxArrayInt rowlabels;
4639
4640 int top, bottom;
4641 while ( iter )
4642 {
4643 r = iter.GetRect();
4644
4645 // TODO: remove this when we can...
4646 // There is a bug in wxMotif that gives garbage update
4647 // rectangles if you jump-scroll a long way by clicking the
4648 // scrollbar with middle button. This is a work-around
4649 //
4650 #if defined(__WXMOTIF__)
4651 int cw, ch;
4652 m_gridWin->GetClientSize( &cw, &ch );
4653 if ( r.GetTop() > ch ) r.SetTop( 0 );
4654 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4655 #endif
4656
4657 // logical bounds of update region
4658 //
4659 int dummy;
4660 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4661 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4662
4663 // find the row labels within these bounds
4664 //
4665 int row;
4666 for ( row = internalYToRow(top); row < m_numRows; row++ )
4667 {
4668 if ( GetRowBottom(row) < top )
4669 continue;
4670
4671 if ( GetRowTop(row) > bottom )
4672 break;
4673
4674 rowlabels.Add( row );
4675 }
4676
4677 iter++ ;
4678 }
4679
4680 return rowlabels;
4681 }
4682
4683
4684 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
4685 {
4686 wxRegionIterator iter( reg );
4687 wxRect r;
4688
4689 wxArrayInt colLabels;
4690
4691 int left, right;
4692 while ( iter )
4693 {
4694 r = iter.GetRect();
4695
4696 // TODO: remove this when we can...
4697 // There is a bug in wxMotif that gives garbage update
4698 // rectangles if you jump-scroll a long way by clicking the
4699 // scrollbar with middle button. This is a work-around
4700 //
4701 #if defined(__WXMOTIF__)
4702 int cw, ch;
4703 m_gridWin->GetClientSize( &cw, &ch );
4704 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4705 r.SetRight( wxMin( r.GetRight(), cw ) );
4706 #endif
4707
4708 // logical bounds of update region
4709 //
4710 int dummy;
4711 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4712 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4713
4714 // find the cells within these bounds
4715 //
4716 int col;
4717 for ( col = internalXToCol(left); col < m_numCols; col++ )
4718 {
4719 if ( GetColRight(col) < left )
4720 continue;
4721
4722 if ( GetColLeft(col) > right )
4723 break;
4724
4725 colLabels.Add( col );
4726 }
4727
4728 iter++ ;
4729 }
4730 return colLabels;
4731 }
4732
4733
4734 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
4735 {
4736 wxRegionIterator iter( reg );
4737 wxRect r;
4738
4739 wxGridCellCoordsArray cellsExposed;
4740
4741 int left, top, right, bottom;
4742 while ( iter )
4743 {
4744 r = iter.GetRect();
4745
4746 // TODO: remove this when we can...
4747 // There is a bug in wxMotif that gives garbage update
4748 // rectangles if you jump-scroll a long way by clicking the
4749 // scrollbar with middle button. This is a work-around
4750 //
4751 #if defined(__WXMOTIF__)
4752 int cw, ch;
4753 m_gridWin->GetClientSize( &cw, &ch );
4754 if ( r.GetTop() > ch ) r.SetTop( 0 );
4755 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4756 r.SetRight( wxMin( r.GetRight(), cw ) );
4757 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4758 #endif
4759
4760 // logical bounds of update region
4761 //
4762 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4763 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
4764
4765 // find the cells within these bounds
4766 //
4767 int row, col;
4768 for ( row = internalYToRow(top); row < m_numRows; row++ )
4769 {
4770 if ( GetRowBottom(row) <= top )
4771 continue;
4772
4773 if ( GetRowTop(row) > bottom )
4774 break;
4775
4776 for ( col = internalXToCol(left); col < m_numCols; col++ )
4777 {
4778 if ( GetColRight(col) <= left )
4779 continue;
4780
4781 if ( GetColLeft(col) > right )
4782 break;
4783
4784 cellsExposed.Add( wxGridCellCoords( row, col ) );
4785 }
4786 }
4787
4788 iter++;
4789 }
4790
4791 return cellsExposed;
4792 }
4793
4794
4795 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
4796 {
4797 int x, y, row;
4798 wxPoint pos( event.GetPosition() );
4799 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4800
4801 if ( event.Dragging() )
4802 {
4803 if (!m_isDragging)
4804 {
4805 m_isDragging = true;
4806 m_rowLabelWin->CaptureMouse();
4807 }
4808
4809 if ( event.LeftIsDown() )
4810 {
4811 switch( m_cursorMode )
4812 {
4813 case WXGRID_CURSOR_RESIZE_ROW:
4814 {
4815 int cw, ch, left, dummy;
4816 m_gridWin->GetClientSize( &cw, &ch );
4817 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4818
4819 wxClientDC dc( m_gridWin );
4820 PrepareDC( dc );
4821 y = wxMax( y,
4822 GetRowTop(m_dragRowOrCol) +
4823 GetRowMinimalHeight(m_dragRowOrCol) );
4824 dc.SetLogicalFunction(wxINVERT);
4825 if ( m_dragLastPos >= 0 )
4826 {
4827 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4828 }
4829 dc.DrawLine( left, y, left+cw, y );
4830 m_dragLastPos = y;
4831 }
4832 break;
4833
4834 case WXGRID_CURSOR_SELECT_ROW:
4835 if ( (row = YToRow( y )) >= 0 )
4836 {
4837 if ( m_selection )
4838 {
4839 m_selection->SelectRow( row,
4840 event.ControlDown(),
4841 event.ShiftDown(),
4842 event.AltDown(),
4843 event.MetaDown() );
4844 }
4845 }
4846
4847 // default label to suppress warnings about "enumeration value
4848 // 'xxx' not handled in switch
4849 default:
4850 break;
4851 }
4852 }
4853 return;
4854 }
4855
4856 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4857 return;
4858
4859 if (m_isDragging)
4860 {
4861 if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse();
4862 m_isDragging = false;
4863 }
4864
4865 // ------------ Entering or leaving the window
4866 //
4867 if ( event.Entering() || event.Leaving() )
4868 {
4869 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4870 }
4871
4872
4873 // ------------ Left button pressed
4874 //
4875 else if ( event.LeftDown() )
4876 {
4877 // don't send a label click event for a hit on the
4878 // edge of the row label - this is probably the user
4879 // wanting to resize the row
4880 //
4881 if ( YToEdgeOfRow(y) < 0 )
4882 {
4883 row = YToRow(y);
4884 if ( row >= 0 &&
4885 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
4886 {
4887 if ( !event.ShiftDown() && !event.ControlDown() )
4888 ClearSelection();
4889 if ( m_selection )
4890 {
4891 if ( event.ShiftDown() )
4892 {
4893 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4894 0,
4895 row,
4896 GetNumberCols() - 1,
4897 event.ControlDown(),
4898 event.ShiftDown(),
4899 event.AltDown(),
4900 event.MetaDown() );
4901 }
4902 else
4903 {
4904 m_selection->SelectRow( row,
4905 event.ControlDown(),
4906 event.ShiftDown(),
4907 event.AltDown(),
4908 event.MetaDown() );
4909 }
4910 }
4911
4912 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
4913 }
4914 }
4915 else
4916 {
4917 // starting to drag-resize a row
4918 //
4919 if ( CanDragRowSize() )
4920 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
4921 }
4922 }
4923
4924
4925 // ------------ Left double click
4926 //
4927 else if (event.LeftDClick() )
4928 {
4929 int row = YToEdgeOfRow(y);
4930 if ( row < 0 )
4931 {
4932 row = YToRow(y);
4933 if ( row >=0 &&
4934 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
4935 {
4936 // no default action at the moment
4937 }
4938 }
4939 else
4940 {
4941 // adjust row height depending on label text
4942 AutoSizeRowLabelSize( row );
4943
4944 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
4945 m_dragLastPos = -1;
4946 }
4947 }
4948
4949
4950 // ------------ Left button released
4951 //
4952 else if ( event.LeftUp() )
4953 {
4954 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4955 {
4956 DoEndDragResizeRow();
4957
4958 // Note: we are ending the event *after* doing
4959 // default processing in this case
4960 //
4961 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4962 }
4963
4964 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4965 m_dragLastPos = -1;
4966 }
4967
4968
4969 // ------------ Right button down
4970 //
4971 else if ( event.RightDown() )
4972 {
4973 row = YToRow(y);
4974 if ( row >=0 &&
4975 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
4976 {
4977 // no default action at the moment
4978 }
4979 }
4980
4981
4982 // ------------ Right double click
4983 //
4984 else if ( event.RightDClick() )
4985 {
4986 row = YToRow(y);
4987 if ( row >= 0 &&
4988 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
4989 {
4990 // no default action at the moment
4991 }
4992 }
4993
4994
4995 // ------------ No buttons down and mouse moving
4996 //
4997 else if ( event.Moving() )
4998 {
4999 m_dragRowOrCol = YToEdgeOfRow( y );
5000 if ( m_dragRowOrCol >= 0 )
5001 {
5002 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5003 {
5004 // don't capture the mouse yet
5005 if ( CanDragRowSize() )
5006 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
5007 }
5008 }
5009 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5010 {
5011 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
5012 }
5013 }
5014 }
5015
5016
5017 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5018 {
5019 int x, y, col;
5020 wxPoint pos( event.GetPosition() );
5021 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5022
5023 if ( event.Dragging() )
5024 {
5025 if (!m_isDragging)
5026 {
5027 m_isDragging = true;
5028 m_colLabelWin->CaptureMouse();
5029 }
5030
5031 if ( event.LeftIsDown() )
5032 {
5033 switch( m_cursorMode )
5034 {
5035 case WXGRID_CURSOR_RESIZE_COL:
5036 {
5037 int cw, ch, dummy, top;
5038 m_gridWin->GetClientSize( &cw, &ch );
5039 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5040
5041 wxClientDC dc( m_gridWin );
5042 PrepareDC( dc );
5043
5044 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5045 GetColMinimalWidth(m_dragRowOrCol));
5046 dc.SetLogicalFunction(wxINVERT);
5047 if ( m_dragLastPos >= 0 )
5048 {
5049 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5050 }
5051 dc.DrawLine( x, top, x, top+ch );
5052 m_dragLastPos = x;
5053 }
5054 break;
5055
5056 case WXGRID_CURSOR_SELECT_COL:
5057 if ( (col = XToCol( x )) >= 0 )
5058 {
5059 if ( m_selection )
5060 {
5061 m_selection->SelectCol( col,
5062 event.ControlDown(),
5063 event.ShiftDown(),
5064 event.AltDown(),
5065 event.MetaDown() );
5066 }
5067 }
5068
5069 // default label to suppress warnings about "enumeration value
5070 // 'xxx' not handled in switch
5071 default:
5072 break;
5073 }
5074 }
5075 return;
5076 }
5077
5078 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5079 return;
5080
5081 if (m_isDragging)
5082 {
5083 if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse();
5084 m_isDragging = false;
5085 }
5086
5087 // ------------ Entering or leaving the window
5088 //
5089 if ( event.Entering() || event.Leaving() )
5090 {
5091 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5092 }
5093
5094
5095 // ------------ Left button pressed
5096 //
5097 else if ( event.LeftDown() )
5098 {
5099 // don't send a label click event for a hit on the
5100 // edge of the col label - this is probably the user
5101 // wanting to resize the col
5102 //
5103 if ( XToEdgeOfCol(x) < 0 )
5104 {
5105 col = XToCol(x);
5106 if ( col >= 0 &&
5107 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
5108 {
5109 if ( !event.ShiftDown() && !event.ControlDown() )
5110 ClearSelection();
5111 if ( m_selection )
5112 {
5113 if ( event.ShiftDown() )
5114 {
5115 m_selection->SelectBlock( 0,
5116 m_currentCellCoords.GetCol(),
5117 GetNumberRows() - 1, col,
5118 event.ControlDown(),
5119 event.ShiftDown(),
5120 event.AltDown(),
5121 event.MetaDown() );
5122 }
5123 else
5124 {
5125 m_selection->SelectCol( col,
5126 event.ControlDown(),
5127 event.ShiftDown(),
5128 event.AltDown(),
5129 event.MetaDown() );
5130 }
5131 }
5132
5133 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5134 }
5135 }
5136 else
5137 {
5138 // starting to drag-resize a col
5139 //
5140 if ( CanDragColSize() )
5141 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
5142 }
5143 }
5144
5145
5146 // ------------ Left double click
5147 //
5148 if ( event.LeftDClick() )
5149 {
5150 int col = XToEdgeOfCol(x);
5151 if ( col < 0 )
5152 {
5153 col = XToCol(x);
5154 if ( col >= 0 &&
5155 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5156 {
5157 // no default action at the moment
5158 }
5159 }
5160 else
5161 {
5162 // adjust column width depending on label text
5163 AutoSizeColLabelSize( col );
5164
5165 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5166 m_dragLastPos = -1;
5167 }
5168 }
5169
5170
5171 // ------------ Left button released
5172 //
5173 else if ( event.LeftUp() )
5174 {
5175 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5176 {
5177 DoEndDragResizeCol();
5178
5179 // Note: we are ending the event *after* doing
5180 // default processing in this case
5181 //
5182 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5183 }
5184
5185 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5186 m_dragLastPos = -1;
5187 }
5188
5189
5190 // ------------ Right button down
5191 //
5192 else if ( event.RightDown() )
5193 {
5194 col = XToCol(x);
5195 if ( col >= 0 &&
5196 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
5197 {
5198 // no default action at the moment
5199 }
5200 }
5201
5202
5203 // ------------ Right double click
5204 //
5205 else if ( event.RightDClick() )
5206 {
5207 col = XToCol(x);
5208 if ( col >= 0 &&
5209 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
5210 {
5211 // no default action at the moment
5212 }
5213 }
5214
5215
5216 // ------------ No buttons down and mouse moving
5217 //
5218 else if ( event.Moving() )
5219 {
5220 m_dragRowOrCol = XToEdgeOfCol( x );
5221 if ( m_dragRowOrCol >= 0 )
5222 {
5223 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5224 {
5225 // don't capture the cursor yet
5226 if ( CanDragColSize() )
5227 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
5228 }
5229 }
5230 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5231 {
5232 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
5233 }
5234 }
5235 }
5236
5237
5238 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
5239 {
5240 if ( event.LeftDown() )
5241 {
5242 // indicate corner label by having both row and
5243 // col args == -1
5244 //
5245 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
5246 {
5247 SelectAll();
5248 }
5249 }
5250
5251 else if ( event.LeftDClick() )
5252 {
5253 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
5254 }
5255
5256 else if ( event.RightDown() )
5257 {
5258 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
5259 {
5260 // no default action at the moment
5261 }
5262 }
5263
5264 else if ( event.RightDClick() )
5265 {
5266 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
5267 {
5268 // no default action at the moment
5269 }
5270 }
5271 }
5272
5273 void wxGrid::ChangeCursorMode(CursorMode mode,
5274 wxWindow *win,
5275 bool captureMouse)
5276 {
5277 #ifdef __WXDEBUG__
5278 static const wxChar *cursorModes[] =
5279 {
5280 _T("SELECT_CELL"),
5281 _T("RESIZE_ROW"),
5282 _T("RESIZE_COL"),
5283 _T("SELECT_ROW"),
5284 _T("SELECT_COL")
5285 };
5286
5287 wxLogTrace(_T("grid"),
5288 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5289 win == m_colLabelWin ? _T("colLabelWin")
5290 : win ? _T("rowLabelWin")
5291 : _T("gridWin"),
5292 cursorModes[m_cursorMode], cursorModes[mode]);
5293 #endif // __WXDEBUG__
5294
5295 if ( mode == m_cursorMode &&
5296 win == m_winCapture &&
5297 captureMouse == (m_winCapture != NULL))
5298 return;
5299
5300 if ( !win )
5301 {
5302 // by default use the grid itself
5303 win = m_gridWin;
5304 }
5305
5306 if ( m_winCapture )
5307 {
5308 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5309 m_winCapture = (wxWindow *)NULL;
5310 }
5311
5312 m_cursorMode = mode;
5313
5314 switch ( m_cursorMode )
5315 {
5316 case WXGRID_CURSOR_RESIZE_ROW:
5317 win->SetCursor( m_rowResizeCursor );
5318 break;
5319
5320 case WXGRID_CURSOR_RESIZE_COL:
5321 win->SetCursor( m_colResizeCursor );
5322 break;
5323
5324 default:
5325 win->SetCursor( *wxSTANDARD_CURSOR );
5326 }
5327
5328 // we need to capture mouse when resizing
5329 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5330 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5331
5332 if ( captureMouse && resize )
5333 {
5334 win->CaptureMouse();
5335 m_winCapture = win;
5336 }
5337 }
5338
5339 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5340 {
5341 int x, y;
5342 wxPoint pos( event.GetPosition() );
5343 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5344
5345 wxGridCellCoords coords;
5346 XYToCell( x, y, coords );
5347
5348 int cell_rows, cell_cols;
5349 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5350 if ((cell_rows < 0) || (cell_cols < 0))
5351 {
5352 coords.SetRow(coords.GetRow() + cell_rows);
5353 coords.SetCol(coords.GetCol() + cell_cols);
5354 }
5355
5356 if ( event.Dragging() )
5357 {
5358 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5359
5360 // Don't start doing anything until the mouse has been drug at
5361 // least 3 pixels in any direction...
5362 if (! m_isDragging)
5363 {
5364 if (m_startDragPos == wxDefaultPosition)
5365 {
5366 m_startDragPos = pos;
5367 return;
5368 }
5369 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5370 return;
5371 }
5372
5373 m_isDragging = true;
5374 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5375 {
5376 // Hide the edit control, so it
5377 // won't interfer with drag-shrinking.
5378 if ( IsCellEditControlShown() )
5379 {
5380 HideCellEditControl();
5381 SaveEditControlValue();
5382 }
5383
5384 // Have we captured the mouse yet?
5385 if (! m_winCapture)
5386 {
5387 m_winCapture = m_gridWin;
5388 m_winCapture->CaptureMouse();
5389 }
5390
5391 if ( coords != wxGridNoCellCoords )
5392 {
5393 if ( event.ControlDown() )
5394 {
5395 if ( m_selectingKeyboard == wxGridNoCellCoords)
5396 m_selectingKeyboard = coords;
5397 HighlightBlock ( m_selectingKeyboard, coords );
5398 }
5399 else
5400 {
5401 if ( !IsSelection() )
5402 {
5403 HighlightBlock( coords, coords );
5404 }
5405 else
5406 {
5407 HighlightBlock( m_currentCellCoords, coords );
5408 }
5409 }
5410
5411 if (! IsVisible(coords))
5412 {
5413 MakeCellVisible(coords);
5414 // TODO: need to introduce a delay or something here. The
5415 // scrolling is way to fast, at least on MSW - also on GTK.
5416 }
5417 }
5418 }
5419 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5420 {
5421 int cw, ch, left, dummy;
5422 m_gridWin->GetClientSize( &cw, &ch );
5423 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5424
5425 wxClientDC dc( m_gridWin );
5426 PrepareDC( dc );
5427 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5428 GetRowMinimalHeight(m_dragRowOrCol) );
5429 dc.SetLogicalFunction(wxINVERT);
5430 if ( m_dragLastPos >= 0 )
5431 {
5432 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5433 }
5434 dc.DrawLine( left, y, left+cw, y );
5435 m_dragLastPos = y;
5436 }
5437 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5438 {
5439 int cw, ch, dummy, top;
5440 m_gridWin->GetClientSize( &cw, &ch );
5441 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5442
5443 wxClientDC dc( m_gridWin );
5444 PrepareDC( dc );
5445 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5446 GetColMinimalWidth(m_dragRowOrCol) );
5447 dc.SetLogicalFunction(wxINVERT);
5448 if ( m_dragLastPos >= 0 )
5449 {
5450 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5451 }
5452 dc.DrawLine( x, top, x, top+ch );
5453 m_dragLastPos = x;
5454 }
5455
5456 return;
5457 }
5458
5459 m_isDragging = false;
5460 m_startDragPos = wxDefaultPosition;
5461
5462 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5463 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5464 // wxGTK
5465 #if 0
5466 if ( event.Entering() || event.Leaving() )
5467 {
5468 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5469 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5470 }
5471 else
5472 #endif // 0
5473
5474 // ------------ Left button pressed
5475 //
5476 if ( event.LeftDown() && coords != wxGridNoCellCoords )
5477 {
5478 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5479 coords.GetRow(),
5480 coords.GetCol(),
5481 event ) )
5482 {
5483 if ( !event.ControlDown() )
5484 ClearSelection();
5485 if ( event.ShiftDown() )
5486 {
5487 if ( m_selection )
5488 {
5489 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5490 m_currentCellCoords.GetCol(),
5491 coords.GetRow(),
5492 coords.GetCol(),
5493 event.ControlDown(),
5494 event.ShiftDown(),
5495 event.AltDown(),
5496 event.MetaDown() );
5497 }
5498 }
5499 else if ( XToEdgeOfCol(x) < 0 &&
5500 YToEdgeOfRow(y) < 0 )
5501 {
5502 DisableCellEditControl();
5503 MakeCellVisible( coords );
5504
5505 if ( event.ControlDown() )
5506 {
5507 if ( m_selection )
5508 {
5509 m_selection->ToggleCellSelection( coords.GetRow(),
5510 coords.GetCol(),
5511 event.ControlDown(),
5512 event.ShiftDown(),
5513 event.AltDown(),
5514 event.MetaDown() );
5515 }
5516 m_selectingTopLeft = wxGridNoCellCoords;
5517 m_selectingBottomRight = wxGridNoCellCoords;
5518 m_selectingKeyboard = coords;
5519 }
5520 else
5521 {
5522 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5523 SetCurrentCell( coords );
5524 if ( m_selection )
5525 {
5526 if ( m_selection->GetSelectionMode() !=
5527 wxGrid::wxGridSelectCells )
5528 {
5529 HighlightBlock( coords, coords );
5530 }
5531 }
5532 }
5533 }
5534 }
5535 }
5536
5537
5538 // ------------ Left double click
5539 //
5540 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5541 {
5542 DisableCellEditControl();
5543
5544 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
5545 {
5546 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5547 coords.GetRow(),
5548 coords.GetCol(),
5549 event ) )
5550 {
5551 // we want double click to select a cell and start editing
5552 // (i.e. to behave in same way as sequence of two slow clicks):
5553 m_waitForSlowClick = true;
5554 }
5555 }
5556
5557 }
5558
5559
5560 // ------------ Left button released
5561 //
5562 else if ( event.LeftUp() )
5563 {
5564 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5565 {
5566 if (m_winCapture)
5567 {
5568 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5569 m_winCapture = NULL;
5570 }
5571
5572 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl())
5573 {
5574 ClearSelection();
5575 EnableCellEditControl();
5576
5577 wxGridCellAttr* attr = GetCellAttr(coords);
5578 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
5579 editor->StartingClick();
5580 editor->DecRef();
5581 attr->DecRef();
5582
5583 m_waitForSlowClick = false;
5584 }
5585 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
5586 m_selectingBottomRight != wxGridNoCellCoords )
5587 {
5588 if ( m_selection )
5589 {
5590 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5591 m_selectingTopLeft.GetCol(),
5592 m_selectingBottomRight.GetRow(),
5593 m_selectingBottomRight.GetCol(),
5594 event.ControlDown(),
5595 event.ShiftDown(),
5596 event.AltDown(),
5597 event.MetaDown() );
5598 }
5599
5600 m_selectingTopLeft = wxGridNoCellCoords;
5601 m_selectingBottomRight = wxGridNoCellCoords;
5602
5603 // Show the edit control, if it has been hidden for
5604 // drag-shrinking.
5605 ShowCellEditControl();
5606 }
5607 }
5608 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5609 {
5610 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5611 DoEndDragResizeRow();
5612
5613 // Note: we are ending the event *after* doing
5614 // default processing in this case
5615 //
5616 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5617 }
5618 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5619 {
5620 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5621 DoEndDragResizeCol();
5622
5623 // Note: we are ending the event *after* doing
5624 // default processing in this case
5625 //
5626 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5627 }
5628
5629 m_dragLastPos = -1;
5630 }
5631
5632
5633 // ------------ Right button down
5634 //
5635 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5636 {
5637 DisableCellEditControl();
5638 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5639 coords.GetRow(),
5640 coords.GetCol(),
5641 event ) )
5642 {
5643 // no default action at the moment
5644 }
5645 }
5646
5647
5648 // ------------ Right double click
5649 //
5650 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5651 {
5652 DisableCellEditControl();
5653 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5654 coords.GetRow(),
5655 coords.GetCol(),
5656 event ) )
5657 {
5658 // no default action at the moment
5659 }
5660 }
5661
5662 // ------------ Moving and no button action
5663 //
5664 else if ( event.Moving() && !event.IsButton() )
5665 {
5666 if( coords.GetRow() < 0 || coords.GetCol() < 0 )
5667 {
5668 // out of grid cell area
5669 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5670 return;
5671 }
5672
5673 int dragRow = YToEdgeOfRow( y );
5674 int dragCol = XToEdgeOfCol( x );
5675
5676 // Dragging on the corner of a cell to resize in both
5677 // directions is not implemented yet...
5678 //
5679 if ( dragRow >= 0 && dragCol >= 0 )
5680 {
5681 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5682 return;
5683 }
5684
5685 if ( dragRow >= 0 )
5686 {
5687 m_dragRowOrCol = dragRow;
5688
5689 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5690 {
5691 if ( CanDragRowSize() && CanDragGridSize() )
5692 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
5693 }
5694
5695 if ( dragCol >= 0 )
5696 {
5697 m_dragRowOrCol = dragCol;
5698 }
5699
5700 return;
5701 }
5702
5703 if ( dragCol >= 0 )
5704 {
5705 m_dragRowOrCol = dragCol;
5706
5707 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5708 {
5709 if ( CanDragColSize() && CanDragGridSize() )
5710 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
5711 }
5712
5713 return;
5714 }
5715
5716 // Neither on a row or col edge
5717 //
5718 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5719 {
5720 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5721 }
5722 }
5723 }
5724
5725
5726 void wxGrid::DoEndDragResizeRow()
5727 {
5728 if ( m_dragLastPos >= 0 )
5729 {
5730 // erase the last line and resize the row
5731 //
5732 int cw, ch, left, dummy;
5733 m_gridWin->GetClientSize( &cw, &ch );
5734 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5735
5736 wxClientDC dc( m_gridWin );
5737 PrepareDC( dc );
5738 dc.SetLogicalFunction( wxINVERT );
5739 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5740 HideCellEditControl();
5741 SaveEditControlValue();
5742
5743 int rowTop = GetRowTop(m_dragRowOrCol);
5744 SetRowSize( m_dragRowOrCol,
5745 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
5746
5747 if ( !GetBatchCount() )
5748 {
5749 // Only needed to get the correct rect.y:
5750 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5751 rect.x = 0;
5752 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5753 rect.width = m_rowLabelWidth;
5754 rect.height = ch - rect.y;
5755 m_rowLabelWin->Refresh( true, &rect );
5756 rect.width = cw;
5757 // if there is a multicell block, paint all of it
5758 if (m_table)
5759 {
5760 int i, cell_rows, cell_cols, subtract_rows = 0;
5761 int leftCol = XToCol(left);
5762 int rightCol = internalXToCol(left+cw);
5763 if (leftCol >= 0)
5764 {
5765 for (i=leftCol; i<rightCol; i++)
5766 {
5767 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
5768 if (cell_rows < subtract_rows)
5769 subtract_rows = cell_rows;
5770 }
5771 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
5772 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5773 rect.height = ch - rect.y;
5774 }
5775 }
5776 m_gridWin->Refresh( false, &rect );
5777 }
5778
5779 ShowCellEditControl();
5780 }
5781 }
5782
5783
5784 void wxGrid::DoEndDragResizeCol()
5785 {
5786 if ( m_dragLastPos >= 0 )
5787 {
5788 // erase the last line and resize the col
5789 //
5790 int cw, ch, dummy, top;
5791 m_gridWin->GetClientSize( &cw, &ch );
5792 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5793
5794 wxClientDC dc( m_gridWin );
5795 PrepareDC( dc );
5796 dc.SetLogicalFunction( wxINVERT );
5797 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5798 HideCellEditControl();
5799 SaveEditControlValue();
5800
5801 int colLeft = GetColLeft(m_dragRowOrCol);
5802 SetColSize( m_dragRowOrCol,
5803 wxMax( m_dragLastPos - colLeft,
5804 GetColMinimalWidth(m_dragRowOrCol) ) );
5805
5806 if ( !GetBatchCount() )
5807 {
5808 // Only needed to get the correct rect.x:
5809 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5810 rect.y = 0;
5811 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5812 rect.width = cw - rect.x;
5813 rect.height = m_colLabelHeight;
5814 m_colLabelWin->Refresh( true, &rect );
5815 rect.height = ch;
5816 // if there is a multicell block, paint all of it
5817 if (m_table)
5818 {
5819 int i, cell_rows, cell_cols, subtract_cols = 0;
5820 int topRow = YToRow(top);
5821 int bottomRow = internalYToRow(top+cw);
5822 if (topRow >= 0)
5823 {
5824 for (i=topRow; i<bottomRow; i++)
5825 {
5826 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
5827 if (cell_cols < subtract_cols)
5828 subtract_cols = cell_cols;
5829 }
5830 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
5831 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5832 rect.width = cw - rect.x;
5833 }
5834 }
5835 m_gridWin->Refresh( false, &rect );
5836 }
5837
5838 ShowCellEditControl();
5839 }
5840 }
5841
5842
5843
5844 //
5845 // ------ interaction with data model
5846 //
5847 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
5848 {
5849 switch ( msg.GetId() )
5850 {
5851 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5852 return GetModelValues();
5853
5854 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5855 return SetModelValues();
5856
5857 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5858 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5859 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5860 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5861 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5862 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5863 return Redimension( msg );
5864
5865 default:
5866 return false;
5867 }
5868 }
5869
5870
5871
5872 // The behaviour of this function depends on the grid table class
5873 // Clear() function. For the default wxGridStringTable class the
5874 // behavious is to replace all cell contents with wxEmptyString but
5875 // not to change the number of rows or cols.
5876 //
5877 void wxGrid::ClearGrid()
5878 {
5879 if ( m_table )
5880 {
5881 if (IsCellEditControlEnabled())
5882 DisableCellEditControl();
5883
5884 m_table->Clear();
5885 if ( !GetBatchCount() ) m_gridWin->Refresh();
5886 }
5887 }
5888
5889
5890 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5891 {
5892 // TODO: something with updateLabels flag
5893
5894 if ( !m_created )
5895 {
5896 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5897 return false;
5898 }
5899
5900 if ( m_table )
5901 {
5902 if (IsCellEditControlEnabled())
5903 DisableCellEditControl();
5904
5905 bool done = m_table->InsertRows( pos, numRows );
5906 return done;
5907
5908 // the table will have sent the results of the insert row
5909 // operation to this view object as a grid table message
5910 }
5911 return false;
5912 }
5913
5914
5915 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
5916 {
5917 // TODO: something with updateLabels flag
5918
5919 if ( !m_created )
5920 {
5921 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5922 return false;
5923 }
5924
5925 if ( m_table )
5926 {
5927 bool done = m_table && m_table->AppendRows( numRows );
5928 return done;
5929 // the table will have sent the results of the append row
5930 // operation to this view object as a grid table message
5931 }
5932 return false;
5933 }
5934
5935
5936 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5937 {
5938 // TODO: something with updateLabels flag
5939
5940 if ( !m_created )
5941 {
5942 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5943 return false;
5944 }
5945
5946 if ( m_table )
5947 {
5948 if (IsCellEditControlEnabled())
5949 DisableCellEditControl();
5950
5951 bool done = m_table->DeleteRows( pos, numRows );
5952 return done;
5953 // the table will have sent the results of the delete row
5954 // operation to this view object as a grid table message
5955 }
5956 return false;
5957 }
5958
5959
5960 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5961 {
5962 // TODO: something with updateLabels flag
5963
5964 if ( !m_created )
5965 {
5966 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5967 return false;
5968 }
5969
5970 if ( m_table )
5971 {
5972 if (IsCellEditControlEnabled())
5973 DisableCellEditControl();
5974
5975 bool done = m_table->InsertCols( pos, numCols );
5976 return done;
5977 // the table will have sent the results of the insert col
5978 // operation to this view object as a grid table message
5979 }
5980 return false;
5981 }
5982
5983
5984 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
5985 {
5986 // TODO: something with updateLabels flag
5987
5988 if ( !m_created )
5989 {
5990 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5991 return false;
5992 }
5993
5994 if ( m_table )
5995 {
5996 bool done = m_table->AppendCols( numCols );
5997 return done;
5998 // the table will have sent the results of the append col
5999 // operation to this view object as a grid table message
6000 }
6001 return false;
6002 }
6003
6004
6005 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6006 {
6007 // TODO: something with updateLabels flag
6008
6009 if ( !m_created )
6010 {
6011 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6012 return false;
6013 }
6014
6015 if ( m_table )
6016 {
6017 if (IsCellEditControlEnabled())
6018 DisableCellEditControl();
6019
6020 bool done = m_table->DeleteCols( pos, numCols );
6021 return done;
6022 // the table will have sent the results of the delete col
6023 // operation to this view object as a grid table message
6024 }
6025 return false;
6026 }
6027
6028
6029
6030 //
6031 // ----- event handlers
6032 //
6033
6034 // Generate a grid event based on a mouse event and
6035 // return the result of ProcessEvent()
6036 //
6037 int wxGrid::SendEvent( const wxEventType type,
6038 int row, int col,
6039 wxMouseEvent& mouseEv )
6040 {
6041 bool claimed;
6042 bool vetoed;
6043
6044 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6045 {
6046 int rowOrCol = (row == -1 ? col : row);
6047
6048 wxGridSizeEvent gridEvt( GetId(),
6049 type,
6050 this,
6051 rowOrCol,
6052 mouseEv.GetX() + GetRowLabelSize(),
6053 mouseEv.GetY() + GetColLabelSize(),
6054 mouseEv.ControlDown(),
6055 mouseEv.ShiftDown(),
6056 mouseEv.AltDown(),
6057 mouseEv.MetaDown() );
6058
6059 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6060 vetoed = !gridEvt.IsAllowed();
6061 }
6062 else if ( type == wxEVT_GRID_RANGE_SELECT )
6063 {
6064 // Right now, it should _never_ end up here!
6065 wxGridRangeSelectEvent gridEvt( GetId(),
6066 type,
6067 this,
6068 m_selectingTopLeft,
6069 m_selectingBottomRight,
6070 true,
6071 mouseEv.ControlDown(),
6072 mouseEv.ShiftDown(),
6073 mouseEv.AltDown(),
6074 mouseEv.MetaDown() );
6075
6076 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6077 vetoed = !gridEvt.IsAllowed();
6078 }
6079 else
6080 {
6081 wxGridEvent gridEvt( GetId(),
6082 type,
6083 this,
6084 row, col,
6085 mouseEv.GetX() + GetRowLabelSize(),
6086 mouseEv.GetY() + GetColLabelSize(),
6087 false,
6088 mouseEv.ControlDown(),
6089 mouseEv.ShiftDown(),
6090 mouseEv.AltDown(),
6091 mouseEv.MetaDown() );
6092 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6093 vetoed = !gridEvt.IsAllowed();
6094 }
6095
6096 // A Veto'd event may not be `claimed' so test this first
6097 if (vetoed) return -1;
6098 return claimed ? 1 : 0;
6099 }
6100
6101
6102 // Generate a grid event of specified type and return the result
6103 // of ProcessEvent().
6104 //
6105 int wxGrid::SendEvent( const wxEventType type,
6106 int row, int col )
6107 {
6108 bool claimed;
6109 bool vetoed;
6110
6111 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6112 {
6113 int rowOrCol = (row == -1 ? col : row);
6114
6115 wxGridSizeEvent gridEvt( GetId(),
6116 type,
6117 this,
6118 rowOrCol );
6119
6120 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6121 vetoed = !gridEvt.IsAllowed();
6122 }
6123 else
6124 {
6125 wxGridEvent gridEvt( GetId(),
6126 type,
6127 this,
6128 row, col );
6129
6130 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6131 vetoed = !gridEvt.IsAllowed();
6132 }
6133
6134 // A Veto'd event may not be `claimed' so test this first
6135 if (vetoed) return -1;
6136 return claimed ? 1 : 0;
6137 }
6138
6139
6140 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6141 {
6142 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
6143 }
6144
6145 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6146 {
6147 // Don't do anything if between Begin/EndBatch...
6148 // EndBatch() will do all this on the last nested one anyway.
6149 if (! GetBatchCount())
6150 {
6151 // Refresh to get correct scrolled position:
6152 wxScrolledWindow::Refresh(eraseb,rect);
6153
6154 if (rect)
6155 {
6156 int rect_x, rect_y, rectWidth, rectHeight;
6157 int width_label, width_cell, height_label, height_cell;
6158 int x, y;
6159
6160 //Copy rectangle can get scroll offsets..
6161 rect_x = rect->GetX();
6162 rect_y = rect->GetY();
6163 rectWidth = rect->GetWidth();
6164 rectHeight = rect->GetHeight();
6165
6166 width_label = m_rowLabelWidth - rect_x;
6167 if (width_label > rectWidth) width_label = rectWidth;
6168
6169 height_label = m_colLabelHeight - rect_y;
6170 if (height_label > rectHeight) height_label = rectHeight;
6171
6172 if (rect_x > m_rowLabelWidth)
6173 {
6174 x = rect_x - m_rowLabelWidth;
6175 width_cell = rectWidth;
6176 }
6177 else
6178 {
6179 x = 0;
6180 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6181 }
6182
6183 if (rect_y > m_colLabelHeight)
6184 {
6185 y = rect_y - m_colLabelHeight;
6186 height_cell = rectHeight;
6187 }
6188 else
6189 {
6190 y = 0;
6191 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6192 }
6193
6194 // Paint corner label part intersecting rect.
6195 if ( width_label > 0 && height_label > 0 )
6196 {
6197 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6198 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6199 }
6200
6201 // Paint col labels part intersecting rect.
6202 if ( width_cell > 0 && height_label > 0 )
6203 {
6204 wxRect anotherrect(x, rect_y, width_cell, height_label);
6205 m_colLabelWin->Refresh(eraseb, &anotherrect);
6206 }
6207
6208 // Paint row labels part intersecting rect.
6209 if ( width_label > 0 && height_cell > 0 )
6210 {
6211 wxRect anotherrect(rect_x, y, width_label, height_cell);
6212 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6213 }
6214
6215 // Paint cell area part intersecting rect.
6216 if ( width_cell > 0 && height_cell > 0 )
6217 {
6218 wxRect anotherrect(x, y, width_cell, height_cell);
6219 m_gridWin->Refresh(eraseb, &anotherrect);
6220 }
6221 }
6222 else
6223 {
6224 m_cornerLabelWin->Refresh(eraseb, NULL);
6225 m_colLabelWin->Refresh(eraseb, NULL);
6226 m_rowLabelWin->Refresh(eraseb, NULL);
6227 m_gridWin->Refresh(eraseb, NULL);
6228 }
6229 }
6230 }
6231
6232 void wxGrid::OnSize( wxSizeEvent& event )
6233 {
6234 // position the child windows
6235 CalcWindowSizes();
6236
6237 // don't call CalcDimensions() from here, the base class handles the size
6238 // changes itself
6239 event.Skip();
6240 }
6241
6242
6243 void wxGrid::OnKeyDown( wxKeyEvent& event )
6244 {
6245 if ( m_inOnKeyDown )
6246 {
6247 // shouldn't be here - we are going round in circles...
6248 //
6249 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6250 }
6251
6252 m_inOnKeyDown = true;
6253
6254 // propagate the event up and see if it gets processed
6255 //
6256 wxWindow *parent = GetParent();
6257 wxKeyEvent keyEvt( event );
6258 keyEvt.SetEventObject( parent );
6259
6260 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6261 {
6262
6263 // try local handlers
6264 //
6265 switch ( event.GetKeyCode() )
6266 {
6267 case WXK_UP:
6268 if ( event.ControlDown() )
6269 {
6270 MoveCursorUpBlock( event.ShiftDown() );
6271 }
6272 else
6273 {
6274 MoveCursorUp( event.ShiftDown() );
6275 }
6276 break;
6277
6278 case WXK_DOWN:
6279 if ( event.ControlDown() )
6280 {
6281 MoveCursorDownBlock( event.ShiftDown() );
6282 }
6283 else
6284 {
6285 MoveCursorDown( event.ShiftDown() );
6286 }
6287 break;
6288
6289 case WXK_LEFT:
6290 if ( event.ControlDown() )
6291 {
6292 MoveCursorLeftBlock( event.ShiftDown() );
6293 }
6294 else
6295 {
6296 MoveCursorLeft( event.ShiftDown() );
6297 }
6298 break;
6299
6300 case WXK_RIGHT:
6301 if ( event.ControlDown() )
6302 {
6303 MoveCursorRightBlock( event.ShiftDown() );
6304 }
6305 else
6306 {
6307 MoveCursorRight( event.ShiftDown() );
6308 }
6309 break;
6310
6311 case WXK_RETURN:
6312 case WXK_NUMPAD_ENTER:
6313 if ( event.ControlDown() )
6314 {
6315 event.Skip(); // to let the edit control have the return
6316 }
6317 else
6318 {
6319 if ( GetGridCursorRow() < GetNumberRows()-1 )
6320 {
6321 MoveCursorDown( event.ShiftDown() );
6322 }
6323 else
6324 {
6325 // at the bottom of a column
6326 DisableCellEditControl();
6327 }
6328 }
6329 break;
6330
6331 case WXK_ESCAPE:
6332 ClearSelection();
6333 break;
6334
6335 case WXK_TAB:
6336 if (event.ShiftDown())
6337 {
6338 if ( GetGridCursorCol() > 0 )
6339 {
6340 MoveCursorLeft( false );
6341 }
6342 else
6343 {
6344 // at left of grid
6345 DisableCellEditControl();
6346 }
6347 }
6348 else
6349 {
6350 if ( GetGridCursorCol() < GetNumberCols()-1 )
6351 {
6352 MoveCursorRight( false );
6353 }
6354 else
6355 {
6356 // at right of grid
6357 DisableCellEditControl();
6358 }
6359 }
6360 break;
6361
6362 case WXK_HOME:
6363 if ( event.ControlDown() )
6364 {
6365 MakeCellVisible( 0, 0 );
6366 SetCurrentCell( 0, 0 );
6367 }
6368 else
6369 {
6370 event.Skip();
6371 }
6372 break;
6373
6374 case WXK_END:
6375 if ( event.ControlDown() )
6376 {
6377 MakeCellVisible( m_numRows-1, m_numCols-1 );
6378 SetCurrentCell( m_numRows-1, m_numCols-1 );
6379 }
6380 else
6381 {
6382 event.Skip();
6383 }
6384 break;
6385
6386 case WXK_PRIOR:
6387 MovePageUp();
6388 break;
6389
6390 case WXK_NEXT:
6391 MovePageDown();
6392 break;
6393
6394 case WXK_SPACE:
6395 if ( event.ControlDown() )
6396 {
6397 if ( m_selection )
6398 {
6399 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
6400 m_currentCellCoords.GetCol(),
6401 event.ControlDown(),
6402 event.ShiftDown(),
6403 event.AltDown(),
6404 event.MetaDown() );
6405 }
6406 break;
6407 }
6408 if ( !IsEditable() )
6409 {
6410 MoveCursorRight( false );
6411 break;
6412 }
6413 // Otherwise fall through to default
6414
6415 default:
6416 // is it possible to edit the current cell at all?
6417 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6418 {
6419 // yes, now check whether the cells editor accepts the key
6420 int row = m_currentCellCoords.GetRow();
6421 int col = m_currentCellCoords.GetCol();
6422 wxGridCellAttr* attr = GetCellAttr(row, col);
6423 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6424
6425 // <F2> is special and will always start editing, for
6426 // other keys - ask the editor itself
6427 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
6428 || editor->IsAcceptedKey(event) )
6429 {
6430 // ensure cell is visble
6431 MakeCellVisible(row, col);
6432 EnableCellEditControl();
6433
6434 // a problem can arise if the cell is not completely
6435 // visible (even after calling MakeCellVisible the
6436 // control is not created and calling StartingKey will
6437 // crash the app
6438 if( editor->IsCreated() && m_cellEditCtrlEnabled ) editor->StartingKey(event);
6439 }
6440 else
6441 {
6442 event.Skip();
6443 }
6444
6445 editor->DecRef();
6446 attr->DecRef();
6447 }
6448 else
6449 {
6450 // let others process char events with modifiers or all
6451 // char events for readonly cells
6452 event.Skip();
6453 }
6454 break;
6455 }
6456 }
6457
6458 m_inOnKeyDown = false;
6459 }
6460
6461 void wxGrid::OnKeyUp( wxKeyEvent& event )
6462 {
6463 // try local handlers
6464 //
6465 if ( event.GetKeyCode() == WXK_SHIFT )
6466 {
6467 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6468 m_selectingBottomRight != wxGridNoCellCoords )
6469 {
6470 if ( m_selection )
6471 {
6472 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6473 m_selectingTopLeft.GetCol(),
6474 m_selectingBottomRight.GetRow(),
6475 m_selectingBottomRight.GetCol(),
6476 event.ControlDown(),
6477 true,
6478 event.AltDown(),
6479 event.MetaDown() );
6480 }
6481 }
6482
6483 m_selectingTopLeft = wxGridNoCellCoords;
6484 m_selectingBottomRight = wxGridNoCellCoords;
6485 m_selectingKeyboard = wxGridNoCellCoords;
6486 }
6487 }
6488
6489 void wxGrid::OnEraseBackground(wxEraseEvent&)
6490 {
6491 }
6492
6493 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
6494 {
6495 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
6496 {
6497 // the event has been intercepted - do nothing
6498 return;
6499 }
6500
6501 wxClientDC dc(m_gridWin);
6502 PrepareDC(dc);
6503
6504 if ( m_currentCellCoords != wxGridNoCellCoords )
6505 {
6506 DisableCellEditControl();
6507
6508 if ( IsVisible( m_currentCellCoords, false ) )
6509 {
6510 wxRect r;
6511 r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
6512 if ( !m_gridLinesEnabled )
6513 {
6514 r.x--;
6515 r.y--;
6516 r.width++;
6517 r.height++;
6518 }
6519
6520 wxGridCellCoordsArray cells = CalcCellsExposed( r );
6521
6522 // Otherwise refresh redraws the highlight!
6523 m_currentCellCoords = coords;
6524
6525 DrawGridCellArea(dc,cells);
6526 DrawAllGridLines( dc, r );
6527 }
6528 }
6529
6530 m_currentCellCoords = coords;
6531
6532 wxGridCellAttr* attr = GetCellAttr(coords);
6533 DrawCellHighlight(dc, attr);
6534 attr->DecRef();
6535 }
6536
6537
6538 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
6539 {
6540 int temp;
6541 wxGridCellCoords updateTopLeft, updateBottomRight;
6542
6543 if ( m_selection )
6544 {
6545 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
6546 {
6547 leftCol = 0;
6548 rightCol = GetNumberCols() - 1;
6549 }
6550 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
6551 {
6552 topRow = 0;
6553 bottomRow = GetNumberRows() - 1;
6554 }
6555 }
6556
6557 if ( topRow > bottomRow )
6558 {
6559 temp = topRow;
6560 topRow = bottomRow;
6561 bottomRow = temp;
6562 }
6563
6564 if ( leftCol > rightCol )
6565 {
6566 temp = leftCol;
6567 leftCol = rightCol;
6568 rightCol = temp;
6569 }
6570
6571 updateTopLeft = wxGridCellCoords( topRow, leftCol );
6572 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
6573
6574 // First the case that we selected a completely new area
6575 if ( m_selectingTopLeft == wxGridNoCellCoords ||
6576 m_selectingBottomRight == wxGridNoCellCoords )
6577 {
6578 wxRect rect;
6579 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
6580 wxGridCellCoords ( bottomRow, rightCol ) );
6581 m_gridWin->Refresh( false, &rect );
6582 }
6583 // Now handle changing an existing selection area.
6584 else if ( m_selectingTopLeft != updateTopLeft ||
6585 m_selectingBottomRight != updateBottomRight )
6586 {
6587 // Compute two optimal update rectangles:
6588 // Either one rectangle is a real subset of the
6589 // other, or they are (almost) disjoint!
6590 wxRect rect[4];
6591 bool need_refresh[4];
6592 need_refresh[0] =
6593 need_refresh[1] =
6594 need_refresh[2] =
6595 need_refresh[3] = false;
6596 int i;
6597
6598 // Store intermediate values
6599 wxCoord oldLeft = m_selectingTopLeft.GetCol();
6600 wxCoord oldTop = m_selectingTopLeft.GetRow();
6601 wxCoord oldRight = m_selectingBottomRight.GetCol();
6602 wxCoord oldBottom = m_selectingBottomRight.GetRow();
6603
6604 // Determine the outer/inner coordinates.
6605 if (oldLeft > leftCol)
6606 {
6607 temp = oldLeft;
6608 oldLeft = leftCol;
6609 leftCol = temp;
6610 }
6611 if (oldTop > topRow )
6612 {
6613 temp = oldTop;
6614 oldTop = topRow;
6615 topRow = temp;
6616 }
6617 if (oldRight < rightCol )
6618 {
6619 temp = oldRight;
6620 oldRight = rightCol;
6621 rightCol = temp;
6622 }
6623 if (oldBottom < bottomRow)
6624 {
6625 temp = oldBottom;
6626 oldBottom = bottomRow;
6627 bottomRow = temp;
6628 }
6629
6630 // Now, either the stuff marked old is the outer
6631 // rectangle or we don't have a situation where one
6632 // is contained in the other.
6633
6634 if ( oldLeft < leftCol )
6635 {
6636 // Refresh the newly selected or deselected
6637 // area to the left of the old or new selection.
6638 need_refresh[0] = true;
6639 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6640 oldLeft ),
6641 wxGridCellCoords ( oldBottom,
6642 leftCol - 1 ) );
6643 }
6644
6645 if ( oldTop < topRow )
6646 {
6647 // Refresh the newly selected or deselected
6648 // area above the old or new selection.
6649 need_refresh[1] = true;
6650 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6651 leftCol ),
6652 wxGridCellCoords ( topRow - 1,
6653 rightCol ) );
6654 }
6655
6656 if ( oldRight > rightCol )
6657 {
6658 // Refresh the newly selected or deselected
6659 // area to the right of the old or new selection.
6660 need_refresh[2] = true;
6661 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6662 rightCol + 1 ),
6663 wxGridCellCoords ( oldBottom,
6664 oldRight ) );
6665 }
6666
6667 if ( oldBottom > bottomRow )
6668 {
6669 // Refresh the newly selected or deselected
6670 // area below the old or new selection.
6671 need_refresh[3] = true;
6672 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6673 leftCol ),
6674 wxGridCellCoords ( oldBottom,
6675 rightCol ) );
6676 }
6677
6678 // various Refresh() calls
6679 for (i = 0; i < 4; i++ )
6680 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6681 m_gridWin->Refresh( false, &(rect[i]) );
6682 }
6683 // Change Selection
6684 m_selectingTopLeft = updateTopLeft;
6685 m_selectingBottomRight = updateBottomRight;
6686 }
6687
6688 //
6689 // ------ functions to get/send data (see also public functions)
6690 //
6691
6692 bool wxGrid::GetModelValues()
6693 {
6694 // Hide the editor, so it won't hide a changed value.
6695 HideCellEditControl();
6696
6697 if ( m_table )
6698 {
6699 // all we need to do is repaint the grid
6700 //
6701 m_gridWin->Refresh();
6702 return true;
6703 }
6704
6705 return false;
6706 }
6707
6708
6709 bool wxGrid::SetModelValues()
6710 {
6711 int row, col;
6712
6713 // Disable the editor, so it won't hide a changed value.
6714 // Do we also want to save the current value of the editor first?
6715 // I think so ...
6716 DisableCellEditControl();
6717
6718 if ( m_table )
6719 {
6720 for ( row = 0; row < m_numRows; row++ )
6721 {
6722 for ( col = 0; col < m_numCols; col++ )
6723 {
6724 m_table->SetValue( row, col, GetCellValue(row, col) );
6725 }
6726 }
6727
6728 return true;
6729 }
6730
6731 return false;
6732 }
6733
6734
6735
6736 // Note - this function only draws cells that are in the list of
6737 // exposed cells (usually set from the update region by
6738 // CalcExposedCells)
6739 //
6740 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
6741 {
6742 if ( !m_numRows || !m_numCols ) return;
6743
6744 int i, numCells = cells.GetCount();
6745 int row, col, cell_rows, cell_cols;
6746 wxGridCellCoordsArray redrawCells;
6747
6748 for ( i = numCells-1; i >= 0; i-- )
6749 {
6750 row = cells[i].GetRow();
6751 col = cells[i].GetCol();
6752 GetCellSize( row, col, &cell_rows, &cell_cols );
6753
6754 // If this cell is part of a multicell block, find owner for repaint
6755 if ( cell_rows <= 0 || cell_cols <= 0 )
6756 {
6757 wxGridCellCoords cell(row+cell_rows, col+cell_cols);
6758 bool marked = false;
6759 for ( int j = 0; j < numCells; j++ )
6760 {
6761 if ( cell == cells[j] )
6762 {
6763 marked = true;
6764 break;
6765 }
6766 }
6767 if (!marked)
6768 {
6769 int count = redrawCells.GetCount();
6770 for (int j = 0; j < count; j++)
6771 {
6772 if ( cell == redrawCells[j] )
6773 {
6774 marked = true;
6775 break;
6776 }
6777 }
6778 if (!marked) redrawCells.Add( cell );
6779 }
6780 continue; // don't bother drawing this cell
6781 }
6782
6783 // If this cell is empty, find cell to left that might want to overflow
6784 if (m_table && m_table->IsEmptyCell(row, col))
6785 {
6786 for ( int l = 0; l < cell_rows; l++ )
6787 {
6788 // find a cell in this row to left alreay marked for repaint
6789 int left = col;
6790 for (int k = 0; k < int(redrawCells.GetCount()); k++)
6791 if ((redrawCells[k].GetCol() < left) &&
6792 (redrawCells[k].GetRow() == row))
6793 left=redrawCells[k].GetCol();
6794
6795 if (left == col) left = 0; // oh well
6796
6797 for (int j = col-1; j >= left; j--)
6798 {
6799 if (!m_table->IsEmptyCell(row+l, j))
6800 {
6801 if (GetCellOverflow(row+l, j))
6802 {
6803 wxGridCellCoords cell(row+l, j);
6804 bool marked = false;
6805
6806 for (int k = 0; k < numCells; k++)
6807 {
6808 if ( cell == cells[k] )
6809 {
6810 marked = true;
6811 break;
6812 }
6813 }
6814 if (!marked)
6815 {
6816 int count = redrawCells.GetCount();
6817 for (int k = 0; k < count; k++)
6818 {
6819 if ( cell == redrawCells[k] )
6820 {
6821 marked = true;
6822 break;
6823 }
6824 }
6825 if (!marked) redrawCells.Add( cell );
6826 }
6827 }
6828 break;
6829 }
6830 }
6831 }
6832 }
6833 DrawCell( dc, cells[i] );
6834 }
6835
6836 numCells = redrawCells.GetCount();
6837
6838 for ( i = numCells - 1; i >= 0; i-- )
6839 {
6840 DrawCell( dc, redrawCells[i] );
6841 }
6842 }
6843
6844
6845 void wxGrid::DrawGridSpace( wxDC& dc )
6846 {
6847 int cw, ch;
6848 m_gridWin->GetClientSize( &cw, &ch );
6849
6850 int right, bottom;
6851 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6852
6853 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6854 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
6855
6856 if ( right > rightCol || bottom > bottomRow )
6857 {
6858 int left, top;
6859 CalcUnscrolledPosition( 0, 0, &left, &top );
6860
6861 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6862 dc.SetPen( *wxTRANSPARENT_PEN );
6863
6864 if ( right > rightCol )
6865 {
6866 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6867 }
6868
6869 if ( bottom > bottomRow )
6870 {
6871 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6872 }
6873 }
6874 }
6875
6876
6877 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6878 {
6879 int row = coords.GetRow();
6880 int col = coords.GetCol();
6881
6882 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6883 return;
6884
6885 // we draw the cell border ourselves
6886 #if !WXGRID_DRAW_LINES
6887 if ( m_gridLinesEnabled )
6888 DrawCellBorder( dc, coords );
6889 #endif
6890
6891 wxGridCellAttr* attr = GetCellAttr(row, col);
6892
6893 bool isCurrent = coords == m_currentCellCoords;
6894
6895 wxRect rect = CellToRect( row, col );
6896
6897 // if the editor is shown, we should use it and not the renderer
6898 // Note: However, only if it is really _shown_, i.e. not hidden!
6899 if ( isCurrent && IsCellEditControlShown() )
6900 {
6901 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6902 editor->PaintBackground(rect, attr);
6903 editor->DecRef();
6904 }
6905 else
6906 {
6907 // but all the rest is drawn by the cell renderer and hence may be
6908 // customized
6909 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6910 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6911 renderer->DecRef();
6912 }
6913
6914 attr->DecRef();
6915 }
6916
6917 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
6918 {
6919 int row = m_currentCellCoords.GetRow();
6920 int col = m_currentCellCoords.GetCol();
6921
6922 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6923 return;
6924
6925 wxRect rect = CellToRect(row, col);
6926
6927 // hmmm... what could we do here to show that the cell is disabled?
6928 // for now, I just draw a thinner border than for the other ones, but
6929 // it doesn't look really good
6930
6931 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6932
6933 if (penWidth > 0)
6934 {
6935 // The center of th drawn line is where the position/width/height of
6936 // the rectangle is actually at, (on wxMSW atr least,) so we will
6937 // reduce the size of the rectangle to compensate for the thickness of
6938 // the line. If this is too strange on non wxMSW platforms then
6939 // please #ifdef this appropriately.
6940 rect.x += penWidth/2;
6941 rect.y += penWidth/2;
6942 rect.width -= penWidth-1;
6943 rect.height -= penWidth-1;
6944
6945
6946 // Now draw the rectangle
6947 // use the cellHighlightColour if the cell is inside a selection, this
6948 // will ensure the cell is always visible.
6949 dc.SetPen(wxPen(IsInSelection(row,col)?m_selectionForeground:m_cellHighlightColour, penWidth, wxSOLID));
6950 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6951 dc.DrawRectangle(rect);
6952 }
6953
6954 #if 0
6955 // VZ: my experiments with 3d borders...
6956
6957 // how to properly set colours for arbitrary bg?
6958 wxCoord x1 = rect.x,
6959 y1 = rect.y,
6960 x2 = rect.x + rect.width -1,
6961 y2 = rect.y + rect.height -1;
6962
6963 dc.SetPen(*wxWHITE_PEN);
6964 dc.DrawLine(x1, y1, x2, y1);
6965 dc.DrawLine(x1, y1, x1, y2);
6966
6967 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
6968 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
6969
6970 dc.SetPen(*wxBLACK_PEN);
6971 dc.DrawLine(x1, y2, x2, y2);
6972 dc.DrawLine(x2, y1, x2, y2+1);
6973 #endif // 0
6974 }
6975
6976
6977 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
6978 {
6979 int row = coords.GetRow();
6980 int col = coords.GetCol();
6981 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6982 return;
6983
6984 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
6985
6986 wxRect rect = CellToRect( row, col );
6987
6988 // right hand border
6989 //
6990 dc.DrawLine( rect.x + rect.width, rect.y,
6991 rect.x + rect.width, rect.y + rect.height + 1 );
6992
6993 // bottom border
6994 //
6995 dc.DrawLine( rect.x, rect.y + rect.height,
6996 rect.x + rect.width, rect.y + rect.height);
6997 }
6998
6999 void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
7000 {
7001 // This if block was previously in wxGrid::OnPaint but that doesn't
7002 // seem to get called under wxGTK - MB
7003 //
7004 if ( m_currentCellCoords == wxGridNoCellCoords &&
7005 m_numRows && m_numCols )
7006 {
7007 m_currentCellCoords.Set(0, 0);
7008 }
7009
7010 if ( IsCellEditControlShown() )
7011 {
7012 // don't show highlight when the edit control is shown
7013 return;
7014 }
7015
7016 // if the active cell was repainted, repaint its highlight too because it
7017 // might have been damaged by the grid lines
7018 size_t count = cells.GetCount();
7019 for ( size_t n = 0; n < count; n++ )
7020 {
7021 if ( cells[n] == m_currentCellCoords )
7022 {
7023 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7024 DrawCellHighlight(dc, attr);
7025 attr->DecRef();
7026
7027 break;
7028 }
7029 }
7030 }
7031
7032 // TODO: remove this ???
7033 // This is used to redraw all grid lines e.g. when the grid line colour
7034 // has been changed
7035 //
7036 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7037 {
7038 #if !WXGRID_DRAW_LINES
7039 return;
7040 #endif
7041
7042 if ( !m_gridLinesEnabled ||
7043 !m_numRows ||
7044 !m_numCols ) return;
7045
7046 int top, bottom, left, right;
7047
7048 #if 0 //#ifndef __WXGTK__
7049 if (reg.IsEmpty())
7050 {
7051 int cw, ch;
7052 m_gridWin->GetClientSize(&cw, &ch);
7053
7054 // virtual coords of visible area
7055 //
7056 CalcUnscrolledPosition( 0, 0, &left, &top );
7057 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7058 }
7059 else
7060 {
7061 wxCoord x, y, w, h;
7062 reg.GetBox(x, y, w, h);
7063 CalcUnscrolledPosition( x, y, &left, &top );
7064 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7065 }
7066 #else
7067 int cw, ch;
7068 m_gridWin->GetClientSize(&cw, &ch);
7069 CalcUnscrolledPosition( 0, 0, &left, &top );
7070 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7071 #endif
7072
7073 // avoid drawing grid lines past the last row and col
7074 //
7075 right = wxMin( right, GetColRight(m_numCols - 1) );
7076 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7077
7078 // no gridlines inside multicells, clip them out
7079 int leftCol = internalXToCol(left);
7080 int topRow = internalYToRow(top);
7081 int rightCol = internalXToCol(right);
7082 int bottomRow = internalYToRow(bottom);
7083 wxRegion clippedcells(0, 0, cw, ch);
7084
7085
7086 int i, j, cell_rows, cell_cols;
7087 wxRect rect;
7088
7089 for (j=topRow; j<bottomRow; j++)
7090 {
7091 for (i=leftCol; i<rightCol; i++)
7092 {
7093 GetCellSize( j, i, &cell_rows, &cell_cols );
7094 if ((cell_rows > 1) || (cell_cols > 1))
7095 {
7096 rect = CellToRect(j,i);
7097 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7098 clippedcells.Subtract(rect);
7099 }
7100 else if ((cell_rows < 0) || (cell_cols < 0))
7101 {
7102 rect = CellToRect(j+cell_rows, i+cell_cols);
7103 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7104 clippedcells.Subtract(rect);
7105 }
7106 }
7107 }
7108 dc.SetClippingRegion( clippedcells );
7109
7110 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
7111
7112 // horizontal grid lines
7113 //
7114 // already declared above - int i;
7115 for ( i = internalYToRow(top); i < m_numRows; i++ )
7116 {
7117 int bot = GetRowBottom(i) - 1;
7118
7119 if ( bot > bottom )
7120 {
7121 break;
7122 }
7123
7124 if ( bot >= top )
7125 {
7126 dc.DrawLine( left, bot, right, bot );
7127 }
7128 }
7129
7130
7131 // vertical grid lines
7132 //
7133 for ( i = internalXToCol(left); i < m_numCols; i++ )
7134 {
7135 int colRight = GetColRight(i) - 1;
7136 if ( colRight > right )
7137 {
7138 break;
7139 }
7140
7141 if ( colRight >= left )
7142 {
7143 dc.DrawLine( colRight, top, colRight, bottom );
7144 }
7145 }
7146 dc.DestroyClippingRegion();
7147 }
7148
7149
7150 void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
7151 {
7152 if ( !m_numRows ) return;
7153
7154 size_t i;
7155 size_t numLabels = rows.GetCount();
7156
7157 for ( i = 0; i < numLabels; i++ )
7158 {
7159 DrawRowLabel( dc, rows[i] );
7160 }
7161 }
7162
7163
7164 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7165 {
7166 if ( GetRowHeight(row) <= 0 )
7167 return;
7168
7169 int rowTop = GetRowTop(row),
7170 rowBottom = GetRowBottom(row) - 1;
7171
7172 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7173 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7174 m_rowLabelWidth-1, rowBottom );
7175
7176 dc.DrawLine( 0, rowTop, 0, rowBottom );
7177
7178 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7179
7180 dc.SetPen( *wxWHITE_PEN );
7181 dc.DrawLine( 1, rowTop, 1, rowBottom );
7182 dc.DrawLine( 1, rowTop, m_rowLabelWidth-1, rowTop );
7183
7184 dc.SetBackgroundMode( wxTRANSPARENT );
7185 dc.SetTextForeground( GetLabelTextColour() );
7186 dc.SetFont( GetLabelFont() );
7187
7188 int hAlign, vAlign;
7189 GetRowLabelAlignment( &hAlign, &vAlign );
7190
7191 wxRect rect;
7192 rect.SetX( 2 );
7193 rect.SetY( GetRowTop(row) + 2 );
7194 rect.SetWidth( m_rowLabelWidth - 4 );
7195 rect.SetHeight( GetRowHeight(row) - 4 );
7196 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7197 }
7198
7199
7200 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
7201 {
7202 if ( !m_numCols ) return;
7203
7204 size_t i;
7205 size_t numLabels = cols.GetCount();
7206
7207 for ( i = 0; i < numLabels; i++ )
7208 {
7209 DrawColLabel( dc, cols[i] );
7210 }
7211 }
7212
7213
7214 void wxGrid::DrawColLabel( wxDC& dc, int col )
7215 {
7216 if ( GetColWidth(col) <= 0 )
7217 return;
7218
7219 int colLeft = GetColLeft(col),
7220 colRight = GetColRight(col) - 1;
7221
7222 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7223 dc.DrawLine( colRight, 0,
7224 colRight, m_colLabelHeight-1 );
7225
7226 dc.DrawLine( colLeft, 0, colRight, 0 );
7227
7228 dc.DrawLine( colLeft, m_colLabelHeight-1,
7229 colRight+1, m_colLabelHeight-1 );
7230
7231 dc.SetPen( *wxWHITE_PEN );
7232 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
7233 dc.DrawLine( colLeft, 1, colRight, 1 );
7234
7235 dc.SetBackgroundMode( wxTRANSPARENT );
7236 dc.SetTextForeground( GetLabelTextColour() );
7237 dc.SetFont( GetLabelFont() );
7238
7239 int hAlign, vAlign, orient;
7240 GetColLabelAlignment( &hAlign, &vAlign );
7241 orient = GetColLabelTextOrientation();
7242
7243 wxRect rect;
7244 rect.SetX( colLeft + 2 );
7245 rect.SetY( 2 );
7246 rect.SetWidth( GetColWidth(col) - 4 );
7247 rect.SetHeight( m_colLabelHeight - 4 );
7248 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
7249 }
7250
7251 void wxGrid::DrawTextRectangle( wxDC& dc,
7252 const wxString& value,
7253 const wxRect& rect,
7254 int horizAlign,
7255 int vertAlign,
7256 int textOrientation )
7257 {
7258 wxArrayString lines;
7259
7260 StringToLines( value, lines );
7261
7262
7263 //Forward to new API.
7264 DrawTextRectangle( dc,
7265 lines,
7266 rect,
7267 horizAlign,
7268 vertAlign,
7269 textOrientation );
7270
7271 }
7272
7273 void wxGrid::DrawTextRectangle( wxDC& dc,
7274 const wxArrayString& lines,
7275 const wxRect& rect,
7276 int horizAlign,
7277 int vertAlign,
7278 int textOrientation )
7279 {
7280 long textWidth, textHeight;
7281 long lineWidth, lineHeight;
7282 int nLines;
7283
7284 dc.SetClippingRegion( rect );
7285
7286 nLines = lines.GetCount();
7287 if( nLines > 0 )
7288 {
7289 int l;
7290 float x = 0.0, y = 0.0;
7291
7292 if( textOrientation == wxHORIZONTAL )
7293 GetTextBoxSize(dc, lines, &textWidth, &textHeight);
7294 else
7295 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
7296
7297 switch( vertAlign )
7298 {
7299 case wxALIGN_BOTTOM:
7300 if( textOrientation == wxHORIZONTAL )
7301 y = rect.y + (rect.height - textHeight - 1);
7302 else
7303 x = rect.x + rect.width - textWidth;
7304 break;
7305
7306 case wxALIGN_CENTRE:
7307 if( textOrientation == wxHORIZONTAL )
7308 y = rect.y + ((rect.height - textHeight)/2);
7309 else
7310 x = rect.x + ((rect.width - textWidth)/2);
7311 break;
7312
7313 case wxALIGN_TOP:
7314 default:
7315 if( textOrientation == wxHORIZONTAL )
7316 y = rect.y + 1;
7317 else
7318 x = rect.x + 1;
7319 break;
7320 }
7321
7322 // Align each line of a multi-line label
7323 for( l = 0; l < nLines; l++ )
7324 {
7325 dc.GetTextExtent(lines[l], &lineWidth, &lineHeight);
7326
7327 switch( horizAlign )
7328 {
7329 case wxALIGN_RIGHT:
7330 if( textOrientation == wxHORIZONTAL )
7331 x = rect.x + (rect.width - lineWidth - 1);
7332 else
7333 y = rect.y + lineWidth + 1;
7334 break;
7335
7336 case wxALIGN_CENTRE:
7337 if( textOrientation == wxHORIZONTAL )
7338 x = rect.x + ((rect.width - lineWidth)/2);
7339 else
7340 y = rect.y + rect.height - ((rect.height - lineWidth)/2);
7341 break;
7342
7343 case wxALIGN_LEFT:
7344 default:
7345 if( textOrientation == wxHORIZONTAL )
7346 x = rect.x + 1;
7347 else
7348 y = rect.y + rect.height - 1;
7349 break;
7350 }
7351
7352 if( textOrientation == wxHORIZONTAL )
7353 {
7354 dc.DrawText( lines[l], (int)x, (int)y );
7355 y += lineHeight;
7356 }
7357 else
7358 {
7359 dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 );
7360 x += lineHeight;
7361 }
7362 }
7363 }
7364 dc.DestroyClippingRegion();
7365 }
7366
7367
7368 // Split multi line text up into an array of strings. Any existing
7369 // contents of the string array are preserved.
7370 //
7371 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
7372 {
7373 int startPos = 0;
7374 int pos;
7375 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
7376 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
7377
7378 while ( startPos < (int)tVal.Length() )
7379 {
7380 pos = tVal.Mid(startPos).Find( eol );
7381 if ( pos < 0 )
7382 {
7383 break;
7384 }
7385 else if ( pos == 0 )
7386 {
7387 lines.Add( wxEmptyString );
7388 }
7389 else
7390 {
7391 lines.Add( value.Mid(startPos, pos) );
7392 }
7393 startPos += pos+1;
7394 }
7395 if ( startPos < (int)value.Length() )
7396 {
7397 lines.Add( value.Mid( startPos ) );
7398 }
7399 }
7400
7401
7402 void wxGrid::GetTextBoxSize( wxDC& dc,
7403 const wxArrayString& lines,
7404 long *width, long *height )
7405 {
7406 long w = 0;
7407 long h = 0;
7408 long lineW, lineH;
7409
7410 size_t i;
7411 for ( i = 0; i < lines.GetCount(); i++ )
7412 {
7413 dc.GetTextExtent( lines[i], &lineW, &lineH );
7414 w = wxMax( w, lineW );
7415 h += lineH;
7416 }
7417
7418 *width = w;
7419 *height = h;
7420 }
7421
7422 //
7423 // ------ Batch processing.
7424 //
7425 void wxGrid::EndBatch()
7426 {
7427 if ( m_batchCount > 0 )
7428 {
7429 m_batchCount--;
7430 if ( !m_batchCount )
7431 {
7432 CalcDimensions();
7433 m_rowLabelWin->Refresh();
7434 m_colLabelWin->Refresh();
7435 m_cornerLabelWin->Refresh();
7436 m_gridWin->Refresh();
7437 }
7438 }
7439 }
7440
7441 // Use this, rather than wxWindow::Refresh(), to force an immediate
7442 // repainting of the grid. Has no effect if you are already inside a
7443 // BeginBatch / EndBatch block.
7444 //
7445 void wxGrid::ForceRefresh()
7446 {
7447 BeginBatch();
7448 EndBatch();
7449 }
7450
7451
7452 //
7453 // ------ Edit control functions
7454 //
7455
7456
7457 void wxGrid::EnableEditing( bool edit )
7458 {
7459 // TODO: improve this ?
7460 //
7461 if ( edit != m_editable )
7462 {
7463 if(!edit) EnableCellEditControl(edit);
7464 m_editable = edit;
7465 }
7466 }
7467
7468
7469 void wxGrid::EnableCellEditControl( bool enable )
7470 {
7471 if (! m_editable)
7472 return;
7473
7474 if ( m_currentCellCoords == wxGridNoCellCoords )
7475 SetCurrentCell( 0, 0 );
7476
7477 if ( enable != m_cellEditCtrlEnabled )
7478 {
7479 if ( enable )
7480 {
7481 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
7482 return;
7483
7484 // this should be checked by the caller!
7485 wxASSERT_MSG( CanEnableCellControl(),
7486 _T("can't enable editing for this cell!") );
7487
7488 // do it before ShowCellEditControl()
7489 m_cellEditCtrlEnabled = enable;
7490
7491 ShowCellEditControl();
7492 }
7493 else
7494 {
7495 //FIXME:add veto support
7496 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
7497
7498 HideCellEditControl();
7499 SaveEditControlValue();
7500
7501 // do it after HideCellEditControl()
7502 m_cellEditCtrlEnabled = enable;
7503 }
7504 }
7505 }
7506
7507 bool wxGrid::IsCurrentCellReadOnly() const
7508 {
7509 // const_cast
7510 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
7511 bool readonly = attr->IsReadOnly();
7512 attr->DecRef();
7513
7514 return readonly;
7515 }
7516
7517 bool wxGrid::CanEnableCellControl() const
7518 {
7519 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
7520 !IsCurrentCellReadOnly();
7521
7522 }
7523
7524 bool wxGrid::IsCellEditControlEnabled() const
7525 {
7526 // the cell edit control might be disable for all cells or just for the
7527 // current one if it's read only
7528 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
7529 }
7530
7531 bool wxGrid::IsCellEditControlShown() const
7532 {
7533 bool isShown = false;
7534
7535 if ( m_cellEditCtrlEnabled )
7536 {
7537 int row = m_currentCellCoords.GetRow();
7538 int col = m_currentCellCoords.GetCol();
7539 wxGridCellAttr* attr = GetCellAttr(row, col);
7540 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
7541 attr->DecRef();
7542
7543 if ( editor )
7544 {
7545 if ( editor->IsCreated() )
7546 {
7547 isShown = editor->GetControl()->IsShown();
7548 }
7549
7550 editor->DecRef();
7551 }
7552 }
7553
7554 return isShown;
7555 }
7556
7557 void wxGrid::ShowCellEditControl()
7558 {
7559 if ( IsCellEditControlEnabled() )
7560 {
7561 if ( !IsVisible( m_currentCellCoords ) )
7562 {
7563 m_cellEditCtrlEnabled = false;
7564 return;
7565 }
7566 else
7567 {
7568 wxRect rect = CellToRect( m_currentCellCoords );
7569 int row = m_currentCellCoords.GetRow();
7570 int col = m_currentCellCoords.GetCol();
7571
7572 // if this is part of a multicell, find owner (topleft)
7573 int cell_rows, cell_cols;
7574 GetCellSize( row, col, &cell_rows, &cell_cols );
7575 if ( cell_rows <= 0 || cell_cols <= 0 )
7576 {
7577 row += cell_rows;
7578 col += cell_cols;
7579 m_currentCellCoords.SetRow( row );
7580 m_currentCellCoords.SetCol( col );
7581 }
7582
7583 // convert to scrolled coords
7584 //
7585 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7586
7587 // done in PaintBackground()
7588 #if 0
7589 // erase the highlight and the cell contents because the editor
7590 // might not cover the entire cell
7591 wxClientDC dc( m_gridWin );
7592 PrepareDC( dc );
7593 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7594 dc.SetPen(*wxTRANSPARENT_PEN);
7595 dc.DrawRectangle(rect);
7596 #endif // 0
7597
7598 // cell is shifted by one pixel
7599 // However, don't allow x or y to become negative
7600 // since the SetSize() method interprets that as
7601 // "don't change."
7602 if (rect.x > 0)
7603 rect.x--;
7604 if (rect.y > 0)
7605 rect.y--;
7606
7607 wxGridCellAttr* attr = GetCellAttr(row, col);
7608 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7609 if ( !editor->IsCreated() )
7610 {
7611 editor->Create(m_gridWin, wxID_ANY,
7612 new wxGridCellEditorEvtHandler(this, editor));
7613
7614 wxGridEditorCreatedEvent evt(GetId(),
7615 wxEVT_GRID_EDITOR_CREATED,
7616 this,
7617 row,
7618 col,
7619 editor->GetControl());
7620 GetEventHandler()->ProcessEvent(evt);
7621 }
7622
7623
7624 // resize editor to overflow into righthand cells if allowed
7625 int maxWidth = rect.width;
7626 wxString value = GetCellValue(row, col);
7627 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
7628 {
7629 int y;
7630 GetTextExtent(value, &maxWidth, &y,
7631 NULL, NULL, &attr->GetFont());
7632 if (maxWidth < rect.width) maxWidth = rect.width;
7633 }
7634 int client_right = m_gridWin->GetClientSize().GetWidth();
7635 if (rect.x+maxWidth > client_right)
7636 maxWidth = client_right - rect.x;
7637
7638 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
7639 {
7640 GetCellSize( row, col, &cell_rows, &cell_cols );
7641 // may have changed earlier
7642 for (int i = col+cell_cols; i < m_numCols; i++)
7643 {
7644 int c_rows, c_cols;
7645 GetCellSize( row, i, &c_rows, &c_cols );
7646 // looks weird going over a multicell
7647 if (m_table->IsEmptyCell(row,i) &&
7648 (rect.width < maxWidth) && (c_rows == 1))
7649 rect.width += GetColWidth(i);
7650 else
7651 break;
7652 }
7653 if (rect.GetRight() > client_right)
7654 rect.SetRight(client_right-1);
7655 }
7656
7657 editor->SetCellAttr(attr);
7658 editor->SetSize( rect );
7659 editor->Show( true, attr );
7660
7661 // recalc dimensions in case we need to
7662 // expand the scrolled window to account for editor
7663 CalcDimensions();
7664
7665 editor->BeginEdit(row, col, this);
7666 editor->SetCellAttr(NULL);
7667
7668 editor->DecRef();
7669 attr->DecRef();
7670 }
7671 }
7672 }
7673
7674
7675 void wxGrid::HideCellEditControl()
7676 {
7677 if ( IsCellEditControlEnabled() )
7678 {
7679 int row = m_currentCellCoords.GetRow();
7680 int col = m_currentCellCoords.GetCol();
7681
7682 wxGridCellAttr* attr = GetCellAttr(row, col);
7683 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7684 editor->Show( false );
7685 editor->DecRef();
7686 attr->DecRef();
7687 m_gridWin->SetFocus();
7688 // refresh whole row to the right
7689 wxRect rect( CellToRect(row, col) );
7690 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
7691 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
7692 m_gridWin->Refresh( false, &rect );
7693 }
7694 }
7695
7696
7697 void wxGrid::SaveEditControlValue()
7698 {
7699 if ( IsCellEditControlEnabled() )
7700 {
7701 int row = m_currentCellCoords.GetRow();
7702 int col = m_currentCellCoords.GetCol();
7703
7704 wxString oldval = GetCellValue(row,col);
7705
7706 wxGridCellAttr* attr = GetCellAttr(row, col);
7707 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7708 bool changed = editor->EndEdit(row, col, this);
7709
7710 editor->DecRef();
7711 attr->DecRef();
7712
7713 if (changed)
7714 {
7715 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
7716 m_currentCellCoords.GetRow(),
7717 m_currentCellCoords.GetCol() ) < 0 ) {
7718
7719 // Event has been vetoed, set the data back.
7720 SetCellValue(row,col,oldval);
7721 }
7722 }
7723 }
7724 }
7725
7726
7727 //
7728 // ------ Grid location functions
7729 // Note that all of these functions work with the logical coordinates of
7730 // grid cells and labels so you will need to convert from device
7731 // coordinates for mouse events etc.
7732 //
7733
7734 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
7735 {
7736 int row = YToRow(y);
7737 int col = XToCol(x);
7738
7739 if ( row == -1 || col == -1 )
7740 {
7741 coords = wxGridNoCellCoords;
7742 }
7743 else
7744 {
7745 coords.Set( row, col );
7746 }
7747 }
7748
7749
7750 // Internal Helper function for computing row or column from some
7751 // (unscrolled) coordinate value, using either
7752 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7753 // of m_rowBottoms/m_ColRights to speed up the search!
7754
7755 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
7756 const wxArrayInt& BorderArray, int nMax,
7757 bool clipToMinMax)
7758 {
7759
7760 if (coord < 0)
7761 return clipToMinMax && (nMax > 0) ? 0 : -1;
7762
7763
7764 if (!defaultDist)
7765 defaultDist = 1;
7766
7767 size_t i_max = coord / defaultDist,
7768 i_min = 0;
7769
7770 if (BorderArray.IsEmpty())
7771 {
7772 if((int) i_max < nMax)
7773 return i_max;
7774 return clipToMinMax ? nMax - 1 : -1;
7775 }
7776
7777 if ( i_max >= BorderArray.GetCount())
7778 i_max = BorderArray.GetCount() - 1;
7779 else
7780 {
7781 if ( coord >= BorderArray[i_max])
7782 {
7783 i_min = i_max;
7784 if (minDist)
7785 i_max = coord / minDist;
7786 else
7787 i_max = BorderArray.GetCount() - 1;
7788 }
7789 if ( i_max >= BorderArray.GetCount())
7790 i_max = BorderArray.GetCount() - 1;
7791 }
7792 if ( coord >= BorderArray[i_max])
7793 return clipToMinMax ? (int)i_max : -1;
7794 if ( coord < BorderArray[0] )
7795 return 0;
7796
7797 while ( i_max - i_min > 0 )
7798 {
7799 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
7800 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7801 if (coord >= BorderArray[ i_max - 1])
7802 return i_max;
7803 else
7804 i_max--;
7805 int median = i_min + (i_max - i_min + 1) / 2;
7806 if (coord < BorderArray[median])
7807 i_max = median;
7808 else
7809 i_min = median;
7810 }
7811 return i_max;
7812 }
7813
7814 int wxGrid::YToRow( int y )
7815 {
7816 return CoordToRowOrCol(y, m_defaultRowHeight,
7817 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
7818 }
7819
7820
7821 int wxGrid::XToCol( int x )
7822 {
7823 return CoordToRowOrCol(x, m_defaultColWidth,
7824 m_minAcceptableColWidth, m_colRights, m_numCols, false);
7825 }
7826
7827
7828 // return the row number that that the y coord is near the edge of, or
7829 // -1 if not near an edge
7830 //
7831 int wxGrid::YToEdgeOfRow( int y )
7832 {
7833 int i;
7834 i = internalYToRow(y);
7835
7836 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
7837 {
7838 // We know that we are in row i, test whether we are
7839 // close enough to lower or upper border, respectively.
7840 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
7841 return i;
7842 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
7843 return i - 1;
7844 }
7845
7846 return -1;
7847 }
7848
7849
7850 // return the col number that that the x coord is near the edge of, or
7851 // -1 if not near an edge
7852 //
7853 int wxGrid::XToEdgeOfCol( int x )
7854 {
7855 int i;
7856 i = internalXToCol(x);
7857
7858 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
7859 {
7860 // We know that we are in column i, test whether we are
7861 // close enough to right or left border, respectively.
7862 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
7863 return i;
7864 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
7865 return i - 1;
7866 }
7867
7868 return -1;
7869 }
7870
7871
7872 wxRect wxGrid::CellToRect( int row, int col )
7873 {
7874 wxRect rect( -1, -1, -1, -1 );
7875
7876 if ( row >= 0 && row < m_numRows &&
7877 col >= 0 && col < m_numCols )
7878 {
7879 int i, cell_rows, cell_cols;
7880 rect.width = rect.height = 0;
7881 GetCellSize( row, col, &cell_rows, &cell_cols );
7882 // if negative then find multicell owner
7883 if (cell_rows < 0) row += cell_rows;
7884 if (cell_cols < 0) col += cell_cols;
7885 GetCellSize( row, col, &cell_rows, &cell_cols );
7886
7887 rect.x = GetColLeft(col);
7888 rect.y = GetRowTop(row);
7889 for (i=col; i<col+cell_cols; i++)
7890 rect.width += GetColWidth(i);
7891 for (i=row; i<row+cell_rows; i++)
7892 rect.height += GetRowHeight(i);
7893 }
7894
7895 // if grid lines are enabled, then the area of the cell is a bit smaller
7896 if (m_gridLinesEnabled) {
7897 rect.width -= 1;
7898 rect.height -= 1;
7899 }
7900 return rect;
7901 }
7902
7903
7904 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
7905 {
7906 // get the cell rectangle in logical coords
7907 //
7908 wxRect r( CellToRect( row, col ) );
7909
7910 // convert to device coords
7911 //
7912 int left, top, right, bottom;
7913 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7914 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7915
7916 // check against the client area of the grid window
7917 //
7918 int cw, ch;
7919 m_gridWin->GetClientSize( &cw, &ch );
7920
7921 if ( wholeCellVisible )
7922 {
7923 // is the cell wholly visible ?
7924 //
7925 return ( left >= 0 && right <= cw &&
7926 top >= 0 && bottom <= ch );
7927 }
7928 else
7929 {
7930 // is the cell partly visible ?
7931 //
7932 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
7933 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
7934 }
7935 }
7936
7937
7938 // make the specified cell location visible by doing a minimal amount
7939 // of scrolling
7940 //
7941 void wxGrid::MakeCellVisible( int row, int col )
7942 {
7943
7944 int i;
7945 int xpos = -1, ypos = -1;
7946
7947 if ( row >= 0 && row < m_numRows &&
7948 col >= 0 && col < m_numCols )
7949 {
7950 // get the cell rectangle in logical coords
7951 //
7952 wxRect r( CellToRect( row, col ) );
7953
7954 // convert to device coords
7955 //
7956 int left, top, right, bottom;
7957 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7958 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7959
7960 int cw, ch;
7961 m_gridWin->GetClientSize( &cw, &ch );
7962
7963 if ( top < 0 )
7964 {
7965 ypos = r.GetTop();
7966 }
7967 else if ( bottom > ch )
7968 {
7969 int h = r.GetHeight();
7970 ypos = r.GetTop();
7971 for ( i = row-1; i >= 0; i-- )
7972 {
7973 int rowHeight = GetRowHeight(i);
7974 if ( h + rowHeight > ch )
7975 break;
7976
7977 h += rowHeight;
7978 ypos -= rowHeight;
7979 }
7980
7981 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7982 // have rounding errors (this is important, because if we do, we
7983 // might not scroll at all and some cells won't be redrawn)
7984 //
7985 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7986 // scroll unit...
7987 ypos += GRID_SCROLL_LINE_Y;
7988 }
7989
7990 if ( left < 0 )
7991 {
7992 xpos = r.GetLeft();
7993 }
7994 else if ( right > cw )
7995 {
7996 // position the view so that the cell is on the right
7997 int x0, y0;
7998 CalcUnscrolledPosition(0, 0, &x0, &y0);
7999 xpos = x0 + (right - cw);
8000
8001 // see comment for ypos above
8002 xpos += GRID_SCROLL_LINE_X;
8003 }
8004
8005 if ( xpos != -1 || ypos != -1 )
8006 {
8007 if ( xpos != -1 )
8008 xpos /= GRID_SCROLL_LINE_X;
8009 if ( ypos != -1 )
8010 ypos /= GRID_SCROLL_LINE_Y;
8011 Scroll( xpos, ypos );
8012 AdjustScrollbars();
8013 }
8014 }
8015 }
8016
8017
8018 //
8019 // ------ Grid cursor movement functions
8020 //
8021
8022 bool wxGrid::MoveCursorUp( bool expandSelection )
8023 {
8024 if ( m_currentCellCoords != wxGridNoCellCoords &&
8025 m_currentCellCoords.GetRow() >= 0 )
8026 {
8027 if ( expandSelection)
8028 {
8029 if ( m_selectingKeyboard == wxGridNoCellCoords )
8030 m_selectingKeyboard = m_currentCellCoords;
8031 if ( m_selectingKeyboard.GetRow() > 0 )
8032 {
8033 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8034 MakeCellVisible( m_selectingKeyboard.GetRow(),
8035 m_selectingKeyboard.GetCol() );
8036 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8037 }
8038 }
8039 else if ( m_currentCellCoords.GetRow() > 0 )
8040 {
8041 ClearSelection();
8042 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
8043 m_currentCellCoords.GetCol() );
8044 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
8045 m_currentCellCoords.GetCol() );
8046 }
8047 else
8048 return false;
8049 return true;
8050 }
8051
8052 return false;
8053 }
8054
8055
8056 bool wxGrid::MoveCursorDown( bool expandSelection )
8057 {
8058 if ( m_currentCellCoords != wxGridNoCellCoords &&
8059 m_currentCellCoords.GetRow() < m_numRows )
8060 {
8061 if ( expandSelection )
8062 {
8063 if ( m_selectingKeyboard == wxGridNoCellCoords )
8064 m_selectingKeyboard = m_currentCellCoords;
8065 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
8066 {
8067 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8068 MakeCellVisible( m_selectingKeyboard.GetRow(),
8069 m_selectingKeyboard.GetCol() );
8070 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8071 }
8072 }
8073 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
8074 {
8075 ClearSelection();
8076 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
8077 m_currentCellCoords.GetCol() );
8078 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
8079 m_currentCellCoords.GetCol() );
8080 }
8081 else
8082 return false;
8083 return true;
8084 }
8085
8086 return false;
8087 }
8088
8089
8090 bool wxGrid::MoveCursorLeft( bool expandSelection )
8091 {
8092 if ( m_currentCellCoords != wxGridNoCellCoords &&
8093 m_currentCellCoords.GetCol() >= 0 )
8094 {
8095 if ( expandSelection )
8096 {
8097 if ( m_selectingKeyboard == wxGridNoCellCoords )
8098 m_selectingKeyboard = m_currentCellCoords;
8099 if ( m_selectingKeyboard.GetCol() > 0 )
8100 {
8101 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8102 MakeCellVisible( m_selectingKeyboard.GetRow(),
8103 m_selectingKeyboard.GetCol() );
8104 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8105 }
8106 }
8107 else if ( m_currentCellCoords.GetCol() > 0 )
8108 {
8109 ClearSelection();
8110 MakeCellVisible( m_currentCellCoords.GetRow(),
8111 m_currentCellCoords.GetCol() - 1 );
8112 SetCurrentCell( m_currentCellCoords.GetRow(),
8113 m_currentCellCoords.GetCol() - 1 );
8114 }
8115 else
8116 return false;
8117 return true;
8118 }
8119
8120 return false;
8121 }
8122
8123
8124 bool wxGrid::MoveCursorRight( bool expandSelection )
8125 {
8126 if ( m_currentCellCoords != wxGridNoCellCoords &&
8127 m_currentCellCoords.GetCol() < m_numCols )
8128 {
8129 if ( expandSelection )
8130 {
8131 if ( m_selectingKeyboard == wxGridNoCellCoords )
8132 m_selectingKeyboard = m_currentCellCoords;
8133 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8134 {
8135 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8136 MakeCellVisible( m_selectingKeyboard.GetRow(),
8137 m_selectingKeyboard.GetCol() );
8138 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8139 }
8140 }
8141 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
8142 {
8143 ClearSelection();
8144 MakeCellVisible( m_currentCellCoords.GetRow(),
8145 m_currentCellCoords.GetCol() + 1 );
8146 SetCurrentCell( m_currentCellCoords.GetRow(),
8147 m_currentCellCoords.GetCol() + 1 );
8148 }
8149 else
8150 return false;
8151 return true;
8152 }
8153
8154 return false;
8155 }
8156
8157
8158 bool wxGrid::MovePageUp()
8159 {
8160 if ( m_currentCellCoords == wxGridNoCellCoords ) return false;
8161
8162 int row = m_currentCellCoords.GetRow();
8163 if ( row > 0 )
8164 {
8165 int cw, ch;
8166 m_gridWin->GetClientSize( &cw, &ch );
8167
8168 int y = GetRowTop(row);
8169 int newRow = internalYToRow( y - ch + 1 );
8170
8171 if ( newRow == row )
8172 {
8173 //row > 0 , so newrow can never be less than 0 here.
8174 newRow = row - 1;
8175 }
8176
8177 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8178 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8179
8180 return true;
8181 }
8182
8183 return false;
8184 }
8185
8186 bool wxGrid::MovePageDown()
8187 {
8188 if ( m_currentCellCoords == wxGridNoCellCoords ) return false;
8189
8190 int row = m_currentCellCoords.GetRow();
8191 if ( (row+1) < m_numRows )
8192 {
8193 int cw, ch;
8194 m_gridWin->GetClientSize( &cw, &ch );
8195
8196 int y = GetRowTop(row);
8197 int newRow = internalYToRow( y + ch );
8198 if ( newRow == row )
8199 {
8200 // row < m_numRows , so newrow can't overflow here.
8201 newRow = row + 1;
8202 }
8203
8204 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8205 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8206
8207 return true;
8208 }
8209
8210 return false;
8211 }
8212
8213 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
8214 {
8215 if ( m_table &&
8216 m_currentCellCoords != wxGridNoCellCoords &&
8217 m_currentCellCoords.GetRow() > 0 )
8218 {
8219 int row = m_currentCellCoords.GetRow();
8220 int col = m_currentCellCoords.GetCol();
8221
8222 if ( m_table->IsEmptyCell(row, col) )
8223 {
8224 // starting in an empty cell: find the next block of
8225 // non-empty cells
8226 //
8227 while ( row > 0 )
8228 {
8229 row-- ;
8230 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8231 }
8232 }
8233 else if ( m_table->IsEmptyCell(row-1, col) )
8234 {
8235 // starting at the top of a block: find the next block
8236 //
8237 row--;
8238 while ( row > 0 )
8239 {
8240 row-- ;
8241 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8242 }
8243 }
8244 else
8245 {
8246 // starting within a block: find the top of the block
8247 //
8248 while ( row > 0 )
8249 {
8250 row-- ;
8251 if ( m_table->IsEmptyCell(row, col) )
8252 {
8253 row++ ;
8254 break;
8255 }
8256 }
8257 }
8258
8259 MakeCellVisible( row, col );
8260 if ( expandSelection )
8261 {
8262 m_selectingKeyboard = wxGridCellCoords( row, col );
8263 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8264 }
8265 else
8266 {
8267 ClearSelection();
8268 SetCurrentCell( row, col );
8269 }
8270 return true;
8271 }
8272
8273 return false;
8274 }
8275
8276 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
8277 {
8278 if ( m_table &&
8279 m_currentCellCoords != wxGridNoCellCoords &&
8280 m_currentCellCoords.GetRow() < m_numRows-1 )
8281 {
8282 int row = m_currentCellCoords.GetRow();
8283 int col = m_currentCellCoords.GetCol();
8284
8285 if ( m_table->IsEmptyCell(row, col) )
8286 {
8287 // starting in an empty cell: find the next block of
8288 // non-empty cells
8289 //
8290 while ( row < m_numRows-1 )
8291 {
8292 row++ ;
8293 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8294 }
8295 }
8296 else if ( m_table->IsEmptyCell(row+1, col) )
8297 {
8298 // starting at the bottom of a block: find the next block
8299 //
8300 row++;
8301 while ( row < m_numRows-1 )
8302 {
8303 row++ ;
8304 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8305 }
8306 }
8307 else
8308 {
8309 // starting within a block: find the bottom of the block
8310 //
8311 while ( row < m_numRows-1 )
8312 {
8313 row++ ;
8314 if ( m_table->IsEmptyCell(row, col) )
8315 {
8316 row-- ;
8317 break;
8318 }
8319 }
8320 }
8321
8322 MakeCellVisible( row, col );
8323 if ( expandSelection )
8324 {
8325 m_selectingKeyboard = wxGridCellCoords( row, col );
8326 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8327 }
8328 else
8329 {
8330 ClearSelection();
8331 SetCurrentCell( row, col );
8332 }
8333
8334 return true;
8335 }
8336
8337 return false;
8338 }
8339
8340 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
8341 {
8342 if ( m_table &&
8343 m_currentCellCoords != wxGridNoCellCoords &&
8344 m_currentCellCoords.GetCol() > 0 )
8345 {
8346 int row = m_currentCellCoords.GetRow();
8347 int col = m_currentCellCoords.GetCol();
8348
8349 if ( m_table->IsEmptyCell(row, col) )
8350 {
8351 // starting in an empty cell: find the next block of
8352 // non-empty cells
8353 //
8354 while ( col > 0 )
8355 {
8356 col-- ;
8357 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8358 }
8359 }
8360 else if ( m_table->IsEmptyCell(row, col-1) )
8361 {
8362 // starting at the left of a block: find the next block
8363 //
8364 col--;
8365 while ( col > 0 )
8366 {
8367 col-- ;
8368 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8369 }
8370 }
8371 else
8372 {
8373 // starting within a block: find the left of the block
8374 //
8375 while ( col > 0 )
8376 {
8377 col-- ;
8378 if ( m_table->IsEmptyCell(row, col) )
8379 {
8380 col++ ;
8381 break;
8382 }
8383 }
8384 }
8385
8386 MakeCellVisible( row, col );
8387 if ( expandSelection )
8388 {
8389 m_selectingKeyboard = wxGridCellCoords( row, col );
8390 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8391 }
8392 else
8393 {
8394 ClearSelection();
8395 SetCurrentCell( row, col );
8396 }
8397
8398 return true;
8399 }
8400
8401 return false;
8402 }
8403
8404 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
8405 {
8406 if ( m_table &&
8407 m_currentCellCoords != wxGridNoCellCoords &&
8408 m_currentCellCoords.GetCol() < m_numCols-1 )
8409 {
8410 int row = m_currentCellCoords.GetRow();
8411 int col = m_currentCellCoords.GetCol();
8412
8413 if ( m_table->IsEmptyCell(row, col) )
8414 {
8415 // starting in an empty cell: find the next block of
8416 // non-empty cells
8417 //
8418 while ( col < m_numCols-1 )
8419 {
8420 col++ ;
8421 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8422 }
8423 }
8424 else if ( m_table->IsEmptyCell(row, col+1) )
8425 {
8426 // starting at the right of a block: find the next block
8427 //
8428 col++;
8429 while ( col < m_numCols-1 )
8430 {
8431 col++ ;
8432 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8433 }
8434 }
8435 else
8436 {
8437 // starting within a block: find the right of the block
8438 //
8439 while ( col < m_numCols-1 )
8440 {
8441 col++ ;
8442 if ( m_table->IsEmptyCell(row, col) )
8443 {
8444 col-- ;
8445 break;
8446 }
8447 }
8448 }
8449
8450 MakeCellVisible( row, col );
8451 if ( expandSelection )
8452 {
8453 m_selectingKeyboard = wxGridCellCoords( row, col );
8454 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8455 }
8456 else
8457 {
8458 ClearSelection();
8459 SetCurrentCell( row, col );
8460 }
8461
8462 return true;
8463 }
8464
8465 return false;
8466 }
8467
8468
8469
8470 //
8471 // ------ Label values and formatting
8472 //
8473
8474 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
8475 {
8476 *horiz = m_rowLabelHorizAlign;
8477 *vert = m_rowLabelVertAlign;
8478 }
8479
8480 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
8481 {
8482 *horiz = m_colLabelHorizAlign;
8483 *vert = m_colLabelVertAlign;
8484 }
8485
8486 int wxGrid::GetColLabelTextOrientation()
8487 {
8488 return m_colLabelTextOrientation;
8489 }
8490
8491 wxString wxGrid::GetRowLabelValue( int row )
8492 {
8493 if ( m_table )
8494 {
8495 return m_table->GetRowLabelValue( row );
8496 }
8497 else
8498 {
8499 wxString s;
8500 s << row;
8501 return s;
8502 }
8503 }
8504
8505 wxString wxGrid::GetColLabelValue( int col )
8506 {
8507 if ( m_table )
8508 {
8509 return m_table->GetColLabelValue( col );
8510 }
8511 else
8512 {
8513 wxString s;
8514 s << col;
8515 return s;
8516 }
8517 }
8518
8519
8520 void wxGrid::SetRowLabelSize( int width )
8521 {
8522 width = wxMax( width, 0 );
8523 if ( width != m_rowLabelWidth )
8524 {
8525 if ( width == 0 )
8526 {
8527 m_rowLabelWin->Show( false );
8528 m_cornerLabelWin->Show( false );
8529 }
8530 else if ( m_rowLabelWidth == 0 )
8531 {
8532 m_rowLabelWin->Show( true );
8533 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( true );
8534 }
8535
8536 m_rowLabelWidth = width;
8537 CalcWindowSizes();
8538 wxScrolledWindow::Refresh( true );
8539 }
8540 }
8541
8542
8543 void wxGrid::SetColLabelSize( int height )
8544 {
8545 height = wxMax( height, 0 );
8546 if ( height != m_colLabelHeight )
8547 {
8548 if ( height == 0 )
8549 {
8550 m_colLabelWin->Show( false );
8551 m_cornerLabelWin->Show( false );
8552 }
8553 else if ( m_colLabelHeight == 0 )
8554 {
8555 m_colLabelWin->Show( true );
8556 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( true );
8557 }
8558
8559 m_colLabelHeight = height;
8560 CalcWindowSizes();
8561 wxScrolledWindow::Refresh( true );
8562 }
8563 }
8564
8565
8566 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
8567 {
8568 if ( m_labelBackgroundColour != colour )
8569 {
8570 m_labelBackgroundColour = colour;
8571 m_rowLabelWin->SetBackgroundColour( colour );
8572 m_colLabelWin->SetBackgroundColour( colour );
8573 m_cornerLabelWin->SetBackgroundColour( colour );
8574
8575 if ( !GetBatchCount() )
8576 {
8577 m_rowLabelWin->Refresh();
8578 m_colLabelWin->Refresh();
8579 m_cornerLabelWin->Refresh();
8580 }
8581 }
8582 }
8583
8584 void wxGrid::SetLabelTextColour( const wxColour& colour )
8585 {
8586 if ( m_labelTextColour != colour )
8587 {
8588 m_labelTextColour = colour;
8589 if ( !GetBatchCount() )
8590 {
8591 m_rowLabelWin->Refresh();
8592 m_colLabelWin->Refresh();
8593 }
8594 }
8595 }
8596
8597 void wxGrid::SetLabelFont( const wxFont& font )
8598 {
8599 m_labelFont = font;
8600 if ( !GetBatchCount() )
8601 {
8602 m_rowLabelWin->Refresh();
8603 m_colLabelWin->Refresh();
8604 }
8605 }
8606
8607 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
8608 {
8609 // allow old (incorrect) defs to be used
8610 switch ( horiz )
8611 {
8612 case wxLEFT: horiz = wxALIGN_LEFT; break;
8613 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8614 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8615 }
8616
8617 switch ( vert )
8618 {
8619 case wxTOP: vert = wxALIGN_TOP; break;
8620 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8621 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8622 }
8623
8624 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8625 {
8626 m_rowLabelHorizAlign = horiz;
8627 }
8628
8629 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8630 {
8631 m_rowLabelVertAlign = vert;
8632 }
8633
8634 if ( !GetBatchCount() )
8635 {
8636 m_rowLabelWin->Refresh();
8637 }
8638 }
8639
8640 void wxGrid::SetColLabelAlignment( int horiz, int vert )
8641 {
8642 // allow old (incorrect) defs to be used
8643 switch ( horiz )
8644 {
8645 case wxLEFT: horiz = wxALIGN_LEFT; break;
8646 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8647 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8648 }
8649
8650 switch ( vert )
8651 {
8652 case wxTOP: vert = wxALIGN_TOP; break;
8653 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8654 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8655 }
8656
8657 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8658 {
8659 m_colLabelHorizAlign = horiz;
8660 }
8661
8662 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8663 {
8664 m_colLabelVertAlign = vert;
8665 }
8666
8667 if ( !GetBatchCount() )
8668 {
8669 m_colLabelWin->Refresh();
8670 }
8671 }
8672
8673 // Note: under MSW, the default column label font must be changed because it
8674 // does not support vertical printing
8675 //
8676 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8677 // pGrid->SetLabelFont(font);
8678 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8679 //
8680 void wxGrid::SetColLabelTextOrientation( int textOrientation )
8681 {
8682 if( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
8683 {
8684 m_colLabelTextOrientation = textOrientation;
8685 }
8686
8687 if ( !GetBatchCount() )
8688 {
8689 m_colLabelWin->Refresh();
8690 }
8691 }
8692
8693 void wxGrid::SetRowLabelValue( int row, const wxString& s )
8694 {
8695 if ( m_table )
8696 {
8697 m_table->SetRowLabelValue( row, s );
8698 if ( !GetBatchCount() )
8699 {
8700 wxRect rect = CellToRect( row, 0);
8701 if ( rect.height > 0 )
8702 {
8703 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
8704 rect.x = 0;
8705 rect.width = m_rowLabelWidth;
8706 m_rowLabelWin->Refresh( true, &rect );
8707 }
8708 }
8709 }
8710 }
8711
8712 void wxGrid::SetColLabelValue( int col, const wxString& s )
8713 {
8714 if ( m_table )
8715 {
8716 m_table->SetColLabelValue( col, s );
8717 if ( !GetBatchCount() )
8718 {
8719 wxRect rect = CellToRect( 0, col );
8720 if ( rect.width > 0 )
8721 {
8722 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
8723 rect.y = 0;
8724 rect.height = m_colLabelHeight;
8725 m_colLabelWin->Refresh( true, &rect );
8726 }
8727 }
8728 }
8729 }
8730
8731 void wxGrid::SetGridLineColour( const wxColour& colour )
8732 {
8733 if ( m_gridLineColour != colour )
8734 {
8735 m_gridLineColour = colour;
8736
8737 wxClientDC dc( m_gridWin );
8738 PrepareDC( dc );
8739 DrawAllGridLines( dc, wxRegion() );
8740 }
8741 }
8742
8743
8744 void wxGrid::SetCellHighlightColour( const wxColour& colour )
8745 {
8746 if ( m_cellHighlightColour != colour )
8747 {
8748 m_cellHighlightColour = colour;
8749
8750 wxClientDC dc( m_gridWin );
8751 PrepareDC( dc );
8752 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
8753 DrawCellHighlight(dc, attr);
8754 attr->DecRef();
8755 }
8756 }
8757
8758 void wxGrid::SetCellHighlightPenWidth(int width)
8759 {
8760 if (m_cellHighlightPenWidth != width) {
8761 m_cellHighlightPenWidth = width;
8762
8763 // Just redrawing the cell highlight is not enough since that won't
8764 // make any visible change if the the thickness is getting smaller.
8765 int row = m_currentCellCoords.GetRow();
8766 int col = m_currentCellCoords.GetCol();
8767 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8768 return;
8769 wxRect rect = CellToRect(row, col);
8770 m_gridWin->Refresh(true, &rect);
8771 }
8772 }
8773
8774 void wxGrid::SetCellHighlightROPenWidth(int width)
8775 {
8776 if (m_cellHighlightROPenWidth != width) {
8777 m_cellHighlightROPenWidth = width;
8778
8779 // Just redrawing the cell highlight is not enough since that won't
8780 // make any visible change if the the thickness is getting smaller.
8781 int row = m_currentCellCoords.GetRow();
8782 int col = m_currentCellCoords.GetCol();
8783 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8784 return;
8785 wxRect rect = CellToRect(row, col);
8786 m_gridWin->Refresh(true, &rect);
8787 }
8788 }
8789
8790 void wxGrid::EnableGridLines( bool enable )
8791 {
8792 if ( enable != m_gridLinesEnabled )
8793 {
8794 m_gridLinesEnabled = enable;
8795
8796 if ( !GetBatchCount() )
8797 {
8798 if ( enable )
8799 {
8800 wxClientDC dc( m_gridWin );
8801 PrepareDC( dc );
8802 DrawAllGridLines( dc, wxRegion() );
8803 }
8804 else
8805 {
8806 m_gridWin->Refresh();
8807 }
8808 }
8809 }
8810 }
8811
8812
8813 int wxGrid::GetDefaultRowSize()
8814 {
8815 return m_defaultRowHeight;
8816 }
8817
8818 int wxGrid::GetRowSize( int row )
8819 {
8820 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
8821
8822 return GetRowHeight(row);
8823 }
8824
8825 int wxGrid::GetDefaultColSize()
8826 {
8827 return m_defaultColWidth;
8828 }
8829
8830 int wxGrid::GetColSize( int col )
8831 {
8832 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
8833
8834 return GetColWidth(col);
8835 }
8836
8837 // ============================================================================
8838 // access to the grid attributes: each of them has a default value in the grid
8839 // itself and may be overidden on a per-cell basis
8840 // ============================================================================
8841
8842 // ----------------------------------------------------------------------------
8843 // setting default attributes
8844 // ----------------------------------------------------------------------------
8845
8846 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
8847 {
8848 m_defaultCellAttr->SetBackgroundColour(col);
8849 #ifdef __WXGTK__
8850 m_gridWin->SetBackgroundColour(col);
8851 #endif
8852 }
8853
8854 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
8855 {
8856 m_defaultCellAttr->SetTextColour(col);
8857 }
8858
8859 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
8860 {
8861 m_defaultCellAttr->SetAlignment(horiz, vert);
8862 }
8863
8864 void wxGrid::SetDefaultCellOverflow( bool allow )
8865 {
8866 m_defaultCellAttr->SetOverflow(allow);
8867 }
8868
8869 void wxGrid::SetDefaultCellFont( const wxFont& font )
8870 {
8871 m_defaultCellAttr->SetFont(font);
8872 }
8873
8874 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
8875 {
8876 m_defaultCellAttr->SetRenderer(renderer);
8877 }
8878
8879 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
8880 {
8881 m_defaultCellAttr->SetEditor(editor);
8882 }
8883
8884 // ----------------------------------------------------------------------------
8885 // access to the default attrbiutes
8886 // ----------------------------------------------------------------------------
8887
8888 wxColour wxGrid::GetDefaultCellBackgroundColour()
8889 {
8890 return m_defaultCellAttr->GetBackgroundColour();
8891 }
8892
8893 wxColour wxGrid::GetDefaultCellTextColour()
8894 {
8895 return m_defaultCellAttr->GetTextColour();
8896 }
8897
8898 wxFont wxGrid::GetDefaultCellFont()
8899 {
8900 return m_defaultCellAttr->GetFont();
8901 }
8902
8903 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
8904 {
8905 m_defaultCellAttr->GetAlignment(horiz, vert);
8906 }
8907
8908 bool wxGrid::GetDefaultCellOverflow()
8909 {
8910 return m_defaultCellAttr->GetOverflow();
8911 }
8912
8913 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
8914 {
8915 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
8916 }
8917
8918 wxGridCellEditor *wxGrid::GetDefaultEditor() const
8919 {
8920 return m_defaultCellAttr->GetEditor(NULL,0,0);
8921 }
8922
8923 // ----------------------------------------------------------------------------
8924 // access to cell attributes
8925 // ----------------------------------------------------------------------------
8926
8927 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
8928 {
8929 wxGridCellAttr *attr = GetCellAttr(row, col);
8930 wxColour colour = attr->GetBackgroundColour();
8931 attr->DecRef();
8932 return colour;
8933 }
8934
8935 wxColour wxGrid::GetCellTextColour( int row, int col )
8936 {
8937 wxGridCellAttr *attr = GetCellAttr(row, col);
8938 wxColour colour = attr->GetTextColour();
8939 attr->DecRef();
8940 return colour;
8941 }
8942
8943 wxFont wxGrid::GetCellFont( int row, int col )
8944 {
8945 wxGridCellAttr *attr = GetCellAttr(row, col);
8946 wxFont font = attr->GetFont();
8947 attr->DecRef();
8948 return font;
8949 }
8950
8951 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
8952 {
8953 wxGridCellAttr *attr = GetCellAttr(row, col);
8954 attr->GetAlignment(horiz, vert);
8955 attr->DecRef();
8956 }
8957
8958 bool wxGrid::GetCellOverflow( int row, int col )
8959 {
8960 wxGridCellAttr *attr = GetCellAttr(row, col);
8961 bool allow = attr->GetOverflow();
8962 attr->DecRef();
8963 return allow;
8964 }
8965
8966 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
8967 {
8968 wxGridCellAttr *attr = GetCellAttr(row, col);
8969 attr->GetSize( num_rows, num_cols );
8970 attr->DecRef();
8971 }
8972
8973 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
8974 {
8975 wxGridCellAttr* attr = GetCellAttr(row, col);
8976 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
8977 attr->DecRef();
8978
8979 return renderer;
8980 }
8981
8982 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
8983 {
8984 wxGridCellAttr* attr = GetCellAttr(row, col);
8985 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8986 attr->DecRef();
8987
8988 return editor;
8989 }
8990
8991 bool wxGrid::IsReadOnly(int row, int col) const
8992 {
8993 wxGridCellAttr* attr = GetCellAttr(row, col);
8994 bool isReadOnly = attr->IsReadOnly();
8995 attr->DecRef();
8996 return isReadOnly;
8997 }
8998
8999 // ----------------------------------------------------------------------------
9000 // attribute support: cache, automatic provider creation, ...
9001 // ----------------------------------------------------------------------------
9002
9003 bool wxGrid::CanHaveAttributes()
9004 {
9005 if ( !m_table )
9006 {
9007 return false;
9008 }
9009
9010 return m_table->CanHaveAttributes();
9011 }
9012
9013 void wxGrid::ClearAttrCache()
9014 {
9015 if ( m_attrCache.row != -1 )
9016 {
9017 wxSafeDecRef(m_attrCache.attr);
9018 m_attrCache.attr = NULL;
9019 m_attrCache.row = -1;
9020 }
9021 }
9022
9023 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9024 {
9025 if ( attr != NULL )
9026 {
9027 wxGrid *self = (wxGrid *)this; // const_cast
9028
9029 self->ClearAttrCache();
9030 self->m_attrCache.row = row;
9031 self->m_attrCache.col = col;
9032 self->m_attrCache.attr = attr;
9033 wxSafeIncRef(attr);
9034 }
9035 }
9036
9037 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9038 {
9039 if ( row == m_attrCache.row && col == m_attrCache.col )
9040 {
9041 *attr = m_attrCache.attr;
9042 wxSafeIncRef(m_attrCache.attr);
9043
9044 #ifdef DEBUG_ATTR_CACHE
9045 gs_nAttrCacheHits++;
9046 #endif
9047
9048 return true;
9049 }
9050 else
9051 {
9052 #ifdef DEBUG_ATTR_CACHE
9053 gs_nAttrCacheMisses++;
9054 #endif
9055 return false;
9056 }
9057 }
9058
9059 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9060 {
9061 wxGridCellAttr *attr = NULL;
9062 // Additional test to avoid looking at the cache e.g. for
9063 // wxNoCellCoords, as this will confuse memory management.
9064 if ( row >= 0 )
9065 {
9066 if ( !LookupAttr(row, col, &attr) )
9067 {
9068 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
9069 : (wxGridCellAttr *)NULL;
9070 CacheAttr(row, col, attr);
9071 }
9072 }
9073 if (attr)
9074 {
9075 attr->SetDefAttr(m_defaultCellAttr);
9076 }
9077 else
9078 {
9079 attr = m_defaultCellAttr;
9080 attr->IncRef();
9081 }
9082
9083 return attr;
9084 }
9085
9086 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9087 {
9088 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
9089
9090 wxCHECK_MSG( m_table, attr,
9091 _T("we may only be called if CanHaveAttributes() returned true and then m_table should be !NULL") );
9092
9093 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9094 if ( !attr )
9095 {
9096 attr = new wxGridCellAttr(m_defaultCellAttr);
9097
9098 // artificially inc the ref count to match DecRef() in caller
9099 attr->IncRef();
9100 m_table->SetAttr(attr, row, col);
9101 }
9102
9103 return attr;
9104 }
9105
9106 // ----------------------------------------------------------------------------
9107 // setting column attributes (wrappers around SetColAttr)
9108 // ----------------------------------------------------------------------------
9109
9110 void wxGrid::SetColFormatBool(int col)
9111 {
9112 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9113 }
9114
9115 void wxGrid::SetColFormatNumber(int col)
9116 {
9117 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9118 }
9119
9120 void wxGrid::SetColFormatFloat(int col, int width, int precision)
9121 {
9122 wxString typeName = wxGRID_VALUE_FLOAT;
9123 if ( (width != -1) || (precision != -1) )
9124 {
9125 typeName << _T(':') << width << _T(',') << precision;
9126 }
9127
9128 SetColFormatCustom(col, typeName);
9129 }
9130
9131 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9132 {
9133 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
9134 if(!attr)
9135 attr = new wxGridCellAttr;
9136 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9137 attr->SetRenderer(renderer);
9138
9139 SetColAttr(col, attr);
9140
9141 }
9142
9143 // ----------------------------------------------------------------------------
9144 // setting cell attributes: this is forwarded to the table
9145 // ----------------------------------------------------------------------------
9146
9147 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9148 {
9149 if ( CanHaveAttributes() )
9150 {
9151 m_table->SetAttr(attr, row, col);
9152 ClearAttrCache();
9153 }
9154 else
9155 {
9156 wxSafeDecRef(attr);
9157 }
9158 }
9159
9160 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9161 {
9162 if ( CanHaveAttributes() )
9163 {
9164 m_table->SetRowAttr(attr, row);
9165 ClearAttrCache();
9166 }
9167 else
9168 {
9169 wxSafeDecRef(attr);
9170 }
9171 }
9172
9173 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
9174 {
9175 if ( CanHaveAttributes() )
9176 {
9177 m_table->SetColAttr(attr, col);
9178 ClearAttrCache();
9179 }
9180 else
9181 {
9182 wxSafeDecRef(attr);
9183 }
9184 }
9185
9186 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
9187 {
9188 if ( CanHaveAttributes() )
9189 {
9190 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9191 attr->SetBackgroundColour(colour);
9192 attr->DecRef();
9193 }
9194 }
9195
9196 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
9197 {
9198 if ( CanHaveAttributes() )
9199 {
9200 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9201 attr->SetTextColour(colour);
9202 attr->DecRef();
9203 }
9204 }
9205
9206 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
9207 {
9208 if ( CanHaveAttributes() )
9209 {
9210 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9211 attr->SetFont(font);
9212 attr->DecRef();
9213 }
9214 }
9215
9216 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
9217 {
9218 if ( CanHaveAttributes() )
9219 {
9220 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9221 attr->SetAlignment(horiz, vert);
9222 attr->DecRef();
9223 }
9224 }
9225
9226 void wxGrid::SetCellOverflow( int row, int col, bool allow )
9227 {
9228 if ( CanHaveAttributes() )
9229 {
9230 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9231 attr->SetOverflow(allow);
9232 attr->DecRef();
9233 }
9234 }
9235
9236 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
9237 {
9238 if ( CanHaveAttributes() )
9239 {
9240 int cell_rows, cell_cols;
9241
9242 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9243 attr->GetSize(&cell_rows, &cell_cols);
9244 attr->SetSize(num_rows, num_cols);
9245 attr->DecRef();
9246
9247 // Cannot set the size of a cell to 0 or negative values
9248 // While it is perfectly legal to do that, this function cannot
9249 // handle all the possibilies, do it by hand by getting the CellAttr.
9250 // You can only set the size of a cell to 1,1 or greater with this fn
9251 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
9252 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9253 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
9254 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9255
9256 // if this was already a multicell then "turn off" the other cells first
9257 if ((cell_rows > 1) || (cell_rows > 1))
9258 {
9259 int i, j;
9260 for (j=row; j<row+cell_rows; j++)
9261 {
9262 for (i=col; i<col+cell_cols; i++)
9263 {
9264 if ((i != col) || (j != row))
9265 {
9266 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9267 attr_stub->SetSize( 1, 1 );
9268 attr_stub->DecRef();
9269 }
9270 }
9271 }
9272 }
9273
9274 // mark the cells that will be covered by this cell to
9275 // negative or zero values to point back at this cell
9276 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
9277 {
9278 int i, j;
9279 for (j=row; j<row+num_rows; j++)
9280 {
9281 for (i=col; i<col+num_cols; i++)
9282 {
9283 if ((i != col) || (j != row))
9284 {
9285 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9286 attr_stub->SetSize( row-j, col-i );
9287 attr_stub->DecRef();
9288 }
9289 }
9290 }
9291 }
9292 }
9293 }
9294
9295 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
9296 {
9297 if ( CanHaveAttributes() )
9298 {
9299 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9300 attr->SetRenderer(renderer);
9301 attr->DecRef();
9302 }
9303 }
9304
9305 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
9306 {
9307 if ( CanHaveAttributes() )
9308 {
9309 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9310 attr->SetEditor(editor);
9311 attr->DecRef();
9312 }
9313 }
9314
9315 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
9316 {
9317 if ( CanHaveAttributes() )
9318 {
9319 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9320 attr->SetReadOnly(isReadOnly);
9321 attr->DecRef();
9322 }
9323 }
9324
9325 // ----------------------------------------------------------------------------
9326 // Data type registration
9327 // ----------------------------------------------------------------------------
9328
9329 void wxGrid::RegisterDataType(const wxString& typeName,
9330 wxGridCellRenderer* renderer,
9331 wxGridCellEditor* editor)
9332 {
9333 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
9334 }
9335
9336
9337 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
9338 {
9339 wxString typeName = m_table->GetTypeName(row, col);
9340 return GetDefaultEditorForType(typeName);
9341 }
9342
9343 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
9344 {
9345 wxString typeName = m_table->GetTypeName(row, col);
9346 return GetDefaultRendererForType(typeName);
9347 }
9348
9349 wxGridCellEditor*
9350 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
9351 {
9352 int index = m_typeRegistry->FindOrCloneDataType(typeName);
9353 if ( index == wxNOT_FOUND )
9354 {
9355 wxFAIL_MSG(wxT("Unknown data type name"));
9356
9357 return NULL;
9358 }
9359
9360 return m_typeRegistry->GetEditor(index);
9361 }
9362
9363 wxGridCellRenderer*
9364 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
9365 {
9366 int index = m_typeRegistry->FindOrCloneDataType(typeName);
9367 if ( index == wxNOT_FOUND )
9368 {
9369 wxFAIL_MSG(wxT("Unknown data type name"));
9370
9371 return NULL;
9372 }
9373
9374 return m_typeRegistry->GetRenderer(index);
9375 }
9376
9377
9378 // ----------------------------------------------------------------------------
9379 // row/col size
9380 // ----------------------------------------------------------------------------
9381
9382 void wxGrid::EnableDragRowSize( bool enable )
9383 {
9384 m_canDragRowSize = enable;
9385 }
9386
9387
9388 void wxGrid::EnableDragColSize( bool enable )
9389 {
9390 m_canDragColSize = enable;
9391 }
9392
9393 void wxGrid::EnableDragGridSize( bool enable )
9394 {
9395 m_canDragGridSize = enable;
9396 }
9397
9398
9399 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
9400 {
9401 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
9402
9403 if ( resizeExistingRows )
9404 {
9405 // since we are resizing all rows to the default row size,
9406 // we can simply clear the row heights and row bottoms
9407 // arrays (which also allows us to take advantage of
9408 // some speed optimisations)
9409 m_rowHeights.Empty();
9410 m_rowBottoms.Empty();
9411 if ( !GetBatchCount() )
9412 CalcDimensions();
9413 }
9414 }
9415
9416 void wxGrid::SetRowSize( int row, int height )
9417 {
9418 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
9419
9420 // See comment in SetColSize
9421 if ( height < GetRowMinimalAcceptableHeight()) { return; }
9422
9423 if ( m_rowHeights.IsEmpty() )
9424 {
9425 // need to really create the array
9426 InitRowHeights();
9427 }
9428
9429 int h = wxMax( 0, height );
9430 int diff = h - m_rowHeights[row];
9431
9432 m_rowHeights[row] = h;
9433 int i;
9434 for ( i = row; i < m_numRows; i++ )
9435 {
9436 m_rowBottoms[i] += diff;
9437 }
9438 if ( !GetBatchCount() )
9439 CalcDimensions();
9440 }
9441
9442 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
9443 {
9444 m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
9445
9446 if ( resizeExistingCols )
9447 {
9448 // since we are resizing all columns to the default column size,
9449 // we can simply clear the col widths and col rights
9450 // arrays (which also allows us to take advantage of
9451 // some speed optimisations)
9452 m_colWidths.Empty();
9453 m_colRights.Empty();
9454 if ( !GetBatchCount() )
9455 CalcDimensions();
9456 }
9457 }
9458
9459 void wxGrid::SetColSize( int col, int width )
9460 {
9461 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
9462
9463 // should we check that it's bigger than GetColMinimalWidth(col) here?
9464 // (VZ)
9465 // No, because it is reasonable to assume the library user know's
9466 // what he is doing. However whe should test against the weaker
9467 // constariant of minimalAcceptableWidth, as this breaks rendering
9468 //
9469 // This test then fixes sf.net bug #645734
9470
9471 if ( width < GetColMinimalAcceptableWidth()) { return; }
9472
9473 if ( m_colWidths.IsEmpty() )
9474 {
9475 // need to really create the array
9476 InitColWidths();
9477 }
9478
9479 // if < 0 calc new width from label
9480 if( width < 0 )
9481 {
9482 long w, h;
9483 wxArrayString lines;
9484 wxClientDC dc(m_colLabelWin);
9485 dc.SetFont(GetLabelFont());
9486 StringToLines(GetColLabelValue(col), lines);
9487 GetTextBoxSize(dc, lines, &w, &h);
9488 width = w + 6;
9489 }
9490 int w = wxMax( 0, width );
9491 int diff = w - m_colWidths[col];
9492 m_colWidths[col] = w;
9493
9494 int i;
9495 for ( i = col; i < m_numCols; i++ )
9496 {
9497 m_colRights[i] += diff;
9498 }
9499 if ( !GetBatchCount() )
9500 CalcDimensions();
9501 }
9502
9503
9504 void wxGrid::SetColMinimalWidth( int col, int width )
9505 {
9506 if (width > GetColMinimalAcceptableWidth()) {
9507 m_colMinWidths[col] = width;
9508 }
9509 }
9510
9511 void wxGrid::SetRowMinimalHeight( int row, int width )
9512 {
9513 if (width > GetRowMinimalAcceptableHeight()) {
9514 m_rowMinHeights[row] = width;
9515 }
9516 }
9517
9518 int wxGrid::GetColMinimalWidth(int col) const
9519 {
9520 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(col);
9521 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
9522 }
9523
9524 int wxGrid::GetRowMinimalHeight(int row) const
9525 {
9526 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(row);
9527 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
9528 }
9529
9530 void wxGrid::SetColMinimalAcceptableWidth( int width )
9531 {
9532 // We do allow a width of 0 since this gives us
9533 // an easy way to temporarily hidding columns.
9534 if ( width<0 )
9535 return;
9536 m_minAcceptableColWidth = width;
9537 }
9538
9539 void wxGrid::SetRowMinimalAcceptableHeight( int height )
9540 {
9541 // We do allow a height of 0 since this gives us
9542 // an easy way to temporarily hidding rows.
9543 if ( height<0 )
9544 return;
9545 m_minAcceptableRowHeight = height;
9546 };
9547
9548 int wxGrid::GetColMinimalAcceptableWidth() const
9549 {
9550 return m_minAcceptableColWidth;
9551 }
9552
9553 int wxGrid::GetRowMinimalAcceptableHeight() const
9554 {
9555 return m_minAcceptableRowHeight;
9556 }
9557
9558 // ----------------------------------------------------------------------------
9559 // auto sizing
9560 // ----------------------------------------------------------------------------
9561
9562 void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
9563 {
9564 wxClientDC dc(m_gridWin);
9565
9566 //Cancel editting of cell
9567 HideCellEditControl();
9568 SaveEditControlValue();
9569
9570 // init both of them to avoid compiler warnings, even if weo nly need one
9571 int row = -1,
9572 col = -1;
9573 if ( column )
9574 col = colOrRow;
9575 else
9576 row = colOrRow;
9577
9578 wxCoord extent, extentMax = 0;
9579 int max = column ? m_numRows : m_numCols;
9580 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
9581 {
9582 if ( column )
9583 row = rowOrCol;
9584 else
9585 col = rowOrCol;
9586
9587 wxGridCellAttr* attr = GetCellAttr(row, col);
9588 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9589 if ( renderer )
9590 {
9591 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
9592 extent = column ? size.x : size.y;
9593 if ( extent > extentMax )
9594 {
9595 extentMax = extent;
9596 }
9597
9598 renderer->DecRef();
9599 }
9600
9601 attr->DecRef();
9602 }
9603
9604 // now also compare with the column label extent
9605 wxCoord w, h;
9606 dc.SetFont( GetLabelFont() );
9607
9608 if ( column )
9609 {
9610 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
9611 if( GetColLabelTextOrientation() == wxVERTICAL )
9612 w = h;
9613 }
9614 else
9615 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
9616
9617 extent = column ? w : h;
9618 if ( extent > extentMax )
9619 {
9620 extentMax = extent;
9621 }
9622
9623 if ( !extentMax )
9624 {
9625 // empty column - give default extent (notice that if extentMax is less
9626 // than default extent but != 0, it's ok)
9627 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
9628 }
9629 else
9630 {
9631 if ( column )
9632 {
9633 // leave some space around text
9634 extentMax += 10;
9635 }
9636 else
9637 {
9638 extentMax += 6;
9639 }
9640 }
9641
9642 if ( column )
9643 {
9644 SetColSize(col, extentMax);
9645 if ( !GetBatchCount() )
9646 {
9647 int cw, ch, dummy;
9648 m_gridWin->GetClientSize( &cw, &ch );
9649 wxRect rect ( CellToRect( 0, col ) );
9650 rect.y = 0;
9651 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
9652 rect.width = cw - rect.x;
9653 rect.height = m_colLabelHeight;
9654 m_colLabelWin->Refresh( true, &rect );
9655 }
9656 }
9657 else
9658 {
9659 SetRowSize(row, extentMax);
9660 if ( !GetBatchCount() )
9661 {
9662 int cw, ch, dummy;
9663 m_gridWin->GetClientSize( &cw, &ch );
9664 wxRect rect ( CellToRect( row, 0 ) );
9665 rect.x = 0;
9666 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9667 rect.width = m_rowLabelWidth;
9668 rect.height = ch - rect.y;
9669 m_rowLabelWin->Refresh( true, &rect );
9670 }
9671 }
9672 if ( setAsMin )
9673 {
9674 if ( column )
9675 SetColMinimalWidth(col, extentMax);
9676 else
9677 SetRowMinimalHeight(row, extentMax);
9678 }
9679 }
9680
9681 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
9682 {
9683 int width = m_rowLabelWidth;
9684
9685 if ( !calcOnly )
9686 BeginBatch();
9687
9688 for ( int col = 0; col < m_numCols; col++ )
9689 {
9690 if ( !calcOnly )
9691 {
9692 AutoSizeColumn(col, setAsMin);
9693 }
9694
9695 width += GetColWidth(col);
9696 }
9697
9698 if ( !calcOnly )
9699 EndBatch();
9700
9701 return width;
9702 }
9703
9704 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
9705 {
9706 int height = m_colLabelHeight;
9707
9708 if ( !calcOnly )
9709 BeginBatch();
9710
9711 for ( int row = 0; row < m_numRows; row++ )
9712 {
9713 if ( !calcOnly )
9714 {
9715 AutoSizeRow(row, setAsMin);
9716 }
9717
9718 height += GetRowHeight(row);
9719 }
9720
9721 if ( !calcOnly )
9722 EndBatch();
9723
9724 return height;
9725 }
9726
9727 void wxGrid::AutoSize()
9728 {
9729 BeginBatch();
9730
9731 wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9732
9733 // round up the size to a multiple of scroll step - this ensures that we
9734 // won't get the scrollbars if we're sized exactly to this width
9735 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9736 // scrollbar steps
9737 wxSize sizeFit(GetScrollX(size.x + m_extraWidth + 1) * GRID_SCROLL_LINE_X,
9738 GetScrollY(size.y + m_extraHeight + 1) * GRID_SCROLL_LINE_Y);
9739
9740 // distribute the extra space between the columns/rows to avoid having
9741 // extra white space
9742
9743 // Remove the extra m_extraWidth + 1 added above
9744 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
9745 if ( diff && m_numCols )
9746 {
9747 // try to resize the columns uniformly
9748 wxCoord diffPerCol = diff / m_numCols;
9749 if ( diffPerCol )
9750 {
9751 for ( int col = 0; col < m_numCols; col++ )
9752 {
9753 SetColSize(col, GetColWidth(col) + diffPerCol);
9754 }
9755 }
9756
9757 // add remaining amount to the last columns
9758 diff -= diffPerCol * m_numCols;
9759 if ( diff )
9760 {
9761 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
9762 {
9763 SetColSize(col, GetColWidth(col) + 1);
9764 }
9765 }
9766 }
9767
9768 // same for rows
9769 diff = sizeFit.y - size.y - (m_extraHeight + 1);
9770 if ( diff && m_numRows )
9771 {
9772 // try to resize the columns uniformly
9773 wxCoord diffPerRow = diff / m_numRows;
9774 if ( diffPerRow )
9775 {
9776 for ( int row = 0; row < m_numRows; row++ )
9777 {
9778 SetRowSize(row, GetRowHeight(row) + diffPerRow);
9779 }
9780 }
9781
9782 // add remaining amount to the last rows
9783 diff -= diffPerRow * m_numRows;
9784 if ( diff )
9785 {
9786 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
9787 {
9788 SetRowSize(row, GetRowHeight(row) + 1);
9789 }
9790 }
9791 }
9792
9793 EndBatch();
9794
9795 SetClientSize(sizeFit);
9796 }
9797
9798 void wxGrid::AutoSizeRowLabelSize( int row )
9799 {
9800 wxArrayString lines;
9801 long w, h;
9802
9803 // Hide the edit control, so it
9804 // won't interfer with drag-shrinking.
9805 if( IsCellEditControlShown() )
9806 {
9807 HideCellEditControl();
9808 SaveEditControlValue();
9809 }
9810
9811 // autosize row height depending on label text
9812 StringToLines( GetRowLabelValue( row ), lines );
9813 wxClientDC dc( m_rowLabelWin );
9814 GetTextBoxSize( dc, lines, &w, &h);
9815 if( h < m_defaultRowHeight )
9816 h = m_defaultRowHeight;
9817 SetRowSize(row, h);
9818 ForceRefresh();
9819 }
9820
9821 void wxGrid::AutoSizeColLabelSize( int col )
9822 {
9823 wxArrayString lines;
9824 long w, h;
9825
9826 // Hide the edit control, so it
9827 // won't interfer with drag-shrinking.
9828 if( IsCellEditControlShown() )
9829 {
9830 HideCellEditControl();
9831 SaveEditControlValue();
9832 }
9833
9834 // autosize column width depending on label text
9835 StringToLines( GetColLabelValue( col ), lines );
9836 wxClientDC dc( m_colLabelWin );
9837 if( GetColLabelTextOrientation() == wxHORIZONTAL )
9838 GetTextBoxSize( dc, lines, &w, &h);
9839 else
9840 GetTextBoxSize( dc, lines, &h, &w);
9841 if( w < m_defaultColWidth )
9842 w = m_defaultColWidth;
9843 SetColSize(col, w);
9844 ForceRefresh();
9845 }
9846
9847 wxSize wxGrid::DoGetBestSize() const
9848 {
9849 // don't set sizes, only calculate them
9850 wxGrid *self = (wxGrid *)this; // const_cast
9851
9852 int width, height;
9853 width = self->SetOrCalcColumnSizes(true);
9854 height = self->SetOrCalcRowSizes(true);
9855
9856 int maxwidth, maxheight;
9857 wxDisplaySize( & maxwidth, & maxheight );
9858
9859 if ( width > maxwidth ) width = maxwidth;
9860 if ( height > maxheight ) height = maxheight;
9861
9862 return wxSize( width, height );
9863 }
9864
9865 void wxGrid::Fit()
9866 {
9867 AutoSize();
9868 }
9869
9870
9871 wxPen& wxGrid::GetDividerPen() const
9872 {
9873 return wxNullPen;
9874 }
9875
9876 // ----------------------------------------------------------------------------
9877 // cell value accessor functions
9878 // ----------------------------------------------------------------------------
9879
9880 void wxGrid::SetCellValue( int row, int col, const wxString& s )
9881 {
9882 if ( m_table )
9883 {
9884 m_table->SetValue( row, col, s );
9885 if ( !GetBatchCount() )
9886 {
9887 int dummy;
9888 wxRect rect( CellToRect( row, col ) );
9889 rect.x = 0;
9890 rect.width = m_gridWin->GetClientSize().GetWidth();
9891 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9892 m_gridWin->Refresh( false, &rect );
9893 }
9894
9895 if ( m_currentCellCoords.GetRow() == row &&
9896 m_currentCellCoords.GetCol() == col &&
9897 IsCellEditControlShown())
9898 // Note: If we are using IsCellEditControlEnabled,
9899 // this interacts badly with calling SetCellValue from
9900 // an EVT_GRID_CELL_CHANGE handler.
9901 {
9902 HideCellEditControl();
9903 ShowCellEditControl(); // will reread data from table
9904 }
9905 }
9906 }
9907
9908
9909 //
9910 // ------ Block, row and col selection
9911 //
9912
9913 void wxGrid::SelectRow( int row, bool addToSelected )
9914 {
9915 if ( IsSelection() && !addToSelected )
9916 ClearSelection();
9917
9918 if ( m_selection )
9919 m_selection->SelectRow( row, false, addToSelected );
9920 }
9921
9922
9923 void wxGrid::SelectCol( int col, bool addToSelected )
9924 {
9925 if ( IsSelection() && !addToSelected )
9926 ClearSelection();
9927
9928 if ( m_selection )
9929 m_selection->SelectCol( col, false, addToSelected );
9930 }
9931
9932
9933 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
9934 bool addToSelected )
9935 {
9936 if ( IsSelection() && !addToSelected )
9937 ClearSelection();
9938
9939 if ( m_selection )
9940 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
9941 false, addToSelected );
9942 }
9943
9944
9945 void wxGrid::SelectAll()
9946 {
9947 if ( m_numRows > 0 && m_numCols > 0 )
9948 {
9949 if ( m_selection )
9950 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
9951 }
9952 }
9953
9954 //
9955 // ------ Cell, row and col deselection
9956 //
9957
9958 void wxGrid::DeselectRow( int row )
9959 {
9960 if ( !m_selection )
9961 return;
9962
9963 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
9964 {
9965 if ( m_selection->IsInSelection(row, 0 ) )
9966 m_selection->ToggleCellSelection( row, 0);
9967 }
9968 else
9969 {
9970 int nCols = GetNumberCols();
9971 for ( int i = 0; i < nCols ; i++ )
9972 {
9973 if ( m_selection->IsInSelection(row, i ) )
9974 m_selection->ToggleCellSelection( row, i);
9975 }
9976 }
9977 }
9978
9979 void wxGrid::DeselectCol( int col )
9980 {
9981 if ( !m_selection )
9982 return;
9983
9984 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
9985 {
9986 if ( m_selection->IsInSelection(0, col ) )
9987 m_selection->ToggleCellSelection( 0, col);
9988 }
9989 else
9990 {
9991 int nRows = GetNumberRows();
9992 for ( int i = 0; i < nRows ; i++ )
9993 {
9994 if ( m_selection->IsInSelection(i, col ) )
9995 m_selection->ToggleCellSelection(i, col);
9996 }
9997 }
9998 }
9999
10000 void wxGrid::DeselectCell( int row, int col )
10001 {
10002 if ( m_selection && m_selection->IsInSelection(row, col) )
10003 m_selection->ToggleCellSelection(row, col);
10004 }
10005
10006 bool wxGrid::IsSelection()
10007 {
10008 return ( m_selection && (m_selection->IsSelection() ||
10009 ( m_selectingTopLeft != wxGridNoCellCoords &&
10010 m_selectingBottomRight != wxGridNoCellCoords) ) );
10011 }
10012
10013 bool wxGrid::IsInSelection( int row, int col ) const
10014 {
10015 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
10016 ( row >= m_selectingTopLeft.GetRow() &&
10017 col >= m_selectingTopLeft.GetCol() &&
10018 row <= m_selectingBottomRight.GetRow() &&
10019 col <= m_selectingBottomRight.GetCol() )) );
10020 }
10021
10022 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10023 {
10024 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10025 return m_selection->m_cellSelection;
10026 }
10027 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10028 {
10029 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10030 return m_selection->m_blockSelectionTopLeft;
10031 }
10032 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10033 {
10034 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10035 return m_selection->m_blockSelectionBottomRight;
10036 }
10037 wxArrayInt wxGrid::GetSelectedRows() const
10038 {
10039 if (!m_selection) { wxArrayInt a; return a; }
10040 return m_selection->m_rowSelection;
10041 }
10042 wxArrayInt wxGrid::GetSelectedCols() const
10043 {
10044 if (!m_selection) { wxArrayInt a; return a; }
10045 return m_selection->m_colSelection;
10046 }
10047
10048
10049 void wxGrid::ClearSelection()
10050 {
10051 m_selectingTopLeft = wxGridNoCellCoords;
10052 m_selectingBottomRight = wxGridNoCellCoords;
10053 if ( m_selection )
10054 m_selection->ClearSelection();
10055 }
10056
10057
10058 // This function returns the rectangle that encloses the given block
10059 // in device coords clipped to the client size of the grid window.
10060 //
10061 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
10062 const wxGridCellCoords &bottomRight )
10063 {
10064 wxRect rect( wxGridNoCellRect );
10065 wxRect cellRect;
10066
10067 cellRect = CellToRect( topLeft );
10068 if ( cellRect != wxGridNoCellRect )
10069 {
10070 rect = cellRect;
10071 }
10072 else
10073 {
10074 rect = wxRect( 0, 0, 0, 0 );
10075 }
10076
10077 cellRect = CellToRect( bottomRight );
10078 if ( cellRect != wxGridNoCellRect )
10079 {
10080 rect += cellRect;
10081 }
10082 else
10083 {
10084 return wxGridNoCellRect;
10085 }
10086
10087 int i, j;
10088 int left = rect.GetLeft();
10089 int top = rect.GetTop();
10090 int right = rect.GetRight();
10091 int bottom = rect.GetBottom();
10092
10093 int leftCol = topLeft.GetCol();
10094 int topRow = topLeft.GetRow();
10095 int rightCol = bottomRight.GetCol();
10096 int bottomRow = bottomRight.GetRow();
10097
10098 if (left > right)
10099 {
10100 i = left;
10101 left = right;
10102 right = i;
10103 i = leftCol;
10104 leftCol=rightCol;
10105 rightCol = i;
10106 }
10107
10108 if (top > bottom)
10109 {
10110 i = top;
10111 top = bottom;
10112 bottom = i;
10113 i = topRow;
10114 topRow = bottomRow;
10115 bottomRow = i;
10116 }
10117
10118
10119 for ( j = topRow; j <= bottomRow; j++ )
10120 {
10121 for ( i = leftCol; i <= rightCol; i++ )
10122 {
10123 if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol))
10124 {
10125 cellRect = CellToRect( j, i );
10126
10127 if (cellRect.x < left)
10128 left = cellRect.x;
10129 if (cellRect.y < top)
10130 top = cellRect.y;
10131 if (cellRect.x + cellRect.width > right)
10132 right = cellRect.x + cellRect.width;
10133 if (cellRect.y + cellRect.height > bottom)
10134 bottom = cellRect.y + cellRect.height;
10135 }
10136 else i = rightCol; // jump over inner cells.
10137 }
10138 }
10139
10140 // convert to scrolled coords
10141 //
10142 CalcScrolledPosition( left, top, &left, &top );
10143 CalcScrolledPosition( right, bottom, &right, &bottom );
10144
10145 int cw, ch;
10146 m_gridWin->GetClientSize( &cw, &ch );
10147
10148 if (right < 0 || bottom < 0 || left > cw || top > ch)
10149 return wxRect( 0, 0, 0, 0);
10150
10151 rect.SetLeft( wxMax(0, left) );
10152 rect.SetTop( wxMax(0, top) );
10153 rect.SetRight( wxMin(cw, right) );
10154 rect.SetBottom( wxMin(ch, bottom) );
10155
10156 return rect;
10157 }
10158
10159 //
10160 // ------ Grid event classes
10161 //
10162
10163 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
10164
10165 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
10166 int row, int col, int x, int y, bool sel,
10167 bool control, bool shift, bool alt, bool meta )
10168 : wxNotifyEvent( type, id )
10169 {
10170 m_row = row;
10171 m_col = col;
10172 m_x = x;
10173 m_y = y;
10174 m_selecting = sel;
10175 m_control = control;
10176 m_shift = shift;
10177 m_alt = alt;
10178 m_meta = meta;
10179
10180 SetEventObject(obj);
10181 }
10182
10183
10184 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
10185
10186 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
10187 int rowOrCol, int x, int y,
10188 bool control, bool shift, bool alt, bool meta )
10189 : wxNotifyEvent( type, id )
10190 {
10191 m_rowOrCol = rowOrCol;
10192 m_x = x;
10193 m_y = y;
10194 m_control = control;
10195 m_shift = shift;
10196 m_alt = alt;
10197 m_meta = meta;
10198
10199 SetEventObject(obj);
10200 }
10201
10202
10203 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
10204
10205 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
10206 const wxGridCellCoords& topLeft,
10207 const wxGridCellCoords& bottomRight,
10208 bool sel, bool control,
10209 bool shift, bool alt, bool meta )
10210 : wxNotifyEvent( type, id )
10211 {
10212 m_topLeft = topLeft;
10213 m_bottomRight = bottomRight;
10214 m_selecting = sel;
10215 m_control = control;
10216 m_shift = shift;
10217 m_alt = alt;
10218 m_meta = meta;
10219
10220 SetEventObject(obj);
10221 }
10222
10223
10224 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
10225
10226 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
10227 wxObject* obj, int row,
10228 int col, wxControl* ctrl)
10229 : wxCommandEvent(type, id)
10230 {
10231 SetEventObject(obj);
10232 m_row = row;
10233 m_col = col;
10234 m_ctrl = ctrl;
10235 }
10236
10237 #endif // wxUSE_GRID
10238