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