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