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