]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
Preserve client data pointers when setting bitmaps in wxBitmapComboBox.
[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 if (!col)
4014 {
4015 event.Skip();
4016 return;
4017 }
4018
4019 wxDataViewRenderer *cell = col->GetRenderer();
4020 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
4021 {
4022 // Unselect all if below the last row ?
4023 event.Skip();
4024 return;
4025 }
4026
4027 wxDataViewColumn* const
4028 expander = GetExpanderColumnOrFirstOne(GetOwner());
4029
4030 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
4031 // button) and also determine the offset of the real cell start, skipping
4032 // the indentation and the expander itself.
4033 bool hoverOverExpander = false;
4034 int itemOffset = 0;
4035 if ((!IsList()) && (expander == col))
4036 {
4037 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
4038
4039 int indent = node->GetIndentLevel();
4040 itemOffset = GetOwner()->GetIndent()*indent;
4041
4042 if ( node->HasChildren() )
4043 {
4044 // we make the rectangle we are looking in a bit bigger than the actual
4045 // visual expander so the user can hit that little thing reliably
4046 wxRect rect(itemOffset,
4047 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
4048 m_lineHeight, m_lineHeight);
4049
4050 if( rect.Contains(x, y) )
4051 {
4052 // So the mouse is over the expander
4053 hoverOverExpander = true;
4054 if (m_underMouse && m_underMouse != node)
4055 {
4056 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4057 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
4058 }
4059 if (m_underMouse != node)
4060 {
4061 // wxLogMessage("Do the row: %d", current);
4062 RefreshRow(current);
4063 }
4064 m_underMouse = node;
4065 }
4066 }
4067
4068 // Account for the expander as well, even if this item doesn't have it,
4069 // its parent does so it still counts for the offset.
4070 itemOffset += m_lineHeight;
4071 }
4072 if (!hoverOverExpander)
4073 {
4074 if (m_underMouse != NULL)
4075 {
4076 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
4077 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
4078 m_underMouse = NULL;
4079 }
4080 }
4081
4082 #if wxUSE_DRAG_AND_DROP
4083 if (event.Dragging())
4084 {
4085 if (m_dragCount == 0)
4086 {
4087 // we have to report the raw, physical coords as we want to be
4088 // able to call HitTest(event.m_pointDrag) from the user code to
4089 // get the item being dragged
4090 m_dragStart = event.GetPosition();
4091 }
4092
4093 m_dragCount++;
4094
4095 if (m_dragCount != 3)
4096 return;
4097
4098 if (event.LeftIsDown())
4099 {
4100 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
4101 &m_dragStart.x, &m_dragStart.y );
4102 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
4103 wxDataViewItem itemDragged = GetItemByRow( drag_item_row );
4104
4105 // Notify cell about drag
4106 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
4107 event.SetEventObject( m_owner );
4108 event.SetItem( itemDragged );
4109 event.SetModel( model );
4110 if (!m_owner->HandleWindowEvent( event ))
4111 return;
4112
4113 if (!event.IsAllowed())
4114 return;
4115
4116 wxDataObject *obj = event.GetDataObject();
4117 if (!obj)
4118 return;
4119
4120 wxDataViewDropSource drag( this, drag_item_row );
4121 drag.SetData( *obj );
4122 /* wxDragResult res = */ drag.DoDragDrop(event.GetDragFlags());
4123 delete obj;
4124 }
4125 return;
4126 }
4127 else
4128 {
4129 m_dragCount = 0;
4130 }
4131 #endif // wxUSE_DRAG_AND_DROP
4132
4133 bool simulateClick = false;
4134
4135 if (event.ButtonDClick())
4136 {
4137 m_renameTimer->Stop();
4138 m_lastOnSame = false;
4139 }
4140
4141 bool ignore_other_columns =
4142 ((expander != col) &&
4143 (model->IsContainer(item)) &&
4144 (!model->HasContainerColumns(item)));
4145
4146 if (event.LeftDClick())
4147 {
4148 if(hoverOverExpander)
4149 {
4150 // a double click on the expander will be converted into a "simulated" normal click
4151 simulateClick = true;
4152 }
4153 else if ( current == m_lineLastClicked )
4154 {
4155 wxWindow *parent = GetParent();
4156 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
4157 le.SetItem( item );
4158 le.SetColumn( col->GetModelColumn() );
4159 le.SetDataViewColumn( col );
4160 le.SetEventObject(parent);
4161 le.SetModel(GetModel());
4162
4163 parent->ProcessWindowEvent(le);
4164 return;
4165 }
4166 else
4167 {
4168 // The first click was on another item, so don't interpret this as
4169 // a double click, but as a simple click instead
4170 simulateClick = true;
4171 }
4172 }
4173
4174 if (event.LeftUp() && !hoverOverExpander)
4175 {
4176 if (m_lineSelectSingleOnUp != (unsigned int)-1)
4177 {
4178 // select single line
4179 SelectAllRows( false );
4180 SelectRow( m_lineSelectSingleOnUp, true );
4181 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
4182 }
4183
4184 // If the user click the expander, we do not do editing even if the column
4185 // with expander are editable
4186 if (m_lastOnSame && !ignore_other_columns)
4187 {
4188 if ((col == m_currentCol) && (current == m_currentRow) &&
4189 IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) )
4190 {
4191 m_renameTimer->Start( 100, true );
4192 }
4193 }
4194
4195 m_lastOnSame = false;
4196 m_lineSelectSingleOnUp = (unsigned int)-1;
4197 }
4198 else if(!event.LeftUp())
4199 {
4200 // This is necessary, because after a DnD operation in
4201 // from and to ourself, the up event is swallowed by the
4202 // DnD code. So on next non-up event (which means here and
4203 // now) m_lineSelectSingleOnUp should be reset.
4204 m_lineSelectSingleOnUp = (unsigned int)-1;
4205 }
4206
4207 if (event.RightDown())
4208 {
4209 m_lineBeforeLastClicked = m_lineLastClicked;
4210 m_lineLastClicked = current;
4211
4212 // If the item is already selected, do not update the selection.
4213 // Multi-selections should not be cleared if a selected item is clicked.
4214 if (!IsRowSelected(current))
4215 {
4216 SelectAllRows(false);
4217 const unsigned oldCurrent = m_currentRow;
4218 ChangeCurrentRow(current);
4219 SelectRow(m_currentRow,true);
4220 RefreshRow(oldCurrent);
4221 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
4222 }
4223 }
4224 else if (event.MiddleDown())
4225 {
4226 }
4227
4228 if((event.LeftDown() || simulateClick) && hoverOverExpander)
4229 {
4230 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
4231
4232 // hoverOverExpander being true tells us that our node must be
4233 // valid and have children.
4234 // So we don't need any extra checks.
4235 if( node->IsOpen() )
4236 Collapse(current);
4237 else
4238 Expand(current);
4239 }
4240 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
4241 {
4242 m_lineBeforeLastClicked = m_lineLastClicked;
4243 m_lineLastClicked = current;
4244
4245 unsigned int oldCurrentRow = m_currentRow;
4246 bool oldWasSelected = IsRowSelected(m_currentRow);
4247
4248 bool cmdModifierDown = event.CmdDown();
4249 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
4250 {
4251 if ( IsSingleSel() || !IsRowSelected(current) )
4252 {
4253 SelectAllRows( false );
4254 ChangeCurrentRow(current);
4255 SelectRow(m_currentRow,true);
4256 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
4257 }
4258 else // multi sel & current is highlighted & no mod keys
4259 {
4260 m_lineSelectSingleOnUp = current;
4261 ChangeCurrentRow(current); // change focus
4262 }
4263 }
4264 else // multi sel & either ctrl or shift is down
4265 {
4266 if (cmdModifierDown)
4267 {
4268 ChangeCurrentRow(current);
4269 ReverseRowSelection(m_currentRow);
4270 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
4271 }
4272 else if (event.ShiftDown())
4273 {
4274 ChangeCurrentRow(current);
4275
4276 unsigned int lineFrom = oldCurrentRow,
4277 lineTo = current;
4278
4279 if ( lineTo < lineFrom )
4280 {
4281 lineTo = lineFrom;
4282 lineFrom = m_currentRow;
4283 }
4284
4285 SelectRows(lineFrom, lineTo, true);
4286 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
4287 }
4288 else // !ctrl, !shift
4289 {
4290 // test in the enclosing if should make it impossible
4291 wxFAIL_MSG( wxT("how did we get here?") );
4292 }
4293 }
4294
4295 if (m_currentRow != oldCurrentRow)
4296 RefreshRow( oldCurrentRow );
4297
4298 wxDataViewColumn *oldCurrentCol = m_currentCol;
4299
4300 // Update selection here...
4301 m_currentCol = col;
4302 m_currentColSetByKeyboard = false;
4303
4304 // This flag is used to decide whether we should start editing the item
4305 // label. We do it if the user clicks twice (but not double clicks,
4306 // i.e. simulateClick is false) on the same item but not if the click
4307 // was used for something else already, e.g. selecting the item (so it
4308 // must have been already selected) or giving the focus to the control
4309 // (so it must have had focus already).
4310 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
4311 (current == oldCurrentRow)) && oldWasSelected &&
4312 HasFocus();
4313
4314 // Call ActivateCell() after everything else as under GTK+
4315 if ( IsCellEditableInMode(item, col, wxDATAVIEW_CELL_ACTIVATABLE) )
4316 {
4317 // notify cell about click
4318 cell->PrepareForItem(model, item, col->GetModelColumn());
4319
4320 wxRect cell_rect( xpos + itemOffset,
4321 GetLineStart( current ),
4322 col->GetWidth() - itemOffset,
4323 GetLineHeight( current ) );
4324
4325 // Report position relative to the cell's custom area, i.e.
4326 // no the entire space as given by the control but the one
4327 // used by the renderer after calculation of alignment etc.
4328
4329 // adjust the rectangle ourselves to account for the alignment
4330 wxRect rectItem = cell_rect;
4331 const int align = cell->GetAlignment();
4332 if ( align != wxDVR_DEFAULT_ALIGNMENT )
4333 {
4334 const wxSize size = cell->GetSize();
4335
4336 if ( size.x >= 0 && size.x < cell_rect.width )
4337 {
4338 if ( align & wxALIGN_CENTER_HORIZONTAL )
4339 rectItem.x += (cell_rect.width - size.x)/2;
4340 else if ( align & wxALIGN_RIGHT )
4341 rectItem.x += cell_rect.width - size.x;
4342 // else: wxALIGN_LEFT is the default
4343 }
4344
4345 if ( size.y >= 0 && size.y < cell_rect.height )
4346 {
4347 if ( align & wxALIGN_CENTER_VERTICAL )
4348 rectItem.y += (cell_rect.height - size.y)/2;
4349 else if ( align & wxALIGN_BOTTOM )
4350 rectItem.y += cell_rect.height - size.y;
4351 // else: wxALIGN_TOP is the default
4352 }
4353 }
4354
4355 wxMouseEvent event2(event);
4356 event2.m_x -= rectItem.x;
4357 event2.m_y -= rectItem.y;
4358 m_owner->CalcUnscrolledPosition(event2.m_x, event2.m_y, &event2.m_x, &event2.m_y);
4359
4360 /* ignore ret */ cell->WXActivateCell
4361 (
4362 cell_rect,
4363 model,
4364 item,
4365 col->GetModelColumn(),
4366 &event2
4367 );
4368 }
4369 }
4370 }
4371
4372 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
4373 {
4374 m_hasFocus = true;
4375
4376 if (HasCurrentRow())
4377 Refresh();
4378
4379 event.Skip();
4380 }
4381
4382 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
4383 {
4384 m_hasFocus = false;
4385
4386 if (HasCurrentRow())
4387 Refresh();
4388
4389 event.Skip();
4390 }
4391
4392 void wxDataViewMainWindow::OnColumnsCountChanged()
4393 {
4394 int editableCount = 0;
4395
4396 const unsigned cols = GetOwner()->GetColumnCount();
4397 for ( unsigned i = 0; i < cols; i++ )
4398 {
4399 wxDataViewColumn *c = GetOwner()->GetColumnAt(i);
4400 if ( c->IsHidden() )
4401 continue;
4402 if ( c->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT )
4403 editableCount++;
4404 }
4405
4406 m_useCellFocus = (editableCount > 1);
4407
4408 UpdateDisplay();
4409 }
4410
4411 //-----------------------------------------------------------------------------
4412 // wxDataViewCtrl
4413 //-----------------------------------------------------------------------------
4414
4415 WX_DEFINE_LIST(wxDataViewColumnList)
4416
4417 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4418 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
4419 EVT_SIZE(wxDataViewCtrl::OnSize)
4420 END_EVENT_TABLE()
4421
4422 wxDataViewCtrl::~wxDataViewCtrl()
4423 {
4424 if (m_notifier)
4425 GetModel()->RemoveNotifier( m_notifier );
4426
4427 m_cols.Clear();
4428 m_colsBestWidths.clear();
4429 }
4430
4431 void wxDataViewCtrl::Init()
4432 {
4433 m_cols.DeleteContents(true);
4434 m_notifier = NULL;
4435
4436 // No sorting column at start
4437 m_sortingColumnIdx = wxNOT_FOUND;
4438
4439 m_headerArea = NULL;
4440
4441 m_colsDirty = false;
4442 }
4443
4444 bool wxDataViewCtrl::Create(wxWindow *parent,
4445 wxWindowID id,
4446 const wxPoint& pos,
4447 const wxSize& size,
4448 long style,
4449 const wxValidator& validator,
4450 const wxString& name)
4451 {
4452 // if ( (style & wxBORDER_MASK) == 0)
4453 // style |= wxBORDER_SUNKEN;
4454
4455 Init();
4456
4457 if (!wxControl::Create( parent, id, pos, size,
4458 style | wxScrolledWindowStyle, validator, name))
4459 return false;
4460
4461 SetInitialSize(size);
4462
4463 #ifdef __WXMAC__
4464 MacSetClipChildren( true );
4465 #endif
4466
4467 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
4468
4469 // We use the cursor keys for moving the selection, not scrolling, so call
4470 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4471 // keyboard events forwarded to us from wxListMainWindow.
4472 DisableKeyboardScrolling();
4473
4474 if (HasFlag(wxDV_NO_HEADER))
4475 m_headerArea = NULL;
4476 else
4477 m_headerArea = new wxDataViewHeaderWindow(this);
4478
4479 SetTargetWindow( m_clientArea );
4480
4481 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4482 if (m_headerArea)
4483 sizer->Add( m_headerArea, 0, wxGROW );
4484 sizer->Add( m_clientArea, 1, wxGROW );
4485 SetSizer( sizer );
4486
4487 return true;
4488 }
4489
4490 wxBorder wxDataViewCtrl::GetDefaultBorder() const
4491 {
4492 return wxBORDER_THEME;
4493 }
4494
4495 #ifdef __WXMSW__
4496 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
4497 WXWPARAM wParam,
4498 WXLPARAM lParam)
4499 {
4500 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4501
4502 #ifndef __WXWINCE__
4503 // we need to process arrows ourselves for scrolling
4504 if ( nMsg == WM_GETDLGCODE )
4505 {
4506 rc |= DLGC_WANTARROWS;
4507 }
4508 #endif
4509
4510 return rc;
4511 }
4512 #endif
4513
4514 wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4515 {
4516 wxSize newsize = size;
4517 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
4518 newsize.y -= m_headerArea->GetSize().y;
4519
4520 return newsize;
4521 }
4522
4523 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4524 {
4525 // We need to override OnSize so that our scrolled
4526 // window a) does call Layout() to use sizers for
4527 // positioning the controls but b) does not query
4528 // the sizer for their size and use that for setting
4529 // the scrollable area as set that ourselves by
4530 // calling SetScrollbar() further down.
4531
4532 Layout();
4533
4534 AdjustScrollbars();
4535
4536 // We must redraw the headers if their height changed. Normally this
4537 // shouldn't happen as the control shouldn't let itself be resized beneath
4538 // its minimal height but avoid the display artefacts that appear if it
4539 // does happen, e.g. because there is really not enough vertical space.
4540 if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea &&
4541 m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y )
4542 {
4543 m_headerArea->Refresh();
4544 }
4545 }
4546
4547 void wxDataViewCtrl::SetFocus()
4548 {
4549 if (m_clientArea)
4550 m_clientArea->SetFocus();
4551 }
4552
4553 bool wxDataViewCtrl::SetFont(const wxFont & font)
4554 {
4555 if (!wxControl::SetFont(font))
4556 return false;
4557
4558 if (m_headerArea)
4559 m_headerArea->SetFont(font);
4560
4561 if (m_clientArea)
4562 {
4563 m_clientArea->SetFont(font);
4564 m_clientArea->SetRowHeight(m_clientArea->GetDefaultRowHeight());
4565 }
4566
4567 if (m_headerArea || m_clientArea)
4568 {
4569 InvalidateColBestWidths();
4570 Layout();
4571 }
4572
4573 return true;
4574 }
4575
4576
4577
4578 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4579 {
4580 if (!wxDataViewCtrlBase::AssociateModel( model ))
4581 return false;
4582
4583 if (model)
4584 {
4585 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4586 model->AddNotifier( m_notifier );
4587 }
4588 else if (m_notifier)
4589 {
4590 m_notifier->Cleared();
4591 m_notifier = NULL;
4592 }
4593
4594 m_clientArea->DestroyTree();
4595
4596 if (model)
4597 {
4598 m_clientArea->BuildTree(model);
4599 }
4600
4601 m_clientArea->UpdateDisplay();
4602
4603 return true;
4604 }
4605
4606 #if wxUSE_DRAG_AND_DROP
4607
4608 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4609 {
4610 return m_clientArea->EnableDragSource( format );
4611 }
4612
4613 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4614 {
4615 return m_clientArea->EnableDropTarget( format );
4616 }
4617
4618 #endif // wxUSE_DRAG_AND_DROP
4619
4620 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4621 {
4622 if (!wxDataViewCtrlBase::AppendColumn(col))
4623 return false;
4624
4625 m_cols.Append( col );
4626 m_colsBestWidths.push_back(CachedColWidthInfo());
4627 OnColumnsCountChanged();
4628 return true;
4629 }
4630
4631 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4632 {
4633 if (!wxDataViewCtrlBase::PrependColumn(col))
4634 return false;
4635
4636 m_cols.Insert( col );
4637 m_colsBestWidths.insert(m_colsBestWidths.begin(), CachedColWidthInfo());
4638 OnColumnsCountChanged();
4639 return true;
4640 }
4641
4642 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4643 {
4644 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4645 return false;
4646
4647 m_cols.Insert( pos, col );
4648 m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, CachedColWidthInfo());
4649 OnColumnsCountChanged();
4650 return true;
4651 }
4652
4653 void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4654 {
4655 if ( m_headerArea )
4656 m_headerArea->UpdateColumn(idx);
4657
4658 m_clientArea->UpdateDisplay();
4659 }
4660
4661 void wxDataViewCtrl::OnColumnsCountChanged()
4662 {
4663 if (m_headerArea)
4664 m_headerArea->SetColumnCount(GetColumnCount());
4665
4666 m_clientArea->OnColumnsCountChanged();
4667 }
4668
4669 void wxDataViewCtrl::DoSetExpanderColumn()
4670 {
4671 m_clientArea->UpdateDisplay();
4672 }
4673
4674 void wxDataViewCtrl::DoSetIndent()
4675 {
4676 m_clientArea->UpdateDisplay();
4677 }
4678
4679 unsigned int wxDataViewCtrl::GetColumnCount() const
4680 {
4681 return m_cols.GetCount();
4682 }
4683
4684 bool wxDataViewCtrl::SetRowHeight( int lineHeight )
4685 {
4686 if ( !m_clientArea )
4687 return false;
4688
4689 m_clientArea->SetRowHeight(lineHeight);
4690
4691 return true;
4692 }
4693
4694 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
4695 {
4696 return m_cols[idx];
4697 }
4698
4699 wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
4700 {
4701 // columns can't be reordered if there is no header window which allows
4702 // to do this
4703 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
4704 : pos;
4705
4706 return GetColumn(idx);
4707 }
4708
4709 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4710 {
4711 const unsigned count = m_cols.size();
4712 for ( unsigned n = 0; n < count; n++ )
4713 {
4714 if ( m_cols[n] == column )
4715 return n;
4716 }
4717
4718 return wxNOT_FOUND;
4719 }
4720
4721 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const
4722 {
4723 if ( m_colsBestWidths[idx].width != 0 )
4724 return m_colsBestWidths[idx].width;
4725
4726 const int count = m_clientArea->GetRowCount();
4727 wxDataViewColumn *column = GetColumn(idx);
4728 wxDataViewRenderer *renderer =
4729 const_cast<wxDataViewRenderer*>(column->GetRenderer());
4730
4731 class MaxWidthCalculator
4732 {
4733 public:
4734 MaxWidthCalculator(const wxDataViewCtrl *dvc,
4735 wxDataViewMainWindow *clientArea,
4736 wxDataViewRenderer *renderer,
4737 const wxDataViewModel *model,
4738 unsigned column,
4739 int expanderSize)
4740 : m_width(0),
4741 m_dvc(dvc),
4742 m_clientArea(clientArea),
4743 m_renderer(renderer),
4744 m_model(model),
4745 m_column(column),
4746 m_expanderSize(expanderSize)
4747
4748 {
4749 m_isExpanderCol =
4750 !clientArea->IsList() &&
4751 (column == 0 ||
4752 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl*>(dvc)) == dvc->GetColumnAt(column));
4753 }
4754
4755 void UpdateWithWidth(int width)
4756 {
4757 m_width = wxMax(m_width, width);
4758 }
4759
4760 void UpdateWithRow(int row)
4761 {
4762 int indent = 0;
4763 wxDataViewItem item;
4764
4765 if ( m_isExpanderCol )
4766 {
4767 wxDataViewTreeNode *node = m_clientArea->GetTreeNodeByRow(row);
4768 item = node->GetItem();
4769 indent = m_dvc->GetIndent() * node->GetIndentLevel() + m_expanderSize;
4770 }
4771 else
4772 {
4773 item = m_clientArea->GetItemByRow(row);
4774 }
4775
4776 m_renderer->PrepareForItem(m_model, item, m_column);
4777 m_width = wxMax(m_width, m_renderer->GetSize().x + indent);
4778 }
4779
4780 int GetMaxWidth() const { return m_width; }
4781
4782 private:
4783 int m_width;
4784 const wxDataViewCtrl *m_dvc;
4785 wxDataViewMainWindow *m_clientArea;
4786 wxDataViewRenderer *m_renderer;
4787 const wxDataViewModel *m_model;
4788 unsigned m_column;
4789 bool m_isExpanderCol;
4790 int m_expanderSize;
4791 };
4792
4793 MaxWidthCalculator calculator(this, m_clientArea, renderer,
4794 GetModel(), column->GetModelColumn(),
4795 m_clientArea->GetRowHeight());
4796
4797 calculator.UpdateWithWidth(column->GetMinWidth());
4798
4799 if ( m_headerArea )
4800 calculator.UpdateWithWidth(m_headerArea->GetColumnTitleWidth(*column));
4801
4802 // The code below deserves some explanation. For very large controls, we
4803 // simply can't afford to calculate sizes for all items, it takes too
4804 // long. So the best we can do is to check the first and the last N/2
4805 // items in the control for some sufficiently large N and calculate best
4806 // sizes from that. That can result in the calculated best width being too
4807 // small for some outliers, but it's better to get slightly imperfect
4808 // result than to wait several seconds after every update. To avoid highly
4809 // visible miscalculations, we also include all currently visible items
4810 // no matter what. Finally, the value of N is determined dynamically by
4811 // measuring how much time we spent on the determining item widths so far.
4812
4813 #if wxUSE_STOPWATCH
4814 int top_part_end = count;
4815 static const long CALC_TIMEOUT = 20/*ms*/;
4816 // don't call wxStopWatch::Time() too often
4817 static const unsigned CALC_CHECK_FREQ = 100;
4818 wxStopWatch timer;
4819 #else
4820 // use some hard-coded limit, that's the best we can do without timer
4821 int top_part_end = wxMin(500, count);
4822 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4823
4824 int row = 0;
4825
4826 for ( row = 0; row < top_part_end; row++ )
4827 {
4828 #if wxUSE_STOPWATCH
4829 if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 &&
4830 timer.Time() > CALC_TIMEOUT )
4831 break;
4832 #endif // wxUSE_STOPWATCH
4833 calculator.UpdateWithRow(row);
4834 }
4835
4836 // row is the first unmeasured item now; that's our value of N/2
4837
4838 if ( row < count )
4839 {
4840 top_part_end = row;
4841
4842 // add bottom N/2 items now:
4843 const int bottom_part_start = wxMax(row, count - row);
4844 for ( row = bottom_part_start; row < count; row++ )
4845 {
4846 calculator.UpdateWithRow(row);
4847 }
4848
4849 // finally, include currently visible items in the calculation:
4850 const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0));
4851 int first_visible = m_clientArea->GetLineAt(origin.y);
4852 int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y);
4853
4854 first_visible = wxMax(first_visible, top_part_end);
4855 last_visible = wxMin(bottom_part_start, last_visible);
4856
4857 for ( row = first_visible; row < last_visible; row++ )
4858 {
4859 calculator.UpdateWithRow(row);
4860 }
4861
4862 wxLogTrace("dataview",
4863 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4864 top_part_end,
4865 count - bottom_part_start,
4866 wxMax(0, last_visible - first_visible),
4867 count);
4868 }
4869
4870 int max_width = calculator.GetMaxWidth();
4871 if ( max_width > 0 )
4872 max_width += 2 * PADDING_RIGHTLEFT;
4873
4874 const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx].width = max_width;
4875 return max_width;
4876 }
4877
4878 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
4879 unsigned int WXUNUSED(new_pos))
4880 {
4881 // do _not_ reorder m_cols elements here, they should always be in the
4882 // order in which columns were added, we only display the columns in
4883 // different order
4884 m_clientArea->UpdateDisplay();
4885 }
4886
4887 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4888 {
4889 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4890 if (!ret)
4891 return false;
4892
4893 m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column));
4894 m_cols.Erase(ret);
4895
4896 if ( m_clientArea->GetCurrentColumn() == column )
4897 m_clientArea->ClearCurrentColumn();
4898
4899 OnColumnsCountChanged();
4900
4901 return true;
4902 }
4903
4904 bool wxDataViewCtrl::ClearColumns()
4905 {
4906 SetExpanderColumn(NULL);
4907 m_cols.Clear();
4908 m_colsBestWidths.clear();
4909
4910 m_clientArea->ClearCurrentColumn();
4911
4912 OnColumnsCountChanged();
4913
4914 return true;
4915 }
4916
4917 void wxDataViewCtrl::InvalidateColBestWidth(int idx)
4918 {
4919 m_colsBestWidths[idx].width = 0;
4920 m_colsBestWidths[idx].dirty = true;
4921 m_colsDirty = true;
4922 }
4923
4924 void wxDataViewCtrl::InvalidateColBestWidths()
4925 {
4926 // mark all columns as dirty:
4927 m_colsBestWidths.clear();
4928 m_colsBestWidths.resize(m_cols.size());
4929 m_colsDirty = true;
4930 }
4931
4932 void wxDataViewCtrl::UpdateColWidths()
4933 {
4934 m_colsDirty = false;
4935
4936 if ( !m_headerArea )
4937 return;
4938
4939 const unsigned len = m_colsBestWidths.size();
4940 for ( unsigned i = 0; i < len; i++ )
4941 {
4942 // Note that we have to have an explicit 'dirty' flag here instead of
4943 // checking if the width==0, as is done in GetBestColumnWidth().
4944 //
4945 // Testing width==0 wouldn't work correctly if some code called
4946 // GetWidth() after col. width invalidation but before
4947 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
4948 // would result in the header's column width getting out of sync with
4949 // the control itself.
4950 if ( m_colsBestWidths[i].dirty )
4951 {
4952 m_headerArea->UpdateColumn(i);
4953 m_colsBestWidths[i].dirty = false;
4954 }
4955 }
4956 }
4957
4958 void wxDataViewCtrl::OnInternalIdle()
4959 {
4960 wxDataViewCtrlBase::OnInternalIdle();
4961
4962 if ( m_colsDirty )
4963 UpdateColWidths();
4964 }
4965
4966 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4967 {
4968 unsigned int len = GetColumnCount();
4969 for ( unsigned int i = 0; i < len; i++ )
4970 {
4971 wxDataViewColumn * col = GetColumnAt(i);
4972 if (column==col)
4973 return i;
4974 }
4975
4976 return wxNOT_FOUND;
4977 }
4978
4979 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4980 {
4981 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
4982 : GetColumn(m_sortingColumnIdx);
4983 }
4984
4985 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4986 {
4987 return GetItemByRow(m_clientArea->GetCurrentRow());
4988 }
4989
4990 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4991 {
4992 const int row = m_clientArea->GetRowByItem(item);
4993
4994 const unsigned oldCurrent = m_clientArea->GetCurrentRow();
4995 if ( static_cast<unsigned>(row) != oldCurrent )
4996 {
4997 m_clientArea->ChangeCurrentRow(row);
4998 m_clientArea->RefreshRow(oldCurrent);
4999 m_clientArea->RefreshRow(row);
5000 }
5001 }
5002
5003 wxDataViewColumn *wxDataViewCtrl::GetCurrentColumn() const
5004 {
5005 return m_clientArea->GetCurrentColumn();
5006 }
5007
5008 int wxDataViewCtrl::GetSelectedItemsCount() const
5009 {
5010 return m_clientArea->GetSelections().size();
5011 }
5012
5013 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
5014 {
5015 sel.Empty();
5016 const wxDataViewSelection& selections = m_clientArea->GetSelections();
5017
5018 const size_t len = selections.size();
5019 for ( size_t i = 0; i < len; i++ )
5020 {
5021 wxDataViewItem item = m_clientArea->GetItemByRow(selections[i]);
5022 if ( item.IsOk() )
5023 {
5024 sel.Add(item);
5025 }
5026 else
5027 {
5028 wxFAIL_MSG( "invalid item in selection - bad internal state" );
5029 }
5030 }
5031
5032 return sel.size();
5033 }
5034
5035 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
5036 {
5037 wxDataViewSelection selection(wxDataViewSelectionCmp);
5038
5039 wxDataViewItem last_parent;
5040
5041 int len = sel.GetCount();
5042 for( int i = 0; i < len; i ++ )
5043 {
5044 wxDataViewItem item = sel[i];
5045 wxDataViewItem parent = GetModel()->GetParent( item );
5046 if (parent)
5047 {
5048 if (parent != last_parent)
5049 ExpandAncestors(item);
5050 }
5051
5052 last_parent = parent;
5053 int row = m_clientArea->GetRowByItem( item );
5054 if( row >= 0 )
5055 selection.Add( static_cast<unsigned int>(row) );
5056 }
5057
5058 m_clientArea->SetSelections( selection );
5059 }
5060
5061 void wxDataViewCtrl::Select( const wxDataViewItem & item )
5062 {
5063 ExpandAncestors( item );
5064
5065 int row = m_clientArea->GetRowByItem( item );
5066 if( row >= 0 )
5067 {
5068 // Unselect all rows before select another in the single select mode
5069 if (m_clientArea->IsSingleSel())
5070 m_clientArea->SelectAllRows(false);
5071
5072 m_clientArea->SelectRow(row, true);
5073
5074 // Also set focus to the selected item
5075 m_clientArea->ChangeCurrentRow( row );
5076 }
5077 }
5078
5079 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
5080 {
5081 int row = m_clientArea->GetRowByItem( item );
5082 if( row >= 0 )
5083 m_clientArea->SelectRow(row, false);
5084 }
5085
5086 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
5087 {
5088 int row = m_clientArea->GetRowByItem( item );
5089 if( row >= 0 )
5090 {
5091 return m_clientArea->IsRowSelected(row);
5092 }
5093 return false;
5094 }
5095
5096 void wxDataViewCtrl::SetAlternateRowColour(const wxColour& colour)
5097 {
5098 m_alternateRowColour = colour;
5099 }
5100
5101 void wxDataViewCtrl::SelectAll()
5102 {
5103 m_clientArea->SelectAllRows(true);
5104 }
5105
5106 void wxDataViewCtrl::UnselectAll()
5107 {
5108 m_clientArea->SelectAllRows(false);
5109 }
5110
5111 void wxDataViewCtrl::EnsureVisible( int row, int column )
5112 {
5113 if( row < 0 )
5114 row = 0;
5115 if( row > (int) m_clientArea->GetRowCount() )
5116 row = m_clientArea->GetRowCount();
5117
5118 int first = m_clientArea->GetFirstVisibleRow();
5119 int last = m_clientArea->GetLastVisibleRow();
5120 if( row < first )
5121 m_clientArea->ScrollTo( row, column );
5122 else if( row > last )
5123 m_clientArea->ScrollTo( row - last + first, column );
5124 else
5125 m_clientArea->ScrollTo( first, column );
5126 }
5127
5128 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
5129 {
5130 ExpandAncestors( item );
5131
5132 m_clientArea->RecalculateDisplay();
5133
5134 int row = m_clientArea->GetRowByItem(item);
5135 if( row >= 0 )
5136 {
5137 if( column == NULL )
5138 EnsureVisible(row, -1);
5139 else
5140 EnsureVisible( row, GetColumnIndex(column) );
5141 }
5142
5143 }
5144
5145 void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
5146 wxDataViewColumn* &column ) const
5147 {
5148 m_clientArea->HitTest(point, item, column);
5149 }
5150
5151 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
5152 const wxDataViewColumn* column ) const
5153 {
5154 return m_clientArea->GetItemRect(item, column);
5155 }
5156
5157 wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
5158 {
5159 return m_clientArea->GetItemByRow( row );
5160 }
5161
5162 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
5163 {
5164 return m_clientArea->GetRowByItem( item );
5165 }
5166
5167 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
5168 {
5169 ExpandAncestors( item );
5170
5171 int row = m_clientArea->GetRowByItem( item );
5172 if (row != -1)
5173 {
5174 m_clientArea->Expand(row);
5175 InvalidateColBestWidths();
5176 }
5177 }
5178
5179 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
5180 {
5181 int row = m_clientArea->GetRowByItem( item );
5182 if (row != -1)
5183 {
5184 m_clientArea->Collapse(row);
5185 InvalidateColBestWidths();
5186 }
5187 }
5188
5189 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
5190 {
5191 int row = m_clientArea->GetRowByItem( item );
5192 if (row != -1)
5193 return m_clientArea->IsExpanded(row);
5194 return false;
5195 }
5196
5197 void wxDataViewCtrl::EditItem(const wxDataViewItem& item, const wxDataViewColumn *column)
5198 {
5199 wxCHECK_RET( item.IsOk(), "invalid item" );
5200 wxCHECK_RET( column, "no column provided" );
5201
5202 m_clientArea->StartEditing(item, column);
5203 }
5204
5205 #endif // !wxUSE_GENERICDATAVIEWCTRL
5206
5207 #endif // wxUSE_DATAVIEWCTRL