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