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