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