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