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