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