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