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