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