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