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