]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
13233aa7e8bf7acb6c5571b69af79e5892d784be
[wxWidgets.git] / src / generic / datavgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_DATAVIEWCTRL
19
20 #include "wx/dataview.h"
21
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
23
24 #ifndef WX_PRECOMP
25 #ifdef __WXMSW__
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #endif
30 #include "wx/sizer.h"
31 #include "wx/log.h"
32 #include "wx/dcclient.h"
33 #include "wx/timer.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
37 #include "wx/frame.h"
38 #endif
39
40 #include "wx/stockitem.h"
41 #include "wx/calctrl.h"
42 #include "wx/popupwin.h"
43 #include "wx/renderer.h"
44 #include "wx/dcbuffer.h"
45 #include "wx/icon.h"
46 #include "wx/list.h"
47 #include "wx/listimpl.cpp"
48 #include "wx/imaglist.h"
49 #include "wx/headerctrl.h"
50 #include "wx/dnd.h"
51 #include "wx/stopwatch.h"
52
53 //-----------------------------------------------------------------------------
54 // classes
55 //-----------------------------------------------------------------------------
56
57 class wxDataViewColumn;
58 class wxDataViewHeaderWindow;
59 class wxDataViewCtrl;
60
61 //-----------------------------------------------------------------------------
62 // classes
63 //-----------------------------------------------------------------------------
64
65 static const int SCROLL_UNIT_X = 15;
66
67 // the cell padding on the left/right
68 static const int PADDING_RIGHTLEFT = 3;
69
70 // the expander space margin
71 static const int EXPANDER_MARGIN = 4;
72
73 #ifdef __WXMSW__
74 static const int EXPANDER_OFFSET = 4;
75 #else
76 static const int EXPANDER_OFFSET = 1;
77 #endif
78
79 // Below is the compare stuff.
80 // For the generic implementation, both the leaf nodes and the nodes are sorted for
81 // fast search when needed
82 static wxDataViewModel* g_model;
83 static int g_column = -2;
84 static bool g_asending = true;
85
86 //-----------------------------------------------------------------------------
87 // wxDataViewColumn
88 //-----------------------------------------------------------------------------
89
90 void wxDataViewColumn::Init(int width, wxAlignment align, int flags)
91 {
92 m_width = width;
93 m_minWidth = 0;
94 m_align = align;
95 m_flags = flags;
96 m_sort = false;
97 m_sortAscending = true;
98 }
99
100 int wxDataViewColumn::GetWidth() const
101 {
102 switch ( m_width )
103 {
104 case wxCOL_WIDTH_DEFAULT:
105 return wxDVC_DEFAULT_WIDTH;
106
107 case wxCOL_WIDTH_AUTOSIZE:
108 wxCHECK_MSG( m_owner, wxDVC_DEFAULT_WIDTH, "no owner control" );
109 return m_owner->GetBestColumnWidth(m_owner->GetColumnIndex(this));
110
111 default:
112 return m_width;
113 }
114 }
115
116 void wxDataViewColumn::UpdateDisplay()
117 {
118 if (m_owner)
119 {
120 int idx = m_owner->GetColumnIndex( this );
121 m_owner->OnColumnChange( idx );
122 }
123 }
124
125 //-----------------------------------------------------------------------------
126 // wxDataViewHeaderWindow
127 //-----------------------------------------------------------------------------
128
129 class wxDataViewHeaderWindow : public wxHeaderCtrl
130 {
131 public:
132 wxDataViewHeaderWindow(wxDataViewCtrl *parent)
133 : wxHeaderCtrl(parent)
134 {
135 }
136
137 wxDataViewCtrl *GetOwner() const
138 { return static_cast<wxDataViewCtrl *>(GetParent()); }
139
140 protected:
141 // implement/override wxHeaderCtrl functions by forwarding them to the main
142 // control
143 virtual const wxHeaderColumn& GetColumn(unsigned int idx) const
144 {
145 return *(GetOwner()->GetColumn(idx));
146 }
147
148 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
149 {
150 wxDataViewCtrl * const owner = GetOwner();
151
152 int widthContents = owner->GetBestColumnWidth(idx);
153 owner->GetColumn(idx)->SetWidth(wxMax(widthTitle, widthContents));
154 owner->OnColumnChange(idx);
155
156 return true;
157 }
158
159 private:
160 bool SendEvent(wxEventType type, unsigned int n)
161 {
162 wxDataViewCtrl * const owner = GetOwner();
163 wxDataViewEvent event(type, owner->GetId());
164
165 event.SetEventObject(owner);
166 event.SetColumn(n);
167 event.SetDataViewColumn(owner->GetColumn(n));
168 event.SetModel(owner->GetModel());
169
170 // for events created by wxDataViewHeaderWindow the
171 // row / value fields are not valid
172 return owner->GetEventHandler()->ProcessEvent(event);
173 }
174
175 void OnClick(wxHeaderCtrlEvent& event)
176 {
177 const unsigned idx = event.GetColumn();
178
179 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, idx) )
180 return;
181
182 // default handling for the column click is to sort by this column or
183 // toggle its sort order
184 wxDataViewCtrl * const owner = GetOwner();
185 wxDataViewColumn * const col = owner->GetColumn(idx);
186 if ( !col->IsSortable() )
187 {
188 // no default handling for non-sortable columns
189 event.Skip();
190 return;
191 }
192
193 if ( col->IsSortKey() )
194 {
195 // already using this column for sorting, just change the order
196 col->ToggleSortOrder();
197 }
198 else // not using this column for sorting yet
199 {
200 // first unset the old sort column if any
201 int oldSortKey = owner->GetSortingColumnIndex();
202 if ( oldSortKey != wxNOT_FOUND )
203 {
204 owner->GetColumn(oldSortKey)->UnsetAsSortKey();
205 owner->OnColumnChange(oldSortKey);
206 }
207
208 owner->SetSortingColumnIndex(idx);
209 col->SetAsSortKey();
210 }
211
212 wxDataViewModel * const model = owner->GetModel();
213 if ( model )
214 model->Resort();
215
216 owner->OnColumnChange(idx);
217
218 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, idx);
219 }
220
221 void OnRClick(wxHeaderCtrlEvent& event)
222 {
223 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK,
224 event.GetColumn()) )
225 event.Skip();
226 }
227
228 void OnResize(wxHeaderCtrlEvent& event)
229 {
230 wxDataViewCtrl * const owner = GetOwner();
231
232 const unsigned col = event.GetColumn();
233 owner->GetColumn(col)->SetWidth(event.GetWidth());
234 GetOwner()->OnColumnChange(col);
235 }
236
237 void OnEndReorder(wxHeaderCtrlEvent& event)
238 {
239 wxDataViewCtrl * const owner = GetOwner();
240 owner->ColumnMoved(owner->GetColumn(event.GetColumn()),
241 event.GetNewOrder());
242 }
243
244 DECLARE_EVENT_TABLE()
245 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow);
246 };
247
248 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow, wxHeaderCtrl)
249 EVT_HEADER_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnClick)
250 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnRClick)
251
252 EVT_HEADER_RESIZING(wxID_ANY, wxDataViewHeaderWindow::OnResize)
253 EVT_HEADER_END_RESIZE(wxID_ANY, wxDataViewHeaderWindow::OnResize)
254
255 EVT_HEADER_END_REORDER(wxID_ANY, wxDataViewHeaderWindow::OnEndReorder)
256 END_EVENT_TABLE()
257
258 //-----------------------------------------------------------------------------
259 // wxDataViewRenameTimer
260 //-----------------------------------------------------------------------------
261
262 class wxDataViewRenameTimer: public wxTimer
263 {
264 private:
265 wxDataViewMainWindow *m_owner;
266
267 public:
268 wxDataViewRenameTimer( wxDataViewMainWindow *owner );
269 void Notify();
270 };
271
272 //-----------------------------------------------------------------------------
273 // wxDataViewTreeNode
274 //-----------------------------------------------------------------------------
275
276 class wxDataViewTreeNode;
277 WX_DEFINE_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes );
278
279 int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1,
280 wxDataViewTreeNode ** node2);
281
282 class wxDataViewTreeNode
283 {
284 public:
285 wxDataViewTreeNode(wxDataViewTreeNode *parent, const wxDataViewItem& item)
286 : m_item(item),
287 m_parent(parent),
288 m_branchData(NULL)
289 {
290 }
291
292 ~wxDataViewTreeNode()
293 {
294 if ( m_branchData )
295 {
296 wxDataViewTreeNodes& nodes = m_branchData->children;
297 for ( wxDataViewTreeNodes::iterator i = nodes.begin();
298 i != nodes.end();
299 ++i )
300 {
301 delete *i;
302 }
303
304 delete m_branchData;
305 }
306 }
307
308 static wxDataViewTreeNode* CreateRootNode()
309 {
310 wxDataViewTreeNode *n = new wxDataViewTreeNode(NULL, wxDataViewItem());
311 n->SetHasChildren(true);
312 n->m_branchData->open = true;
313 return n;
314 }
315
316 wxDataViewTreeNode * GetParent() const { return m_parent; }
317
318 const wxDataViewTreeNodes& GetChildNodes() const
319 {
320 wxASSERT( m_branchData != NULL );
321 return m_branchData->children;
322 }
323
324 void AddChild( wxDataViewTreeNode * node )
325 {
326 if ( !m_branchData )
327 m_branchData = new BranchNodeData;
328
329 m_branchData->children.Add( node );
330 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
331 if (g_column >= -1)
332 m_branchData->children.Sort( &wxGenericTreeModelNodeCmp );
333 }
334
335 void RemoveChild(wxDataViewTreeNode *node)
336 {
337 wxCHECK_RET( m_branchData != NULL, "leaf node doesn't have children" );
338 m_branchData->children.Remove(node);
339 }
340
341 const wxDataViewItem & GetItem() const { return m_item; }
342 void SetItem( const wxDataViewItem & item ) { m_item = item; }
343
344 int GetIndentLevel() const
345 {
346 int ret = 0;
347 const wxDataViewTreeNode * node = this;
348 while( node->GetParent()->GetParent() != NULL )
349 {
350 node = node->GetParent();
351 ret ++;
352 }
353 return ret;
354 }
355
356 bool IsOpen() const
357 {
358 return m_branchData && m_branchData->open;
359 }
360
361 void ToggleOpen()
362 {
363 wxCHECK_RET( m_branchData != NULL, "can't open leaf node" );
364
365 int sum = 0;
366
367 const wxDataViewTreeNodes& nodes = m_branchData->children;
368 const int len = nodes.GetCount();
369 for ( int i = 0;i < len; i ++)
370 sum += 1 + nodes[i]->GetSubTreeCount();
371
372 if (m_branchData->open)
373 {
374 ChangeSubTreeCount(-sum);
375 m_branchData->open = !m_branchData->open;
376 }
377 else
378 {
379 m_branchData->open = !m_branchData->open;
380 ChangeSubTreeCount(+sum);
381 }
382 }
383
384 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
385 // even if GetChildNodes() is empty; see below.
386 bool HasChildren() const
387 {
388 return m_branchData != NULL;
389 }
390
391 void SetHasChildren(bool has)
392 {
393 if ( !has )
394 {
395 wxDELETE(m_branchData);
396 }
397 else if ( m_branchData == NULL )
398 {
399 m_branchData = new BranchNodeData;
400 }
401 }
402
403 int GetSubTreeCount() const
404 {
405 return m_branchData ? m_branchData->subTreeCount : 0;
406 }
407
408 void ChangeSubTreeCount( int num )
409 {
410 wxASSERT( m_branchData != NULL );
411
412 if( !m_branchData->open )
413 return;
414
415 m_branchData->subTreeCount += num;
416 wxASSERT( m_branchData->subTreeCount >= 0 );
417
418 if( m_parent )
419 m_parent->ChangeSubTreeCount(num);
420 }
421
422 void Resort()
423 {
424 if ( !m_branchData )
425 return;
426
427 if (g_column >= -1)
428 {
429 wxDataViewTreeNodes& nodes = m_branchData->children;
430
431 nodes.Sort( &wxGenericTreeModelNodeCmp );
432 int len = nodes.GetCount();
433 for (int i = 0; i < len; i ++)
434 {
435 if ( nodes[i]->HasChildren() )
436 nodes[i]->Resort();
437 }
438 }
439 }
440
441
442 private:
443 wxDataViewTreeNode *m_parent;
444
445 // Corresponding model item.
446 wxDataViewItem m_item;
447
448 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
449 // separate struct in order to conserve memory.
450 struct BranchNodeData
451 {
452 BranchNodeData()
453 : open(false),
454 subTreeCount(0)
455 {
456 }
457
458 // Child nodes. Note that this may be empty even if m_hasChildren in
459 // case this branch of the tree wasn't expanded and realized yet.
460 wxDataViewTreeNodes children;
461
462 // Is the branch node currently open (expanded)?
463 bool open;
464
465 // Total count of expanded (i.e. visible with the help of some
466 // scrolling) items in the subtree, but excluding this node. I.e. it is
467 // 0 for leaves and is the number of rows the subtree occupies for
468 // branch nodes.
469 int subTreeCount;
470 };
471
472 BranchNodeData *m_branchData;
473 };
474
475
476 int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1,
477 wxDataViewTreeNode ** node2)
478 {
479 return g_model->Compare( (*node1)->GetItem(), (*node2)->GetItem(), g_column, g_asending );
480 }
481
482
483 //-----------------------------------------------------------------------------
484 // wxDataViewMainWindow
485 //-----------------------------------------------------------------------------
486
487 WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection);
488
489 class wxDataViewMainWindow: public wxWindow
490 {
491 public:
492 wxDataViewMainWindow( wxDataViewCtrl *parent,
493 wxWindowID id,
494 const wxPoint &pos = wxDefaultPosition,
495 const wxSize &size = wxDefaultSize,
496 const wxString &name = wxT("wxdataviewctrlmainwindow") );
497 virtual ~wxDataViewMainWindow();
498
499 bool IsList() const { return GetModel()->IsListModel(); }
500 bool IsVirtualList() const { return m_root == NULL; }
501
502 // notifications from wxDataViewModel
503 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
504 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
505 bool ItemChanged( const wxDataViewItem &item );
506 bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
507 bool Cleared();
508 void Resort()
509 {
510 if (!IsVirtualList())
511 {
512 SortPrepare();
513 m_root->Resort();
514 }
515 UpdateDisplay();
516 }
517
518 void SortPrepare()
519 {
520 g_model = GetModel();
521 wxDataViewColumn* col = GetOwner()->GetSortingColumn();
522 if( !col )
523 {
524 if (g_model->HasDefaultCompare())
525 g_column = -1;
526 else
527 g_column = -2;
528
529 g_asending = true;
530 return;
531 }
532 g_column = col->GetModelColumn();
533 g_asending = col->IsSortOrderAscending();
534 }
535
536 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
537 wxDataViewCtrl *GetOwner() { return m_owner; }
538 const wxDataViewCtrl *GetOwner() const { return m_owner; }
539
540 wxDataViewModel* GetModel() { return GetOwner()->GetModel(); }
541 const wxDataViewModel* GetModel() const { return GetOwner()->GetModel(); }
542
543 #if wxUSE_DRAG_AND_DROP
544 wxBitmap CreateItemBitmap( unsigned int row, int &indent );
545 #endif // wxUSE_DRAG_AND_DROP
546 void OnPaint( wxPaintEvent &event );
547 void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event);
548 void OnChar( wxKeyEvent &event );
549 void OnMouse( wxMouseEvent &event );
550 void OnSetFocus( wxFocusEvent &event );
551 void OnKillFocus( wxFocusEvent &event );
552
553 void UpdateDisplay();
554 void RecalculateDisplay();
555 void OnInternalIdle();
556
557 void OnRenameTimer();
558
559 void ScrollWindow( int dx, int dy, const wxRect *rect = NULL );
560 void ScrollTo( int rows, int column );
561
562 unsigned GetCurrentRow() const { return m_currentRow; }
563 bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
564 void ChangeCurrentRow( unsigned int row );
565
566 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); }
567 bool IsEmpty() { return GetRowCount() == 0; }
568
569 int GetCountPerPage() const;
570 int GetEndOfLastCol() const;
571 unsigned int GetFirstVisibleRow() const;
572
573 // I change this method to un const because in the tree view,
574 // the displaying number of the tree are changing along with the
575 // expanding/collapsing of the tree nodes
576 unsigned int GetLastVisibleRow();
577 unsigned int GetRowCount();
578
579 const wxDataViewSelection& GetSelections() const { return m_selection; }
580 void SetSelections( const wxDataViewSelection & sel )
581 { m_selection = sel; UpdateDisplay(); }
582 void Select( const wxArrayInt& aSelections );
583 void SelectAllRows( bool on );
584 void SelectRow( unsigned int row, bool on );
585 void SelectRows( unsigned int from, unsigned int to, bool on );
586 void ReverseRowSelection( unsigned int row );
587 bool IsRowSelected( unsigned int row );
588 void SendSelectionChangedEvent( const wxDataViewItem& item);
589
590 void RefreshRow( unsigned int row );
591 void RefreshRows( unsigned int from, unsigned int to );
592 void RefreshRowsAfter( unsigned int firstRow );
593
594 // returns the colour to be used for drawing the rules
595 wxColour GetRuleColour() const
596 {
597 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
598 }
599
600 wxRect GetLineRect( unsigned int row ) const;
601
602 int GetLineStart( unsigned int row ) const; // row * m_lineHeight in fixed mode
603 int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode
604 int GetLineAt( unsigned int y ) const; // y / m_lineHeight in fixed mode
605
606 void SetRowHeight( int lineHeight ) { m_lineHeight = lineHeight; }
607
608 // Some useful functions for row and item mapping
609 wxDataViewItem GetItemByRow( unsigned int row ) const;
610 int GetRowByItem( const wxDataViewItem & item ) const;
611
612 // Methods for building the mapping tree
613 void BuildTree( wxDataViewModel * model );
614 void DestroyTree();
615 void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column );
616 wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column );
617
618 void Expand( unsigned int row );
619 void Collapse( unsigned int row );
620 bool IsExpanded( unsigned int row ) const;
621 bool HasChildren( unsigned int row ) const;
622
623 #if wxUSE_DRAG_AND_DROP
624 bool EnableDragSource( const wxDataFormat &format );
625 bool EnableDropTarget( const wxDataFormat &format );
626
627 void RemoveDropHint();
628 wxDragResult OnDragOver( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def );
629 bool OnDrop( wxDataFormat format, wxCoord x, wxCoord y );
630 wxDragResult OnData( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def );
631 void OnLeave();
632 #endif // wxUSE_DRAG_AND_DROP
633
634 private:
635 wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ) const;
636 // We did not need this temporarily
637 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
638
639 int RecalculateCount();
640
641 // Return false only if the event was vetoed by its handler.
642 bool SendExpanderEvent(wxEventType type, const wxDataViewItem& item);
643
644 wxDataViewTreeNode * FindNode( const wxDataViewItem & item );
645
646 private:
647 wxDataViewCtrl *m_owner;
648 int m_lineHeight;
649 bool m_dirty;
650
651 wxDataViewColumn *m_currentCol;
652 unsigned int m_currentRow;
653 wxDataViewSelection m_selection;
654
655 wxDataViewRenameTimer *m_renameTimer;
656 bool m_lastOnSame;
657
658 bool m_hasFocus;
659
660 #if wxUSE_DRAG_AND_DROP
661 int m_dragCount;
662 wxPoint m_dragStart;
663
664 bool m_dragEnabled;
665 wxDataFormat m_dragFormat;
666
667 bool m_dropEnabled;
668 wxDataFormat m_dropFormat;
669 bool m_dropHint;
670 unsigned int m_dropHintLine;
671 #endif // wxUSE_DRAG_AND_DROP
672
673 // for double click logic
674 unsigned int m_lineLastClicked,
675 m_lineBeforeLastClicked,
676 m_lineSelectSingleOnUp;
677
678 // the pen used to draw horiz/vertical rules
679 wxPen m_penRule;
680
681 // the pen used to draw the expander and the lines
682 wxPen m_penExpander;
683
684 // This is the tree structure of the model
685 wxDataViewTreeNode * m_root;
686 int m_count;
687
688 // This is the tree node under the cursor
689 wxDataViewTreeNode * m_underMouse;
690
691 private:
692 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
693 DECLARE_EVENT_TABLE()
694 };
695
696 // ---------------------------------------------------------
697 // wxGenericDataViewModelNotifier
698 // ---------------------------------------------------------
699
700 class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier
701 {
702 public:
703 wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow )
704 { m_mainWindow = mainWindow; }
705
706 virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item )
707 { return m_mainWindow->ItemAdded( parent , item ); }
708 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
709 { return m_mainWindow->ItemDeleted( parent, item ); }
710 virtual bool ItemChanged( const wxDataViewItem & item )
711 { return m_mainWindow->ItemChanged(item); }
712 virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col )
713 { return m_mainWindow->ValueChanged( item, col ); }
714 virtual bool Cleared()
715 { return m_mainWindow->Cleared(); }
716 virtual void Resort()
717 { m_mainWindow->Resort(); }
718
719 wxDataViewMainWindow *m_mainWindow;
720 };
721
722 // ---------------------------------------------------------
723 // wxDataViewRenderer
724 // ---------------------------------------------------------
725
726 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
727
728 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
729 wxDataViewCellMode mode,
730 int align) :
731 wxDataViewCustomRendererBase( varianttype, mode, align )
732 {
733 m_align = align;
734 m_mode = mode;
735 m_ellipsizeMode = wxELLIPSIZE_MIDDLE;
736 m_dc = NULL;
737 }
738
739 wxDataViewRenderer::~wxDataViewRenderer()
740 {
741 delete m_dc;
742 }
743
744 wxDC *wxDataViewRenderer::GetDC()
745 {
746 if (m_dc == NULL)
747 {
748 if (GetOwner() == NULL)
749 return NULL;
750 if (GetOwner()->GetOwner() == NULL)
751 return NULL;
752 m_dc = new wxClientDC( GetOwner()->GetOwner() );
753 }
754
755 return m_dc;
756 }
757
758 void wxDataViewRenderer::SetAlignment( int align )
759 {
760 m_align=align;
761 }
762
763 int wxDataViewRenderer::GetAlignment() const
764 {
765 return m_align;
766 }
767
768 // ---------------------------------------------------------
769 // wxDataViewCustomRenderer
770 // ---------------------------------------------------------
771
772 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
773
774 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
775 wxDataViewCellMode mode, int align ) :
776 wxDataViewRenderer( varianttype, mode, align )
777 {
778 }
779
780 // ---------------------------------------------------------
781 // wxDataViewTextRenderer
782 // ---------------------------------------------------------
783
784 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
785
786 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype,
787 wxDataViewCellMode mode, int align ) :
788 wxDataViewRenderer( varianttype, mode, align )
789 {
790 }
791
792 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
793 {
794 m_text = value.GetString();
795
796 return true;
797 }
798
799 bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
800 {
801 return false;
802 }
803
804 bool wxDataViewTextRenderer::HasEditorCtrl() const
805 {
806 return true;
807 }
808
809 wxWindow* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent,
810 wxRect labelRect, const wxVariant &value )
811 {
812 wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value,
813 wxPoint(labelRect.x,labelRect.y),
814 wxSize(labelRect.width,labelRect.height),
815 wxTE_PROCESS_ENTER );
816
817 // select the text in the control an place the cursor at the end
818 ctrl->SetInsertionPointEnd();
819 ctrl->SelectAll();
820
821 return ctrl;
822 }
823
824 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant &value )
825 {
826 wxTextCtrl *text = (wxTextCtrl*) editor;
827 value = text->GetValue();
828 return true;
829 }
830
831 bool wxDataViewTextRenderer::Render(wxRect rect, wxDC *dc, int state)
832 {
833 RenderText(m_text, 0, rect, dc, state);
834 return true;
835 }
836
837 wxSize wxDataViewTextRenderer::GetSize() const
838 {
839 if (!m_text.empty())
840 return GetTextExtent(m_text);
841 else
842 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
843 }
844
845 // ---------------------------------------------------------
846 // wxDataViewBitmapRenderer
847 // ---------------------------------------------------------
848
849 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
850
851 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype,
852 wxDataViewCellMode mode, int align ) :
853 wxDataViewRenderer( varianttype, mode, align )
854 {
855 }
856
857 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
858 {
859 if (value.GetType() == wxT("wxBitmap"))
860 m_bitmap << value;
861 if (value.GetType() == wxT("wxIcon"))
862 m_icon << value;
863
864 return true;
865 }
866
867 bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
868 {
869 return false;
870 }
871
872 bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
873 {
874 if (m_bitmap.IsOk())
875 dc->DrawBitmap( m_bitmap, cell.x, cell.y );
876 else if (m_icon.IsOk())
877 dc->DrawIcon( m_icon, cell.x, cell.y );
878
879 return true;
880 }
881
882 wxSize wxDataViewBitmapRenderer::GetSize() const
883 {
884 if (m_bitmap.IsOk())
885 return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
886 else if (m_icon.IsOk())
887 return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
888
889 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
890 }
891
892 // ---------------------------------------------------------
893 // wxDataViewToggleRenderer
894 // ---------------------------------------------------------
895
896 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
897
898 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
899 wxDataViewCellMode mode, int align ) :
900 wxDataViewRenderer( varianttype, mode, align )
901 {
902 m_toggle = false;
903 }
904
905 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
906 {
907 m_toggle = value.GetBool();
908
909 return true;
910 }
911
912 bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
913 {
914 return false;
915 }
916
917 bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
918 {
919 int flags = 0;
920 if (m_toggle)
921 flags |= wxCONTROL_CHECKED;
922 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE ||
923 GetEnabled() == false)
924 flags |= wxCONTROL_DISABLED;
925
926 // check boxes we draw must always have the same, standard size (if it's
927 // bigger than the cell size the checkbox will be truncated because the
928 // caller had set the clipping rectangle to prevent us from drawing outside
929 // the cell)
930 cell.SetSize(GetSize());
931
932 wxRendererNative::Get().DrawCheckBox(
933 GetOwner()->GetOwner(),
934 *dc,
935 cell,
936 flags );
937
938 return true;
939 }
940
941 bool wxDataViewToggleRenderer::WXOnLeftClick(const wxPoint& cursor,
942 const wxRect& WXUNUSED(cell),
943 wxDataViewModel *model,
944 const wxDataViewItem& item,
945 unsigned int col)
946 {
947 // only react to clicks directly on the checkbox, not elsewhere in the same cell:
948 if (!wxRect(GetSize()).Contains(cursor))
949 return false;
950
951 if (model->IsEnabled(item, col))
952 {
953 model->ChangeValue(!m_toggle, item, col);
954 return true;
955 }
956
957 return false;
958 }
959
960 wxSize wxDataViewToggleRenderer::GetSize() const
961 {
962 // the window parameter is not used by GetCheckBoxSize() so it's
963 // safe to pass NULL
964 return wxRendererNative::Get().GetCheckBoxSize(NULL);
965 }
966
967 // ---------------------------------------------------------
968 // wxDataViewProgressRenderer
969 // ---------------------------------------------------------
970
971 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewRenderer)
972
973 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
974 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
975 wxDataViewRenderer( varianttype, mode, align )
976 {
977 m_label = label;
978 m_value = 0;
979 }
980
981 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
982 {
983 m_value = (long) value;
984
985 if (m_value < 0) m_value = 0;
986 if (m_value > 100) m_value = 100;
987
988 return true;
989 }
990
991 bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
992 {
993 value = (long) m_value;
994 return true;
995 }
996
997 bool
998 wxDataViewProgressRenderer::Render(wxRect rect, wxDC *dc, int WXUNUSED(state))
999 {
1000 // deflate the rect to leave a small border between bars in adjacent rows
1001 wxRect bar = rect.Deflate(0, 1);
1002
1003 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1004 dc->SetPen( *wxBLACK_PEN );
1005 dc->DrawRectangle( bar );
1006
1007 bar.width = (int)(bar.width * m_value / 100.);
1008 dc->SetPen( *wxTRANSPARENT_PEN );
1009
1010 const wxDataViewItemAttr& attr = GetAttr();
1011 dc->SetBrush( attr.HasColour() ? wxBrush(attr.GetColour())
1012 : *wxBLUE_BRUSH );
1013 dc->DrawRectangle( bar );
1014
1015 return true;
1016 }
1017
1018 wxSize wxDataViewProgressRenderer::GetSize() const
1019 {
1020 return wxSize(40,12);
1021 }
1022
1023 // ---------------------------------------------------------
1024 // wxDataViewDateRenderer
1025 // ---------------------------------------------------------
1026
1027 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
1028
1029 #if wxUSE_DATE_RENDERER_POPUP
1030
1031 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
1032 {
1033 public:
1034 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
1035 wxDataViewModel *model, const wxDataViewItem & item, unsigned int col) :
1036 wxPopupTransientWindow( parent, wxBORDER_SIMPLE ),
1037 m_item( item )
1038 {
1039 m_model = model;
1040 m_col = col;
1041 m_cal = new wxCalendarCtrl( this, wxID_ANY, *value );
1042 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1043 sizer->Add( m_cal, 1, wxGROW );
1044 SetSizer( sizer );
1045 sizer->Fit( this );
1046 }
1047
1048 void OnCalendar( wxCalendarEvent &event );
1049
1050 wxCalendarCtrl *m_cal;
1051 wxDataViewModel *m_model;
1052 unsigned int m_col;
1053 const wxDataViewItem & m_item;
1054
1055 protected:
1056 virtual void OnDismiss()
1057 {
1058 }
1059
1060 private:
1061 DECLARE_EVENT_TABLE()
1062 };
1063
1064 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
1065 EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar )
1066 END_EVENT_TABLE()
1067
1068 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
1069 {
1070 m_model->ChangeValue( event.GetDate(), m_item, m_col );
1071 DismissAndNotify();
1072 }
1073
1074 #endif // wxUSE_DATE_RENDERER_POPUP
1075
1076 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewRenderer)
1077
1078 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
1079 wxDataViewCellMode mode, int align ) :
1080 wxDataViewRenderer( varianttype, mode, align )
1081 {
1082 }
1083
1084 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
1085 {
1086 m_date = value.GetDateTime();
1087
1088 return true;
1089 }
1090
1091 bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const
1092 {
1093 value = m_date;
1094 return true;
1095 }
1096
1097 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
1098 {
1099 wxString tmp = m_date.FormatDate();
1100 RenderText( tmp, 0, cell, dc, state );
1101 return true;
1102 }
1103
1104 wxSize wxDataViewDateRenderer::GetSize() const
1105 {
1106 return GetTextExtent(m_date.FormatDate());
1107 }
1108
1109 bool wxDataViewDateRenderer::WXOnActivate(const wxRect& WXUNUSED(cell),
1110 wxDataViewModel *model,
1111 const wxDataViewItem& item,
1112 unsigned int col)
1113 {
1114 wxDateTime dtOld = m_date;
1115
1116 #if wxUSE_DATE_RENDERER_POPUP
1117 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
1118 GetOwner()->GetOwner()->GetParent(), &dtOld, model, item, col);
1119 wxPoint pos = wxGetMousePosition();
1120 popup->Move( pos );
1121 popup->Layout();
1122 popup->Popup( popup->m_cal );
1123 #else // !wxUSE_DATE_RENDERER_POPUP
1124 wxMessageBox(dtOld.Format());
1125 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1126
1127 return true;
1128 }
1129
1130 // ---------------------------------------------------------
1131 // wxDataViewIconTextRenderer
1132 // ---------------------------------------------------------
1133
1134 IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewRenderer)
1135
1136 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1137 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1138 wxDataViewRenderer( varianttype, mode, align )
1139 {
1140 SetMode(mode);
1141 SetAlignment(align);
1142 }
1143
1144 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
1145 {
1146 m_value << value;
1147 return true;
1148 }
1149
1150 bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
1151 {
1152 return false;
1153 }
1154
1155 bool wxDataViewIconTextRenderer::Render(wxRect rect, wxDC *dc, int state)
1156 {
1157 int xoffset = 0;
1158
1159 const wxIcon& icon = m_value.GetIcon();
1160 if ( icon.IsOk() )
1161 {
1162 dc->DrawIcon(icon, rect.x, rect.y + (rect.height - icon.GetHeight())/2);
1163 xoffset = icon.GetWidth()+4;
1164 }
1165
1166 RenderText(m_value.GetText(), xoffset, rect, dc, state);
1167
1168 return true;
1169 }
1170
1171 wxSize wxDataViewIconTextRenderer::GetSize() const
1172 {
1173 if (!m_value.GetText().empty())
1174 {
1175 wxSize size = GetTextExtent(m_value.GetText());
1176
1177 if (m_value.GetIcon().IsOk())
1178 size.x += m_value.GetIcon().GetWidth() + 4;
1179 return size;
1180 }
1181 return wxSize(80,20);
1182 }
1183
1184 wxWindow* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
1185 {
1186 wxDataViewIconText iconText;
1187 iconText << value;
1188
1189 wxString text = iconText.GetText();
1190
1191 // adjust the label rect to take the width of the icon into account
1192 if (iconText.GetIcon().IsOk())
1193 {
1194 int w = iconText.GetIcon().GetWidth() + 4;
1195 labelRect.x += w;
1196 labelRect.width -= w;
1197 }
1198
1199 wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, text,
1200 wxPoint(labelRect.x,labelRect.y),
1201 wxSize(labelRect.width,labelRect.height),
1202 wxTE_PROCESS_ENTER );
1203
1204 // select the text in the control an place the cursor at the end
1205 ctrl->SetInsertionPointEnd();
1206 ctrl->SelectAll();
1207
1208 return ctrl;
1209 }
1210
1211 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant& value )
1212 {
1213 wxTextCtrl *text = (wxTextCtrl*) editor;
1214
1215 wxDataViewIconText iconText(text->GetValue(), m_value.GetIcon());
1216 value << iconText;
1217 return true;
1218 }
1219
1220 //-----------------------------------------------------------------------------
1221 // wxDataViewDropTarget
1222 //-----------------------------------------------------------------------------
1223
1224 #if wxUSE_DRAG_AND_DROP
1225
1226 class wxBitmapCanvas: public wxWindow
1227 {
1228 public:
1229 wxBitmapCanvas( wxWindow *parent, const wxBitmap &bitmap, const wxSize &size ) :
1230 wxWindow( parent, wxID_ANY, wxPoint(0,0), size )
1231 {
1232 m_bitmap = bitmap;
1233 Connect( wxEVT_PAINT, wxPaintEventHandler(wxBitmapCanvas::OnPaint) );
1234 }
1235
1236 void OnPaint( wxPaintEvent &WXUNUSED(event) )
1237 {
1238 wxPaintDC dc(this);
1239 dc.DrawBitmap( m_bitmap, 0, 0);
1240 }
1241
1242 wxBitmap m_bitmap;
1243 };
1244
1245 class wxDataViewDropSource: public wxDropSource
1246 {
1247 public:
1248 wxDataViewDropSource( wxDataViewMainWindow *win, unsigned int row ) :
1249 wxDropSource( win )
1250 {
1251 m_win = win;
1252 m_row = row;
1253 m_hint = NULL;
1254 }
1255
1256 ~wxDataViewDropSource()
1257 {
1258 delete m_hint;
1259 }
1260
1261 virtual bool GiveFeedback( wxDragResult WXUNUSED(effect) )
1262 {
1263 wxPoint pos = wxGetMousePosition();
1264
1265 if (!m_hint)
1266 {
1267 int liney = m_win->GetLineStart( m_row );
1268 int linex = 0;
1269 m_win->GetOwner()->CalcUnscrolledPosition( 0, liney, NULL, &liney );
1270 m_win->ClientToScreen( &linex, &liney );
1271 m_dist_x = pos.x - linex;
1272 m_dist_y = pos.y - liney;
1273
1274 int indent = 0;
1275 wxBitmap ib = m_win->CreateItemBitmap( m_row, indent );
1276 m_dist_x -= indent;
1277 m_hint = new wxFrame( m_win->GetParent(), wxID_ANY, wxEmptyString,
1278 wxPoint(pos.x - m_dist_x, pos.y + 5 ),
1279 ib.GetSize(),
1280 wxFRAME_TOOL_WINDOW |
1281 wxFRAME_FLOAT_ON_PARENT |
1282 wxFRAME_NO_TASKBAR |
1283 wxNO_BORDER );
1284 new wxBitmapCanvas( m_hint, ib, ib.GetSize() );
1285 m_hint->Show();
1286 }
1287 else
1288 {
1289 m_hint->Move( pos.x - m_dist_x, pos.y + 5 );
1290 m_hint->SetTransparent( 128 );
1291 }
1292
1293 return false;
1294 }
1295
1296 wxDataViewMainWindow *m_win;
1297 unsigned int m_row;
1298 wxFrame *m_hint;
1299 int m_dist_x,m_dist_y;
1300 };
1301
1302
1303 class wxDataViewDropTarget: public wxDropTarget
1304 {
1305 public:
1306 wxDataViewDropTarget( wxDataObject *obj, wxDataViewMainWindow *win ) :
1307 wxDropTarget( obj )
1308 {
1309 m_win = win;
1310 }
1311
1312 virtual wxDragResult OnDragOver( wxCoord x, wxCoord y, wxDragResult def )
1313 {
1314 wxDataFormat format = GetMatchingPair();
1315 if (format == wxDF_INVALID)
1316 return wxDragNone;
1317 return m_win->OnDragOver( format, x, y, def);
1318 }
1319
1320 virtual bool OnDrop( wxCoord x, wxCoord y )
1321 {
1322 wxDataFormat format = GetMatchingPair();
1323 if (format == wxDF_INVALID)
1324 return false;
1325 return m_win->OnDrop( format, x, y );
1326 }
1327
1328 virtual wxDragResult OnData( wxCoord x, wxCoord y, wxDragResult def )
1329 {
1330 wxDataFormat format = GetMatchingPair();
1331 if (format == wxDF_INVALID)
1332 return wxDragNone;
1333 if (!GetData())
1334 return wxDragNone;
1335 return m_win->OnData( format, x, y, def );
1336 }
1337
1338 virtual void OnLeave()
1339 { m_win->OnLeave(); }
1340
1341 wxDataViewMainWindow *m_win;
1342 };
1343
1344 #endif // wxUSE_DRAG_AND_DROP
1345
1346 //-----------------------------------------------------------------------------
1347 // wxDataViewRenameTimer
1348 //-----------------------------------------------------------------------------
1349
1350 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1351 {
1352 m_owner = owner;
1353 }
1354
1355 void wxDataViewRenameTimer::Notify()
1356 {
1357 m_owner->OnRenameTimer();
1358 }
1359
1360 //-----------------------------------------------------------------------------
1361 // wxDataViewMainWindow
1362 //-----------------------------------------------------------------------------
1363
1364 // The tree building helper, declared firstly
1365 static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
1366 wxDataViewTreeNode * node);
1367
1368 int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
1369 {
1370 if (row1 > row2) return 1;
1371 if (row1 == row2) return 0;
1372 return -1;
1373 }
1374
1375 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
1376
1377 BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1378 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1379 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1380 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
1381 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
1382 EVT_CHAR (wxDataViewMainWindow::OnChar)
1383 END_EVENT_TABLE()
1384
1385 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1386 const wxPoint &pos, const wxSize &size, const wxString &name ) :
1387 wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ),
1388 m_selection( wxDataViewSelectionCmp )
1389
1390 {
1391 SetOwner( parent );
1392
1393 m_lastOnSame = false;
1394 m_renameTimer = new wxDataViewRenameTimer( this );
1395
1396 // TODO: user better initial values/nothing selected
1397 m_currentCol = NULL;
1398 m_currentRow = 0;
1399
1400 m_lineHeight = wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1401
1402 #if wxUSE_DRAG_AND_DROP
1403 m_dragCount = 0;
1404 m_dragStart = wxPoint(0,0);
1405
1406 m_dragEnabled = false;
1407 m_dropEnabled = false;
1408 m_dropHint = false;
1409 m_dropHintLine = (unsigned int) -1;
1410 #endif // wxUSE_DRAG_AND_DROP
1411
1412 m_lineLastClicked = (unsigned int) -1;
1413 m_lineBeforeLastClicked = (unsigned int) -1;
1414 m_lineSelectSingleOnUp = (unsigned int) -1;
1415
1416 m_hasFocus = false;
1417
1418 SetBackgroundColour( *wxWHITE );
1419
1420 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
1421
1422 m_penRule = wxPen(GetRuleColour());
1423
1424 // compose a pen whichcan draw black lines
1425 // TODO: maybe there is something system colour to use
1426 m_penExpander = wxPen(wxColour(0,0,0));
1427
1428 m_root = wxDataViewTreeNode::CreateRootNode();
1429
1430 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1431 m_count = -1;
1432 m_underMouse = NULL;
1433 UpdateDisplay();
1434 }
1435
1436 wxDataViewMainWindow::~wxDataViewMainWindow()
1437 {
1438 DestroyTree();
1439 delete m_renameTimer;
1440 }
1441
1442
1443 #if wxUSE_DRAG_AND_DROP
1444 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat &format )
1445 {
1446 m_dragFormat = format;
1447 m_dragEnabled = format != wxDF_INVALID;
1448
1449 return true;
1450 }
1451
1452 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat &format )
1453 {
1454 m_dropFormat = format;
1455 m_dropEnabled = format != wxDF_INVALID;
1456
1457 if (m_dropEnabled)
1458 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format ), this ) );
1459
1460 return true;
1461 }
1462
1463 void wxDataViewMainWindow::RemoveDropHint()
1464 {
1465 if (m_dropHint)
1466 {
1467 m_dropHint = false;
1468 RefreshRow( m_dropHintLine );
1469 m_dropHintLine = (unsigned int) -1;
1470 }
1471 }
1472
1473 wxDragResult wxDataViewMainWindow::OnDragOver( wxDataFormat format, wxCoord x,
1474 wxCoord y, wxDragResult def )
1475 {
1476 int xx = x;
1477 int yy = y;
1478 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1479 unsigned int row = GetLineAt( yy );
1480
1481 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
1482 {
1483 RemoveDropHint();
1484 return wxDragNone;
1485 }
1486
1487 wxDataViewItem item = GetItemByRow( row );
1488
1489 wxDataViewModel *model = GetModel();
1490
1491 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
1492 event.SetEventObject( m_owner );
1493 event.SetItem( item );
1494 event.SetModel( model );
1495 event.SetDataFormat( format );
1496 if (!m_owner->HandleWindowEvent( event ))
1497 {
1498 RemoveDropHint();
1499 return wxDragNone;
1500 }
1501
1502 if (!event.IsAllowed())
1503 {
1504 RemoveDropHint();
1505 return wxDragNone;
1506 }
1507
1508
1509 if (m_dropHint && (row != m_dropHintLine))
1510 RefreshRow( m_dropHintLine );
1511 m_dropHint = true;
1512 m_dropHintLine = row;
1513 RefreshRow( row );
1514
1515 return def;
1516 }
1517
1518 bool wxDataViewMainWindow::OnDrop( wxDataFormat format, wxCoord x, wxCoord y )
1519 {
1520 RemoveDropHint();
1521
1522 int xx = x;
1523 int yy = y;
1524 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1525 unsigned int row = GetLineAt( yy );
1526
1527 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
1528 return false;
1529
1530 wxDataViewItem item = GetItemByRow( row );
1531
1532 wxDataViewModel *model = GetModel();
1533
1534 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
1535 event.SetEventObject( m_owner );
1536 event.SetItem( item );
1537 event.SetModel( model );
1538 event.SetDataFormat( format );
1539 if (!m_owner->HandleWindowEvent( event ))
1540 return false;
1541
1542 if (!event.IsAllowed())
1543 return false;
1544
1545 return true;
1546 }
1547
1548 wxDragResult wxDataViewMainWindow::OnData( wxDataFormat format, wxCoord x, wxCoord y,
1549 wxDragResult def )
1550 {
1551 int xx = x;
1552 int yy = y;
1553 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1554 unsigned int row = GetLineAt( yy );
1555
1556 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
1557 return wxDragNone;
1558
1559 wxDataViewItem item = GetItemByRow( row );
1560
1561 wxDataViewModel *model = GetModel();
1562
1563 wxCustomDataObject *obj = (wxCustomDataObject *) GetDropTarget()->GetDataObject();
1564
1565 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, m_owner->GetId() );
1566 event.SetEventObject( m_owner );
1567 event.SetItem( item );
1568 event.SetModel( model );
1569 event.SetDataFormat( format );
1570 event.SetDataSize( obj->GetSize() );
1571 event.SetDataBuffer( obj->GetData() );
1572 if (!m_owner->HandleWindowEvent( event ))
1573 return wxDragNone;
1574
1575 if (!event.IsAllowed())
1576 return wxDragNone;
1577
1578 return def;
1579 }
1580
1581 void wxDataViewMainWindow::OnLeave()
1582 {
1583 RemoveDropHint();
1584 }
1585
1586 wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent )
1587 {
1588 int height = GetLineHeight( row );
1589 int width = 0;
1590 unsigned int cols = GetOwner()->GetColumnCount();
1591 unsigned int col;
1592 for (col = 0; col < cols; col++)
1593 {
1594 wxDataViewColumn *column = GetOwner()->GetColumnAt(col);
1595 if (column->IsHidden())
1596 continue; // skip it!
1597 width += column->GetWidth();
1598 }
1599
1600 indent = 0;
1601 if (!IsList())
1602 {
1603 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
1604 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
1605 indent = indent + m_lineHeight;
1606 // try to use the m_lineHeight as the expander space
1607 }
1608 width -= indent;
1609
1610 wxBitmap bitmap( width, height );
1611 wxMemoryDC dc( bitmap );
1612 dc.SetFont( GetFont() );
1613 dc.SetPen( *wxBLACK_PEN );
1614 dc.SetBrush( *wxWHITE_BRUSH );
1615 dc.DrawRectangle( 0,0,width,height );
1616
1617 wxDataViewModel *model = m_owner->GetModel();
1618
1619 wxDataViewColumn *expander = GetOwner()->GetExpanderColumn();
1620 if (!expander)
1621 {
1622 // TODO-RTL: last column for RTL support
1623 expander = GetOwner()->GetColumnAt( 0 );
1624 GetOwner()->SetExpanderColumn(expander);
1625 }
1626
1627
1628 int x = 0;
1629 for (col = 0; col < cols; col++)
1630 {
1631 wxDataViewColumn *column = GetOwner()->GetColumnAt( col );
1632 wxDataViewRenderer *cell = column->GetRenderer();
1633
1634 if (column->IsHidden())
1635 continue; // skip it!
1636
1637 width = column->GetWidth();
1638
1639 if (column == expander)
1640 width -= indent;
1641
1642 wxDataViewItem item = GetItemByRow( row );
1643 cell->PrepareForItem(model, item, column->GetModelColumn());
1644
1645 wxRect item_rect(x, 0, width, height);
1646 item_rect.Deflate(PADDING_RIGHTLEFT, 0);
1647
1648 // dc.SetClippingRegion( item_rect );
1649 cell->WXCallRender(item_rect, &dc, 0);
1650 // dc.DestroyClippingRegion();
1651
1652 x += width;
1653 }
1654
1655 return bitmap;
1656 }
1657
1658 #endif // wxUSE_DRAG_AND_DROP
1659
1660
1661 void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1662 {
1663 wxDataViewModel *model = GetModel();
1664 wxAutoBufferedPaintDC dc( this );
1665
1666 #ifdef __WXMSW__
1667 dc.SetBrush(GetOwner()->GetBackgroundColour());
1668 dc.SetPen( *wxTRANSPARENT_PEN );
1669 dc.DrawRectangle(GetClientSize());
1670 #endif
1671
1672 if ( IsEmpty() )
1673 {
1674 // No items to draw.
1675 return;
1676 }
1677
1678 // prepare the DC
1679 GetOwner()->PrepareDC( dc );
1680 dc.SetFont( GetFont() );
1681
1682 wxRect update = GetUpdateRegion().GetBox();
1683 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
1684
1685 // compute which items needs to be redrawn
1686 unsigned int item_start = GetLineAt( wxMax(0,update.y) );
1687 unsigned int item_count =
1688 wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1),
1689 (int)(GetRowCount( ) - item_start));
1690 unsigned int item_last = item_start + item_count;
1691
1692 // Send the event to wxDataViewCtrl itself.
1693 wxWindow * const parent = GetParent();
1694 wxDataViewEvent cache_event(wxEVT_COMMAND_DATAVIEW_CACHE_HINT, parent->GetId());
1695 cache_event.SetEventObject(parent);
1696 cache_event.SetCache(item_start, item_last - 1);
1697 parent->ProcessWindowEvent(cache_event);
1698
1699 // compute which columns needs to be redrawn
1700 unsigned int cols = GetOwner()->GetColumnCount();
1701 if ( !cols )
1702 {
1703 // we assume that we have at least one column below and painting an
1704 // empty control is unnecessary anyhow
1705 return;
1706 }
1707
1708 unsigned int col_start = 0;
1709 unsigned int x_start;
1710 for (x_start = 0; col_start < cols; col_start++)
1711 {
1712 wxDataViewColumn *col = GetOwner()->GetColumnAt(col_start);
1713 if (col->IsHidden())
1714 continue; // skip it!
1715
1716 unsigned int w = col->GetWidth();
1717 if (x_start+w >= (unsigned int)update.x)
1718 break;
1719
1720 x_start += w;
1721 }
1722
1723 unsigned int col_last = col_start;
1724 unsigned int x_last = x_start;
1725 for (; col_last < cols; col_last++)
1726 {
1727 wxDataViewColumn *col = GetOwner()->GetColumnAt(col_last);
1728 if (col->IsHidden())
1729 continue; // skip it!
1730
1731 if (x_last > (unsigned int)update.GetRight())
1732 break;
1733
1734 x_last += col->GetWidth();
1735 }
1736
1737 // Draw horizontal rules if required
1738 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
1739 {
1740 dc.SetPen(m_penRule);
1741 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1742
1743 for (unsigned int i = item_start; i <= item_last; i++)
1744 {
1745 int y = GetLineStart( i );
1746 dc.DrawLine(x_start, y, x_last, y);
1747 }
1748 }
1749
1750 // Draw vertical rules if required
1751 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
1752 {
1753 dc.SetPen(m_penRule);
1754 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1755
1756 // NB: Vertical rules are drawn in the last pixel of a column so that
1757 // they align perfectly with native MSW wxHeaderCtrl as well as for
1758 // consistency with MSW native list control. There's no vertical
1759 // rule at the most-left side of the control.
1760
1761 int x = x_start - 1;
1762 for (unsigned int i = col_start; i < col_last; i++)
1763 {
1764 wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
1765 if (col->IsHidden())
1766 continue; // skip it
1767
1768 x += col->GetWidth();
1769
1770 dc.DrawLine(x, GetLineStart( item_start ),
1771 x, GetLineStart( item_last ) );
1772 }
1773 }
1774
1775 // redraw the background for the items which are selected/current
1776 for (unsigned int item = item_start; item < item_last; item++)
1777 {
1778 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
1779 if (selected || item == m_currentRow)
1780 {
1781 int flags = selected ? (int)wxCONTROL_SELECTED : 0;
1782 if (item == m_currentRow)
1783 flags |= wxCONTROL_CURRENT;
1784 if (m_hasFocus)
1785 flags |= wxCONTROL_FOCUSED;
1786
1787 wxRect rect( x_start, GetLineStart( item ),
1788 x_last - x_start, GetLineHeight( item ) );
1789 wxRendererNative::Get().DrawItemSelectionRect
1790 (
1791 this,
1792 dc,
1793 rect,
1794 flags
1795 );
1796 }
1797 }
1798
1799 #if wxUSE_DRAG_AND_DROP
1800 if (m_dropHint)
1801 {
1802 wxRect rect( x_start, GetLineStart( m_dropHintLine ),
1803 x_last - x_start, GetLineHeight( m_dropHintLine ) );
1804 dc.SetPen( *wxBLACK_PEN );
1805 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1806 dc.DrawRectangle( rect );
1807 }
1808 #endif // wxUSE_DRAG_AND_DROP
1809
1810 wxDataViewColumn *expander = GetOwner()->GetExpanderColumn();
1811 if (!expander)
1812 {
1813 // TODO-RTL: last column for RTL support
1814 expander = GetOwner()->GetColumnAt( 0 );
1815 GetOwner()->SetExpanderColumn(expander);
1816 }
1817
1818 // redraw all cells for all rows which must be repainted and all columns
1819 wxRect cell_rect;
1820 cell_rect.x = x_start;
1821 for (unsigned int i = col_start; i < col_last; i++)
1822 {
1823 wxDataViewColumn *col = GetOwner()->GetColumnAt( i );
1824 wxDataViewRenderer *cell = col->GetRenderer();
1825 cell_rect.width = col->GetWidth();
1826
1827 if ( col->IsHidden() || cell_rect.width <= 0 )
1828 continue; // skip it!
1829
1830 for (unsigned int item = item_start; item < item_last; item++)
1831 {
1832 // get the cell value and set it into the renderer
1833 wxDataViewTreeNode *node = NULL;
1834 wxDataViewItem dataitem;
1835
1836 if (!IsVirtualList())
1837 {
1838 node = GetTreeNodeByRow(item);
1839 if( node == NULL )
1840 continue;
1841
1842 dataitem = node->GetItem();
1843
1844 // Skip all columns of "container" rows except the expander
1845 // column itself unless HasContainerColumns() overrides this.
1846 if ( col != GetOwner()->GetExpanderColumn() &&
1847 model->IsContainer(dataitem) &&
1848 !model->HasContainerColumns(dataitem) )
1849 continue;
1850 }
1851 else
1852 {
1853 dataitem = wxDataViewItem( wxUIntToPtr(item+1) );
1854 }
1855
1856 cell->PrepareForItem(model, dataitem, col->GetModelColumn());
1857
1858 // update cell_rect
1859 cell_rect.y = GetLineStart( item );
1860 cell_rect.height = GetLineHeight( item );
1861
1862 // deal with the expander
1863 int indent = 0;
1864 if ((!IsList()) && (col == expander))
1865 {
1866 // Calculate the indent first
1867 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
1868
1869 // we reserve m_lineHeight of horizontal space for the expander
1870 // but leave EXPANDER_MARGIN around the expander itself
1871 int exp_x = cell_rect.x + indent + EXPANDER_MARGIN;
1872
1873 indent += m_lineHeight;
1874
1875 // draw expander if needed and visible
1876 if ( node->HasChildren() && exp_x < cell_rect.GetRight() )
1877 {
1878 dc.SetPen( m_penExpander );
1879 dc.SetBrush( wxNullBrush );
1880
1881 int exp_size = m_lineHeight - 2*EXPANDER_MARGIN;
1882 int exp_y = cell_rect.y + (cell_rect.height - exp_size)/2
1883 + EXPANDER_MARGIN - EXPANDER_OFFSET;
1884
1885 const wxRect rect(exp_x, exp_y, exp_size, exp_size);
1886
1887 int flag = 0;
1888 if ( m_underMouse == node )
1889 flag |= wxCONTROL_CURRENT;
1890 if ( node->IsOpen() )
1891 flag |= wxCONTROL_EXPANDED;
1892
1893 // ensure that we don't overflow the cell (which might
1894 // happen if the column is very narrow)
1895 wxDCClipper clip(dc, cell_rect);
1896
1897 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag);
1898 }
1899
1900 // force the expander column to left-center align
1901 cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
1902 }
1903
1904 wxRect item_rect = cell_rect;
1905 item_rect.Deflate(PADDING_RIGHTLEFT, 0);
1906
1907 // account for the tree indent (harmless if we're not indented)
1908 item_rect.x += indent;
1909 item_rect.width -= indent;
1910
1911 if ( item_rect.width <= 0 )
1912 continue;
1913
1914 int state = 0;
1915 if (m_hasFocus && (m_selection.Index(item) != wxNOT_FOUND))
1916 state |= wxDATAVIEW_CELL_SELECTED;
1917
1918 // TODO: it would be much more efficient to create a clipping
1919 // region for the entire column being rendered (in the OnPaint
1920 // of wxDataViewMainWindow) instead of a single clip region for
1921 // each cell. However it would mean that each renderer should
1922 // respect the given wxRect's top & bottom coords, eventually
1923 // violating only the left & right coords - however the user can
1924 // make its own renderer and thus we cannot be sure of that.
1925 wxDCClipper clip(dc, item_rect);
1926
1927 cell->WXCallRender(item_rect, &dc, state);
1928 }
1929
1930 cell_rect.x += cell_rect.width;
1931 }
1932 }
1933
1934 void wxDataViewMainWindow::OnRenameTimer()
1935 {
1936 // We have to call this here because changes may just have
1937 // been made and no screen update taken place.
1938 if ( m_dirty )
1939 {
1940 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1941 // (needs to be tested!)
1942 wxSafeYield();
1943 }
1944
1945 wxDataViewItem item = GetItemByRow( m_currentRow );
1946
1947 wxRect labelRect = GetItemRect(item, m_currentCol);
1948
1949 m_currentCol->GetRenderer()->StartEditing( item, labelRect );
1950 }
1951
1952 //-----------------------------------------------------------------------------
1953 // Helper class for do operation on the tree node
1954 //-----------------------------------------------------------------------------
1955 class DoJob
1956 {
1957 public:
1958 DoJob() { }
1959 virtual ~DoJob() { }
1960
1961 // The return value control how the tree-walker tranverse the tree
1962 enum
1963 {
1964 DONE, // Job done, stop traversing and return
1965 SKIP_SUBTREE, // Ignore the current node's subtree and continue
1966 CONTINUE // Job not done, continue
1967 };
1968
1969 virtual int operator() ( wxDataViewTreeNode * node ) = 0;
1970 };
1971
1972 bool Walker( wxDataViewTreeNode * node, DoJob & func )
1973 {
1974 wxCHECK_MSG( node, false, "can't walk NULL node" );
1975
1976 switch( func( node ) )
1977 {
1978 case DoJob::DONE:
1979 return true;
1980 case DoJob::SKIP_SUBTREE:
1981 return false;
1982 case DoJob::CONTINUE:
1983 break;
1984 }
1985
1986 if ( node->HasChildren() )
1987 {
1988 const wxDataViewTreeNodes& nodes = node->GetChildNodes();
1989
1990 for ( wxDataViewTreeNodes::const_iterator i = nodes.begin();
1991 i != nodes.end();
1992 ++i )
1993 {
1994 if ( Walker(*i, func) )
1995 return true;
1996 }
1997 }
1998
1999 return false;
2000 }
2001
2002 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
2003 {
2004 if (IsVirtualList())
2005 {
2006 wxDataViewVirtualListModel *list_model =
2007 (wxDataViewVirtualListModel*) GetModel();
2008 m_count = list_model->GetCount();
2009 }
2010 else
2011 {
2012 SortPrepare();
2013
2014 wxDataViewTreeNode *parentNode = FindNode(parent);
2015
2016 if ( !parentNode )
2017 return false;
2018
2019 wxDataViewTreeNode *itemNode = new wxDataViewTreeNode(parentNode, item);
2020 itemNode->SetHasChildren(GetModel()->IsContainer(item));
2021
2022 parentNode->SetHasChildren(true);
2023 parentNode->AddChild(itemNode);
2024 parentNode->ChangeSubTreeCount(+1);
2025
2026 m_count = -1;
2027 }
2028
2029 GetOwner()->UpdateColBestWidths();
2030 UpdateDisplay();
2031
2032 return true;
2033 }
2034
2035 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
2036 const wxDataViewItem& item)
2037 {
2038 if (IsVirtualList())
2039 {
2040 wxDataViewVirtualListModel *list_model =
2041 (wxDataViewVirtualListModel*) GetModel();
2042 m_count = list_model->GetCount();
2043
2044 if ( !m_selection.empty() )
2045 {
2046 const int row = GetRowByItem(item);
2047
2048 int rowIndexInSelection = wxNOT_FOUND;
2049
2050 const size_t selCount = m_selection.size();
2051 for ( size_t i = 0; i < selCount; i++ )
2052 {
2053 if ( m_selection[i] == (unsigned)row )
2054 rowIndexInSelection = i;
2055 else if ( m_selection[i] > (unsigned)row )
2056 m_selection[i]--;
2057 }
2058
2059 if ( rowIndexInSelection != wxNOT_FOUND )
2060 m_selection.RemoveAt(rowIndexInSelection);
2061 }
2062
2063 }
2064 else // general case
2065 {
2066 wxDataViewTreeNode *parentNode = FindNode(parent);
2067
2068 // Notice that it is possible that the item being deleted is not in the
2069 // tree at all, for example we could be deleting a never shown (because
2070 // collapsed) item in a tree model. So it's not an error if we don't know
2071 // about this item, just return without doing anything then.
2072 if ( !parentNode )
2073 return false;
2074
2075 wxCHECK_MSG( parentNode->HasChildren(), false, "parent node doesn't have children?" );
2076 const wxDataViewTreeNodes& parentsChildren = parentNode->GetChildNodes();
2077
2078 // We can't use FindNode() to find 'item', because it was already
2079 // removed from the model by the time ItemDeleted() is called, so we
2080 // have to do it manually. We keep track of its position as well for
2081 // later use.
2082 int itemPosInNode = 0;
2083 wxDataViewTreeNode *itemNode = NULL;
2084 for ( wxDataViewTreeNodes::const_iterator i = parentsChildren.begin();
2085 i != parentsChildren.end();
2086 ++i, ++itemPosInNode )
2087 {
2088 if( (*i)->GetItem() == item )
2089 {
2090 itemNode = *i;
2091 break;
2092 }
2093 }
2094
2095 // If the parent wasn't expanded, it's possible that we didn't have a
2096 // node corresponding to 'item' and so there's nothing left to do.
2097 if ( !itemNode )
2098 {
2099 // If this was the last child to be removed, it's possible the parent
2100 // node became a leaf. Let's ask the model about it.
2101 if ( parentNode->GetChildNodes().empty() )
2102 parentNode->SetHasChildren(GetModel()->IsContainer(parent));
2103
2104 return false;
2105 }
2106
2107 // Delete the item from wxDataViewTreeNode representation:
2108 const int itemsDeleted = 1 + itemNode->GetSubTreeCount();
2109
2110 parentNode->RemoveChild(itemNode);
2111 delete itemNode;
2112 parentNode->ChangeSubTreeCount(-itemsDeleted);
2113
2114 // Make the row number invalid and get a new valid one when user call GetRowCount
2115 m_count = -1;
2116
2117 // If this was the last child to be removed, it's possible the parent
2118 // node became a leaf. Let's ask the model about it.
2119 if ( parentNode->GetChildNodes().empty() )
2120 parentNode->SetHasChildren(GetModel()->IsContainer(parent));
2121
2122 // Update selection by removing 'item' and its entire children tree from the selection.
2123 if ( !m_selection.empty() )
2124 {
2125 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
2126 // the parent ('parentNode') and position in its list of children
2127 int itemRow;
2128 if ( itemPosInNode == 0 )
2129 {
2130 // 1st child, row number is that of the parent parentNode + 1
2131 itemRow = GetRowByItem(parentNode->GetItem()) + 1;
2132 }
2133 else
2134 {
2135 // row number is that of the sibling above 'item' + its subtree if any + 1
2136 const wxDataViewTreeNode *siblingNode = parentNode->GetChildNodes()[itemPosInNode - 1];
2137
2138 itemRow = GetRowByItem(siblingNode->GetItem()) +
2139 siblingNode->GetSubTreeCount() +
2140 1;
2141 }
2142
2143 wxDataViewSelection newsel(wxDataViewSelectionCmp);
2144
2145 const size_t numSelections = m_selection.size();
2146 for ( size_t i = 0; i < numSelections; ++i )
2147 {
2148 const int s = m_selection[i];
2149 if ( s < itemRow )
2150 newsel.push_back(s);
2151 else if ( s >= itemRow + itemsDeleted )
2152 newsel.push_back(s - itemsDeleted);
2153 // else: deleted item, remove from selection
2154 }
2155
2156 m_selection = newsel;
2157 }
2158 }
2159
2160 // Change the current row to the last row if the current exceed the max row number
2161 if( m_currentRow > GetRowCount() )
2162 ChangeCurrentRow(m_count - 1);
2163
2164 GetOwner()->UpdateColBestWidths();
2165 UpdateDisplay();
2166
2167 return true;
2168 }
2169
2170 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
2171 {
2172 SortPrepare();
2173 g_model->Resort();
2174
2175 GetOwner()->UpdateColBestWidths();
2176
2177 // Send event
2178 wxWindow *parent = GetParent();
2179 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2180 le.SetEventObject(parent);
2181 le.SetModel(GetModel());
2182 le.SetItem(item);
2183 parent->GetEventHandler()->ProcessEvent(le);
2184
2185 return true;
2186 }
2187
2188 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int model_column )
2189 {
2190 int view_column = -1;
2191 unsigned int n_col = m_owner->GetColumnCount();
2192 for (unsigned i = 0; i < n_col; i++)
2193 {
2194 wxDataViewColumn *column = m_owner->GetColumn( i );
2195 if (column->GetModelColumn() == model_column)
2196 {
2197 view_column = (int) i;
2198 break;
2199 }
2200 }
2201 if (view_column == -1)
2202 return false;
2203
2204 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2205 /*#define MAX_VIRTUAL_WIDTH 100000
2206
2207 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2208 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2209 Refresh( true, &rect );
2210
2211 return true;
2212 */
2213 SortPrepare();
2214 g_model->Resort();
2215
2216 GetOwner()->UpdateColBestWidth(view_column);
2217
2218 // Send event
2219 wxWindow *parent = GetParent();
2220 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2221 le.SetEventObject(parent);
2222 le.SetModel(GetModel());
2223 le.SetItem(item);
2224 le.SetColumn(view_column);
2225 le.SetDataViewColumn(GetOwner()->GetColumn(view_column));
2226 parent->GetEventHandler()->ProcessEvent(le);
2227
2228 return true;
2229 }
2230
2231 bool wxDataViewMainWindow::Cleared()
2232 {
2233 DestroyTree();
2234 m_selection.Clear();
2235
2236 SortPrepare();
2237 BuildTree( GetModel() );
2238
2239 GetOwner()->UpdateColBestWidths();
2240 UpdateDisplay();
2241
2242 return true;
2243 }
2244
2245 void wxDataViewMainWindow::UpdateDisplay()
2246 {
2247 m_dirty = true;
2248 m_underMouse = NULL;
2249 }
2250
2251 void wxDataViewMainWindow::OnInternalIdle()
2252 {
2253 wxWindow::OnInternalIdle();
2254
2255 if (m_dirty)
2256 {
2257 RecalculateDisplay();
2258 m_dirty = false;
2259 }
2260 }
2261
2262 void wxDataViewMainWindow::RecalculateDisplay()
2263 {
2264 wxDataViewModel *model = GetModel();
2265 if (!model)
2266 {
2267 Refresh();
2268 return;
2269 }
2270
2271 int width = GetEndOfLastCol();
2272 int height = GetLineStart( GetRowCount() );
2273
2274 SetVirtualSize( width, height );
2275 GetOwner()->SetScrollRate( 10, m_lineHeight );
2276
2277 Refresh();
2278 }
2279
2280 void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2281 {
2282 m_underMouse = NULL;
2283
2284 wxWindow::ScrollWindow( dx, dy, rect );
2285
2286 if (GetOwner()->m_headerArea)
2287 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
2288 }
2289
2290 void wxDataViewMainWindow::ScrollTo( int rows, int column )
2291 {
2292 m_underMouse = NULL;
2293
2294 int x, y;
2295 m_owner->GetScrollPixelsPerUnit( &x, &y );
2296 int sy = GetLineStart( rows )/y;
2297 int sx = 0;
2298 if( column != -1 )
2299 {
2300 wxRect rect = GetClientRect();
2301 int colnum = 0;
2302 int x_start, w = 0;
2303 int xx, yy, xe;
2304 m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
2305 for (x_start = 0; colnum < column; colnum++)
2306 {
2307 wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum);
2308 if (col->IsHidden())
2309 continue; // skip it!
2310
2311 w = col->GetWidth();
2312 x_start += w;
2313 }
2314
2315 int x_end = x_start + w;
2316 xe = xx + rect.width;
2317 if( x_end > xe )
2318 {
2319 sx = ( xx + x_end - xe )/x;
2320 }
2321 if( x_start < xx )
2322 {
2323 sx = x_start/x;
2324 }
2325 }
2326 m_owner->Scroll( sx, sy );
2327 }
2328
2329 int wxDataViewMainWindow::GetCountPerPage() const
2330 {
2331 wxSize size = GetClientSize();
2332 return size.y / m_lineHeight;
2333 }
2334
2335 int wxDataViewMainWindow::GetEndOfLastCol() const
2336 {
2337 int width = 0;
2338 unsigned int i;
2339 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
2340 {
2341 const wxDataViewColumn *c =
2342 const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i );
2343
2344 if (!c->IsHidden())
2345 width += c->GetWidth();
2346 }
2347 return width;
2348 }
2349
2350 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2351 {
2352 int x = 0;
2353 int y = 0;
2354 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2355
2356 return GetLineAt( y );
2357 }
2358
2359 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2360 {
2361 wxSize client_size = GetClientSize();
2362 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
2363 &client_size.x, &client_size.y );
2364
2365 // we should deal with the pixel here
2366 unsigned int row = GetLineAt(client_size.y) - 1;
2367
2368 return wxMin( GetRowCount()-1, row );
2369 }
2370
2371 unsigned int wxDataViewMainWindow::GetRowCount()
2372 {
2373 if ( m_count == -1 )
2374 {
2375 m_count = RecalculateCount();
2376 UpdateDisplay();
2377 }
2378 return m_count;
2379 }
2380
2381 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
2382 {
2383 m_currentRow = row;
2384
2385 // send event
2386 }
2387
2388 void wxDataViewMainWindow::SelectAllRows( bool on )
2389 {
2390 if (IsEmpty())
2391 return;
2392
2393 if (on)
2394 {
2395 m_selection.Clear();
2396 for (unsigned int i = 0; i < GetRowCount(); i++)
2397 m_selection.Add( i );
2398 Refresh();
2399 }
2400 else
2401 {
2402 unsigned int first_visible = GetFirstVisibleRow();
2403 unsigned int last_visible = GetLastVisibleRow();
2404 unsigned int i;
2405 for (i = 0; i < m_selection.GetCount(); i++)
2406 {
2407 unsigned int row = m_selection[i];
2408 if ((row >= first_visible) && (row <= last_visible))
2409 RefreshRow( row );
2410 }
2411 m_selection.Clear();
2412 }
2413 }
2414
2415 void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
2416 {
2417 if (m_selection.Index( row ) == wxNOT_FOUND)
2418 {
2419 if (on)
2420 {
2421 m_selection.Add( row );
2422 RefreshRow( row );
2423 }
2424 }
2425 else
2426 {
2427 if (!on)
2428 {
2429 m_selection.Remove( row );
2430 RefreshRow( row );
2431 }
2432 }
2433 }
2434
2435 void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
2436 {
2437 if (from > to)
2438 {
2439 unsigned int tmp = from;
2440 from = to;
2441 to = tmp;
2442 }
2443
2444 unsigned int i;
2445 for (i = from; i <= to; i++)
2446 {
2447 if (m_selection.Index( i ) == wxNOT_FOUND)
2448 {
2449 if (on)
2450 m_selection.Add( i );
2451 }
2452 else
2453 {
2454 if (!on)
2455 m_selection.Remove( i );
2456 }
2457 }
2458 RefreshRows( from, to );
2459 }
2460
2461 void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2462 {
2463 for (size_t i=0; i < aSelections.GetCount(); i++)
2464 {
2465 int n = aSelections[i];
2466
2467 m_selection.Add( n );
2468 RefreshRow( n );
2469 }
2470 }
2471
2472 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
2473 {
2474 if (m_selection.Index( row ) == wxNOT_FOUND)
2475 m_selection.Add( row );
2476 else
2477 m_selection.Remove( row );
2478 RefreshRow( row );
2479 }
2480
2481 bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
2482 {
2483 return (m_selection.Index( row ) != wxNOT_FOUND);
2484 }
2485
2486 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item)
2487 {
2488 wxWindow *parent = GetParent();
2489 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId());
2490
2491 le.SetEventObject(parent);
2492 le.SetModel(GetModel());
2493 le.SetItem( item );
2494
2495 parent->GetEventHandler()->ProcessEvent(le);
2496 }
2497
2498 void wxDataViewMainWindow::RefreshRow( unsigned int row )
2499 {
2500 wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) );
2501 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2502
2503 wxSize client_size = GetClientSize();
2504 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2505 wxRect intersect_rect = client_rect.Intersect( rect );
2506 if (intersect_rect.width > 0)
2507 Refresh( true, &intersect_rect );
2508 }
2509
2510 void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
2511 {
2512 if (from > to)
2513 {
2514 unsigned int tmp = to;
2515 to = from;
2516 from = tmp;
2517 }
2518
2519 wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) );
2520 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2521
2522 wxSize client_size = GetClientSize();
2523 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2524 wxRect intersect_rect = client_rect.Intersect( rect );
2525 if (intersect_rect.width > 0)
2526 Refresh( true, &intersect_rect );
2527 }
2528
2529 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
2530 {
2531 wxSize client_size = GetClientSize();
2532 int start = GetLineStart( firstRow );
2533 m_owner->CalcScrolledPosition( start, 0, &start, NULL );
2534 if (start > client_size.y) return;
2535
2536 wxRect rect( 0, start, client_size.x, client_size.y - start );
2537
2538 Refresh( true, &rect );
2539 }
2540
2541 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
2542 {
2543 wxCHECK_RET( newCurrent < GetRowCount(),
2544 wxT("invalid item index in OnArrowChar()") );
2545
2546 // if there is no selection, we cannot move it anywhere
2547 if (!HasCurrentRow())
2548 return;
2549
2550 unsigned int oldCurrent = m_currentRow;
2551
2552 // in single selection we just ignore Shift as we can't select several
2553 // items anyhow
2554 if ( event.ShiftDown() && !IsSingleSel() )
2555 {
2556 RefreshRow( oldCurrent );
2557
2558 ChangeCurrentRow( newCurrent );
2559
2560 // select all the items between the old and the new one
2561 if ( oldCurrent > newCurrent )
2562 {
2563 newCurrent = oldCurrent;
2564 oldCurrent = m_currentRow;
2565 }
2566
2567 SelectRows( oldCurrent, newCurrent, true );
2568 if (oldCurrent!=newCurrent)
2569 SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
2570 }
2571 else // !shift
2572 {
2573 RefreshRow( oldCurrent );
2574
2575 // all previously selected items are unselected unless ctrl is held
2576 if ( !event.ControlDown() )
2577 SelectAllRows(false);
2578
2579 ChangeCurrentRow( newCurrent );
2580
2581 if ( !event.ControlDown() )
2582 {
2583 SelectRow( m_currentRow, true );
2584 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
2585 }
2586 else
2587 RefreshRow( m_currentRow );
2588 }
2589
2590 GetOwner()->EnsureVisible( m_currentRow, -1 );
2591 }
2592
2593 wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2594 {
2595 wxRect rect;
2596 rect.x = 0;
2597 rect.y = GetLineStart( row );
2598 rect.width = GetEndOfLastCol();
2599 rect.height = GetLineHeight( row );
2600
2601 return rect;
2602 }
2603
2604 int wxDataViewMainWindow::GetLineStart( unsigned int row ) const
2605 {
2606 const wxDataViewModel *model = GetModel();
2607
2608 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2609 {
2610 // TODO make more efficient
2611
2612 int start = 0;
2613
2614 unsigned int r;
2615 for (r = 0; r < row; r++)
2616 {
2617 const wxDataViewTreeNode* node = GetTreeNodeByRow(r);
2618 if (!node) return start;
2619
2620 wxDataViewItem item = node->GetItem();
2621
2622 unsigned int cols = GetOwner()->GetColumnCount();
2623 unsigned int col;
2624 int height = m_lineHeight;
2625 for (col = 0; col < cols; col++)
2626 {
2627 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2628 if (column->IsHidden())
2629 continue; // skip it!
2630
2631 if ((col != 0) &&
2632 model->IsContainer(item) &&
2633 !model->HasContainerColumns(item))
2634 continue; // skip it!
2635
2636 wxDataViewRenderer *renderer =
2637 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2638 renderer->PrepareForItem(model, item, column->GetModelColumn());
2639
2640 height = wxMax( height, renderer->GetSize().y );
2641 }
2642
2643 start += height;
2644 }
2645
2646 return start;
2647 }
2648 else
2649 {
2650 return row * m_lineHeight;
2651 }
2652 }
2653
2654 int wxDataViewMainWindow::GetLineAt( unsigned int y ) const
2655 {
2656 const wxDataViewModel *model = GetModel();
2657
2658 // check for the easy case first
2659 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
2660 return y / m_lineHeight;
2661
2662 // TODO make more efficient
2663 unsigned int row = 0;
2664 unsigned int yy = 0;
2665 for (;;)
2666 {
2667 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2668 if (!node)
2669 {
2670 // not really correct...
2671 return row + ((y-yy) / m_lineHeight);
2672 }
2673
2674 wxDataViewItem item = node->GetItem();
2675
2676 unsigned int cols = GetOwner()->GetColumnCount();
2677 unsigned int col;
2678 int height = m_lineHeight;
2679 for (col = 0; col < cols; col++)
2680 {
2681 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2682 if (column->IsHidden())
2683 continue; // skip it!
2684
2685 if ((col != 0) &&
2686 model->IsContainer(item) &&
2687 !model->HasContainerColumns(item))
2688 continue; // skip it!
2689
2690 wxDataViewRenderer *renderer =
2691 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2692 renderer->PrepareForItem(model, item, column->GetModelColumn());
2693
2694 height = wxMax( height, renderer->GetSize().y );
2695 }
2696
2697 yy += height;
2698 if (y < yy)
2699 return row;
2700
2701 row++;
2702 }
2703 }
2704
2705 int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const
2706 {
2707 const wxDataViewModel *model = GetModel();
2708
2709 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2710 {
2711 wxASSERT( !IsVirtualList() );
2712
2713 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2714 // wxASSERT( node );
2715 if (!node) return m_lineHeight;
2716
2717 wxDataViewItem item = node->GetItem();
2718
2719 int height = m_lineHeight;
2720
2721 unsigned int cols = GetOwner()->GetColumnCount();
2722 unsigned int col;
2723 for (col = 0; col < cols; col++)
2724 {
2725 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2726 if (column->IsHidden())
2727 continue; // skip it!
2728
2729 if ((col != 0) &&
2730 model->IsContainer(item) &&
2731 !model->HasContainerColumns(item))
2732 continue; // skip it!
2733
2734 wxDataViewRenderer *renderer =
2735 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2736 renderer->PrepareForItem(model, item, column->GetModelColumn());
2737
2738 height = wxMax( height, renderer->GetSize().y );
2739 }
2740
2741 return height;
2742 }
2743 else
2744 {
2745 return m_lineHeight;
2746 }
2747 }
2748
2749
2750 class RowToTreeNodeJob: public DoJob
2751 {
2752 public:
2753 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
2754 {
2755 this->row = row;
2756 this->current = current;
2757 ret = NULL;
2758 parent = node;
2759 }
2760
2761 virtual int operator() ( wxDataViewTreeNode * node )
2762 {
2763 current ++;
2764 if( current == static_cast<int>(row))
2765 {
2766 ret = node;
2767 return DoJob::DONE;
2768 }
2769
2770 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2771 {
2772 current += node->GetSubTreeCount();
2773 return DoJob::SKIP_SUBTREE;
2774 }
2775 else
2776 {
2777 parent = node;
2778
2779 // If the current node has only leaf children, we can find the
2780 // desired node directly. This can speed up finding the node
2781 // in some cases, and will have a very good effect for list views.
2782 if ( node->HasChildren() &&
2783 (int)node->GetChildNodes().size() == node->GetSubTreeCount() )
2784 {
2785 const int index = static_cast<int>(row) - current - 1;
2786 ret = node->GetChildNodes()[index];
2787 return DoJob::DONE;
2788 }
2789
2790 return DoJob::CONTINUE;
2791 }
2792 }
2793
2794 wxDataViewTreeNode * GetResult() const
2795 { return ret; }
2796
2797 private:
2798 unsigned int row;
2799 int current;
2800 wxDataViewTreeNode * ret;
2801 wxDataViewTreeNode * parent;
2802 };
2803
2804 wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const
2805 {
2806 wxASSERT( !IsVirtualList() );
2807
2808 RowToTreeNodeJob job( row , -2, m_root );
2809 Walker( m_root , job );
2810 return job.GetResult();
2811 }
2812
2813 wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
2814 {
2815 if (IsVirtualList())
2816 {
2817 return wxDataViewItem( wxUIntToPtr(row+1) );
2818 }
2819 else
2820 {
2821 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
2822 return node ? node->GetItem() : wxDataViewItem();
2823 }
2824 }
2825
2826 bool
2827 wxDataViewMainWindow::SendExpanderEvent(wxEventType type,
2828 const wxDataViewItem& item)
2829 {
2830 wxWindow *parent = GetParent();
2831 wxDataViewEvent le(type, parent->GetId());
2832
2833 le.SetEventObject(parent);
2834 le.SetModel(GetModel());
2835 le.SetItem( item );
2836
2837 return !parent->ProcessWindowEvent(le) || le.IsAllowed();
2838 }
2839
2840 bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const
2841 {
2842 if (IsList())
2843 return false;
2844
2845 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2846 if (!node)
2847 return false;
2848
2849 if (!node->HasChildren())
2850 return false;
2851
2852 return node->IsOpen();
2853 }
2854
2855 bool wxDataViewMainWindow::HasChildren( unsigned int row ) const
2856 {
2857 if (IsList())
2858 return false;
2859
2860 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2861 if (!node)
2862 return false;
2863
2864 if (!node->HasChildren())
2865 return false;
2866
2867 return true;
2868 }
2869
2870 void wxDataViewMainWindow::Expand( unsigned int row )
2871 {
2872 if (IsList())
2873 return;
2874
2875 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2876 if (!node)
2877 return;
2878
2879 if (!node->HasChildren())
2880 return;
2881
2882 if (!node->IsOpen())
2883 {
2884 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, node->GetItem()) )
2885 {
2886 // Vetoed by the event handler.
2887 return;
2888 }
2889
2890 node->ToggleOpen();
2891
2892 // build the children of current node
2893 if( node->GetChildNodes().empty() )
2894 {
2895 SortPrepare();
2896 ::BuildTreeHelper(GetModel(), node->GetItem(), node);
2897 }
2898
2899 // By expanding the node all row indices that are currently in the selection list
2900 // and are greater than our node have become invalid. So we have to correct that now.
2901 const unsigned rowAdjustment = node->GetSubTreeCount();
2902 for(unsigned i=0; i<m_selection.size(); ++i)
2903 {
2904 const unsigned testRow = m_selection[i];
2905 // all rows above us are not affected, so skip them
2906 if(testRow <= row)
2907 continue;
2908
2909 m_selection[i] += rowAdjustment;
2910 }
2911
2912 if(m_currentRow > row)
2913 ChangeCurrentRow(m_currentRow + rowAdjustment);
2914
2915 m_count = -1;
2916 UpdateDisplay();
2917 // Send the expanded event
2918 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
2919 }
2920 }
2921
2922 void wxDataViewMainWindow::Collapse(unsigned int row)
2923 {
2924 if (IsList())
2925 return;
2926
2927 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
2928 if (!node)
2929 return;
2930
2931 if (!node->HasChildren())
2932 return;
2933
2934 if (node->IsOpen())
2935 {
2936 if ( !SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem()) )
2937 {
2938 // Vetoed by the event handler.
2939 return;
2940 }
2941
2942 // Find out if there are selected items below the current node.
2943 bool selectCollapsingRow = false;
2944 const unsigned rowAdjustment = node->GetSubTreeCount();
2945 unsigned maxRowToBeTested = row + rowAdjustment;
2946 for(unsigned i=0; i<m_selection.size(); ++i)
2947 {
2948 const unsigned testRow = m_selection[i];
2949 if(testRow > row && testRow <= maxRowToBeTested)
2950 {
2951 selectCollapsingRow = true;
2952 // get out as soon as we have found a node that is selected
2953 break;
2954 }
2955 }
2956
2957 node->ToggleOpen();
2958
2959 // If the node to be closed has selected items the user won't see those any longer.
2960 // We select the collapsing node in this case.
2961 if(selectCollapsingRow)
2962 {
2963 SelectAllRows(false);
2964 ChangeCurrentRow(row);
2965 SelectRow(row, true);
2966 SendSelectionChangedEvent(GetItemByRow(row));
2967 }
2968 else
2969 {
2970 // if there were no selected items below our node we still need to "fix" the
2971 // selection list to adjust for the changing of the row indices.
2972 // We actually do the opposite of what we are doing in Expand().
2973 for(unsigned i=0; i<m_selection.size(); ++i)
2974 {
2975 const unsigned testRow = m_selection[i];
2976 // all rows above us are not affected, so skip them
2977 if(testRow <= row)
2978 continue;
2979
2980 m_selection[i] -= rowAdjustment;
2981 }
2982
2983 // if the "current row" is being collapsed away we change it to the current row ;-)
2984 if(m_currentRow > row && m_currentRow <= maxRowToBeTested)
2985 ChangeCurrentRow(row);
2986 else if(m_currentRow > row)
2987 ChangeCurrentRow(m_currentRow - rowAdjustment);
2988 }
2989
2990 m_count = -1;
2991 UpdateDisplay();
2992 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,node->GetItem());
2993 }
2994 }
2995
2996 wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
2997 {
2998 const wxDataViewModel * model = GetModel();
2999 if( model == NULL )
3000 return NULL;
3001
3002 if (!item.IsOk())
3003 return m_root;
3004
3005 // Compose the parent-chain for the item we are looking for
3006 wxVector<wxDataViewItem> parentChain;
3007 wxDataViewItem it( item );
3008 while( it.IsOk() )
3009 {
3010 parentChain.push_back(it);
3011 it = model->GetParent(it);
3012 }
3013
3014 // Find the item along the parent-chain.
3015 // This algorithm is designed to speed up the node-finding method
3016 wxDataViewTreeNode* node = m_root;
3017 for( unsigned iter = parentChain.size()-1; ; --iter )
3018 {
3019 if( node->HasChildren() )
3020 {
3021 if( node->GetChildNodes().empty() )
3022 {
3023 // Even though the item is a container, it doesn't have any
3024 // child nodes in the control's representation yet. We have
3025 // to realize its subtree now.
3026 SortPrepare();
3027 ::BuildTreeHelper(model, node->GetItem(), node);
3028 }
3029
3030 const wxDataViewTreeNodes& nodes = node->GetChildNodes();
3031 bool found = false;
3032
3033 for (unsigned i = 0; i < nodes.GetCount(); ++i)
3034 {
3035 wxDataViewTreeNode* currentNode = nodes[i];
3036 if (currentNode->GetItem() == parentChain[iter])
3037 {
3038 if (currentNode->GetItem() == item)
3039 return currentNode;
3040
3041 node = currentNode;
3042 found = true;
3043 break;
3044 }
3045 }
3046 if (!found)
3047 return NULL;
3048 }
3049 else
3050 return NULL;
3051
3052 if ( !iter )
3053 break;
3054 }
3055 return NULL;
3056 }
3057
3058 void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item,
3059 wxDataViewColumn* &column )
3060 {
3061 wxDataViewColumn *col = NULL;
3062 unsigned int cols = GetOwner()->GetColumnCount();
3063 unsigned int colnum = 0;
3064 int x, y;
3065 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
3066 for (unsigned x_start = 0; colnum < cols; colnum++)
3067 {
3068 col = GetOwner()->GetColumnAt(colnum);
3069 if (col->IsHidden())
3070 continue; // skip it!
3071
3072 unsigned int w = col->GetWidth();
3073 if (x_start+w >= (unsigned int)x)
3074 break;
3075
3076 x_start += w;
3077 }
3078
3079 column = col;
3080 item = GetItemByRow( GetLineAt( y ) );
3081 }
3082
3083 wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item,
3084 const wxDataViewColumn* column )
3085 {
3086 int xpos = 0;
3087 int width = 0;
3088
3089 unsigned int cols = GetOwner()->GetColumnCount();
3090 // If column is null the loop will compute the combined width of all columns.
3091 // Otherwise, it will compute the x position of the column we are looking for.
3092 for (unsigned int i = 0; i < cols; i++)
3093 {
3094 wxDataViewColumn* col = GetOwner()->GetColumnAt( i );
3095
3096 if (col == column)
3097 break;
3098
3099 if (col->IsHidden())
3100 continue; // skip it!
3101
3102 xpos += col->GetWidth();
3103 width += col->GetWidth();
3104 }
3105
3106 if(column != 0)
3107 {
3108 // If we have a column, we need can get its width directly.
3109 if(column->IsHidden())
3110 width = 0;
3111 else
3112 width = column->GetWidth();
3113
3114 }
3115 else
3116 {
3117 // If we have no column, we reset the x position back to zero.
3118 xpos = 0;
3119 }
3120
3121 // we have to take an expander column into account and compute its indentation
3122 // to get the correct x position where the actual text is
3123 int indent = 0;
3124 int row = GetRowByItem(item);
3125 if (!IsList() && (column == 0 || GetOwner()->GetExpanderColumn() == column) )
3126 {
3127 wxDataViewTreeNode* node = GetTreeNodeByRow(row);
3128 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
3129 indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander
3130 }
3131
3132 wxRect itemRect( xpos + indent,
3133 GetLineStart( row ),
3134 width - indent,
3135 GetLineHeight( row ) );
3136
3137 GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y,
3138 &itemRect.x, &itemRect.y );
3139
3140 return itemRect;
3141 }
3142
3143 int wxDataViewMainWindow::RecalculateCount()
3144 {
3145 if (IsVirtualList())
3146 {
3147 wxDataViewVirtualListModel *list_model =
3148 (wxDataViewVirtualListModel*) GetModel();
3149
3150 return list_model->GetCount();
3151 }
3152 else
3153 {
3154 return m_root->GetSubTreeCount();
3155 }
3156 }
3157
3158 class ItemToRowJob : public DoJob
3159 {
3160 public:
3161 ItemToRowJob(const wxDataViewItem& item_, wxVector<wxDataViewItem>::reverse_iterator iter)
3162 : m_iter(iter),
3163 item(item_)
3164 {
3165 ret = -1;
3166 }
3167
3168 // Maybe binary search will help to speed up this process
3169 virtual int operator() ( wxDataViewTreeNode * node)
3170 {
3171 ret ++;
3172 if( node->GetItem() == item )
3173 {
3174 return DoJob::DONE;
3175 }
3176
3177 if( node->GetItem() == *m_iter )
3178 {
3179 m_iter++;
3180 return DoJob::CONTINUE;
3181 }
3182 else
3183 {
3184 ret += node->GetSubTreeCount();
3185 return DoJob::SKIP_SUBTREE;
3186 }
3187
3188 }
3189
3190 // the row number is begin from zero
3191 int GetResult() const
3192 { return ret -1; }
3193
3194 private:
3195 wxVector<wxDataViewItem>::reverse_iterator m_iter;
3196 wxDataViewItem item;
3197 int ret;
3198
3199 };
3200
3201 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const
3202 {
3203 const wxDataViewModel * model = GetModel();
3204 if( model == NULL )
3205 return -1;
3206
3207 if (IsVirtualList())
3208 {
3209 return wxPtrToUInt( item.GetID() ) -1;
3210 }
3211 else
3212 {
3213 if( !item.IsOk() )
3214 return -1;
3215
3216 // Compose the parent-chain of the item we are looking for
3217 wxVector<wxDataViewItem> parentChain;
3218 wxDataViewItem it( item );
3219 while( it.IsOk() )
3220 {
3221 parentChain.push_back(it);
3222 it = model->GetParent(it);
3223 }
3224
3225 // add an 'invalid' item to represent our 'invisible' root node
3226 parentChain.push_back(wxDataViewItem());
3227
3228 // the parent chain was created by adding the deepest parent first.
3229 // so if we want to start at the root node, we have to iterate backwards through the vector
3230 ItemToRowJob job( item, parentChain.rbegin() );
3231 Walker( m_root, job );
3232 return job.GetResult();
3233 }
3234 }
3235
3236 static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
3237 wxDataViewTreeNode * node)
3238 {
3239 if( !model->IsContainer( item ) )
3240 return;
3241
3242 wxDataViewItemArray children;
3243 unsigned int num = model->GetChildren( item, children);
3244
3245 for ( unsigned int index = 0; index < num; index++ )
3246 {
3247 wxDataViewTreeNode *n = new wxDataViewTreeNode(node, children[index]);
3248
3249 if( model->IsContainer(children[index]) )
3250 n->SetHasChildren( true );
3251
3252 node->AddChild(n);
3253 }
3254
3255 wxASSERT( node->IsOpen() );
3256 node->ChangeSubTreeCount(+num);
3257 }
3258
3259 void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3260 {
3261 DestroyTree();
3262
3263 if (GetModel()->IsVirtualListModel())
3264 {
3265 m_count = -1;
3266 return;
3267 }
3268
3269 m_root = wxDataViewTreeNode::CreateRootNode();
3270
3271 // First we define a invalid item to fetch the top-level elements
3272 wxDataViewItem item;
3273 SortPrepare();
3274 BuildTreeHelper( model, item, m_root);
3275 m_count = -1;
3276 }
3277
3278 void wxDataViewMainWindow::DestroyTree()
3279 {
3280 if (!IsVirtualList())
3281 {
3282 wxDELETE(m_root);
3283 m_count = 0;
3284 }
3285 }
3286
3287 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3288 {
3289 wxWindow * const parent = GetParent();
3290
3291 // propagate the char event upwards
3292 wxKeyEvent eventForParent(event);
3293 eventForParent.SetEventObject(parent);
3294 if ( parent->ProcessWindowEvent(eventForParent) )
3295 return;
3296
3297 if ( parent->HandleAsNavigationKey(event) )
3298 return;
3299
3300 // no item -> nothing to do
3301 if (!HasCurrentRow())
3302 {
3303 event.Skip();
3304 return;
3305 }
3306
3307 // don't use m_linesPerPage directly as it might not be computed yet
3308 const int pageSize = GetCountPerPage();
3309 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
3310
3311 switch ( event.GetKeyCode() )
3312 {
3313 case WXK_RETURN:
3314 case WXK_SPACE:
3315 {
3316 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,
3317 parent->GetId());
3318 le.SetItem( GetItemByRow(m_currentRow) );
3319 le.SetEventObject(parent);
3320 le.SetModel(GetModel());
3321
3322 parent->GetEventHandler()->ProcessEvent(le);
3323 }
3324 break;
3325
3326 case WXK_UP:
3327 if ( m_currentRow > 0 )
3328 OnArrowChar( m_currentRow - 1, event );
3329 break;
3330
3331 case WXK_DOWN:
3332 if ( m_currentRow + 1 < GetRowCount() )
3333 OnArrowChar( m_currentRow + 1, event );
3334 break;
3335 // Add the process for tree expanding/collapsing
3336 case WXK_LEFT:
3337 {
3338 if (IsList())
3339 break;
3340
3341 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
3342 if (!node)
3343 break;
3344
3345 if (node->HasChildren() && node->IsOpen())
3346 {
3347 Collapse(m_currentRow);
3348 }
3349 else // if the node is already closed we move the selection to its parent
3350 {
3351 wxDataViewTreeNode *parent_node = node->GetParent();
3352
3353 if (parent_node)
3354 {
3355 int parent = GetRowByItem( parent_node->GetItem() );
3356 if ( parent >= 0 )
3357 {
3358 unsigned int row = m_currentRow;
3359 SelectRow( row, false);
3360 SelectRow( parent, true );
3361 ChangeCurrentRow( parent );
3362 GetOwner()->EnsureVisible( parent, -1 );
3363 SendSelectionChangedEvent( parent_node->GetItem() );
3364 }
3365 }
3366 }
3367 break;
3368 }
3369 case WXK_RIGHT:
3370 {
3371 if (!IsExpanded( m_currentRow ))
3372 Expand( m_currentRow );
3373 else
3374 {
3375 unsigned int row = m_currentRow;
3376 SelectRow( row, false );
3377 SelectRow( row + 1, true );
3378 ChangeCurrentRow( row + 1 );
3379 GetOwner()->EnsureVisible( row + 1, -1 );
3380 SendSelectionChangedEvent( GetItemByRow(row+1) );
3381 }
3382 break;
3383 }
3384 case WXK_END:
3385 {
3386 if (!IsEmpty())
3387 OnArrowChar( GetRowCount() - 1, event );
3388 break;
3389 }
3390 case WXK_HOME:
3391 if (!IsEmpty())
3392 OnArrowChar( 0, event );
3393 break;
3394
3395 case WXK_PAGEUP:
3396 {
3397 int steps = pageSize - 1;
3398 int index = m_currentRow - steps;
3399 if (index < 0)
3400 index = 0;
3401
3402 OnArrowChar( index, event );
3403 }
3404 break;
3405
3406 case WXK_PAGEDOWN:
3407 {
3408 int steps = pageSize - 1;
3409 unsigned int index = m_currentRow + steps;
3410 unsigned int count = GetRowCount();
3411 if ( index >= count )
3412 index = count - 1;
3413
3414 OnArrowChar( index, event );
3415 }
3416 break;
3417
3418 case WXK_F2:
3419 {
3420 if(m_selection.size() == 1)
3421 {
3422 // TODO: we need to revise that when we have a concept for a 'current column'
3423 GetOwner()->StartEditor(GetItemByRow(m_selection[0]), 0);
3424 }
3425 }
3426 break;
3427
3428 default:
3429 event.Skip();
3430 }
3431 }
3432
3433 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3434 {
3435 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3436 {
3437 // let the base handle mouse wheel events.
3438 event.Skip();
3439 return;
3440 }
3441
3442 // set the focus to ourself if any of the mouse buttons are pressed
3443 if(event.ButtonDown() && !HasFocus())
3444 SetFocus();
3445
3446 int x = event.GetX();
3447 int y = event.GetY();
3448 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
3449 wxDataViewColumn *col = NULL;
3450
3451 int xpos = 0;
3452 unsigned int cols = GetOwner()->GetColumnCount();
3453 unsigned int i;
3454 for (i = 0; i < cols; i++)
3455 {
3456 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
3457 if (c->IsHidden())
3458 continue; // skip it!
3459
3460 if (x < xpos + c->GetWidth())
3461 {
3462 col = c;
3463 break;
3464 }
3465 xpos += c->GetWidth();
3466 }
3467 if (!col)
3468 {
3469 event.Skip();
3470 return;
3471 }
3472
3473 wxDataViewRenderer *cell = col->GetRenderer();
3474 unsigned int current = GetLineAt( y );
3475 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
3476 {
3477 // Unselect all if below the last row ?
3478 event.Skip();
3479 return;
3480 }
3481
3482 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3483 // button) and also determine the offset of the real cell start, skipping
3484 // the indentation and the expander itself.
3485 bool hoverOverExpander = false;
3486 int expanderOffset = 0;
3487 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col))
3488 {
3489 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3490 if( node!=NULL && node->HasChildren() )
3491 {
3492 int indent = node->GetIndentLevel();
3493 indent = GetOwner()->GetIndent()*indent;
3494
3495 // we make the rectangle we are looking in a bit bigger than the actual
3496 // visual expander so the user can hit that little thing reliably
3497 wxRect rect( xpos + indent,
3498 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
3499 m_lineHeight, m_lineHeight);
3500
3501 expanderOffset = indent + m_lineHeight;
3502
3503 if( rect.Contains(x, y) )
3504 {
3505 // So the mouse is over the expander
3506 hoverOverExpander = true;
3507 if (m_underMouse && m_underMouse != node)
3508 {
3509 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3510 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
3511 }
3512 if (m_underMouse != node)
3513 {
3514 // wxLogMessage("Do the row: %d", current);
3515 RefreshRow(current);
3516 }
3517 m_underMouse = node;
3518 }
3519 }
3520 }
3521 if (!hoverOverExpander)
3522 {
3523 if (m_underMouse != NULL)
3524 {
3525 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3526 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
3527 m_underMouse = NULL;
3528 }
3529 }
3530
3531 wxDataViewModel *model = GetModel();
3532
3533 #if wxUSE_DRAG_AND_DROP
3534 if (event.Dragging())
3535 {
3536 if (m_dragCount == 0)
3537 {
3538 // we have to report the raw, physical coords as we want to be
3539 // able to call HitTest(event.m_pointDrag) from the user code to
3540 // get the item being dragged
3541 m_dragStart = event.GetPosition();
3542 }
3543
3544 m_dragCount++;
3545
3546 if (m_dragCount != 3)
3547 return;
3548
3549 if (event.LeftIsDown())
3550 {
3551 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
3552 &m_dragStart.x, &m_dragStart.y );
3553 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
3554 wxDataViewItem item = GetItemByRow( drag_item_row );
3555
3556 // Notify cell about drag
3557 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3558 event.SetEventObject( m_owner );
3559 event.SetItem( item );
3560 event.SetModel( model );
3561 if (!m_owner->HandleWindowEvent( event ))
3562 return;
3563
3564 if (!event.IsAllowed())
3565 return;
3566
3567 wxDataObject *obj = event.GetDataObject();
3568 if (!obj)
3569 return;
3570
3571 wxDataViewDropSource drag( this, drag_item_row );
3572 drag.SetData( *obj );
3573 /* wxDragResult res = */ drag.DoDragDrop();
3574 delete obj;
3575 }
3576 return;
3577 }
3578 else
3579 {
3580 m_dragCount = 0;
3581 }
3582 #endif // wxUSE_DRAG_AND_DROP
3583
3584 bool simulateClick = false;
3585
3586 if (event.ButtonDClick())
3587 {
3588 m_renameTimer->Stop();
3589 m_lastOnSame = false;
3590 }
3591
3592 wxDataViewItem item = GetItemByRow(current);
3593 bool ignore_other_columns =
3594 ((GetOwner()->GetExpanderColumn() != col) &&
3595 (model->IsContainer(item)) &&
3596 (!model->HasContainerColumns(item)));
3597
3598 if (event.LeftDClick())
3599 {
3600 if(hoverOverExpander)
3601 {
3602 // a double click on the expander will be converted into a "simulated" normal click
3603 simulateClick = true;
3604 }
3605 else if ( current == m_lineLastClicked )
3606 {
3607 if ((!ignore_other_columns) && (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE))
3608 {
3609 const unsigned colIdx = col->GetModelColumn();
3610
3611 cell->PrepareForItem(model, item, colIdx);
3612
3613 wxRect cell_rect( xpos, GetLineStart( current ),
3614 col->GetWidth(), GetLineHeight( current ) );
3615 cell->WXOnActivate( cell_rect, model, item, colIdx );
3616 }
3617 else
3618 {
3619 wxWindow *parent = GetParent();
3620 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
3621 le.SetItem( item );
3622 le.SetColumn( col->GetModelColumn() );
3623 le.SetDataViewColumn( col );
3624 le.SetEventObject(parent);
3625 le.SetModel(GetModel());
3626
3627 parent->GetEventHandler()->ProcessEvent(le);
3628 }
3629 return;
3630 }
3631 else
3632 {
3633 // The first click was on another item, so don't interpret this as
3634 // a double click, but as a simple click instead
3635 simulateClick = true;
3636 }
3637 }
3638
3639 if (event.LeftUp() && !hoverOverExpander)
3640 {
3641 if (m_lineSelectSingleOnUp != (unsigned int)-1)
3642 {
3643 // select single line
3644 SelectAllRows( false );
3645 SelectRow( m_lineSelectSingleOnUp, true );
3646 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
3647 }
3648
3649 // If the user click the expander, we do not do editing even if the column
3650 // with expander are editable
3651 if (m_lastOnSame && !ignore_other_columns)
3652 {
3653 if ((col == m_currentCol) && (current == m_currentRow) &&
3654 (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) )
3655 {
3656 m_renameTimer->Start( 100, true );
3657 }
3658 }
3659
3660 m_lastOnSame = false;
3661 m_lineSelectSingleOnUp = (unsigned int)-1;
3662 }
3663 else if(!event.LeftUp())
3664 {
3665 // This is necessary, because after a DnD operation in
3666 // from and to ourself, the up event is swallowed by the
3667 // DnD code. So on next non-up event (which means here and
3668 // now) m_lineSelectSingleOnUp should be reset.
3669 m_lineSelectSingleOnUp = (unsigned int)-1;
3670 }
3671
3672 if (event.RightDown())
3673 {
3674 m_lineBeforeLastClicked = m_lineLastClicked;
3675 m_lineLastClicked = current;
3676
3677 // If the item is already selected, do not update the selection.
3678 // Multi-selections should not be cleared if a selected item is clicked.
3679 if (!IsRowSelected(current))
3680 {
3681 SelectAllRows(false);
3682 const unsigned oldCurrent = m_currentRow;
3683 ChangeCurrentRow(current);
3684 SelectRow(m_currentRow,true);
3685 RefreshRow(oldCurrent);
3686 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
3687 }
3688 }
3689 else if (event.RightUp())
3690 {
3691 wxVariant value;
3692 model->GetValue( value, item, col->GetModelColumn() );
3693 wxWindow *parent = GetParent();
3694 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
3695 le.SetItem( item );
3696 le.SetColumn( col->GetModelColumn() );
3697 le.SetDataViewColumn( col );
3698 le.SetEventObject(parent);
3699 le.SetModel(GetModel());
3700 le.SetValue(value);
3701 parent->GetEventHandler()->ProcessEvent(le);
3702 }
3703 else if (event.MiddleDown())
3704 {
3705 }
3706
3707 if((event.LeftDown() || simulateClick) && hoverOverExpander)
3708 {
3709 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
3710
3711 // hoverOverExpander being true tells us that our node must be
3712 // valid and have children.
3713 // So we don't need any extra checks.
3714 if( node->IsOpen() )
3715 Collapse(current);
3716 else
3717 Expand(current);
3718 }
3719 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
3720 {
3721 m_lineBeforeLastClicked = m_lineLastClicked;
3722 m_lineLastClicked = current;
3723
3724 unsigned int oldCurrentRow = m_currentRow;
3725 bool oldWasSelected = IsRowSelected(m_currentRow);
3726
3727 bool cmdModifierDown = event.CmdDown();
3728 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3729 {
3730 if ( IsSingleSel() || !IsRowSelected(current) )
3731 {
3732 SelectAllRows( false );
3733 ChangeCurrentRow(current);
3734 SelectRow(m_currentRow,true);
3735 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
3736 }
3737 else // multi sel & current is highlighted & no mod keys
3738 {
3739 m_lineSelectSingleOnUp = current;
3740 ChangeCurrentRow(current); // change focus
3741 }
3742 }
3743 else // multi sel & either ctrl or shift is down
3744 {
3745 if (cmdModifierDown)
3746 {
3747 ChangeCurrentRow(current);
3748 ReverseRowSelection(m_currentRow);
3749 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
3750 }
3751 else if (event.ShiftDown())
3752 {
3753 ChangeCurrentRow(current);
3754
3755 unsigned int lineFrom = oldCurrentRow,
3756 lineTo = current;
3757
3758 if ( lineTo < lineFrom )
3759 {
3760 lineTo = lineFrom;
3761 lineFrom = m_currentRow;
3762 }
3763
3764 SelectRows(lineFrom, lineTo, true);
3765 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
3766 }
3767 else // !ctrl, !shift
3768 {
3769 // test in the enclosing if should make it impossible
3770 wxFAIL_MSG( wxT("how did we get here?") );
3771 }
3772 }
3773
3774 if (m_currentRow != oldCurrentRow)
3775 RefreshRow( oldCurrentRow );
3776
3777 wxDataViewColumn *oldCurrentCol = m_currentCol;
3778
3779 // Update selection here...
3780 m_currentCol = col;
3781
3782 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
3783 (current == oldCurrentRow)) && oldWasSelected;
3784
3785 // Call LeftClick after everything else as under GTK+
3786 if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
3787 {
3788 // notify cell about click
3789 cell->PrepareForItem(model, item, col->GetModelColumn());
3790
3791 wxRect cell_rect( xpos + expanderOffset,
3792 GetLineStart( current ),
3793 col->GetWidth() - expanderOffset,
3794 GetLineHeight( current ) );
3795
3796 // Report position relative to the cell's custom area, i.e.
3797 // no the entire space as given by the control but the one
3798 // used by the renderer after calculation of alignment etc.
3799
3800 // adjust the rectangle ourselves to account for the alignment
3801 wxRect rectItem = cell_rect;
3802 const int align = cell->GetAlignment();
3803 if ( align != wxDVR_DEFAULT_ALIGNMENT )
3804 {
3805 const wxSize size = cell->GetSize();
3806
3807 if ( size.x >= 0 && size.x < cell_rect.width )
3808 {
3809 if ( align & wxALIGN_CENTER_HORIZONTAL )
3810 rectItem.x += (cell_rect.width - size.x)/2;
3811 else if ( align & wxALIGN_RIGHT )
3812 rectItem.x += cell_rect.width - size.x;
3813 // else: wxALIGN_LEFT is the default
3814 }
3815
3816 if ( size.y >= 0 && size.y < cell_rect.height )
3817 {
3818 if ( align & wxALIGN_CENTER_VERTICAL )
3819 rectItem.y += (cell_rect.height - size.y)/2;
3820 else if ( align & wxALIGN_BOTTOM )
3821 rectItem.y += cell_rect.height - size.y;
3822 // else: wxALIGN_TOP is the default
3823 }
3824 }
3825
3826 wxPoint pos( event.GetPosition() );
3827 pos.x -= rectItem.x;
3828 pos.y -= rectItem.y;
3829
3830 m_owner->CalcUnscrolledPosition( pos.x, pos.y, &pos.x, &pos.y );
3831
3832 /* ignore ret */ cell->WXOnLeftClick( pos, cell_rect,
3833 model, item, col->GetModelColumn());
3834 }
3835 }
3836 }
3837
3838 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3839 {
3840 m_hasFocus = true;
3841
3842 if (HasCurrentRow())
3843 Refresh();
3844
3845 event.Skip();
3846 }
3847
3848 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3849 {
3850 m_hasFocus = false;
3851
3852 if (HasCurrentRow())
3853 Refresh();
3854
3855 event.Skip();
3856 }
3857
3858 //-----------------------------------------------------------------------------
3859 // wxDataViewCtrl
3860 //-----------------------------------------------------------------------------
3861
3862 WX_DEFINE_LIST(wxDataViewColumnList)
3863
3864 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3865 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3866 EVT_SIZE(wxDataViewCtrl::OnSize)
3867 END_EVENT_TABLE()
3868
3869 wxDataViewCtrl::~wxDataViewCtrl()
3870 {
3871 if (m_notifier)
3872 GetModel()->RemoveNotifier( m_notifier );
3873
3874 m_cols.Clear();
3875 m_colsBestWidths.clear();
3876 }
3877
3878 void wxDataViewCtrl::Init()
3879 {
3880 m_cols.DeleteContents(true);
3881 m_notifier = NULL;
3882
3883 // No sorting column at start
3884 m_sortingColumnIdx = wxNOT_FOUND;
3885
3886 m_headerArea = NULL;
3887 }
3888
3889 bool wxDataViewCtrl::Create(wxWindow *parent,
3890 wxWindowID id,
3891 const wxPoint& pos,
3892 const wxSize& size,
3893 long style,
3894 const wxValidator& validator,
3895 const wxString& name)
3896 {
3897 // if ( (style & wxBORDER_MASK) == 0)
3898 // style |= wxBORDER_SUNKEN;
3899
3900 Init();
3901
3902 if (!wxControl::Create( parent, id, pos, size,
3903 style | wxScrolledWindowStyle, validator, name))
3904 return false;
3905
3906 SetInitialSize(size);
3907
3908 #ifdef __WXMAC__
3909 MacSetClipChildren( true );
3910 #endif
3911
3912 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
3913
3914 // We use the cursor keys for moving the selection, not scrolling, so call
3915 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3916 // keyboard events forwarded to us from wxListMainWindow.
3917 DisableKeyboardScrolling();
3918
3919 if (HasFlag(wxDV_NO_HEADER))
3920 m_headerArea = NULL;
3921 else
3922 m_headerArea = new wxDataViewHeaderWindow(this);
3923
3924 SetTargetWindow( m_clientArea );
3925
3926 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
3927 if (m_headerArea)
3928 sizer->Add( m_headerArea, 0, wxGROW );
3929 sizer->Add( m_clientArea, 1, wxGROW );
3930 SetSizer( sizer );
3931
3932 return true;
3933 }
3934
3935 wxBorder wxDataViewCtrl::GetDefaultBorder() const
3936 {
3937 return wxBORDER_THEME;
3938 }
3939
3940 #ifdef __WXMSW__
3941 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
3942 WXWPARAM wParam,
3943 WXLPARAM lParam)
3944 {
3945 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
3946
3947 #ifndef __WXWINCE__
3948 // we need to process arrows ourselves for scrolling
3949 if ( nMsg == WM_GETDLGCODE )
3950 {
3951 rc |= DLGC_WANTARROWS;
3952 }
3953 #endif
3954
3955 return rc;
3956 }
3957 #endif
3958
3959 wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
3960 {
3961 wxSize newsize = size;
3962 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
3963 newsize.y -= m_headerArea->GetSize().y;
3964
3965 return newsize;
3966 }
3967
3968 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
3969 {
3970 // We need to override OnSize so that our scrolled
3971 // window a) does call Layout() to use sizers for
3972 // positioning the controls but b) does not query
3973 // the sizer for their size and use that for setting
3974 // the scrollable area as set that ourselves by
3975 // calling SetScrollbar() further down.
3976
3977 Layout();
3978
3979 AdjustScrollbars();
3980
3981 // We must redraw the headers if their height changed. Normally this
3982 // shouldn't happen as the control shouldn't let itself be resized beneath
3983 // its minimal height but avoid the display artefacts that appear if it
3984 // does happen, e.g. because there is really not enough vertical space.
3985 if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea &&
3986 m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y )
3987 {
3988 m_headerArea->Refresh();
3989 }
3990 }
3991
3992 void wxDataViewCtrl::SetFocus()
3993 {
3994 if (m_clientArea)
3995 m_clientArea->SetFocus();
3996 }
3997
3998 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
3999 {
4000 if (!wxDataViewCtrlBase::AssociateModel( model ))
4001 return false;
4002
4003 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4004
4005 model->AddNotifier( m_notifier );
4006
4007 m_clientArea->DestroyTree();
4008
4009 m_clientArea->BuildTree(model);
4010
4011 m_clientArea->UpdateDisplay();
4012
4013 return true;
4014 }
4015
4016 #if wxUSE_DRAG_AND_DROP
4017
4018 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4019 {
4020 return m_clientArea->EnableDragSource( format );
4021 }
4022
4023 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4024 {
4025 return m_clientArea->EnableDropTarget( format );
4026 }
4027
4028 #endif // wxUSE_DRAG_AND_DROP
4029
4030 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4031 {
4032 if (!wxDataViewCtrlBase::AppendColumn(col))
4033 return false;
4034
4035 m_cols.Append( col );
4036 m_colsBestWidths.push_back(0);
4037 OnColumnsCountChanged();
4038 return true;
4039 }
4040
4041 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4042 {
4043 if (!wxDataViewCtrlBase::PrependColumn(col))
4044 return false;
4045
4046 m_cols.Insert( col );
4047 m_colsBestWidths.insert(m_colsBestWidths.begin(), 0);
4048 OnColumnsCountChanged();
4049 return true;
4050 }
4051
4052 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4053 {
4054 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4055 return false;
4056
4057 m_cols.Insert( pos, col );
4058 m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, 0);
4059 OnColumnsCountChanged();
4060 return true;
4061 }
4062
4063 void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4064 {
4065 if ( m_headerArea )
4066 m_headerArea->UpdateColumn(idx);
4067
4068 m_clientArea->UpdateDisplay();
4069 }
4070
4071 void wxDataViewCtrl::OnColumnsCountChanged()
4072 {
4073 if (m_headerArea)
4074 m_headerArea->SetColumnCount(GetColumnCount());
4075
4076 m_clientArea->UpdateDisplay();
4077 }
4078
4079 void wxDataViewCtrl::DoSetExpanderColumn()
4080 {
4081 m_clientArea->UpdateDisplay();
4082 }
4083
4084 void wxDataViewCtrl::DoSetIndent()
4085 {
4086 m_clientArea->UpdateDisplay();
4087 }
4088
4089 unsigned int wxDataViewCtrl::GetColumnCount() const
4090 {
4091 return m_cols.GetCount();
4092 }
4093
4094 bool wxDataViewCtrl::SetRowHeight( int lineHeight )
4095 {
4096 if ( !m_clientArea )
4097 return false;
4098
4099 m_clientArea->SetRowHeight(lineHeight);
4100
4101 return true;
4102 }
4103
4104 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
4105 {
4106 return m_cols[idx];
4107 }
4108
4109 wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
4110 {
4111 // columns can't be reordered if there is no header window which allows
4112 // to do this
4113 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
4114 : pos;
4115
4116 return GetColumn(idx);
4117 }
4118
4119 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4120 {
4121 const unsigned count = m_cols.size();
4122 for ( unsigned n = 0; n < count; n++ )
4123 {
4124 if ( m_cols[n] == column )
4125 return n;
4126 }
4127
4128 return wxNOT_FOUND;
4129 }
4130
4131 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const
4132 {
4133 if ( m_colsBestWidths[idx] != 0 )
4134 return m_colsBestWidths[idx];
4135
4136 const int count = m_clientArea->GetRowCount();
4137 wxDataViewColumn *column = GetColumn(idx);
4138 wxDataViewRenderer *renderer =
4139 const_cast<wxDataViewRenderer*>(column->GetRenderer());
4140
4141 class MaxWidthCalculator
4142 {
4143 public:
4144 MaxWidthCalculator(wxDataViewMainWindow *clientArea,
4145 wxDataViewRenderer *renderer,
4146 const wxDataViewModel *model,
4147 unsigned column)
4148 : m_width(0),
4149 m_clientArea(clientArea),
4150 m_renderer(renderer),
4151 m_model(model),
4152 m_column(column)
4153 {
4154 }
4155
4156 void UpdateWithWidth(int width)
4157 {
4158 m_width = wxMax(m_width, width);
4159 }
4160
4161 void UpdateWithRow(int row)
4162 {
4163 wxDataViewItem item = m_clientArea->GetItemByRow(row);
4164 m_renderer->PrepareForItem(m_model, item, m_column);
4165 m_width = wxMax(m_width, m_renderer->GetSize().x);
4166 }
4167
4168 int GetMaxWidth() const { return m_width; }
4169
4170 private:
4171 int m_width;
4172 wxDataViewMainWindow *m_clientArea;
4173 wxDataViewRenderer *m_renderer;
4174 const wxDataViewModel *m_model;
4175 unsigned m_column;
4176 };
4177
4178 MaxWidthCalculator calculator(m_clientArea, renderer,
4179 GetModel(), column->GetModelColumn());
4180
4181 if ( m_headerArea )
4182 {
4183 int header_width = m_headerArea->GetTextExtent(column->GetTitle()).x;
4184 // Labels on native MSW header are indented on both sides
4185 header_width +=
4186 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea);
4187 calculator.UpdateWithWidth(header_width);
4188 }
4189
4190 // The code below deserves some explanation. For very large controls, we
4191 // simply can't afford to calculate sizes for all items, it takes too
4192 // long. So the best we can do is to check the first and the last N/2
4193 // items in the control for some sufficiently large N and calculate best
4194 // sizes from that. That can result in the calculated best width being too
4195 // small for some outliers, but it's better to get slightly imperfect
4196 // result than to wait several seconds after every update. To avoid highly
4197 // visible miscalculations, we also include all currently visible items
4198 // no matter what. Finally, the value of N is determined dynamically by
4199 // measuring how much time we spent on the determining item widths so far.
4200
4201 #if wxUSE_STOPWATCH
4202 int top_part_end = count;
4203 static const long CALC_TIMEOUT = 20/*ms*/;
4204 // don't call wxStopWatch::Time() too often
4205 static const unsigned CALC_CHECK_FREQ = 100;
4206 wxStopWatch timer;
4207 #else
4208 // use some hard-coded limit, that's the best we can do without timer
4209 int top_part_end = wxMin(500, count);
4210 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4211
4212 int row = 0;
4213
4214 for ( row = 0; row < top_part_end; row++ )
4215 {
4216 #if wxUSE_STOPWATCH
4217 if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 &&
4218 timer.Time() > CALC_TIMEOUT )
4219 break;
4220 #endif // wxUSE_STOPWATCH
4221 calculator.UpdateWithRow(row);
4222 }
4223
4224 // row is the first unmeasured item now; that's our value of N/2
4225
4226 if ( row < count )
4227 {
4228 top_part_end = row;
4229
4230 // add bottom N/2 items now:
4231 const int bottom_part_start = wxMax(row, count - row);
4232 for ( row = bottom_part_start; row < count; row++ )
4233 {
4234 calculator.UpdateWithRow(row);
4235 }
4236
4237 // finally, include currently visible items in the calculation:
4238 const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0));
4239 int first_visible = m_clientArea->GetLineAt(origin.y);
4240 int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y);
4241
4242 first_visible = wxMax(first_visible, top_part_end);
4243 last_visible = wxMin(bottom_part_start, last_visible);
4244
4245 for ( row = first_visible; row < last_visible; row++ )
4246 {
4247 calculator.UpdateWithRow(row);
4248 }
4249
4250 wxLogTrace("dataview",
4251 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4252 top_part_end,
4253 count - bottom_part_start,
4254 wxMax(0, last_visible - first_visible),
4255 count);
4256 }
4257
4258 int max_width = calculator.GetMaxWidth();
4259 if ( max_width > 0 )
4260 max_width += 2 * PADDING_RIGHTLEFT;
4261
4262 const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx] = max_width;
4263 return max_width;
4264 }
4265
4266 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
4267 unsigned int WXUNUSED(new_pos))
4268 {
4269 // do _not_ reorder m_cols elements here, they should always be in the
4270 // order in which columns were added, we only display the columns in
4271 // different order
4272 m_clientArea->UpdateDisplay();
4273 }
4274
4275 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4276 {
4277 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4278 if (!ret)
4279 return false;
4280
4281 m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column));
4282 m_cols.Erase(ret);
4283 OnColumnsCountChanged();
4284
4285 return true;
4286 }
4287
4288 bool wxDataViewCtrl::ClearColumns()
4289 {
4290 m_cols.Clear();
4291 m_colsBestWidths.clear();
4292 OnColumnsCountChanged();
4293 return true;
4294 }
4295
4296 void wxDataViewCtrl::UpdateColBestWidth(int idx)
4297 {
4298 m_colsBestWidths[idx] = 0;
4299
4300 if ( m_headerArea )
4301 m_headerArea->UpdateColumn(idx);
4302 }
4303
4304 void wxDataViewCtrl::UpdateColBestWidths()
4305 {
4306 m_colsBestWidths.clear();
4307 m_colsBestWidths.resize(m_cols.size());
4308
4309 if ( m_headerArea )
4310 {
4311 const unsigned cols = m_headerArea->GetColumnCount();
4312 for ( unsigned i = 0; i < cols; i++ )
4313 m_headerArea->UpdateColumn(i);
4314 }
4315 }
4316
4317 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4318 {
4319 #if 1
4320 unsigned int len = GetColumnCount();
4321 for ( unsigned int i = 0; i < len; i++ )
4322 {
4323 wxDataViewColumn * col = GetColumnAt(i);
4324 if (column==col)
4325 return i;
4326 }
4327
4328 return wxNOT_FOUND;
4329 #else
4330 // This returns the position in pixels which is not what we want.
4331 int ret = 0,
4332 dummy = 0;
4333 unsigned int len = GetColumnCount();
4334 for ( unsigned int i = 0; i < len; i++ )
4335 {
4336 wxDataViewColumn * col = GetColumnAt(i);
4337 if (col->IsHidden())
4338 continue;
4339 ret += col->GetWidth();
4340 if (column==col)
4341 {
4342 CalcScrolledPosition( ret, dummy, &ret, &dummy );
4343 break;
4344 }
4345 }
4346 return ret;
4347 #endif
4348 }
4349
4350 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4351 {
4352 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
4353 : GetColumn(m_sortingColumnIdx);
4354 }
4355
4356 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4357 {
4358 return GetItemByRow(m_clientArea->GetCurrentRow());
4359 }
4360
4361 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4362 {
4363 const int row = m_clientArea->GetRowByItem(item);
4364
4365 const unsigned oldCurrent = m_clientArea->GetCurrentRow();
4366 if ( static_cast<unsigned>(row) != oldCurrent )
4367 {
4368 m_clientArea->ChangeCurrentRow(row);
4369 m_clientArea->RefreshRow(oldCurrent);
4370 m_clientArea->RefreshRow(row);
4371 }
4372 }
4373
4374 int wxDataViewCtrl::GetSelectedItemsCount() const
4375 {
4376 return m_clientArea->GetSelections().size();
4377 }
4378
4379 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4380 {
4381 sel.Empty();
4382 const wxDataViewSelection& selections = m_clientArea->GetSelections();
4383
4384 const size_t len = selections.size();
4385 for ( size_t i = 0; i < len; i++ )
4386 {
4387 wxDataViewItem item = m_clientArea->GetItemByRow(selections[i]);
4388 if ( item.IsOk() )
4389 {
4390 sel.Add(item);
4391 }
4392 else
4393 {
4394 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4395 }
4396 }
4397
4398 return sel.size();
4399 }
4400
4401 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4402 {
4403 wxDataViewSelection selection(wxDataViewSelectionCmp);
4404
4405 wxDataViewItem last_parent;
4406
4407 int len = sel.GetCount();
4408 for( int i = 0; i < len; i ++ )
4409 {
4410 wxDataViewItem item = sel[i];
4411 wxDataViewItem parent = GetModel()->GetParent( item );
4412 if (parent)
4413 {
4414 if (parent != last_parent)
4415 ExpandAncestors(item);
4416 }
4417
4418 last_parent = parent;
4419 int row = m_clientArea->GetRowByItem( item );
4420 if( row >= 0 )
4421 selection.Add( static_cast<unsigned int>(row) );
4422 }
4423
4424 m_clientArea->SetSelections( selection );
4425 }
4426
4427 void wxDataViewCtrl::Select( const wxDataViewItem & item )
4428 {
4429 ExpandAncestors( item );
4430
4431 int row = m_clientArea->GetRowByItem( item );
4432 if( row >= 0 )
4433 {
4434 // Unselect all rows before select another in the single select mode
4435 if (m_clientArea->IsSingleSel())
4436 m_clientArea->SelectAllRows(false);
4437
4438 m_clientArea->SelectRow(row, true);
4439
4440 // Also set focus to the selected item
4441 m_clientArea->ChangeCurrentRow( row );
4442 }
4443 }
4444
4445 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4446 {
4447 int row = m_clientArea->GetRowByItem( item );
4448 if( row >= 0 )
4449 m_clientArea->SelectRow(row, false);
4450 }
4451
4452 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4453 {
4454 int row = m_clientArea->GetRowByItem( item );
4455 if( row >= 0 )
4456 {
4457 return m_clientArea->IsRowSelected(row);
4458 }
4459 return false;
4460 }
4461
4462 void wxDataViewCtrl::SelectAll()
4463 {
4464 m_clientArea->SelectAllRows(true);
4465 }
4466
4467 void wxDataViewCtrl::UnselectAll()
4468 {
4469 m_clientArea->SelectAllRows(false);
4470 }
4471
4472 void wxDataViewCtrl::EnsureVisible( int row, int column )
4473 {
4474 if( row < 0 )
4475 row = 0;
4476 if( row > (int) m_clientArea->GetRowCount() )
4477 row = m_clientArea->GetRowCount();
4478
4479 int first = m_clientArea->GetFirstVisibleRow();
4480 int last = m_clientArea->GetLastVisibleRow();
4481 if( row < first )
4482 m_clientArea->ScrollTo( row, column );
4483 else if( row > last )
4484 m_clientArea->ScrollTo( row - last + first, column );
4485 else
4486 m_clientArea->ScrollTo( first, column );
4487 }
4488
4489 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
4490 {
4491 ExpandAncestors( item );
4492
4493 m_clientArea->RecalculateDisplay();
4494
4495 int row = m_clientArea->GetRowByItem(item);
4496 if( row >= 0 )
4497 {
4498 if( column == NULL )
4499 EnsureVisible(row, -1);
4500 else
4501 EnsureVisible( row, GetColumnIndex(column) );
4502 }
4503
4504 }
4505
4506 void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
4507 wxDataViewColumn* &column ) const
4508 {
4509 m_clientArea->HitTest(point, item, column);
4510 }
4511
4512 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
4513 const wxDataViewColumn* column ) const
4514 {
4515 return m_clientArea->GetItemRect(item, column);
4516 }
4517
4518 wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
4519 {
4520 return m_clientArea->GetItemByRow( row );
4521 }
4522
4523 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
4524 {
4525 return m_clientArea->GetRowByItem( item );
4526 }
4527
4528 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4529 {
4530 ExpandAncestors( item );
4531
4532 int row = m_clientArea->GetRowByItem( item );
4533 if (row != -1)
4534 m_clientArea->Expand(row);
4535 }
4536
4537 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4538 {
4539 int row = m_clientArea->GetRowByItem( item );
4540 if (row != -1)
4541 m_clientArea->Collapse(row);
4542 }
4543
4544 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4545 {
4546 int row = m_clientArea->GetRowByItem( item );
4547 if (row != -1)
4548 return m_clientArea->IsExpanded(row);
4549 return false;
4550 }
4551
4552 void wxDataViewCtrl::StartEditor( const wxDataViewItem & item, unsigned int column )
4553 {
4554 wxDataViewColumn* col = GetColumn( column );
4555 if (!col)
4556 return;
4557
4558 wxRect itemRect = GetItemRect(item, col);
4559 wxDataViewRenderer* renderer = col->GetRenderer();
4560 if (renderer->GetMode() == wxDATAVIEW_CELL_EDITABLE)
4561 renderer->StartEditing(item, itemRect);
4562 }
4563
4564 #endif // !wxUSE_GENERICDATAVIEWCTRL
4565
4566 #endif // wxUSE_DATAVIEWCTRL