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