hide the cell editor if the grid loses focus
[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 == -1)
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 if ( (row = YToRow( y )) >= 0 )
4909 {
4910 if ( m_selection )
4911 {
4912 m_selection->SelectRow( row,
4913 event.ControlDown(),
4914 event.ShiftDown(),
4915 event.AltDown(),
4916 event.MetaDown() );
4917 }
4918 }
4919
4920 // default label to suppress warnings about "enumeration value
4921 // 'xxx' not handled in switch
4922 default:
4923 break;
4924 }
4925 }
4926 return;
4927 }
4928
4929 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4930 return;
4931
4932 if (m_isDragging)
4933 {
4934 if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse();
4935 m_isDragging = false;
4936 }
4937
4938 // ------------ Entering or leaving the window
4939 //
4940 if ( event.Entering() || event.Leaving() )
4941 {
4942 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4943 }
4944
4945
4946 // ------------ Left button pressed
4947 //
4948 else if ( event.LeftDown() )
4949 {
4950 // don't send a label click event for a hit on the
4951 // edge of the row label - this is probably the user
4952 // wanting to resize the row
4953 //
4954 if ( YToEdgeOfRow(y) < 0 )
4955 {
4956 row = YToRow(y);
4957 if ( row >= 0 &&
4958 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
4959 {
4960 if ( !event.ShiftDown() && !event.ControlDown() )
4961 ClearSelection();
4962 if ( m_selection )
4963 {
4964 if ( event.ShiftDown() )
4965 {
4966 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4967 0,
4968 row,
4969 GetNumberCols() - 1,
4970 event.ControlDown(),
4971 event.ShiftDown(),
4972 event.AltDown(),
4973 event.MetaDown() );
4974 }
4975 else
4976 {
4977 m_selection->SelectRow( row,
4978 event.ControlDown(),
4979 event.ShiftDown(),
4980 event.AltDown(),
4981 event.MetaDown() );
4982 }
4983 }
4984
4985 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
4986 }
4987 }
4988 else
4989 {
4990 // starting to drag-resize a row
4991 //
4992 if ( CanDragRowSize() )
4993 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
4994 }
4995 }
4996
4997
4998 // ------------ Left double click
4999 //
5000 else if (event.LeftDClick() )
5001 {
5002 int row = YToEdgeOfRow(y);
5003 if ( row < 0 )
5004 {
5005 row = YToRow(y);
5006 if ( row >=0 &&
5007 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5008 {
5009 // no default action at the moment
5010 }
5011 }
5012 else
5013 {
5014 // adjust row height depending on label text
5015 AutoSizeRowLabelSize( row );
5016
5017 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5018 m_dragLastPos = -1;
5019 }
5020 }
5021
5022
5023 // ------------ Left button released
5024 //
5025 else if ( event.LeftUp() )
5026 {
5027 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5028 {
5029 DoEndDragResizeRow();
5030
5031 // Note: we are ending the event *after* doing
5032 // default processing in this case
5033 //
5034 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5035 }
5036
5037 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5038 m_dragLastPos = -1;
5039 }
5040
5041
5042 // ------------ Right button down
5043 //
5044 else if ( event.RightDown() )
5045 {
5046 row = YToRow(y);
5047 if ( row >=0 &&
5048 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
5049 {
5050 // no default action at the moment
5051 }
5052 }
5053
5054
5055 // ------------ Right double click
5056 //
5057 else if ( event.RightDClick() )
5058 {
5059 row = YToRow(y);
5060 if ( row >= 0 &&
5061 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
5062 {
5063 // no default action at the moment
5064 }
5065 }
5066
5067
5068 // ------------ No buttons down and mouse moving
5069 //
5070 else if ( event.Moving() )
5071 {
5072 m_dragRowOrCol = YToEdgeOfRow( y );
5073 if ( m_dragRowOrCol >= 0 )
5074 {
5075 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5076 {
5077 // don't capture the mouse yet
5078 if ( CanDragRowSize() )
5079 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
5080 }
5081 }
5082 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5083 {
5084 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
5085 }
5086 }
5087 }
5088
5089
5090 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5091 {
5092 int x, y, col;
5093 wxPoint pos( event.GetPosition() );
5094 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5095
5096 if ( event.Dragging() )
5097 {
5098 if (!m_isDragging)
5099 {
5100 m_isDragging = true;
5101 m_colLabelWin->CaptureMouse();
5102 }
5103
5104 if ( event.LeftIsDown() )
5105 {
5106 switch( m_cursorMode )
5107 {
5108 case WXGRID_CURSOR_RESIZE_COL:
5109 {
5110 int cw, ch, dummy, top;
5111 m_gridWin->GetClientSize( &cw, &ch );
5112 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5113
5114 wxClientDC dc( m_gridWin );
5115 PrepareDC( dc );
5116
5117 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5118 GetColMinimalWidth(m_dragRowOrCol));
5119 dc.SetLogicalFunction(wxINVERT);
5120 if ( m_dragLastPos >= 0 )
5121 {
5122 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5123 }
5124 dc.DrawLine( x, top, x, top+ch );
5125 m_dragLastPos = x;
5126 }
5127 break;
5128
5129 case WXGRID_CURSOR_SELECT_COL:
5130 if ( (col = XToCol( x )) >= 0 )
5131 {
5132 if ( m_selection )
5133 {
5134 m_selection->SelectCol( col,
5135 event.ControlDown(),
5136 event.ShiftDown(),
5137 event.AltDown(),
5138 event.MetaDown() );
5139 }
5140 }
5141
5142 // default label to suppress warnings about "enumeration value
5143 // 'xxx' not handled in switch
5144 default:
5145 break;
5146 }
5147 }
5148 return;
5149 }
5150
5151 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5152 return;
5153
5154 if (m_isDragging)
5155 {
5156 if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse();
5157 m_isDragging = false;
5158 }
5159
5160 // ------------ Entering or leaving the window
5161 //
5162 if ( event.Entering() || event.Leaving() )
5163 {
5164 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5165 }
5166
5167
5168 // ------------ Left button pressed
5169 //
5170 else if ( event.LeftDown() )
5171 {
5172 // don't send a label click event for a hit on the
5173 // edge of the col label - this is probably the user
5174 // wanting to resize the col
5175 //
5176 if ( XToEdgeOfCol(x) < 0 )
5177 {
5178 col = XToCol(x);
5179 if ( col >= 0 &&
5180 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
5181 {
5182 if ( !event.ShiftDown() && !event.ControlDown() )
5183 ClearSelection();
5184 if ( m_selection )
5185 {
5186 if ( event.ShiftDown() )
5187 {
5188 m_selection->SelectBlock( 0,
5189 m_currentCellCoords.GetCol(),
5190 GetNumberRows() - 1, col,
5191 event.ControlDown(),
5192 event.ShiftDown(),
5193 event.AltDown(),
5194 event.MetaDown() );
5195 }
5196 else
5197 {
5198 m_selection->SelectCol( col,
5199 event.ControlDown(),
5200 event.ShiftDown(),
5201 event.AltDown(),
5202 event.MetaDown() );
5203 }
5204 }
5205
5206 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5207 }
5208 }
5209 else
5210 {
5211 // starting to drag-resize a col
5212 //
5213 if ( CanDragColSize() )
5214 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
5215 }
5216 }
5217
5218
5219 // ------------ Left double click
5220 //
5221 if ( event.LeftDClick() )
5222 {
5223 int col = XToEdgeOfCol(x);
5224 if ( col < 0 )
5225 {
5226 col = XToCol(x);
5227 if ( col >= 0 &&
5228 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5229 {
5230 // no default action at the moment
5231 }
5232 }
5233 else
5234 {
5235 // adjust column width depending on label text
5236 AutoSizeColLabelSize( col );
5237
5238 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5239 m_dragLastPos = -1;
5240 }
5241 }
5242
5243
5244 // ------------ Left button released
5245 //
5246 else if ( event.LeftUp() )
5247 {
5248 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5249 {
5250 DoEndDragResizeCol();
5251
5252 // Note: we are ending the event *after* doing
5253 // default processing in this case
5254 //
5255 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5256 }
5257
5258 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5259 m_dragLastPos = -1;
5260 }
5261
5262
5263 // ------------ Right button down
5264 //
5265 else if ( event.RightDown() )
5266 {
5267 col = XToCol(x);
5268 if ( col >= 0 &&
5269 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
5270 {
5271 // no default action at the moment
5272 }
5273 }
5274
5275
5276 // ------------ Right double click
5277 //
5278 else if ( event.RightDClick() )
5279 {
5280 col = XToCol(x);
5281 if ( col >= 0 &&
5282 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
5283 {
5284 // no default action at the moment
5285 }
5286 }
5287
5288
5289 // ------------ No buttons down and mouse moving
5290 //
5291 else if ( event.Moving() )
5292 {
5293 m_dragRowOrCol = XToEdgeOfCol( x );
5294 if ( m_dragRowOrCol >= 0 )
5295 {
5296 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5297 {
5298 // don't capture the cursor yet
5299 if ( CanDragColSize() )
5300 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
5301 }
5302 }
5303 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5304 {
5305 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
5306 }
5307 }
5308 }
5309
5310
5311 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
5312 {
5313 if ( event.LeftDown() )
5314 {
5315 // indicate corner label by having both row and
5316 // col args == -1
5317 //
5318 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
5319 {
5320 SelectAll();
5321 }
5322 }
5323
5324 else if ( event.LeftDClick() )
5325 {
5326 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
5327 }
5328
5329 else if ( event.RightDown() )
5330 {
5331 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
5332 {
5333 // no default action at the moment
5334 }
5335 }
5336
5337 else if ( event.RightDClick() )
5338 {
5339 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
5340 {
5341 // no default action at the moment
5342 }
5343 }
5344 }
5345
5346 void wxGrid::ChangeCursorMode(CursorMode mode,
5347 wxWindow *win,
5348 bool captureMouse)
5349 {
5350 #ifdef __WXDEBUG__
5351 static const wxChar *cursorModes[] =
5352 {
5353 _T("SELECT_CELL"),
5354 _T("RESIZE_ROW"),
5355 _T("RESIZE_COL"),
5356 _T("SELECT_ROW"),
5357 _T("SELECT_COL")
5358 };
5359
5360 wxLogTrace(_T("grid"),
5361 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5362 win == m_colLabelWin ? _T("colLabelWin")
5363 : win ? _T("rowLabelWin")
5364 : _T("gridWin"),
5365 cursorModes[m_cursorMode], cursorModes[mode]);
5366 #endif // __WXDEBUG__
5367
5368 if ( mode == m_cursorMode &&
5369 win == m_winCapture &&
5370 captureMouse == (m_winCapture != NULL))
5371 return;
5372
5373 if ( !win )
5374 {
5375 // by default use the grid itself
5376 win = m_gridWin;
5377 }
5378
5379 if ( m_winCapture )
5380 {
5381 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5382 m_winCapture = (wxWindow *)NULL;
5383 }
5384
5385 m_cursorMode = mode;
5386
5387 switch ( m_cursorMode )
5388 {
5389 case WXGRID_CURSOR_RESIZE_ROW:
5390 win->SetCursor( m_rowResizeCursor );
5391 break;
5392
5393 case WXGRID_CURSOR_RESIZE_COL:
5394 win->SetCursor( m_colResizeCursor );
5395 break;
5396
5397 default:
5398 win->SetCursor( *wxSTANDARD_CURSOR );
5399 }
5400
5401 // we need to capture mouse when resizing
5402 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5403 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5404
5405 if ( captureMouse && resize )
5406 {
5407 win->CaptureMouse();
5408 m_winCapture = win;
5409 }
5410 }
5411
5412 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5413 {
5414 int x, y;
5415 wxPoint pos( event.GetPosition() );
5416 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5417
5418 wxGridCellCoords coords;
5419 XYToCell( x, y, coords );
5420
5421 int cell_rows, cell_cols;
5422 bool isFirstDrag = !m_isDragging;
5423 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5424 if ((cell_rows < 0) || (cell_cols < 0))
5425 {
5426 coords.SetRow(coords.GetRow() + cell_rows);
5427 coords.SetCol(coords.GetCol() + cell_cols);
5428 }
5429
5430 if ( event.Dragging() )
5431 {
5432 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5433
5434 // Don't start doing anything until the mouse has been drug at
5435 // least 3 pixels in any direction...
5436 if (! m_isDragging)
5437 {
5438 if (m_startDragPos == wxDefaultPosition)
5439 {
5440 m_startDragPos = pos;
5441 return;
5442 }
5443 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5444 return;
5445 }
5446
5447 m_isDragging = true;
5448 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5449 {
5450 // Hide the edit control, so it
5451 // won't interfer with drag-shrinking.
5452 if ( IsCellEditControlShown() )
5453 {
5454 HideCellEditControl();
5455 SaveEditControlValue();
5456 }
5457
5458 // Have we captured the mouse yet?
5459 if (! m_winCapture)
5460 {
5461 m_winCapture = m_gridWin;
5462 m_winCapture->CaptureMouse();
5463 }
5464
5465 if ( coords != wxGridNoCellCoords )
5466 {
5467 if ( event.ControlDown() )
5468 {
5469 if ( m_selectingKeyboard == wxGridNoCellCoords)
5470 m_selectingKeyboard = coords;
5471 HighlightBlock ( m_selectingKeyboard, coords );
5472 }
5473 else if ( CanDragCell() )
5474 {
5475 if ( isFirstDrag )
5476 {
5477 if ( m_selectingKeyboard == wxGridNoCellCoords)
5478 m_selectingKeyboard = coords;
5479
5480 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5481 coords.GetRow(),
5482 coords.GetCol(),
5483 event );
5484 }
5485 }
5486 else
5487 {
5488 if ( !IsSelection() )
5489 {
5490 HighlightBlock( coords, coords );
5491 }
5492 else
5493 {
5494 HighlightBlock( m_currentCellCoords, coords );
5495 }
5496 }
5497
5498 if (! IsVisible(coords))
5499 {
5500 MakeCellVisible(coords);
5501 // TODO: need to introduce a delay or something here. The
5502 // scrolling is way to fast, at least on MSW - also on GTK.
5503 }
5504 }
5505 }
5506 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5507 {
5508 int cw, ch, left, dummy;
5509 m_gridWin->GetClientSize( &cw, &ch );
5510 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5511
5512 wxClientDC dc( m_gridWin );
5513 PrepareDC( dc );
5514 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5515 GetRowMinimalHeight(m_dragRowOrCol) );
5516 dc.SetLogicalFunction(wxINVERT);
5517 if ( m_dragLastPos >= 0 )
5518 {
5519 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5520 }
5521 dc.DrawLine( left, y, left+cw, y );
5522 m_dragLastPos = y;
5523 }
5524 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5525 {
5526 int cw, ch, dummy, top;
5527 m_gridWin->GetClientSize( &cw, &ch );
5528 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5529
5530 wxClientDC dc( m_gridWin );
5531 PrepareDC( dc );
5532 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5533 GetColMinimalWidth(m_dragRowOrCol) );
5534 dc.SetLogicalFunction(wxINVERT);
5535 if ( m_dragLastPos >= 0 )
5536 {
5537 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5538 }
5539 dc.DrawLine( x, top, x, top+ch );
5540 m_dragLastPos = x;
5541 }
5542
5543 return;
5544 }
5545
5546 m_isDragging = false;
5547 m_startDragPos = wxDefaultPosition;
5548
5549 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5550 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5551 // wxGTK
5552 #if 0
5553 if ( event.Entering() || event.Leaving() )
5554 {
5555 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5556 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5557 }
5558 else
5559 #endif // 0
5560
5561 // ------------ Left button pressed
5562 //
5563 if ( event.LeftDown() && coords != wxGridNoCellCoords )
5564 {
5565 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5566 coords.GetRow(),
5567 coords.GetCol(),
5568 event ) )
5569 {
5570 if ( !event.ControlDown() )
5571 ClearSelection();
5572 if ( event.ShiftDown() )
5573 {
5574 if ( m_selection )
5575 {
5576 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5577 m_currentCellCoords.GetCol(),
5578 coords.GetRow(),
5579 coords.GetCol(),
5580 event.ControlDown(),
5581 event.ShiftDown(),
5582 event.AltDown(),
5583 event.MetaDown() );
5584 }
5585 }
5586 else if ( XToEdgeOfCol(x) < 0 &&
5587 YToEdgeOfRow(y) < 0 )
5588 {
5589 DisableCellEditControl();
5590 MakeCellVisible( coords );
5591
5592 if ( event.ControlDown() )
5593 {
5594 if ( m_selection )
5595 {
5596 m_selection->ToggleCellSelection( coords.GetRow(),
5597 coords.GetCol(),
5598 event.ControlDown(),
5599 event.ShiftDown(),
5600 event.AltDown(),
5601 event.MetaDown() );
5602 }
5603 m_selectingTopLeft = wxGridNoCellCoords;
5604 m_selectingBottomRight = wxGridNoCellCoords;
5605 m_selectingKeyboard = coords;
5606 }
5607 else
5608 {
5609 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5610 SetCurrentCell( coords );
5611 if ( m_selection )
5612 {
5613 if ( m_selection->GetSelectionMode() !=
5614 wxGrid::wxGridSelectCells )
5615 {
5616 HighlightBlock( coords, coords );
5617 }
5618 }
5619 }
5620 }
5621 }
5622 }
5623
5624
5625 // ------------ Left double click
5626 //
5627 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5628 {
5629 DisableCellEditControl();
5630
5631 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
5632 {
5633 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5634 coords.GetRow(),
5635 coords.GetCol(),
5636 event ) )
5637 {
5638 // we want double click to select a cell and start editing
5639 // (i.e. to behave in same way as sequence of two slow clicks):
5640 m_waitForSlowClick = true;
5641 }
5642 }
5643
5644 }
5645
5646
5647 // ------------ Left button released
5648 //
5649 else if ( event.LeftUp() )
5650 {
5651 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5652 {
5653 if (m_winCapture)
5654 {
5655 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5656 m_winCapture = NULL;
5657 }
5658
5659 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl())
5660 {
5661 ClearSelection();
5662 EnableCellEditControl();
5663
5664 wxGridCellAttr* attr = GetCellAttr(coords);
5665 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
5666 editor->StartingClick();
5667 editor->DecRef();
5668 attr->DecRef();
5669
5670 m_waitForSlowClick = false;
5671 }
5672 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
5673 m_selectingBottomRight != wxGridNoCellCoords )
5674 {
5675 if ( m_selection )
5676 {
5677 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5678 m_selectingTopLeft.GetCol(),
5679 m_selectingBottomRight.GetRow(),
5680 m_selectingBottomRight.GetCol(),
5681 event.ControlDown(),
5682 event.ShiftDown(),
5683 event.AltDown(),
5684 event.MetaDown() );
5685 }
5686
5687 m_selectingTopLeft = wxGridNoCellCoords;
5688 m_selectingBottomRight = wxGridNoCellCoords;
5689
5690 // Show the edit control, if it has been hidden for
5691 // drag-shrinking.
5692 ShowCellEditControl();
5693 }
5694 }
5695 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5696 {
5697 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5698 DoEndDragResizeRow();
5699
5700 // Note: we are ending the event *after* doing
5701 // default processing in this case
5702 //
5703 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5704 }
5705 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5706 {
5707 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5708 DoEndDragResizeCol();
5709
5710 // Note: we are ending the event *after* doing
5711 // default processing in this case
5712 //
5713 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5714 }
5715
5716 m_dragLastPos = -1;
5717 }
5718
5719
5720 // ------------ Right button down
5721 //
5722 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5723 {
5724 DisableCellEditControl();
5725 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5726 coords.GetRow(),
5727 coords.GetCol(),
5728 event ) )
5729 {
5730 // no default action at the moment
5731 }
5732 }
5733
5734
5735 // ------------ Right double click
5736 //
5737 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5738 {
5739 DisableCellEditControl();
5740 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5741 coords.GetRow(),
5742 coords.GetCol(),
5743 event ) )
5744 {
5745 // no default action at the moment
5746 }
5747 }
5748
5749 // ------------ Moving and no button action
5750 //
5751 else if ( event.Moving() && !event.IsButton() )
5752 {
5753 if( coords.GetRow() < 0 || coords.GetCol() < 0 )
5754 {
5755 // out of grid cell area
5756 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5757 return;
5758 }
5759
5760 int dragRow = YToEdgeOfRow( y );
5761 int dragCol = XToEdgeOfCol( x );
5762
5763 // Dragging on the corner of a cell to resize in both
5764 // directions is not implemented yet...
5765 //
5766 if ( dragRow >= 0 && dragCol >= 0 )
5767 {
5768 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5769 return;
5770 }
5771
5772 if ( dragRow >= 0 )
5773 {
5774 m_dragRowOrCol = dragRow;
5775
5776 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5777 {
5778 if ( CanDragRowSize() && CanDragGridSize() )
5779 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
5780 }
5781
5782 if ( dragCol >= 0 )
5783 {
5784 m_dragRowOrCol = dragCol;
5785 }
5786
5787 return;
5788 }
5789
5790 if ( dragCol >= 0 )
5791 {
5792 m_dragRowOrCol = dragCol;
5793
5794 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5795 {
5796 if ( CanDragColSize() && CanDragGridSize() )
5797 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
5798 }
5799
5800 return;
5801 }
5802
5803 // Neither on a row or col edge
5804 //
5805 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5806 {
5807 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5808 }
5809 }
5810 }
5811
5812
5813 void wxGrid::DoEndDragResizeRow()
5814 {
5815 if ( m_dragLastPos >= 0 )
5816 {
5817 // erase the last line and resize the row
5818 //
5819 int cw, ch, left, dummy;
5820 m_gridWin->GetClientSize( &cw, &ch );
5821 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5822
5823 wxClientDC dc( m_gridWin );
5824 PrepareDC( dc );
5825 dc.SetLogicalFunction( wxINVERT );
5826 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5827 HideCellEditControl();
5828 SaveEditControlValue();
5829
5830 int rowTop = GetRowTop(m_dragRowOrCol);
5831 SetRowSize( m_dragRowOrCol,
5832 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
5833
5834 if ( !GetBatchCount() )
5835 {
5836 // Only needed to get the correct rect.y:
5837 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5838 rect.x = 0;
5839 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5840 rect.width = m_rowLabelWidth;
5841 rect.height = ch - rect.y;
5842 m_rowLabelWin->Refresh( true, &rect );
5843 rect.width = cw;
5844 // if there is a multicell block, paint all of it
5845 if (m_table)
5846 {
5847 int i, cell_rows, cell_cols, subtract_rows = 0;
5848 int leftCol = XToCol(left);
5849 int rightCol = internalXToCol(left+cw);
5850 if (leftCol >= 0)
5851 {
5852 for (i=leftCol; i<rightCol; i++)
5853 {
5854 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
5855 if (cell_rows < subtract_rows)
5856 subtract_rows = cell_rows;
5857 }
5858 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
5859 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5860 rect.height = ch - rect.y;
5861 }
5862 }
5863 m_gridWin->Refresh( false, &rect );
5864 }
5865
5866 ShowCellEditControl();
5867 }
5868 }
5869
5870
5871 void wxGrid::DoEndDragResizeCol()
5872 {
5873 if ( m_dragLastPos >= 0 )
5874 {
5875 // erase the last line and resize the col
5876 //
5877 int cw, ch, dummy, top;
5878 m_gridWin->GetClientSize( &cw, &ch );
5879 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5880
5881 wxClientDC dc( m_gridWin );
5882 PrepareDC( dc );
5883 dc.SetLogicalFunction( wxINVERT );
5884 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5885 HideCellEditControl();
5886 SaveEditControlValue();
5887
5888 int colLeft = GetColLeft(m_dragRowOrCol);
5889 SetColSize( m_dragRowOrCol,
5890 wxMax( m_dragLastPos - colLeft,
5891 GetColMinimalWidth(m_dragRowOrCol) ) );
5892
5893 if ( !GetBatchCount() )
5894 {
5895 // Only needed to get the correct rect.x:
5896 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5897 rect.y = 0;
5898 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5899 rect.width = cw - rect.x;
5900 rect.height = m_colLabelHeight;
5901 m_colLabelWin->Refresh( true, &rect );
5902 rect.height = ch;
5903 // if there is a multicell block, paint all of it
5904 if (m_table)
5905 {
5906 int i, cell_rows, cell_cols, subtract_cols = 0;
5907 int topRow = YToRow(top);
5908 int bottomRow = internalYToRow(top+cw);
5909 if (topRow >= 0)
5910 {
5911 for (i=topRow; i<bottomRow; i++)
5912 {
5913 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
5914 if (cell_cols < subtract_cols)
5915 subtract_cols = cell_cols;
5916 }
5917 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
5918 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5919 rect.width = cw - rect.x;
5920 }
5921 }
5922 m_gridWin->Refresh( false, &rect );
5923 }
5924
5925 ShowCellEditControl();
5926 }
5927 }
5928
5929
5930
5931 //
5932 // ------ interaction with data model
5933 //
5934 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
5935 {
5936 switch ( msg.GetId() )
5937 {
5938 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5939 return GetModelValues();
5940
5941 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5942 return SetModelValues();
5943
5944 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5945 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5946 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5947 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5948 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5949 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5950 return Redimension( msg );
5951
5952 default:
5953 return false;
5954 }
5955 }
5956
5957
5958
5959 // The behaviour of this function depends on the grid table class
5960 // Clear() function. For the default wxGridStringTable class the
5961 // behavious is to replace all cell contents with wxEmptyString but
5962 // not to change the number of rows or cols.
5963 //
5964 void wxGrid::ClearGrid()
5965 {
5966 if ( m_table )
5967 {
5968 if (IsCellEditControlEnabled())
5969 DisableCellEditControl();
5970
5971 m_table->Clear();
5972 if ( !GetBatchCount() ) m_gridWin->Refresh();
5973 }
5974 }
5975
5976
5977 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5978 {
5979 // TODO: something with updateLabels flag
5980
5981 if ( !m_created )
5982 {
5983 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5984 return false;
5985 }
5986
5987 if ( m_table )
5988 {
5989 if (IsCellEditControlEnabled())
5990 DisableCellEditControl();
5991
5992 bool done = m_table->InsertRows( pos, numRows );
5993 return done;
5994
5995 // the table will have sent the results of the insert row
5996 // operation to this view object as a grid table message
5997 }
5998 return false;
5999 }
6000
6001
6002 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
6003 {
6004 // TODO: something with updateLabels flag
6005
6006 if ( !m_created )
6007 {
6008 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6009 return false;
6010 }
6011
6012 if ( m_table )
6013 {
6014 bool done = m_table && m_table->AppendRows( numRows );
6015 return done;
6016 // the table will have sent the results of the append row
6017 // operation to this view object as a grid table message
6018 }
6019 return false;
6020 }
6021
6022
6023 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6024 {
6025 // TODO: something with updateLabels flag
6026
6027 if ( !m_created )
6028 {
6029 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6030 return false;
6031 }
6032
6033 if ( m_table )
6034 {
6035 if (IsCellEditControlEnabled())
6036 DisableCellEditControl();
6037
6038 bool done = m_table->DeleteRows( pos, numRows );
6039 return done;
6040 // the table will have sent the results of the delete row
6041 // operation to this view object as a grid table message
6042 }
6043 return false;
6044 }
6045
6046
6047 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6048 {
6049 // TODO: something with updateLabels flag
6050
6051 if ( !m_created )
6052 {
6053 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6054 return false;
6055 }
6056
6057 if ( m_table )
6058 {
6059 if (IsCellEditControlEnabled())
6060 DisableCellEditControl();
6061
6062 bool done = m_table->InsertCols( pos, numCols );
6063 return done;
6064 // the table will have sent the results of the insert col
6065 // operation to this view object as a grid table message
6066 }
6067 return false;
6068 }
6069
6070
6071 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
6072 {
6073 // TODO: something with updateLabels flag
6074
6075 if ( !m_created )
6076 {
6077 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6078 return false;
6079 }
6080
6081 if ( m_table )
6082 {
6083 bool done = m_table->AppendCols( numCols );
6084 return done;
6085 // the table will have sent the results of the append col
6086 // operation to this view object as a grid table message
6087 }
6088 return false;
6089 }
6090
6091
6092 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6093 {
6094 // TODO: something with updateLabels flag
6095
6096 if ( !m_created )
6097 {
6098 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6099 return false;
6100 }
6101
6102 if ( m_table )
6103 {
6104 if (IsCellEditControlEnabled())
6105 DisableCellEditControl();
6106
6107 bool done = m_table->DeleteCols( pos, numCols );
6108 return done;
6109 // the table will have sent the results of the delete col
6110 // operation to this view object as a grid table message
6111 }
6112 return false;
6113 }
6114
6115
6116
6117 //
6118 // ----- event handlers
6119 //
6120
6121 // Generate a grid event based on a mouse event and
6122 // return the result of ProcessEvent()
6123 //
6124 int wxGrid::SendEvent( const wxEventType type,
6125 int row, int col,
6126 wxMouseEvent& mouseEv )
6127 {
6128 bool claimed;
6129 bool vetoed;
6130
6131 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6132 {
6133 int rowOrCol = (row == -1 ? col : row);
6134
6135 wxGridSizeEvent gridEvt( GetId(),
6136 type,
6137 this,
6138 rowOrCol,
6139 mouseEv.GetX() + GetRowLabelSize(),
6140 mouseEv.GetY() + GetColLabelSize(),
6141 mouseEv.ControlDown(),
6142 mouseEv.ShiftDown(),
6143 mouseEv.AltDown(),
6144 mouseEv.MetaDown() );
6145
6146 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6147 vetoed = !gridEvt.IsAllowed();
6148 }
6149 else if ( type == wxEVT_GRID_RANGE_SELECT )
6150 {
6151 // Right now, it should _never_ end up here!
6152 wxGridRangeSelectEvent gridEvt( GetId(),
6153 type,
6154 this,
6155 m_selectingTopLeft,
6156 m_selectingBottomRight,
6157 true,
6158 mouseEv.ControlDown(),
6159 mouseEv.ShiftDown(),
6160 mouseEv.AltDown(),
6161 mouseEv.MetaDown() );
6162
6163 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6164 vetoed = !gridEvt.IsAllowed();
6165 }
6166 else
6167 {
6168 wxGridEvent gridEvt( GetId(),
6169 type,
6170 this,
6171 row, col,
6172 mouseEv.GetX() + GetRowLabelSize(),
6173 mouseEv.GetY() + GetColLabelSize(),
6174 false,
6175 mouseEv.ControlDown(),
6176 mouseEv.ShiftDown(),
6177 mouseEv.AltDown(),
6178 mouseEv.MetaDown() );
6179 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6180 vetoed = !gridEvt.IsAllowed();
6181 }
6182
6183 // A Veto'd event may not be `claimed' so test this first
6184 if (vetoed) return -1;
6185 return claimed ? 1 : 0;
6186 }
6187
6188
6189 // Generate a grid event of specified type and return the result
6190 // of ProcessEvent().
6191 //
6192 int wxGrid::SendEvent( const wxEventType type,
6193 int row, int col )
6194 {
6195 bool claimed;
6196 bool vetoed;
6197
6198 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6199 {
6200 int rowOrCol = (row == -1 ? col : row);
6201
6202 wxGridSizeEvent gridEvt( GetId(),
6203 type,
6204 this,
6205 rowOrCol );
6206
6207 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6208 vetoed = !gridEvt.IsAllowed();
6209 }
6210 else
6211 {
6212 wxGridEvent gridEvt( GetId(),
6213 type,
6214 this,
6215 row, col );
6216
6217 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6218 vetoed = !gridEvt.IsAllowed();
6219 }
6220
6221 // A Veto'd event may not be `claimed' so test this first
6222 if (vetoed) return -1;
6223 return claimed ? 1 : 0;
6224 }
6225
6226
6227 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6228 {
6229 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
6230 }
6231
6232 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6233 {
6234 // Don't do anything if between Begin/EndBatch...
6235 // EndBatch() will do all this on the last nested one anyway.
6236 if (! GetBatchCount())
6237 {
6238 // Refresh to get correct scrolled position:
6239 wxScrolledWindow::Refresh(eraseb,rect);
6240
6241 if (rect)
6242 {
6243 int rect_x, rect_y, rectWidth, rectHeight;
6244 int width_label, width_cell, height_label, height_cell;
6245 int x, y;
6246
6247 //Copy rectangle can get scroll offsets..
6248 rect_x = rect->GetX();
6249 rect_y = rect->GetY();
6250 rectWidth = rect->GetWidth();
6251 rectHeight = rect->GetHeight();
6252
6253 width_label = m_rowLabelWidth - rect_x;
6254 if (width_label > rectWidth) width_label = rectWidth;
6255
6256 height_label = m_colLabelHeight - rect_y;
6257 if (height_label > rectHeight) height_label = rectHeight;
6258
6259 if (rect_x > m_rowLabelWidth)
6260 {
6261 x = rect_x - m_rowLabelWidth;
6262 width_cell = rectWidth;
6263 }
6264 else
6265 {
6266 x = 0;
6267 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6268 }
6269
6270 if (rect_y > m_colLabelHeight)
6271 {
6272 y = rect_y - m_colLabelHeight;
6273 height_cell = rectHeight;
6274 }
6275 else
6276 {
6277 y = 0;
6278 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6279 }
6280
6281 // Paint corner label part intersecting rect.
6282 if ( width_label > 0 && height_label > 0 )
6283 {
6284 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6285 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6286 }
6287
6288 // Paint col labels part intersecting rect.
6289 if ( width_cell > 0 && height_label > 0 )
6290 {
6291 wxRect anotherrect(x, rect_y, width_cell, height_label);
6292 m_colLabelWin->Refresh(eraseb, &anotherrect);
6293 }
6294
6295 // Paint row labels part intersecting rect.
6296 if ( width_label > 0 && height_cell > 0 )
6297 {
6298 wxRect anotherrect(rect_x, y, width_label, height_cell);
6299 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6300 }
6301
6302 // Paint cell area part intersecting rect.
6303 if ( width_cell > 0 && height_cell > 0 )
6304 {
6305 wxRect anotherrect(x, y, width_cell, height_cell);
6306 m_gridWin->Refresh(eraseb, &anotherrect);
6307 }
6308 }
6309 else
6310 {
6311 m_cornerLabelWin->Refresh(eraseb, NULL);
6312 m_colLabelWin->Refresh(eraseb, NULL);
6313 m_rowLabelWin->Refresh(eraseb, NULL);
6314 m_gridWin->Refresh(eraseb, NULL);
6315 }
6316 }
6317 }
6318
6319 void wxGrid::OnSize( wxSizeEvent& event )
6320 {
6321 // position the child windows
6322 CalcWindowSizes();
6323
6324 // don't call CalcDimensions() from here, the base class handles the size
6325 // changes itself
6326 event.Skip();
6327 }
6328
6329
6330 void wxGrid::OnKeyDown( wxKeyEvent& event )
6331 {
6332 if ( m_inOnKeyDown )
6333 {
6334 // shouldn't be here - we are going round in circles...
6335 //
6336 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6337 }
6338
6339 m_inOnKeyDown = true;
6340
6341 // propagate the event up and see if it gets processed
6342 //
6343 wxWindow *parent = GetParent();
6344 wxKeyEvent keyEvt( event );
6345 keyEvt.SetEventObject( parent );
6346
6347 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6348 {
6349
6350 // try local handlers
6351 //
6352 switch ( event.GetKeyCode() )
6353 {
6354 case WXK_UP:
6355 if ( event.ControlDown() )
6356 {
6357 MoveCursorUpBlock( event.ShiftDown() );
6358 }
6359 else
6360 {
6361 MoveCursorUp( event.ShiftDown() );
6362 }
6363 break;
6364
6365 case WXK_DOWN:
6366 if ( event.ControlDown() )
6367 {
6368 MoveCursorDownBlock( event.ShiftDown() );
6369 }
6370 else
6371 {
6372 MoveCursorDown( event.ShiftDown() );
6373 }
6374 break;
6375
6376 case WXK_LEFT:
6377 if ( event.ControlDown() )
6378 {
6379 MoveCursorLeftBlock( event.ShiftDown() );
6380 }
6381 else
6382 {
6383 MoveCursorLeft( event.ShiftDown() );
6384 }
6385 break;
6386
6387 case WXK_RIGHT:
6388 if ( event.ControlDown() )
6389 {
6390 MoveCursorRightBlock( event.ShiftDown() );
6391 }
6392 else
6393 {
6394 MoveCursorRight( event.ShiftDown() );
6395 }
6396 break;
6397
6398 case WXK_RETURN:
6399 case WXK_NUMPAD_ENTER:
6400 if ( event.ControlDown() )
6401 {
6402 event.Skip(); // to let the edit control have the return
6403 }
6404 else
6405 {
6406 if ( GetGridCursorRow() < GetNumberRows()-1 )
6407 {
6408 MoveCursorDown( event.ShiftDown() );
6409 }
6410 else
6411 {
6412 // at the bottom of a column
6413 DisableCellEditControl();
6414 }
6415 }
6416 break;
6417
6418 case WXK_ESCAPE:
6419 ClearSelection();
6420 break;
6421
6422 case WXK_TAB:
6423 if (event.ShiftDown())
6424 {
6425 if ( GetGridCursorCol() > 0 )
6426 {
6427 MoveCursorLeft( false );
6428 }
6429 else
6430 {
6431 // at left of grid
6432 DisableCellEditControl();
6433 }
6434 }
6435 else
6436 {
6437 if ( GetGridCursorCol() < GetNumberCols()-1 )
6438 {
6439 MoveCursorRight( false );
6440 }
6441 else
6442 {
6443 // at right of grid
6444 DisableCellEditControl();
6445 }
6446 }
6447 break;
6448
6449 case WXK_HOME:
6450 if ( event.ControlDown() )
6451 {
6452 MakeCellVisible( 0, 0 );
6453 SetCurrentCell( 0, 0 );
6454 }
6455 else
6456 {
6457 event.Skip();
6458 }
6459 break;
6460
6461 case WXK_END:
6462 if ( event.ControlDown() )
6463 {
6464 MakeCellVisible( m_numRows-1, m_numCols-1 );
6465 SetCurrentCell( m_numRows-1, m_numCols-1 );
6466 }
6467 else
6468 {
6469 event.Skip();
6470 }
6471 break;
6472
6473 case WXK_PRIOR:
6474 MovePageUp();
6475 break;
6476
6477 case WXK_NEXT:
6478 MovePageDown();
6479 break;
6480
6481 case WXK_SPACE:
6482 if ( event.ControlDown() )
6483 {
6484 if ( m_selection )
6485 {
6486 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
6487 m_currentCellCoords.GetCol(),
6488 event.ControlDown(),
6489 event.ShiftDown(),
6490 event.AltDown(),
6491 event.MetaDown() );
6492 }
6493 break;
6494 }
6495 if ( !IsEditable() )
6496 {
6497 MoveCursorRight( false );
6498 break;
6499 }
6500 // Otherwise fall through to default
6501
6502 default:
6503 event.Skip();
6504 break;
6505 }
6506 }
6507
6508 m_inOnKeyDown = false;
6509 }
6510
6511 void wxGrid::OnKeyUp( wxKeyEvent& event )
6512 {
6513 // try local handlers
6514 //
6515 if ( event.GetKeyCode() == WXK_SHIFT )
6516 {
6517 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6518 m_selectingBottomRight != wxGridNoCellCoords )
6519 {
6520 if ( m_selection )
6521 {
6522 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6523 m_selectingTopLeft.GetCol(),
6524 m_selectingBottomRight.GetRow(),
6525 m_selectingBottomRight.GetCol(),
6526 event.ControlDown(),
6527 true,
6528 event.AltDown(),
6529 event.MetaDown() );
6530 }
6531 }
6532
6533 m_selectingTopLeft = wxGridNoCellCoords;
6534 m_selectingBottomRight = wxGridNoCellCoords;
6535 m_selectingKeyboard = wxGridNoCellCoords;
6536 }
6537 }
6538
6539 void wxGrid::OnChar( wxKeyEvent& event )
6540 {
6541 // is it possible to edit the current cell at all?
6542 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6543 {
6544 // yes, now check whether the cells editor accepts the key
6545 int row = m_currentCellCoords.GetRow();
6546 int col = m_currentCellCoords.GetCol();
6547 wxGridCellAttr* attr = GetCellAttr(row, col);
6548 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6549
6550 // <F2> is special and will always start editing, for
6551 // other keys - ask the editor itself
6552 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
6553 || editor->IsAcceptedKey(event) )
6554 {
6555 // ensure cell is visble
6556 MakeCellVisible(row, col);
6557 EnableCellEditControl();
6558
6559 // a problem can arise if the cell is not completely
6560 // visible (even after calling MakeCellVisible the
6561 // control is not created and calling StartingKey will
6562 // crash the app
6563 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
6564 editor->StartingKey(event);
6565 }
6566 else
6567 {
6568 event.Skip();
6569 }
6570
6571 editor->DecRef();
6572 attr->DecRef();
6573 }
6574 else
6575 {
6576 event.Skip();
6577 }
6578 }
6579
6580
6581 void wxGrid::OnEraseBackground(wxEraseEvent&)
6582 {
6583 }
6584
6585 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
6586 {
6587 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
6588 {
6589 // the event has been intercepted - do nothing
6590 return;
6591 }
6592
6593 wxClientDC dc(m_gridWin);
6594 PrepareDC(dc);
6595
6596 if ( m_currentCellCoords != wxGridNoCellCoords )
6597 {
6598 DisableCellEditControl();
6599
6600 if ( IsVisible( m_currentCellCoords, false ) )
6601 {
6602 wxRect r;
6603 r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
6604 if ( !m_gridLinesEnabled )
6605 {
6606 r.x--;
6607 r.y--;
6608 r.width++;
6609 r.height++;
6610 }
6611
6612 wxGridCellCoordsArray cells = CalcCellsExposed( r );
6613
6614 // Otherwise refresh redraws the highlight!
6615 m_currentCellCoords = coords;
6616
6617 DrawGridCellArea(dc,cells);
6618 DrawAllGridLines( dc, r );
6619 }
6620 }
6621
6622 m_currentCellCoords = coords;
6623
6624 wxGridCellAttr* attr = GetCellAttr(coords);
6625 DrawCellHighlight(dc, attr);
6626 attr->DecRef();
6627 }
6628
6629
6630 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
6631 {
6632 int temp;
6633 wxGridCellCoords updateTopLeft, updateBottomRight;
6634
6635 if ( m_selection )
6636 {
6637 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
6638 {
6639 leftCol = 0;
6640 rightCol = GetNumberCols() - 1;
6641 }
6642 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
6643 {
6644 topRow = 0;
6645 bottomRow = GetNumberRows() - 1;
6646 }
6647 }
6648
6649 if ( topRow > bottomRow )
6650 {
6651 temp = topRow;
6652 topRow = bottomRow;
6653 bottomRow = temp;
6654 }
6655
6656 if ( leftCol > rightCol )
6657 {
6658 temp = leftCol;
6659 leftCol = rightCol;
6660 rightCol = temp;
6661 }
6662
6663 updateTopLeft = wxGridCellCoords( topRow, leftCol );
6664 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
6665
6666 // First the case that we selected a completely new area
6667 if ( m_selectingTopLeft == wxGridNoCellCoords ||
6668 m_selectingBottomRight == wxGridNoCellCoords )
6669 {
6670 wxRect rect;
6671 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
6672 wxGridCellCoords ( bottomRow, rightCol ) );
6673 m_gridWin->Refresh( false, &rect );
6674 }
6675 // Now handle changing an existing selection area.
6676 else if ( m_selectingTopLeft != updateTopLeft ||
6677 m_selectingBottomRight != updateBottomRight )
6678 {
6679 // Compute two optimal update rectangles:
6680 // Either one rectangle is a real subset of the
6681 // other, or they are (almost) disjoint!
6682 wxRect rect[4];
6683 bool need_refresh[4];
6684 need_refresh[0] =
6685 need_refresh[1] =
6686 need_refresh[2] =
6687 need_refresh[3] = false;
6688 int i;
6689
6690 // Store intermediate values
6691 wxCoord oldLeft = m_selectingTopLeft.GetCol();
6692 wxCoord oldTop = m_selectingTopLeft.GetRow();
6693 wxCoord oldRight = m_selectingBottomRight.GetCol();
6694 wxCoord oldBottom = m_selectingBottomRight.GetRow();
6695
6696 // Determine the outer/inner coordinates.
6697 if (oldLeft > leftCol)
6698 {
6699 temp = oldLeft;
6700 oldLeft = leftCol;
6701 leftCol = temp;
6702 }
6703 if (oldTop > topRow )
6704 {
6705 temp = oldTop;
6706 oldTop = topRow;
6707 topRow = temp;
6708 }
6709 if (oldRight < rightCol )
6710 {
6711 temp = oldRight;
6712 oldRight = rightCol;
6713 rightCol = temp;
6714 }
6715 if (oldBottom < bottomRow)
6716 {
6717 temp = oldBottom;
6718 oldBottom = bottomRow;
6719 bottomRow = temp;
6720 }
6721
6722 // Now, either the stuff marked old is the outer
6723 // rectangle or we don't have a situation where one
6724 // is contained in the other.
6725
6726 if ( oldLeft < leftCol )
6727 {
6728 // Refresh the newly selected or deselected
6729 // area to the left of the old or new selection.
6730 need_refresh[0] = true;
6731 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6732 oldLeft ),
6733 wxGridCellCoords ( oldBottom,
6734 leftCol - 1 ) );
6735 }
6736
6737 if ( oldTop < topRow )
6738 {
6739 // Refresh the newly selected or deselected
6740 // area above the old or new selection.
6741 need_refresh[1] = true;
6742 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6743 leftCol ),
6744 wxGridCellCoords ( topRow - 1,
6745 rightCol ) );
6746 }
6747
6748 if ( oldRight > rightCol )
6749 {
6750 // Refresh the newly selected or deselected
6751 // area to the right of the old or new selection.
6752 need_refresh[2] = true;
6753 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6754 rightCol + 1 ),
6755 wxGridCellCoords ( oldBottom,
6756 oldRight ) );
6757 }
6758
6759 if ( oldBottom > bottomRow )
6760 {
6761 // Refresh the newly selected or deselected
6762 // area below the old or new selection.
6763 need_refresh[3] = true;
6764 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6765 leftCol ),
6766 wxGridCellCoords ( oldBottom,
6767 rightCol ) );
6768 }
6769
6770 // various Refresh() calls
6771 for (i = 0; i < 4; i++ )
6772 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6773 m_gridWin->Refresh( false, &(rect[i]) );
6774 }
6775 // Change Selection
6776 m_selectingTopLeft = updateTopLeft;
6777 m_selectingBottomRight = updateBottomRight;
6778 }
6779
6780 //
6781 // ------ functions to get/send data (see also public functions)
6782 //
6783
6784 bool wxGrid::GetModelValues()
6785 {
6786 // Hide the editor, so it won't hide a changed value.
6787 HideCellEditControl();
6788
6789 if ( m_table )
6790 {
6791 // all we need to do is repaint the grid
6792 //
6793 m_gridWin->Refresh();
6794 return true;
6795 }
6796
6797 return false;
6798 }
6799
6800
6801 bool wxGrid::SetModelValues()
6802 {
6803 int row, col;
6804
6805 // Disable the editor, so it won't hide a changed value.
6806 // Do we also want to save the current value of the editor first?
6807 // I think so ...
6808 DisableCellEditControl();
6809
6810 if ( m_table )
6811 {
6812 for ( row = 0; row < m_numRows; row++ )
6813 {
6814 for ( col = 0; col < m_numCols; col++ )
6815 {
6816 m_table->SetValue( row, col, GetCellValue(row, col) );
6817 }
6818 }
6819
6820 return true;
6821 }
6822
6823 return false;
6824 }
6825
6826
6827
6828 // Note - this function only draws cells that are in the list of
6829 // exposed cells (usually set from the update region by
6830 // CalcExposedCells)
6831 //
6832 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
6833 {
6834 if ( !m_numRows || !m_numCols ) return;
6835
6836 int i, numCells = cells.GetCount();
6837 int row, col, cell_rows, cell_cols;
6838 wxGridCellCoordsArray redrawCells;
6839
6840 for ( i = numCells-1; i >= 0; i-- )
6841 {
6842 row = cells[i].GetRow();
6843 col = cells[i].GetCol();
6844 GetCellSize( row, col, &cell_rows, &cell_cols );
6845
6846 // If this cell is part of a multicell block, find owner for repaint
6847 if ( cell_rows <= 0 || cell_cols <= 0 )
6848 {
6849 wxGridCellCoords cell(row+cell_rows, col+cell_cols);
6850 bool marked = false;
6851 for ( int j = 0; j < numCells; j++ )
6852 {
6853 if ( cell == cells[j] )
6854 {
6855 marked = true;
6856 break;
6857 }
6858 }
6859 if (!marked)
6860 {
6861 int count = redrawCells.GetCount();
6862 for (int j = 0; j < count; j++)
6863 {
6864 if ( cell == redrawCells[j] )
6865 {
6866 marked = true;
6867 break;
6868 }
6869 }
6870 if (!marked) redrawCells.Add( cell );
6871 }
6872 continue; // don't bother drawing this cell
6873 }
6874
6875 // If this cell is empty, find cell to left that might want to overflow
6876 if (m_table && m_table->IsEmptyCell(row, col))
6877 {
6878 for ( int l = 0; l < cell_rows; l++ )
6879 {
6880 // find a cell in this row to left alreay marked for repaint
6881 int left = col;
6882 for (int k = 0; k < int(redrawCells.GetCount()); k++)
6883 if ((redrawCells[k].GetCol() < left) &&
6884 (redrawCells[k].GetRow() == row))
6885 left=redrawCells[k].GetCol();
6886
6887 if (left == col) left = 0; // oh well
6888
6889 for (int j = col-1; j >= left; j--)
6890 {
6891 if (!m_table->IsEmptyCell(row+l, j))
6892 {
6893 if (GetCellOverflow(row+l, j))
6894 {
6895 wxGridCellCoords cell(row+l, j);
6896 bool marked = false;
6897
6898 for (int k = 0; k < numCells; k++)
6899 {
6900 if ( cell == cells[k] )
6901 {
6902 marked = true;
6903 break;
6904 }
6905 }
6906 if (!marked)
6907 {
6908 int count = redrawCells.GetCount();
6909 for (int k = 0; k < count; k++)
6910 {
6911 if ( cell == redrawCells[k] )
6912 {
6913 marked = true;
6914 break;
6915 }
6916 }
6917 if (!marked) redrawCells.Add( cell );
6918 }
6919 }
6920 break;
6921 }
6922 }
6923 }
6924 }
6925 DrawCell( dc, cells[i] );
6926 }
6927
6928 numCells = redrawCells.GetCount();
6929
6930 for ( i = numCells - 1; i >= 0; i-- )
6931 {
6932 DrawCell( dc, redrawCells[i] );
6933 }
6934 }
6935
6936
6937 void wxGrid::DrawGridSpace( wxDC& dc )
6938 {
6939 int cw, ch;
6940 m_gridWin->GetClientSize( &cw, &ch );
6941
6942 int right, bottom;
6943 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6944
6945 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6946 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
6947
6948 if ( right > rightCol || bottom > bottomRow )
6949 {
6950 int left, top;
6951 CalcUnscrolledPosition( 0, 0, &left, &top );
6952
6953 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6954 dc.SetPen( *wxTRANSPARENT_PEN );
6955
6956 if ( right > rightCol )
6957 {
6958 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6959 }
6960
6961 if ( bottom > bottomRow )
6962 {
6963 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6964 }
6965 }
6966 }
6967
6968
6969 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6970 {
6971 int row = coords.GetRow();
6972 int col = coords.GetCol();
6973
6974 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6975 return;
6976
6977 // we draw the cell border ourselves
6978 #if !WXGRID_DRAW_LINES
6979 if ( m_gridLinesEnabled )
6980 DrawCellBorder( dc, coords );
6981 #endif
6982
6983 wxGridCellAttr* attr = GetCellAttr(row, col);
6984
6985 bool isCurrent = coords == m_currentCellCoords;
6986
6987 wxRect rect = CellToRect( row, col );
6988
6989 // if the editor is shown, we should use it and not the renderer
6990 // Note: However, only if it is really _shown_, i.e. not hidden!
6991 if ( isCurrent && IsCellEditControlShown() )
6992 {
6993 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6994 editor->PaintBackground(rect, attr);
6995 editor->DecRef();
6996 }
6997 else
6998 {
6999 // but all the rest is drawn by the cell renderer and hence may be
7000 // customized
7001 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7002 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7003 renderer->DecRef();
7004 }
7005
7006 attr->DecRef();
7007 }
7008
7009 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
7010 {
7011 int row = m_currentCellCoords.GetRow();
7012 int col = m_currentCellCoords.GetCol();
7013
7014 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7015 return;
7016
7017 wxRect rect = CellToRect(row, col);
7018
7019 // hmmm... what could we do here to show that the cell is disabled?
7020 // for now, I just draw a thinner border than for the other ones, but
7021 // it doesn't look really good
7022
7023 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7024
7025 if (penWidth > 0)
7026 {
7027 // The center of th drawn line is where the position/width/height of
7028 // the rectangle is actually at, (on wxMSW atr least,) so we will
7029 // reduce the size of the rectangle to compensate for the thickness of
7030 // the line. If this is too strange on non wxMSW platforms then
7031 // please #ifdef this appropriately.
7032 rect.x += penWidth/2;
7033 rect.y += penWidth/2;
7034 rect.width -= penWidth-1;
7035 rect.height -= penWidth-1;
7036
7037
7038 // Now draw the rectangle
7039 // use the cellHighlightColour if the cell is inside a selection, this
7040 // will ensure the cell is always visible.
7041 dc.SetPen(wxPen(IsInSelection(row,col)?m_selectionForeground:m_cellHighlightColour, penWidth, wxSOLID));
7042 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7043 dc.DrawRectangle(rect);
7044 }
7045
7046 #if 0
7047 // VZ: my experiments with 3d borders...
7048
7049 // how to properly set colours for arbitrary bg?
7050 wxCoord x1 = rect.x,
7051 y1 = rect.y,
7052 x2 = rect.x + rect.width -1,
7053 y2 = rect.y + rect.height -1;
7054
7055 dc.SetPen(*wxWHITE_PEN);
7056 dc.DrawLine(x1, y1, x2, y1);
7057 dc.DrawLine(x1, y1, x1, y2);
7058
7059 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
7060 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
7061
7062 dc.SetPen(*wxBLACK_PEN);
7063 dc.DrawLine(x1, y2, x2, y2);
7064 dc.DrawLine(x2, y1, x2, y2+1);
7065 #endif // 0
7066 }
7067
7068
7069 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7070 {
7071 int row = coords.GetRow();
7072 int col = coords.GetCol();
7073 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7074 return;
7075
7076 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
7077
7078 wxRect rect = CellToRect( row, col );
7079
7080 // right hand border
7081 //
7082 dc.DrawLine( rect.x + rect.width, rect.y,
7083 rect.x + rect.width, rect.y + rect.height + 1 );
7084
7085 // bottom border
7086 //
7087 dc.DrawLine( rect.x, rect.y + rect.height,
7088 rect.x + rect.width, rect.y + rect.height);
7089 }
7090
7091 void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
7092 {
7093 // This if block was previously in wxGrid::OnPaint but that doesn't
7094 // seem to get called under wxGTK - MB
7095 //
7096 if ( m_currentCellCoords == wxGridNoCellCoords &&
7097 m_numRows && m_numCols )
7098 {
7099 m_currentCellCoords.Set(0, 0);
7100 }
7101
7102 if ( IsCellEditControlShown() )
7103 {
7104 // don't show highlight when the edit control is shown
7105 return;
7106 }
7107
7108 // if the active cell was repainted, repaint its highlight too because it
7109 // might have been damaged by the grid lines
7110 size_t count = cells.GetCount();
7111 for ( size_t n = 0; n < count; n++ )
7112 {
7113 if ( cells[n] == m_currentCellCoords )
7114 {
7115 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7116 DrawCellHighlight(dc, attr);
7117 attr->DecRef();
7118
7119 break;
7120 }
7121 }
7122 }
7123
7124 // TODO: remove this ???
7125 // This is used to redraw all grid lines e.g. when the grid line colour
7126 // has been changed
7127 //
7128 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7129 {
7130 #if !WXGRID_DRAW_LINES
7131 return;
7132 #endif
7133
7134 if ( !m_gridLinesEnabled ||
7135 !m_numRows ||
7136 !m_numCols ) return;
7137
7138 int top, bottom, left, right;
7139
7140 #if 0 //#ifndef __WXGTK__
7141 if (reg.IsEmpty())
7142 {
7143 int cw, ch;
7144 m_gridWin->GetClientSize(&cw, &ch);
7145
7146 // virtual coords of visible area
7147 //
7148 CalcUnscrolledPosition( 0, 0, &left, &top );
7149 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7150 }
7151 else
7152 {
7153 wxCoord x, y, w, h;
7154 reg.GetBox(x, y, w, h);
7155 CalcUnscrolledPosition( x, y, &left, &top );
7156 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7157 }
7158 #else
7159 int cw, ch;
7160 m_gridWin->GetClientSize(&cw, &ch);
7161 CalcUnscrolledPosition( 0, 0, &left, &top );
7162 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7163 #endif
7164
7165 // avoid drawing grid lines past the last row and col
7166 //
7167 right = wxMin( right, GetColRight(m_numCols - 1) );
7168 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7169
7170 // no gridlines inside multicells, clip them out
7171 int leftCol = internalXToCol(left);
7172 int topRow = internalYToRow(top);
7173 int rightCol = internalXToCol(right);
7174 int bottomRow = internalYToRow(bottom);
7175
7176 #ifndef __WXMAC__
7177 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7178 wxRegion clippedcells(0, 0, cw, ch);
7179
7180 int i, j, cell_rows, cell_cols;
7181 wxRect rect;
7182
7183 for (j=topRow; j<bottomRow; j++)
7184 {
7185 for (i=leftCol; i<rightCol; i++)
7186 {
7187 GetCellSize( j, i, &cell_rows, &cell_cols );
7188 if ((cell_rows > 1) || (cell_cols > 1))
7189 {
7190 rect = CellToRect(j,i);
7191 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7192 clippedcells.Subtract(rect);
7193 }
7194 else if ((cell_rows < 0) || (cell_cols < 0))
7195 {
7196 rect = CellToRect(j+cell_rows, i+cell_cols);
7197 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7198 clippedcells.Subtract(rect);
7199 }
7200 }
7201 }
7202 #else
7203 wxRegion clippedcells( left , top, right - left, bottom - top);
7204
7205 int i, j, cell_rows, cell_cols;
7206 wxRect rect;
7207
7208 for (j=topRow; j<bottomRow; j++)
7209 {
7210 for (i=leftCol; i<rightCol; i++)
7211 {
7212 GetCellSize( j, i, &cell_rows, &cell_cols );
7213 if ((cell_rows > 1) || (cell_cols > 1))
7214 {
7215 rect = CellToRect(j,i);
7216 clippedcells.Subtract(rect);
7217 }
7218 else if ((cell_rows < 0) || (cell_cols < 0))
7219 {
7220 rect = CellToRect(j+cell_rows, i+cell_cols);
7221 clippedcells.Subtract(rect);
7222 }
7223 }
7224 }
7225 #endif
7226 dc.SetClippingRegion( clippedcells );
7227
7228 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
7229
7230 // horizontal grid lines
7231 //
7232 // already declared above - int i;
7233 for ( i = internalYToRow(top); i < m_numRows; i++ )
7234 {
7235 int bot = GetRowBottom(i) - 1;
7236
7237 if ( bot > bottom )
7238 {
7239 break;
7240 }
7241
7242 if ( bot >= top )
7243 {
7244 dc.DrawLine( left, bot, right, bot );
7245 }
7246 }
7247
7248
7249 // vertical grid lines
7250 //
7251 for ( i = internalXToCol(left); i < m_numCols; i++ )
7252 {
7253 int colRight = GetColRight(i) - 1;
7254 if ( colRight > right )
7255 {
7256 break;
7257 }
7258
7259 if ( colRight >= left )
7260 {
7261 dc.DrawLine( colRight, top, colRight, bottom );
7262 }
7263 }
7264 dc.DestroyClippingRegion();
7265 }
7266
7267
7268 void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
7269 {
7270 if ( !m_numRows ) return;
7271
7272 size_t i;
7273 size_t numLabels = rows.GetCount();
7274
7275 for ( i = 0; i < numLabels; i++ )
7276 {
7277 DrawRowLabel( dc, rows[i] );
7278 }
7279 }
7280
7281
7282 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7283 {
7284 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7285 return;
7286
7287 wxRect rect;
7288 #ifdef __WXGTK20__
7289 rect.SetX( 1 );
7290 rect.SetY( GetRowTop(row) + 1 );
7291 rect.SetWidth( m_rowLabelWidth - 2 );
7292 rect.SetHeight( GetRowHeight(row) - 2 );
7293
7294 CalcScrolledPosition( 0, rect.y, NULL, &rect.y );
7295
7296 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7297
7298 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7299 #else
7300 int rowTop = GetRowTop(row),
7301 rowBottom = GetRowBottom(row) - 1;
7302
7303 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7304 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7305 m_rowLabelWidth-1, rowBottom );
7306
7307 dc.DrawLine( 0, rowTop, 0, rowBottom );
7308
7309 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7310
7311 dc.SetPen( *wxWHITE_PEN );
7312 dc.DrawLine( 1, rowTop, 1, rowBottom );
7313 dc.DrawLine( 1, rowTop, m_rowLabelWidth-1, rowTop );
7314 #endif
7315 dc.SetBackgroundMode( wxTRANSPARENT );
7316 dc.SetTextForeground( GetLabelTextColour() );
7317 dc.SetFont( GetLabelFont() );
7318
7319 int hAlign, vAlign;
7320 GetRowLabelAlignment( &hAlign, &vAlign );
7321
7322 rect.SetX( 2 );
7323 rect.SetY( GetRowTop(row) + 2 );
7324 rect.SetWidth( m_rowLabelWidth - 4 );
7325 rect.SetHeight( GetRowHeight(row) - 4 );
7326 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7327 }
7328
7329
7330 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
7331 {
7332 if ( !m_numCols ) return;
7333
7334 size_t i;
7335 size_t numLabels = cols.GetCount();
7336
7337 for ( i = 0; i < numLabels; i++ )
7338 {
7339 DrawColLabel( dc, cols[i] );
7340 }
7341 }
7342
7343
7344 void wxGrid::DrawColLabel( wxDC& dc, int col )
7345 {
7346 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7347 return;
7348
7349 int colLeft = GetColLeft(col);
7350
7351 wxRect rect;
7352 #ifdef __WXGTK20__
7353 rect.SetX( colLeft + 1 );
7354 rect.SetY( 1 );
7355 rect.SetWidth( GetColWidth(col) - 2 );
7356 rect.SetHeight( m_colLabelHeight - 2 );
7357
7358 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7359
7360 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7361 #else
7362 int colRight = GetColRight(col) - 1;
7363
7364 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7365 dc.DrawLine( colRight, 0,
7366 colRight, m_colLabelHeight-1 );
7367
7368 dc.DrawLine( colLeft, 0, colRight, 0 );
7369
7370 dc.DrawLine( colLeft, m_colLabelHeight-1,
7371 colRight+1, m_colLabelHeight-1 );
7372
7373 dc.SetPen( *wxWHITE_PEN );
7374 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
7375 dc.DrawLine( colLeft, 1, colRight, 1 );
7376 #endif
7377 dc.SetBackgroundMode( wxTRANSPARENT );
7378 dc.SetTextForeground( GetLabelTextColour() );
7379 dc.SetFont( GetLabelFont() );
7380
7381 int hAlign, vAlign, orient;
7382 GetColLabelAlignment( &hAlign, &vAlign );
7383 orient = GetColLabelTextOrientation();
7384
7385 rect.SetX( colLeft + 2 );
7386 rect.SetY( 2 );
7387 rect.SetWidth( GetColWidth(col) - 4 );
7388 rect.SetHeight( m_colLabelHeight - 4 );
7389 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
7390 }
7391
7392 void wxGrid::DrawTextRectangle( wxDC& dc,
7393 const wxString& value,
7394 const wxRect& rect,
7395 int horizAlign,
7396 int vertAlign,
7397 int textOrientation )
7398 {
7399 wxArrayString lines;
7400
7401 StringToLines( value, lines );
7402
7403
7404 //Forward to new API.
7405 DrawTextRectangle( dc,
7406 lines,
7407 rect,
7408 horizAlign,
7409 vertAlign,
7410 textOrientation );
7411
7412 }
7413
7414 void wxGrid::DrawTextRectangle( wxDC& dc,
7415 const wxArrayString& lines,
7416 const wxRect& rect,
7417 int horizAlign,
7418 int vertAlign,
7419 int textOrientation )
7420 {
7421 long textWidth, textHeight;
7422 long lineWidth, lineHeight;
7423 int nLines;
7424
7425 dc.SetClippingRegion( rect );
7426
7427 nLines = lines.GetCount();
7428 if( nLines > 0 )
7429 {
7430 int l;
7431 float x = 0.0, y = 0.0;
7432
7433 if( textOrientation == wxHORIZONTAL )
7434 GetTextBoxSize(dc, lines, &textWidth, &textHeight);
7435 else
7436 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
7437
7438 switch( vertAlign )
7439 {
7440 case wxALIGN_BOTTOM:
7441 if( textOrientation == wxHORIZONTAL )
7442 y = rect.y + (rect.height - textHeight - 1);
7443 else
7444 x = rect.x + rect.width - textWidth;
7445 break;
7446
7447 case wxALIGN_CENTRE:
7448 if( textOrientation == wxHORIZONTAL )
7449 y = rect.y + ((rect.height - textHeight)/2);
7450 else
7451 x = rect.x + ((rect.width - textWidth)/2);
7452 break;
7453
7454 case wxALIGN_TOP:
7455 default:
7456 if( textOrientation == wxHORIZONTAL )
7457 y = rect.y + 1;
7458 else
7459 x = rect.x + 1;
7460 break;
7461 }
7462
7463 // Align each line of a multi-line label
7464 for( l = 0; l < nLines; l++ )
7465 {
7466 dc.GetTextExtent(lines[l], &lineWidth, &lineHeight);
7467
7468 switch( horizAlign )
7469 {
7470 case wxALIGN_RIGHT:
7471 if( textOrientation == wxHORIZONTAL )
7472 x = rect.x + (rect.width - lineWidth - 1);
7473 else
7474 y = rect.y + lineWidth + 1;
7475 break;
7476
7477 case wxALIGN_CENTRE:
7478 if( textOrientation == wxHORIZONTAL )
7479 x = rect.x + ((rect.width - lineWidth)/2);
7480 else
7481 y = rect.y + rect.height - ((rect.height - lineWidth)/2);
7482 break;
7483
7484 case wxALIGN_LEFT:
7485 default:
7486 if( textOrientation == wxHORIZONTAL )
7487 x = rect.x + 1;
7488 else
7489 y = rect.y + rect.height - 1;
7490 break;
7491 }
7492
7493 if( textOrientation == wxHORIZONTAL )
7494 {
7495 dc.DrawText( lines[l], (int)x, (int)y );
7496 y += lineHeight;
7497 }
7498 else
7499 {
7500 dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 );
7501 x += lineHeight;
7502 }
7503 }
7504 }
7505 dc.DestroyClippingRegion();
7506 }
7507
7508
7509 // Split multi line text up into an array of strings. Any existing
7510 // contents of the string array are preserved.
7511 //
7512 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
7513 {
7514 int startPos = 0;
7515 int pos;
7516 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
7517 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
7518
7519 while ( startPos < (int)tVal.Length() )
7520 {
7521 pos = tVal.Mid(startPos).Find( eol );
7522 if ( pos < 0 )
7523 {
7524 break;
7525 }
7526 else if ( pos == 0 )
7527 {
7528 lines.Add( wxEmptyString );
7529 }
7530 else
7531 {
7532 lines.Add( value.Mid(startPos, pos) );
7533 }
7534 startPos += pos+1;
7535 }
7536 if ( startPos < (int)value.Length() )
7537 {
7538 lines.Add( value.Mid( startPos ) );
7539 }
7540 }
7541
7542
7543 void wxGrid::GetTextBoxSize( wxDC& dc,
7544 const wxArrayString& lines,
7545 long *width, long *height )
7546 {
7547 long w = 0;
7548 long h = 0;
7549 long lineW, lineH;
7550
7551 size_t i;
7552 for ( i = 0; i < lines.GetCount(); i++ )
7553 {
7554 dc.GetTextExtent( lines[i], &lineW, &lineH );
7555 w = wxMax( w, lineW );
7556 h += lineH;
7557 }
7558
7559 *width = w;
7560 *height = h;
7561 }
7562
7563 //
7564 // ------ Batch processing.
7565 //
7566 void wxGrid::EndBatch()
7567 {
7568 if ( m_batchCount > 0 )
7569 {
7570 m_batchCount--;
7571 if ( !m_batchCount )
7572 {
7573 CalcDimensions();
7574 m_rowLabelWin->Refresh();
7575 m_colLabelWin->Refresh();
7576 m_cornerLabelWin->Refresh();
7577 m_gridWin->Refresh();
7578 }
7579 }
7580 }
7581
7582 // Use this, rather than wxWindow::Refresh(), to force an immediate
7583 // repainting of the grid. Has no effect if you are already inside a
7584 // BeginBatch / EndBatch block.
7585 //
7586 void wxGrid::ForceRefresh()
7587 {
7588 BeginBatch();
7589 EndBatch();
7590 }
7591
7592 bool wxGrid::Enable(bool enable)
7593 {
7594 if ( !wxScrolledWindow::Enable(enable) )
7595 return false;
7596
7597 // redraw in the new state
7598 m_gridWin->Refresh();
7599
7600 return true;
7601 }
7602
7603 //
7604 // ------ Edit control functions
7605 //
7606
7607
7608 void wxGrid::EnableEditing( bool edit )
7609 {
7610 // TODO: improve this ?
7611 //
7612 if ( edit != m_editable )
7613 {
7614 if(!edit) EnableCellEditControl(edit);
7615 m_editable = edit;
7616 }
7617 }
7618
7619
7620 void wxGrid::EnableCellEditControl( bool enable )
7621 {
7622 if (! m_editable)
7623 return;
7624
7625 if ( m_currentCellCoords == wxGridNoCellCoords )
7626 SetCurrentCell( 0, 0 );
7627
7628 if ( enable != m_cellEditCtrlEnabled )
7629 {
7630 if ( enable )
7631 {
7632 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
7633 return;
7634
7635 // this should be checked by the caller!
7636 wxASSERT_MSG( CanEnableCellControl(),
7637 _T("can't enable editing for this cell!") );
7638
7639 // do it before ShowCellEditControl()
7640 m_cellEditCtrlEnabled = enable;
7641
7642 ShowCellEditControl();
7643 }
7644 else
7645 {
7646 //FIXME:add veto support
7647 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
7648
7649 HideCellEditControl();
7650 SaveEditControlValue();
7651
7652 // do it after HideCellEditControl()
7653 m_cellEditCtrlEnabled = enable;
7654 }
7655 }
7656 }
7657
7658 bool wxGrid::IsCurrentCellReadOnly() const
7659 {
7660 // const_cast
7661 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
7662 bool readonly = attr->IsReadOnly();
7663 attr->DecRef();
7664
7665 return readonly;
7666 }
7667
7668 bool wxGrid::CanEnableCellControl() const
7669 {
7670 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
7671 !IsCurrentCellReadOnly();
7672
7673 }
7674
7675 bool wxGrid::IsCellEditControlEnabled() const
7676 {
7677 // the cell edit control might be disable for all cells or just for the
7678 // current one if it's read only
7679 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
7680 }
7681
7682 bool wxGrid::IsCellEditControlShown() const
7683 {
7684 bool isShown = false;
7685
7686 if ( m_cellEditCtrlEnabled )
7687 {
7688 int row = m_currentCellCoords.GetRow();
7689 int col = m_currentCellCoords.GetCol();
7690 wxGridCellAttr* attr = GetCellAttr(row, col);
7691 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
7692 attr->DecRef();
7693
7694 if ( editor )
7695 {
7696 if ( editor->IsCreated() )
7697 {
7698 isShown = editor->GetControl()->IsShown();
7699 }
7700
7701 editor->DecRef();
7702 }
7703 }
7704
7705 return isShown;
7706 }
7707
7708 void wxGrid::ShowCellEditControl()
7709 {
7710 if ( IsCellEditControlEnabled() )
7711 {
7712 if ( !IsVisible( m_currentCellCoords ) )
7713 {
7714 m_cellEditCtrlEnabled = false;
7715 return;
7716 }
7717 else
7718 {
7719 wxRect rect = CellToRect( m_currentCellCoords );
7720 int row = m_currentCellCoords.GetRow();
7721 int col = m_currentCellCoords.GetCol();
7722
7723 // if this is part of a multicell, find owner (topleft)
7724 int cell_rows, cell_cols;
7725 GetCellSize( row, col, &cell_rows, &cell_cols );
7726 if ( cell_rows <= 0 || cell_cols <= 0 )
7727 {
7728 row += cell_rows;
7729 col += cell_cols;
7730 m_currentCellCoords.SetRow( row );
7731 m_currentCellCoords.SetCol( col );
7732 }
7733
7734 // convert to scrolled coords
7735 //
7736 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7737
7738 // done in PaintBackground()
7739 #if 0
7740 // erase the highlight and the cell contents because the editor
7741 // might not cover the entire cell
7742 wxClientDC dc( m_gridWin );
7743 PrepareDC( dc );
7744 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7745 dc.SetPen(*wxTRANSPARENT_PEN);
7746 dc.DrawRectangle(rect);
7747 #endif // 0
7748
7749 // cell is shifted by one pixel
7750 // However, don't allow x or y to become negative
7751 // since the SetSize() method interprets that as
7752 // "don't change."
7753 if (rect.x > 0)
7754 rect.x--;
7755 if (rect.y > 0)
7756 rect.y--;
7757
7758 wxGridCellAttr* attr = GetCellAttr(row, col);
7759 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7760 if ( !editor->IsCreated() )
7761 {
7762 editor->Create(m_gridWin, wxID_ANY,
7763 new wxGridCellEditorEvtHandler(this, editor));
7764
7765 wxGridEditorCreatedEvent evt(GetId(),
7766 wxEVT_GRID_EDITOR_CREATED,
7767 this,
7768 row,
7769 col,
7770 editor->GetControl());
7771 GetEventHandler()->ProcessEvent(evt);
7772 }
7773
7774
7775 // resize editor to overflow into righthand cells if allowed
7776 int maxWidth = rect.width;
7777 wxString value = GetCellValue(row, col);
7778 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
7779 {
7780 int y;
7781 GetTextExtent(value, &maxWidth, &y,
7782 NULL, NULL, &attr->GetFont());
7783 if (maxWidth < rect.width) maxWidth = rect.width;
7784 }
7785 int client_right = m_gridWin->GetClientSize().GetWidth();
7786 if (rect.x+maxWidth > client_right)
7787 maxWidth = client_right - rect.x;
7788
7789 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
7790 {
7791 GetCellSize( row, col, &cell_rows, &cell_cols );
7792 // may have changed earlier
7793 for (int i = col+cell_cols; i < m_numCols; i++)
7794 {
7795 int c_rows, c_cols;
7796 GetCellSize( row, i, &c_rows, &c_cols );
7797 // looks weird going over a multicell
7798 if (m_table->IsEmptyCell(row,i) &&
7799 (rect.width < maxWidth) && (c_rows == 1))
7800 rect.width += GetColWidth(i);
7801 else
7802 break;
7803 }
7804 if (rect.GetRight() > client_right)
7805 rect.SetRight(client_right-1);
7806 }
7807
7808 editor->SetCellAttr(attr);
7809 editor->SetSize( rect );
7810 editor->Show( true, attr );
7811
7812 // recalc dimensions in case we need to
7813 // expand the scrolled window to account for editor
7814 CalcDimensions();
7815
7816 editor->BeginEdit(row, col, this);
7817 editor->SetCellAttr(NULL);
7818
7819 editor->DecRef();
7820 attr->DecRef();
7821 }
7822 }
7823 }
7824
7825
7826 void wxGrid::HideCellEditControl()
7827 {
7828 if ( IsCellEditControlEnabled() )
7829 {
7830 int row = m_currentCellCoords.GetRow();
7831 int col = m_currentCellCoords.GetCol();
7832
7833 wxGridCellAttr* attr = GetCellAttr(row, col);
7834 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7835 editor->Show( false );
7836 editor->DecRef();
7837 attr->DecRef();
7838 m_gridWin->SetFocus();
7839 // refresh whole row to the right
7840 wxRect rect( CellToRect(row, col) );
7841 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
7842 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
7843 #ifdef __WXMAC__
7844 // ensure that the pixels under the focus ring get refreshed as well
7845 rect.Inflate(10,10);
7846 #endif
7847 m_gridWin->Refresh( false, &rect );
7848 }
7849 }
7850
7851
7852 void wxGrid::SaveEditControlValue()
7853 {
7854 if ( IsCellEditControlEnabled() )
7855 {
7856 int row = m_currentCellCoords.GetRow();
7857 int col = m_currentCellCoords.GetCol();
7858
7859 wxString oldval = GetCellValue(row,col);
7860
7861 wxGridCellAttr* attr = GetCellAttr(row, col);
7862 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7863 bool changed = editor->EndEdit(row, col, this);
7864
7865 editor->DecRef();
7866 attr->DecRef();
7867
7868 if (changed)
7869 {
7870 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
7871 m_currentCellCoords.GetRow(),
7872 m_currentCellCoords.GetCol() ) < 0 ) {
7873
7874 // Event has been vetoed, set the data back.
7875 SetCellValue(row,col,oldval);
7876 }
7877 }
7878 }
7879 }
7880
7881
7882 //
7883 // ------ Grid location functions
7884 // Note that all of these functions work with the logical coordinates of
7885 // grid cells and labels so you will need to convert from device
7886 // coordinates for mouse events etc.
7887 //
7888
7889 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
7890 {
7891 int row = YToRow(y);
7892 int col = XToCol(x);
7893
7894 if ( row == -1 || col == -1 )
7895 {
7896 coords = wxGridNoCellCoords;
7897 }
7898 else
7899 {
7900 coords.Set( row, col );
7901 }
7902 }
7903
7904
7905 // Internal Helper function for computing row or column from some
7906 // (unscrolled) coordinate value, using either
7907 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7908 // of m_rowBottoms/m_ColRights to speed up the search!
7909
7910 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
7911 const wxArrayInt& BorderArray, int nMax,
7912 bool clipToMinMax)
7913 {
7914
7915 if (coord < 0)
7916 return clipToMinMax && (nMax > 0) ? 0 : -1;
7917
7918
7919 if (!defaultDist)
7920 defaultDist = 1;
7921
7922 size_t i_max = coord / defaultDist,
7923 i_min = 0;
7924
7925 if (BorderArray.IsEmpty())
7926 {
7927 if((int) i_max < nMax)
7928 return i_max;
7929 return clipToMinMax ? nMax - 1 : -1;
7930 }
7931
7932 if ( i_max >= BorderArray.GetCount())
7933 i_max = BorderArray.GetCount() - 1;
7934 else
7935 {
7936 if ( coord >= BorderArray[i_max])
7937 {
7938 i_min = i_max;
7939 if (minDist)
7940 i_max = coord / minDist;
7941 else
7942 i_max = BorderArray.GetCount() - 1;
7943 }
7944 if ( i_max >= BorderArray.GetCount())
7945 i_max = BorderArray.GetCount() - 1;
7946 }
7947 if ( coord >= BorderArray[i_max])
7948 return clipToMinMax ? (int)i_max : -1;
7949 if ( coord < BorderArray[0] )
7950 return 0;
7951
7952 while ( i_max - i_min > 0 )
7953 {
7954 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
7955 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7956 if (coord >= BorderArray[ i_max - 1])
7957 return i_max;
7958 else
7959 i_max--;
7960 int median = i_min + (i_max - i_min + 1) / 2;
7961 if (coord < BorderArray[median])
7962 i_max = median;
7963 else
7964 i_min = median;
7965 }
7966 return i_max;
7967 }
7968
7969 int wxGrid::YToRow( int y )
7970 {
7971 return CoordToRowOrCol(y, m_defaultRowHeight,
7972 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
7973 }
7974
7975
7976 int wxGrid::XToCol( int x )
7977 {
7978 return CoordToRowOrCol(x, m_defaultColWidth,
7979 m_minAcceptableColWidth, m_colRights, m_numCols, false);
7980 }
7981
7982
7983 // return the row number that that the y coord is near the edge of, or
7984 // -1 if not near an edge
7985 //
7986 int wxGrid::YToEdgeOfRow( int y )
7987 {
7988 int i;
7989 i = internalYToRow(y);
7990
7991 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
7992 {
7993 // We know that we are in row i, test whether we are
7994 // close enough to lower or upper border, respectively.
7995 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
7996 return i;
7997 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
7998 return i - 1;
7999 }
8000
8001 return -1;
8002 }
8003
8004
8005 // return the col number that that the x coord is near the edge of, or
8006 // -1 if not near an edge
8007 //
8008 int wxGrid::XToEdgeOfCol( int x )
8009 {
8010 int i;
8011 i = internalXToCol(x);
8012
8013 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
8014 {
8015 // We know that we are in column i, test whether we are
8016 // close enough to right or left border, respectively.
8017 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8018 return i;
8019 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
8020 return i - 1;
8021 }
8022
8023 return -1;
8024 }
8025
8026
8027 wxRect wxGrid::CellToRect( int row, int col )
8028 {
8029 wxRect rect( -1, -1, -1, -1 );
8030
8031 if ( row >= 0 && row < m_numRows &&
8032 col >= 0 && col < m_numCols )
8033 {
8034 int i, cell_rows, cell_cols;
8035 rect.width = rect.height = 0;
8036 GetCellSize( row, col, &cell_rows, &cell_cols );
8037 // if negative then find multicell owner
8038 if (cell_rows < 0) row += cell_rows;
8039 if (cell_cols < 0) col += cell_cols;
8040 GetCellSize( row, col, &cell_rows, &cell_cols );
8041
8042 rect.x = GetColLeft(col);
8043 rect.y = GetRowTop(row);
8044 for (i=col; i<col+cell_cols; i++)
8045 rect.width += GetColWidth(i);
8046 for (i=row; i<row+cell_rows; i++)
8047 rect.height += GetRowHeight(i);
8048 }
8049
8050 // if grid lines are enabled, then the area of the cell is a bit smaller
8051 if (m_gridLinesEnabled) {
8052 rect.width -= 1;
8053 rect.height -= 1;
8054 }
8055 return rect;
8056 }
8057
8058
8059 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
8060 {
8061 // get the cell rectangle in logical coords
8062 //
8063 wxRect r( CellToRect( row, col ) );
8064
8065 // convert to device coords
8066 //
8067 int left, top, right, bottom;
8068 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8069 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8070
8071 // check against the client area of the grid window
8072 //
8073 int cw, ch;
8074 m_gridWin->GetClientSize( &cw, &ch );
8075
8076 if ( wholeCellVisible )
8077 {
8078 // is the cell wholly visible ?
8079 //
8080 return ( left >= 0 && right <= cw &&
8081 top >= 0 && bottom <= ch );
8082 }
8083 else
8084 {
8085 // is the cell partly visible ?
8086 //
8087 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
8088 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
8089 }
8090 }
8091
8092
8093 // make the specified cell location visible by doing a minimal amount
8094 // of scrolling
8095 //
8096 void wxGrid::MakeCellVisible( int row, int col )
8097 {
8098
8099 int i;
8100 int xpos = -1, ypos = -1;
8101
8102 if ( row >= 0 && row < m_numRows &&
8103 col >= 0 && col < m_numCols )
8104 {
8105 // get the cell rectangle in logical coords
8106 //
8107 wxRect r( CellToRect( row, col ) );
8108
8109 // convert to device coords
8110 //
8111 int left, top, right, bottom;
8112 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8113 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8114
8115 int cw, ch;
8116 m_gridWin->GetClientSize( &cw, &ch );
8117
8118 if ( top < 0 )
8119 {
8120 ypos = r.GetTop();
8121 }
8122 else if ( bottom > ch )
8123 {
8124 int h = r.GetHeight();
8125 ypos = r.GetTop();
8126 for ( i = row-1; i >= 0; i-- )
8127 {
8128 int rowHeight = GetRowHeight(i);
8129 if ( h + rowHeight > ch )
8130 break;
8131
8132 h += rowHeight;
8133 ypos -= rowHeight;
8134 }
8135
8136 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8137 // have rounding errors (this is important, because if we do, we
8138 // might not scroll at all and some cells won't be redrawn)
8139 //
8140 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8141 // scroll unit...
8142 ypos += m_scrollLineY;
8143 }
8144
8145 if ( left < 0 )
8146 {
8147 xpos = r.GetLeft();
8148 }
8149 else if ( right > cw )
8150 {
8151 // position the view so that the cell is on the right
8152 int x0, y0;
8153 CalcUnscrolledPosition(0, 0, &x0, &y0);
8154 xpos = x0 + (right - cw);
8155
8156 // see comment for ypos above
8157 xpos += m_scrollLineX;
8158 }
8159
8160 if ( xpos != -1 || ypos != -1 )
8161 {
8162 if ( xpos != -1 )
8163 xpos /= m_scrollLineX;
8164 if ( ypos != -1 )
8165 ypos /= m_scrollLineY;
8166 Scroll( xpos, ypos );
8167 AdjustScrollbars();
8168 }
8169 }
8170 }
8171
8172
8173 //
8174 // ------ Grid cursor movement functions
8175 //
8176
8177 bool wxGrid::MoveCursorUp( bool expandSelection )
8178 {
8179 if ( m_currentCellCoords != wxGridNoCellCoords &&
8180 m_currentCellCoords.GetRow() >= 0 )
8181 {
8182 if ( expandSelection)
8183 {
8184 if ( m_selectingKeyboard == wxGridNoCellCoords )
8185 m_selectingKeyboard = m_currentCellCoords;
8186 if ( m_selectingKeyboard.GetRow() > 0 )
8187 {
8188 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8189 MakeCellVisible( m_selectingKeyboard.GetRow(),
8190 m_selectingKeyboard.GetCol() );
8191 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8192 }
8193 }
8194 else if ( m_currentCellCoords.GetRow() > 0 )
8195 {
8196 ClearSelection();
8197 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
8198 m_currentCellCoords.GetCol() );
8199 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
8200 m_currentCellCoords.GetCol() );
8201 }
8202 else
8203 return false;
8204 return true;
8205 }
8206
8207 return false;
8208 }
8209
8210
8211 bool wxGrid::MoveCursorDown( bool expandSelection )
8212 {
8213 if ( m_currentCellCoords != wxGridNoCellCoords &&
8214 m_currentCellCoords.GetRow() < m_numRows )
8215 {
8216 if ( expandSelection )
8217 {
8218 if ( m_selectingKeyboard == wxGridNoCellCoords )
8219 m_selectingKeyboard = m_currentCellCoords;
8220 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
8221 {
8222 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8223 MakeCellVisible( m_selectingKeyboard.GetRow(),
8224 m_selectingKeyboard.GetCol() );
8225 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8226 }
8227 }
8228 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
8229 {
8230 ClearSelection();
8231 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
8232 m_currentCellCoords.GetCol() );
8233 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
8234 m_currentCellCoords.GetCol() );
8235 }
8236 else
8237 return false;
8238 return true;
8239 }
8240
8241 return false;
8242 }
8243
8244
8245 bool wxGrid::MoveCursorLeft( bool expandSelection )
8246 {
8247 if ( m_currentCellCoords != wxGridNoCellCoords &&
8248 m_currentCellCoords.GetCol() >= 0 )
8249 {
8250 if ( expandSelection )
8251 {
8252 if ( m_selectingKeyboard == wxGridNoCellCoords )
8253 m_selectingKeyboard = m_currentCellCoords;
8254 if ( m_selectingKeyboard.GetCol() > 0 )
8255 {
8256 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8257 MakeCellVisible( m_selectingKeyboard.GetRow(),
8258 m_selectingKeyboard.GetCol() );
8259 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8260 }
8261 }
8262 else if ( m_currentCellCoords.GetCol() > 0 )
8263 {
8264 ClearSelection();
8265 MakeCellVisible( m_currentCellCoords.GetRow(),
8266 m_currentCellCoords.GetCol() - 1 );
8267 SetCurrentCell( m_currentCellCoords.GetRow(),
8268 m_currentCellCoords.GetCol() - 1 );
8269 }
8270 else
8271 return false;
8272 return true;
8273 }
8274
8275 return false;
8276 }
8277
8278
8279 bool wxGrid::MoveCursorRight( bool expandSelection )
8280 {
8281 if ( m_currentCellCoords != wxGridNoCellCoords &&
8282 m_currentCellCoords.GetCol() < m_numCols )
8283 {
8284 if ( expandSelection )
8285 {
8286 if ( m_selectingKeyboard == wxGridNoCellCoords )
8287 m_selectingKeyboard = m_currentCellCoords;
8288 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8289 {
8290 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8291 MakeCellVisible( m_selectingKeyboard.GetRow(),
8292 m_selectingKeyboard.GetCol() );
8293 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8294 }
8295 }
8296 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
8297 {
8298 ClearSelection();
8299 MakeCellVisible( m_currentCellCoords.GetRow(),
8300 m_currentCellCoords.GetCol() + 1 );
8301 SetCurrentCell( m_currentCellCoords.GetRow(),
8302 m_currentCellCoords.GetCol() + 1 );
8303 }
8304 else
8305 return false;
8306 return true;
8307 }
8308
8309 return false;
8310 }
8311
8312
8313 bool wxGrid::MovePageUp()
8314 {
8315 if ( m_currentCellCoords == wxGridNoCellCoords ) return false;
8316
8317 int row = m_currentCellCoords.GetRow();
8318 if ( row > 0 )
8319 {
8320 int cw, ch;
8321 m_gridWin->GetClientSize( &cw, &ch );
8322
8323 int y = GetRowTop(row);
8324 int newRow = internalYToRow( y - ch + 1 );
8325
8326 if ( newRow == row )
8327 {
8328 //row > 0 , so newrow can never be less than 0 here.
8329 newRow = row - 1;
8330 }
8331
8332 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8333 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8334
8335 return true;
8336 }
8337
8338 return false;
8339 }
8340
8341 bool wxGrid::MovePageDown()
8342 {
8343 if ( m_currentCellCoords == wxGridNoCellCoords ) return false;
8344
8345 int row = m_currentCellCoords.GetRow();
8346 if ( (row+1) < m_numRows )
8347 {
8348 int cw, ch;
8349 m_gridWin->GetClientSize( &cw, &ch );
8350
8351 int y = GetRowTop(row);
8352 int newRow = internalYToRow( y + ch );
8353 if ( newRow == row )
8354 {
8355 // row < m_numRows , so newrow can't overflow here.
8356 newRow = row + 1;
8357 }
8358
8359 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8360 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8361
8362 return true;
8363 }
8364
8365 return false;
8366 }
8367
8368 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
8369 {
8370 if ( m_table &&
8371 m_currentCellCoords != wxGridNoCellCoords &&
8372 m_currentCellCoords.GetRow() > 0 )
8373 {
8374 int row = m_currentCellCoords.GetRow();
8375 int col = m_currentCellCoords.GetCol();
8376
8377 if ( m_table->IsEmptyCell(row, col) )
8378 {
8379 // starting in an empty cell: find the next block of
8380 // non-empty cells
8381 //
8382 while ( row > 0 )
8383 {
8384 row-- ;
8385 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8386 }
8387 }
8388 else if ( m_table->IsEmptyCell(row-1, col) )
8389 {
8390 // starting at the top of a block: find the next block
8391 //
8392 row--;
8393 while ( row > 0 )
8394 {
8395 row-- ;
8396 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8397 }
8398 }
8399 else
8400 {
8401 // starting within a block: find the top of the block
8402 //
8403 while ( row > 0 )
8404 {
8405 row-- ;
8406 if ( m_table->IsEmptyCell(row, col) )
8407 {
8408 row++ ;
8409 break;
8410 }
8411 }
8412 }
8413
8414 MakeCellVisible( row, col );
8415 if ( expandSelection )
8416 {
8417 m_selectingKeyboard = wxGridCellCoords( row, col );
8418 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8419 }
8420 else
8421 {
8422 ClearSelection();
8423 SetCurrentCell( row, col );
8424 }
8425 return true;
8426 }
8427
8428 return false;
8429 }
8430
8431 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
8432 {
8433 if ( m_table &&
8434 m_currentCellCoords != wxGridNoCellCoords &&
8435 m_currentCellCoords.GetRow() < m_numRows-1 )
8436 {
8437 int row = m_currentCellCoords.GetRow();
8438 int col = m_currentCellCoords.GetCol();
8439
8440 if ( m_table->IsEmptyCell(row, col) )
8441 {
8442 // starting in an empty cell: find the next block of
8443 // non-empty cells
8444 //
8445 while ( row < m_numRows-1 )
8446 {
8447 row++ ;
8448 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8449 }
8450 }
8451 else if ( m_table->IsEmptyCell(row+1, col) )
8452 {
8453 // starting at the bottom of a block: find the next block
8454 //
8455 row++;
8456 while ( row < m_numRows-1 )
8457 {
8458 row++ ;
8459 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8460 }
8461 }
8462 else
8463 {
8464 // starting within a block: find the bottom of the block
8465 //
8466 while ( row < m_numRows-1 )
8467 {
8468 row++ ;
8469 if ( m_table->IsEmptyCell(row, col) )
8470 {
8471 row-- ;
8472 break;
8473 }
8474 }
8475 }
8476
8477 MakeCellVisible( row, col );
8478 if ( expandSelection )
8479 {
8480 m_selectingKeyboard = wxGridCellCoords( row, col );
8481 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8482 }
8483 else
8484 {
8485 ClearSelection();
8486 SetCurrentCell( row, col );
8487 }
8488
8489 return true;
8490 }
8491
8492 return false;
8493 }
8494
8495 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
8496 {
8497 if ( m_table &&
8498 m_currentCellCoords != wxGridNoCellCoords &&
8499 m_currentCellCoords.GetCol() > 0 )
8500 {
8501 int row = m_currentCellCoords.GetRow();
8502 int col = m_currentCellCoords.GetCol();
8503
8504 if ( m_table->IsEmptyCell(row, col) )
8505 {
8506 // starting in an empty cell: find the next block of
8507 // non-empty cells
8508 //
8509 while ( col > 0 )
8510 {
8511 col-- ;
8512 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8513 }
8514 }
8515 else if ( m_table->IsEmptyCell(row, col-1) )
8516 {
8517 // starting at the left of a block: find the next block
8518 //
8519 col--;
8520 while ( col > 0 )
8521 {
8522 col-- ;
8523 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8524 }
8525 }
8526 else
8527 {
8528 // starting within a block: find the left of the block
8529 //
8530 while ( col > 0 )
8531 {
8532 col-- ;
8533 if ( m_table->IsEmptyCell(row, col) )
8534 {
8535 col++ ;
8536 break;
8537 }
8538 }
8539 }
8540
8541 MakeCellVisible( row, col );
8542 if ( expandSelection )
8543 {
8544 m_selectingKeyboard = wxGridCellCoords( row, col );
8545 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8546 }
8547 else
8548 {
8549 ClearSelection();
8550 SetCurrentCell( row, col );
8551 }
8552
8553 return true;
8554 }
8555
8556 return false;
8557 }
8558
8559 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
8560 {
8561 if ( m_table &&
8562 m_currentCellCoords != wxGridNoCellCoords &&
8563 m_currentCellCoords.GetCol() < m_numCols-1 )
8564 {
8565 int row = m_currentCellCoords.GetRow();
8566 int col = m_currentCellCoords.GetCol();
8567
8568 if ( m_table->IsEmptyCell(row, col) )
8569 {
8570 // starting in an empty cell: find the next block of
8571 // non-empty cells
8572 //
8573 while ( col < m_numCols-1 )
8574 {
8575 col++ ;
8576 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8577 }
8578 }
8579 else if ( m_table->IsEmptyCell(row, col+1) )
8580 {
8581 // starting at the right of a block: find the next block
8582 //
8583 col++;
8584 while ( col < m_numCols-1 )
8585 {
8586 col++ ;
8587 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8588 }
8589 }
8590 else
8591 {
8592 // starting within a block: find the right of the block
8593 //
8594 while ( col < m_numCols-1 )
8595 {
8596 col++ ;
8597 if ( m_table->IsEmptyCell(row, col) )
8598 {
8599 col-- ;
8600 break;
8601 }
8602 }
8603 }
8604
8605 MakeCellVisible( row, col );
8606 if ( expandSelection )
8607 {
8608 m_selectingKeyboard = wxGridCellCoords( row, col );
8609 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8610 }
8611 else
8612 {
8613 ClearSelection();
8614 SetCurrentCell( row, col );
8615 }
8616
8617 return true;
8618 }
8619
8620 return false;
8621 }
8622
8623
8624
8625 //
8626 // ------ Label values and formatting
8627 //
8628
8629 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
8630 {
8631 *horiz = m_rowLabelHorizAlign;
8632 *vert = m_rowLabelVertAlign;
8633 }
8634
8635 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
8636 {
8637 *horiz = m_colLabelHorizAlign;
8638 *vert = m_colLabelVertAlign;
8639 }
8640
8641 int wxGrid::GetColLabelTextOrientation()
8642 {
8643 return m_colLabelTextOrientation;
8644 }
8645
8646 wxString wxGrid::GetRowLabelValue( int row )
8647 {
8648 if ( m_table )
8649 {
8650 return m_table->GetRowLabelValue( row );
8651 }
8652 else
8653 {
8654 wxString s;
8655 s << row;
8656 return s;
8657 }
8658 }
8659
8660 wxString wxGrid::GetColLabelValue( int col )
8661 {
8662 if ( m_table )
8663 {
8664 return m_table->GetColLabelValue( col );
8665 }
8666 else
8667 {
8668 wxString s;
8669 s << col;
8670 return s;
8671 }
8672 }
8673
8674
8675 void wxGrid::SetRowLabelSize( int width )
8676 {
8677 width = wxMax( width, 0 );
8678 if ( width != m_rowLabelWidth )
8679 {
8680 if ( width == 0 )
8681 {
8682 m_rowLabelWin->Show( false );
8683 m_cornerLabelWin->Show( false );
8684 }
8685 else if ( m_rowLabelWidth == 0 )
8686 {
8687 m_rowLabelWin->Show( true );
8688 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( true );
8689 }
8690
8691 m_rowLabelWidth = width;
8692 CalcWindowSizes();
8693 wxScrolledWindow::Refresh( true );
8694 }
8695 }
8696
8697
8698 void wxGrid::SetColLabelSize( int height )
8699 {
8700 height = wxMax( height, 0 );
8701 if ( height != m_colLabelHeight )
8702 {
8703 if ( height == 0 )
8704 {
8705 m_colLabelWin->Show( false );
8706 m_cornerLabelWin->Show( false );
8707 }
8708 else if ( m_colLabelHeight == 0 )
8709 {
8710 m_colLabelWin->Show( true );
8711 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( true );
8712 }
8713
8714 m_colLabelHeight = height;
8715 CalcWindowSizes();
8716 wxScrolledWindow::Refresh( true );
8717 }
8718 }
8719
8720
8721 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
8722 {
8723 if ( m_labelBackgroundColour != colour )
8724 {
8725 m_labelBackgroundColour = colour;
8726 m_rowLabelWin->SetBackgroundColour( colour );
8727 m_colLabelWin->SetBackgroundColour( colour );
8728 m_cornerLabelWin->SetBackgroundColour( colour );
8729
8730 if ( !GetBatchCount() )
8731 {
8732 m_rowLabelWin->Refresh();
8733 m_colLabelWin->Refresh();
8734 m_cornerLabelWin->Refresh();
8735 }
8736 }
8737 }
8738
8739 void wxGrid::SetLabelTextColour( const wxColour& colour )
8740 {
8741 if ( m_labelTextColour != colour )
8742 {
8743 m_labelTextColour = colour;
8744 if ( !GetBatchCount() )
8745 {
8746 m_rowLabelWin->Refresh();
8747 m_colLabelWin->Refresh();
8748 }
8749 }
8750 }
8751
8752 void wxGrid::SetLabelFont( const wxFont& font )
8753 {
8754 m_labelFont = font;
8755 if ( !GetBatchCount() )
8756 {
8757 m_rowLabelWin->Refresh();
8758 m_colLabelWin->Refresh();
8759 }
8760 }
8761
8762 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
8763 {
8764 // allow old (incorrect) defs to be used
8765 switch ( horiz )
8766 {
8767 case wxLEFT: horiz = wxALIGN_LEFT; break;
8768 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8769 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8770 }
8771
8772 switch ( vert )
8773 {
8774 case wxTOP: vert = wxALIGN_TOP; break;
8775 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8776 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8777 }
8778
8779 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8780 {
8781 m_rowLabelHorizAlign = horiz;
8782 }
8783
8784 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8785 {
8786 m_rowLabelVertAlign = vert;
8787 }
8788
8789 if ( !GetBatchCount() )
8790 {
8791 m_rowLabelWin->Refresh();
8792 }
8793 }
8794
8795 void wxGrid::SetColLabelAlignment( int horiz, int vert )
8796 {
8797 // allow old (incorrect) defs to be used
8798 switch ( horiz )
8799 {
8800 case wxLEFT: horiz = wxALIGN_LEFT; break;
8801 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8802 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8803 }
8804
8805 switch ( vert )
8806 {
8807 case wxTOP: vert = wxALIGN_TOP; break;
8808 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8809 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8810 }
8811
8812 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8813 {
8814 m_colLabelHorizAlign = horiz;
8815 }
8816
8817 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8818 {
8819 m_colLabelVertAlign = vert;
8820 }
8821
8822 if ( !GetBatchCount() )
8823 {
8824 m_colLabelWin->Refresh();
8825 }
8826 }
8827
8828 // Note: under MSW, the default column label font must be changed because it
8829 // does not support vertical printing
8830 //
8831 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8832 // pGrid->SetLabelFont(font);
8833 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8834 //
8835 void wxGrid::SetColLabelTextOrientation( int textOrientation )
8836 {
8837 if( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
8838 {
8839 m_colLabelTextOrientation = textOrientation;
8840 }
8841
8842 if ( !GetBatchCount() )
8843 {
8844 m_colLabelWin->Refresh();
8845 }
8846 }
8847
8848 void wxGrid::SetRowLabelValue( int row, const wxString& s )
8849 {
8850 if ( m_table )
8851 {
8852 m_table->SetRowLabelValue( row, s );
8853 if ( !GetBatchCount() )
8854 {
8855 wxRect rect = CellToRect( row, 0);
8856 if ( rect.height > 0 )
8857 {
8858 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
8859 rect.x = 0;
8860 rect.width = m_rowLabelWidth;
8861 m_rowLabelWin->Refresh( true, &rect );
8862 }
8863 }
8864 }
8865 }
8866
8867 void wxGrid::SetColLabelValue( int col, const wxString& s )
8868 {
8869 if ( m_table )
8870 {
8871 m_table->SetColLabelValue( col, s );
8872 if ( !GetBatchCount() )
8873 {
8874 wxRect rect = CellToRect( 0, col );
8875 if ( rect.width > 0 )
8876 {
8877 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
8878 rect.y = 0;
8879 rect.height = m_colLabelHeight;
8880 m_colLabelWin->Refresh( true, &rect );
8881 }
8882 }
8883 }
8884 }
8885
8886 void wxGrid::SetGridLineColour( const wxColour& colour )
8887 {
8888 if ( m_gridLineColour != colour )
8889 {
8890 m_gridLineColour = colour;
8891
8892 wxClientDC dc( m_gridWin );
8893 PrepareDC( dc );
8894 DrawAllGridLines( dc, wxRegion() );
8895 }
8896 }
8897
8898
8899 void wxGrid::SetCellHighlightColour( const wxColour& colour )
8900 {
8901 if ( m_cellHighlightColour != colour )
8902 {
8903 m_cellHighlightColour = colour;
8904
8905 wxClientDC dc( m_gridWin );
8906 PrepareDC( dc );
8907 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
8908 DrawCellHighlight(dc, attr);
8909 attr->DecRef();
8910 }
8911 }
8912
8913 void wxGrid::SetCellHighlightPenWidth(int width)
8914 {
8915 if (m_cellHighlightPenWidth != width) {
8916 m_cellHighlightPenWidth = width;
8917
8918 // Just redrawing the cell highlight is not enough since that won't
8919 // make any visible change if the the thickness is getting smaller.
8920 int row = m_currentCellCoords.GetRow();
8921 int col = m_currentCellCoords.GetCol();
8922 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8923 return;
8924 wxRect rect = CellToRect(row, col);
8925 m_gridWin->Refresh(true, &rect);
8926 }
8927 }
8928
8929 void wxGrid::SetCellHighlightROPenWidth(int width)
8930 {
8931 if (m_cellHighlightROPenWidth != width) {
8932 m_cellHighlightROPenWidth = width;
8933
8934 // Just redrawing the cell highlight is not enough since that won't
8935 // make any visible change if the the thickness is getting smaller.
8936 int row = m_currentCellCoords.GetRow();
8937 int col = m_currentCellCoords.GetCol();
8938 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8939 return;
8940 wxRect rect = CellToRect(row, col);
8941 m_gridWin->Refresh(true, &rect);
8942 }
8943 }
8944
8945 void wxGrid::EnableGridLines( bool enable )
8946 {
8947 if ( enable != m_gridLinesEnabled )
8948 {
8949 m_gridLinesEnabled = enable;
8950
8951 if ( !GetBatchCount() )
8952 {
8953 if ( enable )
8954 {
8955 wxClientDC dc( m_gridWin );
8956 PrepareDC( dc );
8957 DrawAllGridLines( dc, wxRegion() );
8958 }
8959 else
8960 {
8961 m_gridWin->Refresh();
8962 }
8963 }
8964 }
8965 }
8966
8967
8968 int wxGrid::GetDefaultRowSize()
8969 {
8970 return m_defaultRowHeight;
8971 }
8972
8973 int wxGrid::GetRowSize( int row )
8974 {
8975 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
8976
8977 return GetRowHeight(row);
8978 }
8979
8980 int wxGrid::GetDefaultColSize()
8981 {
8982 return m_defaultColWidth;
8983 }
8984
8985 int wxGrid::GetColSize( int col )
8986 {
8987 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
8988
8989 return GetColWidth(col);
8990 }
8991
8992 // ============================================================================
8993 // access to the grid attributes: each of them has a default value in the grid
8994 // itself and may be overidden on a per-cell basis
8995 // ============================================================================
8996
8997 // ----------------------------------------------------------------------------
8998 // setting default attributes
8999 // ----------------------------------------------------------------------------
9000
9001 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9002 {
9003 m_defaultCellAttr->SetBackgroundColour(col);
9004 #ifdef __WXGTK__
9005 m_gridWin->SetBackgroundColour(col);
9006 #endif
9007 }
9008
9009 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9010 {
9011 m_defaultCellAttr->SetTextColour(col);
9012 }
9013
9014 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9015 {
9016 m_defaultCellAttr->SetAlignment(horiz, vert);
9017 }
9018
9019 void wxGrid::SetDefaultCellOverflow( bool allow )
9020 {
9021 m_defaultCellAttr->SetOverflow(allow);
9022 }
9023
9024 void wxGrid::SetDefaultCellFont( const wxFont& font )
9025 {
9026 m_defaultCellAttr->SetFont(font);
9027 }
9028
9029
9030 // For editors and renderers the type registry takes precedence over the
9031 // default attr, so we need to register the new editor/renderer for the string
9032 // data type in order to make setting a default editor/renderer appear to
9033 // work correctly.
9034
9035 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9036 {
9037 RegisterDataType(wxGRID_VALUE_STRING,
9038 renderer,
9039 GetDefaultEditorForType(wxGRID_VALUE_STRING));
9040 }
9041
9042 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9043 {
9044 RegisterDataType(wxGRID_VALUE_STRING,
9045 GetDefaultRendererForType(wxGRID_VALUE_STRING),
9046 editor);
9047 }
9048
9049 // ----------------------------------------------------------------------------
9050 // access to the default attrbiutes
9051 // ----------------------------------------------------------------------------
9052
9053 wxColour wxGrid::GetDefaultCellBackgroundColour()
9054 {
9055 return m_defaultCellAttr->GetBackgroundColour();
9056 }
9057
9058 wxColour wxGrid::GetDefaultCellTextColour()
9059 {
9060 return m_defaultCellAttr->GetTextColour();
9061 }
9062
9063 wxFont wxGrid::GetDefaultCellFont()
9064 {
9065 return m_defaultCellAttr->GetFont();
9066 }
9067
9068 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
9069 {
9070 m_defaultCellAttr->GetAlignment(horiz, vert);
9071 }
9072
9073 bool wxGrid::GetDefaultCellOverflow()
9074 {
9075 return m_defaultCellAttr->GetOverflow();
9076 }
9077
9078 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9079 {
9080 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
9081 }
9082
9083 wxGridCellEditor *wxGrid::GetDefaultEditor() const
9084 {
9085 return m_defaultCellAttr->GetEditor(NULL,0,0);
9086 }
9087
9088 // ----------------------------------------------------------------------------
9089 // access to cell attributes
9090 // ----------------------------------------------------------------------------
9091
9092 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
9093 {
9094 wxGridCellAttr *attr = GetCellAttr(row, col);
9095 wxColour colour = attr->GetBackgroundColour();
9096 attr->DecRef();
9097 return colour;
9098 }
9099
9100 wxColour wxGrid::GetCellTextColour( int row, int col )
9101 {
9102 wxGridCellAttr *attr = GetCellAttr(row, col);
9103 wxColour colour = attr->GetTextColour();
9104 attr->DecRef();
9105 return colour;
9106 }
9107
9108 wxFont wxGrid::GetCellFont( int row, int col )
9109 {
9110 wxGridCellAttr *attr = GetCellAttr(row, col);
9111 wxFont font = attr->GetFont();
9112 attr->DecRef();
9113 return font;
9114 }
9115
9116 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
9117 {
9118 wxGridCellAttr *attr = GetCellAttr(row, col);
9119 attr->GetAlignment(horiz, vert);
9120 attr->DecRef();
9121 }
9122
9123 bool wxGrid::GetCellOverflow( int row, int col )
9124 {
9125 wxGridCellAttr *attr = GetCellAttr(row, col);
9126 bool allow = attr->GetOverflow();
9127 attr->DecRef();
9128 return allow;
9129 }
9130
9131 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
9132 {
9133 wxGridCellAttr *attr = GetCellAttr(row, col);
9134 attr->GetSize( num_rows, num_cols );
9135 attr->DecRef();
9136 }
9137
9138 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
9139 {
9140 wxGridCellAttr* attr = GetCellAttr(row, col);
9141 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9142 attr->DecRef();
9143
9144 return renderer;
9145 }
9146
9147 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
9148 {
9149 wxGridCellAttr* attr = GetCellAttr(row, col);
9150 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9151 attr->DecRef();
9152
9153 return editor;
9154 }
9155
9156 bool wxGrid::IsReadOnly(int row, int col) const
9157 {
9158 wxGridCellAttr* attr = GetCellAttr(row, col);
9159 bool isReadOnly = attr->IsReadOnly();
9160 attr->DecRef();
9161 return isReadOnly;
9162 }
9163
9164 // ----------------------------------------------------------------------------
9165 // attribute support: cache, automatic provider creation, ...
9166 // ----------------------------------------------------------------------------
9167
9168 bool wxGrid::CanHaveAttributes()
9169 {
9170 if ( !m_table )
9171 {
9172 return false;
9173 }
9174
9175 return m_table->CanHaveAttributes();
9176 }
9177
9178 void wxGrid::ClearAttrCache()
9179 {
9180 if ( m_attrCache.row != -1 )
9181 {
9182 wxSafeDecRef(m_attrCache.attr);
9183 m_attrCache.attr = NULL;
9184 m_attrCache.row = -1;
9185 }
9186 }
9187
9188 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9189 {
9190 if ( attr != NULL )
9191 {
9192 wxGrid *self = (wxGrid *)this; // const_cast
9193
9194 self->ClearAttrCache();
9195 self->m_attrCache.row = row;
9196 self->m_attrCache.col = col;
9197 self->m_attrCache.attr = attr;
9198 wxSafeIncRef(attr);
9199 }
9200 }
9201
9202 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9203 {
9204 if ( row == m_attrCache.row && col == m_attrCache.col )
9205 {
9206 *attr = m_attrCache.attr;
9207 wxSafeIncRef(m_attrCache.attr);
9208
9209 #ifdef DEBUG_ATTR_CACHE
9210 gs_nAttrCacheHits++;
9211 #endif
9212
9213 return true;
9214 }
9215 else
9216 {
9217 #ifdef DEBUG_ATTR_CACHE
9218 gs_nAttrCacheMisses++;
9219 #endif
9220 return false;
9221 }
9222 }
9223
9224 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9225 {
9226 wxGridCellAttr *attr = NULL;
9227 // Additional test to avoid looking at the cache e.g. for
9228 // wxNoCellCoords, as this will confuse memory management.
9229 if ( row >= 0 )
9230 {
9231 if ( !LookupAttr(row, col, &attr) )
9232 {
9233 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
9234 : (wxGridCellAttr *)NULL;
9235 CacheAttr(row, col, attr);
9236 }
9237 }
9238 if (attr)
9239 {
9240 attr->SetDefAttr(m_defaultCellAttr);
9241 }
9242 else
9243 {
9244 attr = m_defaultCellAttr;
9245 attr->IncRef();
9246 }
9247
9248 return attr;
9249 }
9250
9251 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9252 {
9253 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
9254 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
9255
9256 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9257 wxCHECK_MSG( m_table, attr, _T("must have a table") );
9258
9259 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9260 if ( !attr )
9261 {
9262 attr = new wxGridCellAttr(m_defaultCellAttr);
9263
9264 // artificially inc the ref count to match DecRef() in caller
9265 attr->IncRef();
9266 m_table->SetAttr(attr, row, col);
9267 }
9268
9269 return attr;
9270 }
9271
9272 // ----------------------------------------------------------------------------
9273 // setting column attributes (wrappers around SetColAttr)
9274 // ----------------------------------------------------------------------------
9275
9276 void wxGrid::SetColFormatBool(int col)
9277 {
9278 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9279 }
9280
9281 void wxGrid::SetColFormatNumber(int col)
9282 {
9283 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9284 }
9285
9286 void wxGrid::SetColFormatFloat(int col, int width, int precision)
9287 {
9288 wxString typeName = wxGRID_VALUE_FLOAT;
9289 if ( (width != -1) || (precision != -1) )
9290 {
9291 typeName << _T(':') << width << _T(',') << precision;
9292 }
9293
9294 SetColFormatCustom(col, typeName);
9295 }
9296
9297 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9298 {
9299 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
9300 if(!attr)
9301 attr = new wxGridCellAttr;
9302 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9303 attr->SetRenderer(renderer);
9304
9305 SetColAttr(col, attr);
9306
9307 }
9308
9309 // ----------------------------------------------------------------------------
9310 // setting cell attributes: this is forwarded to the table
9311 // ----------------------------------------------------------------------------
9312
9313 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9314 {
9315 if ( CanHaveAttributes() )
9316 {
9317 m_table->SetAttr(attr, row, col);
9318 ClearAttrCache();
9319 }
9320 else
9321 {
9322 wxSafeDecRef(attr);
9323 }
9324 }
9325
9326 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9327 {
9328 if ( CanHaveAttributes() )
9329 {
9330 m_table->SetRowAttr(attr, row);
9331 ClearAttrCache();
9332 }
9333 else
9334 {
9335 wxSafeDecRef(attr);
9336 }
9337 }
9338
9339 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
9340 {
9341 if ( CanHaveAttributes() )
9342 {
9343 m_table->SetColAttr(attr, col);
9344 ClearAttrCache();
9345 }
9346 else
9347 {
9348 wxSafeDecRef(attr);
9349 }
9350 }
9351
9352 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
9353 {
9354 if ( CanHaveAttributes() )
9355 {
9356 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9357 attr->SetBackgroundColour(colour);
9358 attr->DecRef();
9359 }
9360 }
9361
9362 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
9363 {
9364 if ( CanHaveAttributes() )
9365 {
9366 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9367 attr->SetTextColour(colour);
9368 attr->DecRef();
9369 }
9370 }
9371
9372 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
9373 {
9374 if ( CanHaveAttributes() )
9375 {
9376 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9377 attr->SetFont(font);
9378 attr->DecRef();
9379 }
9380 }
9381
9382 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
9383 {
9384 if ( CanHaveAttributes() )
9385 {
9386 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9387 attr->SetAlignment(horiz, vert);
9388 attr->DecRef();
9389 }
9390 }
9391
9392 void wxGrid::SetCellOverflow( int row, int col, bool allow )
9393 {
9394 if ( CanHaveAttributes() )
9395 {
9396 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9397 attr->SetOverflow(allow);
9398 attr->DecRef();
9399 }
9400 }
9401
9402 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
9403 {
9404 if ( CanHaveAttributes() )
9405 {
9406 int cell_rows, cell_cols;
9407
9408 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9409 attr->GetSize(&cell_rows, &cell_cols);
9410 attr->SetSize(num_rows, num_cols);
9411 attr->DecRef();
9412
9413 // Cannot set the size of a cell to 0 or negative values
9414 // While it is perfectly legal to do that, this function cannot
9415 // handle all the possibilies, do it by hand by getting the CellAttr.
9416 // You can only set the size of a cell to 1,1 or greater with this fn
9417 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
9418 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9419 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
9420 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9421
9422 // if this was already a multicell then "turn off" the other cells first
9423 if ((cell_rows > 1) || (cell_rows > 1))
9424 {
9425 int i, j;
9426 for (j=row; j<row+cell_rows; j++)
9427 {
9428 for (i=col; i<col+cell_cols; i++)
9429 {
9430 if ((i != col) || (j != row))
9431 {
9432 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9433 attr_stub->SetSize( 1, 1 );
9434 attr_stub->DecRef();
9435 }
9436 }
9437 }
9438 }
9439
9440 // mark the cells that will be covered by this cell to
9441 // negative or zero values to point back at this cell
9442 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
9443 {
9444 int i, j;
9445 for (j=row; j<row+num_rows; j++)
9446 {
9447 for (i=col; i<col+num_cols; i++)
9448 {
9449 if ((i != col) || (j != row))
9450 {
9451 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9452 attr_stub->SetSize( row-j, col-i );
9453 attr_stub->DecRef();
9454 }
9455 }
9456 }
9457 }
9458 }
9459 }
9460
9461 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
9462 {
9463 if ( CanHaveAttributes() )
9464 {
9465 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9466 attr->SetRenderer(renderer);
9467 attr->DecRef();
9468 }
9469 }
9470
9471 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
9472 {
9473 if ( CanHaveAttributes() )
9474 {
9475 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9476 attr->SetEditor(editor);
9477 attr->DecRef();
9478 }
9479 }
9480
9481 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
9482 {
9483 if ( CanHaveAttributes() )
9484 {
9485 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9486 attr->SetReadOnly(isReadOnly);
9487 attr->DecRef();
9488 }
9489 }
9490
9491 // ----------------------------------------------------------------------------
9492 // Data type registration
9493 // ----------------------------------------------------------------------------
9494
9495 void wxGrid::RegisterDataType(const wxString& typeName,
9496 wxGridCellRenderer* renderer,
9497 wxGridCellEditor* editor)
9498 {
9499 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
9500 }
9501
9502
9503 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
9504 {
9505 wxString typeName = m_table->GetTypeName(row, col);
9506 return GetDefaultEditorForType(typeName);
9507 }
9508
9509 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
9510 {
9511 wxString typeName = m_table->GetTypeName(row, col);
9512 return GetDefaultRendererForType(typeName);
9513 }
9514
9515 wxGridCellEditor*
9516 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
9517 {
9518 int index = m_typeRegistry->FindOrCloneDataType(typeName);
9519 if ( index == wxNOT_FOUND )
9520 {
9521 wxFAIL_MSG(wxT("Unknown data type name"));
9522
9523 return NULL;
9524 }
9525
9526 return m_typeRegistry->GetEditor(index);
9527 }
9528
9529 wxGridCellRenderer*
9530 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
9531 {
9532 int index = m_typeRegistry->FindOrCloneDataType(typeName);
9533 if ( index == wxNOT_FOUND )
9534 {
9535 wxFAIL_MSG(wxT("Unknown data type name"));
9536
9537 return NULL;
9538 }
9539
9540 return m_typeRegistry->GetRenderer(index);
9541 }
9542
9543
9544 // ----------------------------------------------------------------------------
9545 // row/col size
9546 // ----------------------------------------------------------------------------
9547
9548 void wxGrid::EnableDragRowSize( bool enable )
9549 {
9550 m_canDragRowSize = enable;
9551 }
9552
9553
9554 void wxGrid::EnableDragColSize( bool enable )
9555 {
9556 m_canDragColSize = enable;
9557 }
9558
9559 void wxGrid::EnableDragGridSize( bool enable )
9560 {
9561 m_canDragGridSize = enable;
9562 }
9563
9564 void wxGrid::EnableDragCell( bool enable )
9565 {
9566 m_canDragCell = enable;
9567 }
9568
9569 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
9570 {
9571 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
9572
9573 if ( resizeExistingRows )
9574 {
9575 // since we are resizing all rows to the default row size,
9576 // we can simply clear the row heights and row bottoms
9577 // arrays (which also allows us to take advantage of
9578 // some speed optimisations)
9579 m_rowHeights.Empty();
9580 m_rowBottoms.Empty();
9581 if ( !GetBatchCount() )
9582 CalcDimensions();
9583 }
9584 }
9585
9586 void wxGrid::SetRowSize( int row, int height )
9587 {
9588 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
9589
9590 // See comment in SetColSize
9591 if ( height < GetRowMinimalAcceptableHeight()) { return; }
9592
9593 if ( m_rowHeights.IsEmpty() )
9594 {
9595 // need to really create the array
9596 InitRowHeights();
9597 }
9598
9599 int h = wxMax( 0, height );
9600 int diff = h - m_rowHeights[row];
9601
9602 m_rowHeights[row] = h;
9603 int i;
9604 for ( i = row; i < m_numRows; i++ )
9605 {
9606 m_rowBottoms[i] += diff;
9607 }
9608 if ( !GetBatchCount() )
9609 CalcDimensions();
9610 }
9611
9612 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
9613 {
9614 m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
9615
9616 if ( resizeExistingCols )
9617 {
9618 // since we are resizing all columns to the default column size,
9619 // we can simply clear the col widths and col rights
9620 // arrays (which also allows us to take advantage of
9621 // some speed optimisations)
9622 m_colWidths.Empty();
9623 m_colRights.Empty();
9624 if ( !GetBatchCount() )
9625 CalcDimensions();
9626 }
9627 }
9628
9629 void wxGrid::SetColSize( int col, int width )
9630 {
9631 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
9632
9633 // should we check that it's bigger than GetColMinimalWidth(col) here?
9634 // (VZ)
9635 // No, because it is reasonable to assume the library user know's
9636 // what he is doing. However whe should test against the weaker
9637 // constariant of minimalAcceptableWidth, as this breaks rendering
9638 //
9639 // This test then fixes sf.net bug #645734
9640
9641 if ( width < GetColMinimalAcceptableWidth()) { return; }
9642
9643 if ( m_colWidths.IsEmpty() )
9644 {
9645 // need to really create the array
9646 InitColWidths();
9647 }
9648
9649 // if < 0 calc new width from label
9650 if( width < 0 )
9651 {
9652 long w, h;
9653 wxArrayString lines;
9654 wxClientDC dc(m_colLabelWin);
9655 dc.SetFont(GetLabelFont());
9656 StringToLines(GetColLabelValue(col), lines);
9657 GetTextBoxSize(dc, lines, &w, &h);
9658 width = w + 6;
9659 }
9660 int w = wxMax( 0, width );
9661 int diff = w - m_colWidths[col];
9662 m_colWidths[col] = w;
9663
9664 int i;
9665 for ( i = col; i < m_numCols; i++ )
9666 {
9667 m_colRights[i] += diff;
9668 }
9669 if ( !GetBatchCount() )
9670 CalcDimensions();
9671 }
9672
9673
9674 void wxGrid::SetColMinimalWidth( int col, int width )
9675 {
9676 if (width > GetColMinimalAcceptableWidth()) {
9677 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
9678 m_colMinWidths[key] = width;
9679 }
9680 }
9681
9682 void wxGrid::SetRowMinimalHeight( int row, int width )
9683 {
9684 if (width > GetRowMinimalAcceptableHeight()) {
9685 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
9686 m_rowMinHeights[key] = width;
9687 }
9688 }
9689
9690 int wxGrid::GetColMinimalWidth(int col) const
9691 {
9692 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
9693 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
9694 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
9695 }
9696
9697 int wxGrid::GetRowMinimalHeight(int row) const
9698 {
9699 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
9700 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
9701 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
9702 }
9703
9704 void wxGrid::SetColMinimalAcceptableWidth( int width )
9705 {
9706 // We do allow a width of 0 since this gives us
9707 // an easy way to temporarily hidding columns.
9708 if ( width<0 )
9709 return;
9710 m_minAcceptableColWidth = width;
9711 }
9712
9713 void wxGrid::SetRowMinimalAcceptableHeight( int height )
9714 {
9715 // We do allow a height of 0 since this gives us
9716 // an easy way to temporarily hidding rows.
9717 if ( height<0 )
9718 return;
9719 m_minAcceptableRowHeight = height;
9720 };
9721
9722 int wxGrid::GetColMinimalAcceptableWidth() const
9723 {
9724 return m_minAcceptableColWidth;
9725 }
9726
9727 int wxGrid::GetRowMinimalAcceptableHeight() const
9728 {
9729 return m_minAcceptableRowHeight;
9730 }
9731
9732 // ----------------------------------------------------------------------------
9733 // auto sizing
9734 // ----------------------------------------------------------------------------
9735
9736 void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
9737 {
9738 wxClientDC dc(m_gridWin);
9739
9740 //Cancel editting of cell
9741 HideCellEditControl();
9742 SaveEditControlValue();
9743
9744 // init both of them to avoid compiler warnings, even if weo nly need one
9745 int row = -1,
9746 col = -1;
9747 if ( column )
9748 col = colOrRow;
9749 else
9750 row = colOrRow;
9751
9752 wxCoord extent, extentMax = 0;
9753 int max = column ? m_numRows : m_numCols;
9754 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
9755 {
9756 if ( column )
9757 row = rowOrCol;
9758 else
9759 col = rowOrCol;
9760
9761 wxGridCellAttr* attr = GetCellAttr(row, col);
9762 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9763 if ( renderer )
9764 {
9765 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
9766 extent = column ? size.x : size.y;
9767 if ( extent > extentMax )
9768 {
9769 extentMax = extent;
9770 }
9771
9772 renderer->DecRef();
9773 }
9774
9775 attr->DecRef();
9776 }
9777
9778 // now also compare with the column label extent
9779 wxCoord w, h;
9780 dc.SetFont( GetLabelFont() );
9781
9782 if ( column )
9783 {
9784 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
9785 if( GetColLabelTextOrientation() == wxVERTICAL )
9786 w = h;
9787 }
9788 else
9789 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
9790
9791 extent = column ? w : h;
9792 if ( extent > extentMax )
9793 {
9794 extentMax = extent;
9795 }
9796
9797 if ( !extentMax )
9798 {
9799 // empty column - give default extent (notice that if extentMax is less
9800 // than default extent but != 0, it's ok)
9801 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
9802 }
9803 else
9804 {
9805 if ( column )
9806 {
9807 // leave some space around text
9808 extentMax += 10;
9809 }
9810 else
9811 {
9812 extentMax += 6;
9813 }
9814 }
9815
9816 if ( column )
9817 {
9818 SetColSize(col, extentMax);
9819 if ( !GetBatchCount() )
9820 {
9821 int cw, ch, dummy;
9822 m_gridWin->GetClientSize( &cw, &ch );
9823 wxRect rect ( CellToRect( 0, col ) );
9824 rect.y = 0;
9825 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
9826 rect.width = cw - rect.x;
9827 rect.height = m_colLabelHeight;
9828 m_colLabelWin->Refresh( true, &rect );
9829 }
9830 }
9831 else
9832 {
9833 SetRowSize(row, extentMax);
9834 if ( !GetBatchCount() )
9835 {
9836 int cw, ch, dummy;
9837 m_gridWin->GetClientSize( &cw, &ch );
9838 wxRect rect ( CellToRect( row, 0 ) );
9839 rect.x = 0;
9840 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9841 rect.width = m_rowLabelWidth;
9842 rect.height = ch - rect.y;
9843 m_rowLabelWin->Refresh( true, &rect );
9844 }
9845 }
9846 if ( setAsMin )
9847 {
9848 if ( column )
9849 SetColMinimalWidth(col, extentMax);
9850 else
9851 SetRowMinimalHeight(row, extentMax);
9852 }
9853 }
9854
9855 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
9856 {
9857 int width = m_rowLabelWidth;
9858
9859 if ( !calcOnly )
9860 BeginBatch();
9861
9862 for ( int col = 0; col < m_numCols; col++ )
9863 {
9864 if ( !calcOnly )
9865 {
9866 AutoSizeColumn(col, setAsMin);
9867 }
9868
9869 width += GetColWidth(col);
9870 }
9871
9872 if ( !calcOnly )
9873 EndBatch();
9874
9875 return width;
9876 }
9877
9878 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
9879 {
9880 int height = m_colLabelHeight;
9881
9882 if ( !calcOnly )
9883 BeginBatch();
9884
9885 for ( int row = 0; row < m_numRows; row++ )
9886 {
9887 if ( !calcOnly )
9888 {
9889 AutoSizeRow(row, setAsMin);
9890 }
9891
9892 height += GetRowHeight(row);
9893 }
9894
9895 if ( !calcOnly )
9896 EndBatch();
9897
9898 return height;
9899 }
9900
9901 void wxGrid::AutoSize()
9902 {
9903 BeginBatch();
9904
9905 wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9906
9907 // round up the size to a multiple of scroll step - this ensures that we
9908 // won't get the scrollbars if we're sized exactly to this width
9909 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9910 // scrollbar steps
9911 wxSize sizeFit(GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX,
9912 GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY);
9913
9914 // distribute the extra space between the columns/rows to avoid having
9915 // extra white space
9916
9917 // Remove the extra m_extraWidth + 1 added above
9918 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
9919 if ( diff && m_numCols )
9920 {
9921 // try to resize the columns uniformly
9922 wxCoord diffPerCol = diff / m_numCols;
9923 if ( diffPerCol )
9924 {
9925 for ( int col = 0; col < m_numCols; col++ )
9926 {
9927 SetColSize(col, GetColWidth(col) + diffPerCol);
9928 }
9929 }
9930
9931 // add remaining amount to the last columns
9932 diff -= diffPerCol * m_numCols;
9933 if ( diff )
9934 {
9935 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
9936 {
9937 SetColSize(col, GetColWidth(col) + 1);
9938 }
9939 }
9940 }
9941
9942 // same for rows
9943 diff = sizeFit.y - size.y - (m_extraHeight + 1);
9944 if ( diff && m_numRows )
9945 {
9946 // try to resize the columns uniformly
9947 wxCoord diffPerRow = diff / m_numRows;
9948 if ( diffPerRow )
9949 {
9950 for ( int row = 0; row < m_numRows; row++ )
9951 {
9952 SetRowSize(row, GetRowHeight(row) + diffPerRow);
9953 }
9954 }
9955
9956 // add remaining amount to the last rows
9957 diff -= diffPerRow * m_numRows;
9958 if ( diff )
9959 {
9960 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
9961 {
9962 SetRowSize(row, GetRowHeight(row) + 1);
9963 }
9964 }
9965 }
9966
9967 EndBatch();
9968
9969 SetClientSize(sizeFit);
9970 }
9971
9972 void wxGrid::AutoSizeRowLabelSize( int row )
9973 {
9974 wxArrayString lines;
9975 long w, h;
9976
9977 // Hide the edit control, so it
9978 // won't interfer with drag-shrinking.
9979 if( IsCellEditControlShown() )
9980 {
9981 HideCellEditControl();
9982 SaveEditControlValue();
9983 }
9984
9985 // autosize row height depending on label text
9986 StringToLines( GetRowLabelValue( row ), lines );
9987 wxClientDC dc( m_rowLabelWin );
9988 GetTextBoxSize( dc, lines, &w, &h);
9989 if( h < m_defaultRowHeight )
9990 h = m_defaultRowHeight;
9991 SetRowSize(row, h);
9992 ForceRefresh();
9993 }
9994
9995 void wxGrid::AutoSizeColLabelSize( int col )
9996 {
9997 wxArrayString lines;
9998 long w, h;
9999
10000 // Hide the edit control, so it
10001 // won't interfer with drag-shrinking.
10002 if( IsCellEditControlShown() )
10003 {
10004 HideCellEditControl();
10005 SaveEditControlValue();
10006 }
10007
10008 // autosize column width depending on label text
10009 StringToLines( GetColLabelValue( col ), lines );
10010 wxClientDC dc( m_colLabelWin );
10011 if( GetColLabelTextOrientation() == wxHORIZONTAL )
10012 GetTextBoxSize( dc, lines, &w, &h);
10013 else
10014 GetTextBoxSize( dc, lines, &h, &w);
10015 if( w < m_defaultColWidth )
10016 w = m_defaultColWidth;
10017 SetColSize(col, w);
10018 ForceRefresh();
10019 }
10020
10021 wxSize wxGrid::DoGetBestSize() const
10022 {
10023 // don't set sizes, only calculate them
10024 wxGrid *self = (wxGrid *)this; // const_cast
10025
10026 int width, height;
10027 width = self->SetOrCalcColumnSizes(true);
10028 height = self->SetOrCalcRowSizes(true);
10029
10030 if (!width) width=100;
10031 if (!height) height=80;
10032
10033 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10034 // of the scrollbars, is there any magic incantaion for that?
10035 int xpu, ypu;
10036 GetScrollPixelsPerUnit(&xpu, &ypu);
10037 if (xpu)
10038 width += 1 + xpu - (width % xpu);
10039 if (ypu)
10040 height += 1 + ypu - (height % ypu);
10041
10042 // limit to 1/4 of the screen size
10043 int maxwidth, maxheight;
10044 wxDisplaySize( & maxwidth, & maxheight );
10045 maxwidth /= 2;
10046 maxheight /= 2;
10047 if ( width > maxwidth ) width = maxwidth;
10048 if ( height > maxheight ) height = maxheight;
10049
10050
10051 wxSize best(width, height);
10052 // NOTE: This size should be cached, but first we need to add calls to
10053 // InvalidateBestSize everywhere that could change the results of this
10054 // calculation.
10055 // CacheBestSize(size);
10056 return best;
10057 }
10058
10059 void wxGrid::Fit()
10060 {
10061 AutoSize();
10062 }
10063
10064
10065 wxPen& wxGrid::GetDividerPen() const
10066 {
10067 return wxNullPen;
10068 }
10069
10070 // ----------------------------------------------------------------------------
10071 // cell value accessor functions
10072 // ----------------------------------------------------------------------------
10073
10074 void wxGrid::SetCellValue( int row, int col, const wxString& s )
10075 {
10076 if ( m_table )
10077 {
10078 m_table->SetValue( row, col, s );
10079 if ( !GetBatchCount() )
10080 {
10081 int dummy;
10082 wxRect rect( CellToRect( row, col ) );
10083 rect.x = 0;
10084 rect.width = m_gridWin->GetClientSize().GetWidth();
10085 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10086 m_gridWin->Refresh( false, &rect );
10087 }
10088
10089 if ( m_currentCellCoords.GetRow() == row &&
10090 m_currentCellCoords.GetCol() == col &&
10091 IsCellEditControlShown())
10092 // Note: If we are using IsCellEditControlEnabled,
10093 // this interacts badly with calling SetCellValue from
10094 // an EVT_GRID_CELL_CHANGE handler.
10095 {
10096 HideCellEditControl();
10097 ShowCellEditControl(); // will reread data from table
10098 }
10099 }
10100 }
10101
10102
10103 //
10104 // ------ Block, row and col selection
10105 //
10106
10107 void wxGrid::SelectRow( int row, bool addToSelected )
10108 {
10109 if ( IsSelection() && !addToSelected )
10110 ClearSelection();
10111
10112 if ( m_selection )
10113 m_selection->SelectRow( row, false, addToSelected );
10114 }
10115
10116
10117 void wxGrid::SelectCol( int col, bool addToSelected )
10118 {
10119 if ( IsSelection() && !addToSelected )
10120 ClearSelection();
10121
10122 if ( m_selection )
10123 m_selection->SelectCol( col, false, addToSelected );
10124 }
10125
10126
10127 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
10128 bool addToSelected )
10129 {
10130 if ( IsSelection() && !addToSelected )
10131 ClearSelection();
10132
10133 if ( m_selection )
10134 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
10135 false, addToSelected );
10136 }
10137
10138
10139 void wxGrid::SelectAll()
10140 {
10141 if ( m_numRows > 0 && m_numCols > 0 )
10142 {
10143 if ( m_selection )
10144 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
10145 }
10146 }
10147
10148 //
10149 // ------ Cell, row and col deselection
10150 //
10151
10152 void wxGrid::DeselectRow( int row )
10153 {
10154 if ( !m_selection )
10155 return;
10156
10157 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10158 {
10159 if ( m_selection->IsInSelection(row, 0 ) )
10160 m_selection->ToggleCellSelection( row, 0);
10161 }
10162 else
10163 {
10164 int nCols = GetNumberCols();
10165 for ( int i = 0; i < nCols ; i++ )
10166 {
10167 if ( m_selection->IsInSelection(row, i ) )
10168 m_selection->ToggleCellSelection( row, i);
10169 }
10170 }
10171 }
10172
10173 void wxGrid::DeselectCol( int col )
10174 {
10175 if ( !m_selection )
10176 return;
10177
10178 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10179 {
10180 if ( m_selection->IsInSelection(0, col ) )
10181 m_selection->ToggleCellSelection( 0, col);
10182 }
10183 else
10184 {
10185 int nRows = GetNumberRows();
10186 for ( int i = 0; i < nRows ; i++ )
10187 {
10188 if ( m_selection->IsInSelection(i, col ) )
10189 m_selection->ToggleCellSelection(i, col);
10190 }
10191 }
10192 }
10193
10194 void wxGrid::DeselectCell( int row, int col )
10195 {
10196 if ( m_selection && m_selection->IsInSelection(row, col) )
10197 m_selection->ToggleCellSelection(row, col);
10198 }
10199
10200 bool wxGrid::IsSelection()
10201 {
10202 return ( m_selection && (m_selection->IsSelection() ||
10203 ( m_selectingTopLeft != wxGridNoCellCoords &&
10204 m_selectingBottomRight != wxGridNoCellCoords) ) );
10205 }
10206
10207 bool wxGrid::IsInSelection( int row, int col ) const
10208 {
10209 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
10210 ( row >= m_selectingTopLeft.GetRow() &&
10211 col >= m_selectingTopLeft.GetCol() &&
10212 row <= m_selectingBottomRight.GetRow() &&
10213 col <= m_selectingBottomRight.GetCol() )) );
10214 }
10215
10216 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10217 {
10218 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10219 return m_selection->m_cellSelection;
10220 }
10221 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10222 {
10223 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10224 return m_selection->m_blockSelectionTopLeft;
10225 }
10226 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10227 {
10228 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10229 return m_selection->m_blockSelectionBottomRight;
10230 }
10231 wxArrayInt wxGrid::GetSelectedRows() const
10232 {
10233 if (!m_selection) { wxArrayInt a; return a; }
10234 return m_selection->m_rowSelection;
10235 }
10236 wxArrayInt wxGrid::GetSelectedCols() const
10237 {
10238 if (!m_selection) { wxArrayInt a; return a; }
10239 return m_selection->m_colSelection;
10240 }
10241
10242
10243 void wxGrid::ClearSelection()
10244 {
10245 m_selectingTopLeft = wxGridNoCellCoords;
10246 m_selectingBottomRight = wxGridNoCellCoords;
10247 if ( m_selection )
10248 m_selection->ClearSelection();
10249 }
10250
10251
10252 // This function returns the rectangle that encloses the given block
10253 // in device coords clipped to the client size of the grid window.
10254 //
10255 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
10256 const wxGridCellCoords &bottomRight )
10257 {
10258 wxRect rect( wxGridNoCellRect );
10259 wxRect cellRect;
10260
10261 cellRect = CellToRect( topLeft );
10262 if ( cellRect != wxGridNoCellRect )
10263 {
10264 rect = cellRect;
10265 }
10266 else
10267 {
10268 rect = wxRect(0,0,0,0);
10269 }
10270
10271 cellRect = CellToRect( bottomRight );
10272 if ( cellRect != wxGridNoCellRect )
10273 {
10274 rect += cellRect;
10275 }
10276 else
10277 {
10278 return wxGridNoCellRect;
10279 }
10280
10281 int i, j;
10282 int left = rect.GetLeft();
10283 int top = rect.GetTop();
10284 int right = rect.GetRight();
10285 int bottom = rect.GetBottom();
10286
10287 int leftCol = topLeft.GetCol();
10288 int topRow = topLeft.GetRow();
10289 int rightCol = bottomRight.GetCol();
10290 int bottomRow = bottomRight.GetRow();
10291
10292 if (left > right)
10293 {
10294 i = left;
10295 left = right;
10296 right = i;
10297 i = leftCol;
10298 leftCol=rightCol;
10299 rightCol = i;
10300 }
10301
10302 if (top > bottom)
10303 {
10304 i = top;
10305 top = bottom;
10306 bottom = i;
10307 i = topRow;
10308 topRow = bottomRow;
10309 bottomRow = i;
10310 }
10311
10312
10313 for ( j = topRow; j <= bottomRow; j++ )
10314 {
10315 for ( i = leftCol; i <= rightCol; i++ )
10316 {
10317 if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol))
10318 {
10319 cellRect = CellToRect( j, i );
10320
10321 if (cellRect.x < left)
10322 left = cellRect.x;
10323 if (cellRect.y < top)
10324 top = cellRect.y;
10325 if (cellRect.x + cellRect.width > right)
10326 right = cellRect.x + cellRect.width;
10327 if (cellRect.y + cellRect.height > bottom)
10328 bottom = cellRect.y + cellRect.height;
10329 }
10330 else i = rightCol; // jump over inner cells.
10331 }
10332 }
10333
10334 // convert to scrolled coords
10335 //
10336 CalcScrolledPosition( left, top, &left, &top );
10337 CalcScrolledPosition( right, bottom, &right, &bottom );
10338
10339 int cw, ch;
10340 m_gridWin->GetClientSize( &cw, &ch );
10341
10342 if (right < 0 || bottom < 0 || left > cw || top > ch)
10343 return wxRect(0,0,0,0);
10344
10345 rect.SetLeft( wxMax(0, left) );
10346 rect.SetTop( wxMax(0, top) );
10347 rect.SetRight( wxMin(cw, right) );
10348 rect.SetBottom( wxMin(ch, bottom) );
10349
10350 return rect;
10351 }
10352
10353 //
10354 // ------ Grid event classes
10355 //
10356
10357 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
10358
10359 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
10360 int row, int col, int x, int y, bool sel,
10361 bool control, bool shift, bool alt, bool meta )
10362 : wxNotifyEvent( type, id )
10363 {
10364 m_row = row;
10365 m_col = col;
10366 m_x = x;
10367 m_y = y;
10368 m_selecting = sel;
10369 m_control = control;
10370 m_shift = shift;
10371 m_alt = alt;
10372 m_meta = meta;
10373
10374 SetEventObject(obj);
10375 }
10376
10377
10378 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
10379
10380 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
10381 int rowOrCol, int x, int y,
10382 bool control, bool shift, bool alt, bool meta )
10383 : wxNotifyEvent( type, id )
10384 {
10385 m_rowOrCol = rowOrCol;
10386 m_x = x;
10387 m_y = y;
10388 m_control = control;
10389 m_shift = shift;
10390 m_alt = alt;
10391 m_meta = meta;
10392
10393 SetEventObject(obj);
10394 }
10395
10396
10397 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
10398
10399 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
10400 const wxGridCellCoords& topLeft,
10401 const wxGridCellCoords& bottomRight,
10402 bool sel, bool control,
10403 bool shift, bool alt, bool meta )
10404 : wxNotifyEvent( type, id )
10405 {
10406 m_topLeft = topLeft;
10407 m_bottomRight = bottomRight;
10408 m_selecting = sel;
10409 m_control = control;
10410 m_shift = shift;
10411 m_alt = alt;
10412 m_meta = meta;
10413
10414 SetEventObject(obj);
10415 }
10416
10417
10418 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
10419
10420 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
10421 wxObject* obj, int row,
10422 int col, wxControl* ctrl)
10423 : wxCommandEvent(type, id)
10424 {
10425 SetEventObject(obj);
10426 m_row = row;
10427 m_col = col;
10428 m_ctrl = ctrl;
10429 }
10430
10431 #endif // wxUSE_GRID
10432