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