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