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