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