]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
44b0bd4ef83bce2d75b688356c19166899ad4d7b
[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 )
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 if ( !iter )
3065 break;
3066 }
3067 return NULL;
3068 }
3069
3070 void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item,
3071 wxDataViewColumn* &column )
3072 {
3073 wxDataViewColumn *col = NULL;
3074 unsigned int cols = GetOwner()->GetColumnCount();
3075 unsigned int colnum = 0;
3076 int x, y;
3077 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
3078 for (unsigned x_start = 0; colnum < cols; colnum++)
3079 {
3080 col = GetOwner()->GetColumnAt(colnum);
3081 if (col->IsHidden())
3082 continue; // skip it!
3083
3084 unsigned int w = col->GetWidth();
3085 if (x_start+w >= (unsigned int)x)
3086 break;
3087
3088 x_start += w;
3089 }
3090
3091 column = col;
3092 item = GetItemByRow( GetLineAt( y ) );
3093 }
3094
3095 wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item,
3096 const wxDataViewColumn* column )
3097 {
3098 int xpos = 0;
3099 int width = 0;
3100
3101 unsigned int cols = GetOwner()->GetColumnCount();
3102 // If column is null the loop will compute the combined width of all columns.
3103 // Otherwise, it will compute the x position of the column we are looking for.
3104 for (unsigned int i = 0; i < cols; i++)
3105 {
3106 wxDataViewColumn* col = GetOwner()->GetColumnAt( i );
3107
3108 if (col == column)
3109 break;
3110
3111 if (col->IsHidden())
3112 continue; // skip it!
3113
3114 xpos += col->GetWidth();
3115 width += col->GetWidth();
3116 }
3117
3118 if(column != 0)
3119 {
3120 // If we have a column, we need can get its width directly.
3121 if(column->IsHidden())
3122 width = 0;
3123 else
3124 width = column->GetWidth();
3125
3126 }
3127 else
3128 {
3129 // If we have no column, we reset the x position back to zero.
3130 xpos = 0;
3131 }
3132
3133 // we have to take an expander column into account and compute its indentation
3134 // to get the correct x position where the actual text is
3135 int indent = 0;
3136 int row = GetRowByItem(item);
3137 if (!IsList() && (column == 0 || GetOwner()->GetExpanderColumn() == column) )
3138 {
3139 wxDataViewTreeNode* node = GetTreeNodeByRow(row);
3140 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
3141 indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander
3142
3143 if(!node->HasChildren())
3144 delete node;
3145 }
3146
3147 wxRect itemRect( xpos + indent,
3148 GetLineStart( row ),
3149 width - indent,
3150 GetLineHeight( row ) );
3151
3152 GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y,
3153 &itemRect.x, &itemRect.y );
3154
3155 return itemRect;
3156 }
3157
3158 int wxDataViewMainWindow::RecalculateCount()
3159 {
3160 if (IsVirtualList())
3161 {
3162 wxDataViewVirtualListModel *list_model =
3163 (wxDataViewVirtualListModel*) GetOwner()->GetModel();
3164
3165 return list_model->GetCount();
3166 }
3167 else
3168 {
3169 return m_root->GetSubTreeCount();
3170 }
3171 }
3172
3173 class ItemToRowJob : public DoJob
3174 {
3175 public:
3176 ItemToRowJob(const wxDataViewItem& item_, wxVector<wxDataViewItem>::reverse_iterator iter)
3177 : m_iter(iter),
3178 item(item_)
3179 {
3180 ret = -1;
3181 }
3182
3183 // Maybe binary search will help to speed up this process
3184 virtual int operator() ( wxDataViewTreeNode * node)
3185 {
3186 ret ++;
3187 if( node->GetItem() == item )
3188 {
3189 return DoJob::OK;
3190 }
3191
3192 if( node->GetItem() == *m_iter )
3193 {
3194 m_iter++;
3195 return DoJob::CONT;
3196 }
3197 else
3198 {
3199 ret += node->GetSubTreeCount();
3200 return DoJob::IGR;
3201 }
3202
3203 }
3204
3205 virtual int operator() ( void * n )
3206 {
3207 ret ++;
3208 if( n == item.GetID() )
3209 return DoJob::OK;
3210 return DoJob::CONT;
3211 }
3212
3213 // the row number is begin from zero
3214 int GetResult() const
3215 { return ret -1; }
3216
3217 private:
3218 wxVector<wxDataViewItem>::reverse_iterator m_iter;
3219 wxDataViewItem item;
3220 int ret;
3221
3222 };
3223
3224 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const
3225 {
3226 const wxDataViewModel * model = GetOwner()->GetModel();
3227 if( model == NULL )
3228 return -1;
3229
3230 if (IsVirtualList())
3231 {
3232 return wxPtrToUInt( item.GetID() ) -1;
3233 }
3234 else
3235 {
3236 if( !item.IsOk() )
3237 return -1;
3238
3239 // Compose the parent-chain of the item we are looking for
3240 wxVector<wxDataViewItem> parentChain;
3241 wxDataViewItem it( item );
3242 while( it.IsOk() )
3243 {
3244 parentChain.push_back(it);
3245 it = model->GetParent(it);
3246 }
3247
3248 // add an 'invalid' item to represent our 'invisible' root node
3249 parentChain.push_back(wxDataViewItem());
3250
3251 // the parent chain was created by adding the deepest parent first.
3252 // so if we want to start at the root node, we have to iterate backwards through the vector
3253 ItemToRowJob job( item, parentChain.rbegin() );
3254 Walker( m_root, job );
3255 return job.GetResult();
3256 }
3257 }
3258
3259 static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
3260 wxDataViewTreeNode * node)
3261 {
3262 if( !model->IsContainer( item ) )
3263 return;
3264
3265 wxDataViewItemArray children;
3266 unsigned int num = model->GetChildren( item, children);
3267
3268 unsigned int index = 0;
3269 while( index < num )
3270 {
3271 if( model->IsContainer( children[index] ) )
3272 {
3273 wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
3274 n->SetItem(children[index]);
3275 n->SetHasChildren( true );
3276 node->AddNode( n );
3277 }
3278 else
3279 {
3280 node->AddLeaf( children[index].GetID() );
3281 }
3282 index ++;
3283 }
3284 node->SetSubTreeCount( num );
3285 wxDataViewTreeNode * n = node->GetParent();
3286 if( n != NULL)
3287 n->ChangeSubTreeCount(num);
3288
3289 }
3290
3291 void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3292 {
3293 DestroyTree();
3294
3295 if (GetOwner()->GetModel()->IsVirtualListModel())
3296 {
3297 m_count = -1;
3298 return;
3299 }
3300
3301 m_root = new wxDataViewTreeNode( NULL );
3302 m_root->SetHasChildren(true);
3303
3304 // First we define a invalid item to fetch the top-level elements
3305 wxDataViewItem item;
3306 SortPrepare();
3307 BuildTreeHelper( model, item, m_root);
3308 m_count = -1;
3309 }
3310
3311 static void DestroyTreeHelper( wxDataViewTreeNode * node )
3312 {
3313 if( node->GetNodeNumber() != 0 )
3314 {
3315 int len = node->GetNodeNumber();
3316 wxDataViewTreeNodes& nodes = node->GetNodes();
3317 for (int i = 0; i < len; i++)
3318 DestroyTreeHelper(nodes[i]);
3319 }
3320 delete node;
3321 }
3322
3323 void wxDataViewMainWindow::DestroyTree()
3324 {
3325 if (!IsVirtualList())
3326 {
3327 ::DestroyTreeHelper(m_root);
3328 m_count = 0;
3329 m_root = NULL;
3330 }
3331 }
3332
3333 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3334 {
3335 wxWindow * const parent = GetParent();
3336
3337 // propagate the char event upwards
3338 wxKeyEvent eventForParent(event);
3339 eventForParent.SetEventObject(parent);
3340 if ( parent->ProcessWindowEvent(eventForParent) )
3341 return;
3342
3343 if ( parent->HandleAsNavigationKey(event) )
3344 return;
3345
3346 // no item -> nothing to do
3347 if (!HasCurrentRow())
3348 {
3349 event.Skip();
3350 return;
3351 }
3352
3353 // don't use m_linesPerPage directly as it might not be computed yet
3354 const int pageSize = GetCountPerPage();
3355 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
3356
3357 switch ( event.GetKeyCode() )
3358 {
3359 case WXK_RETURN:
3360 {
3361 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,
3362 parent->GetId());
3363 le.SetItem( GetItemByRow(m_currentRow) );
3364 le.SetEventObject(parent);
3365 le.SetModel(GetOwner()->GetModel());
3366
3367 parent->GetEventHandler()->ProcessEvent(le);
3368 }
3369 break;
3370
3371 case WXK_UP:
3372 if ( m_currentRow > 0 )
3373 OnArrowChar( m_currentRow - 1, event );
3374 break;
3375
3376 case WXK_DOWN:
3377 if ( m_currentRow < GetRowCount() - 1 )
3378 OnArrowChar( m_currentRow + 1, event );
3379 break;
3380 // Add the process for tree expanding/collapsing
3381 case WXK_LEFT:
3382 {
3383 if (IsList())
3384 break;
3385
3386 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
3387 if (!node)
3388 break;
3389
3390 if (node->HasChildren() && node->IsOpen())
3391 {
3392 Collapse(m_currentRow);
3393 }
3394 else // if the node is already closed we move the selection to its parent
3395 {
3396 wxDataViewTreeNode *parent_node = node->GetParent();
3397
3398 if(!node->HasChildren())
3399 delete node;
3400
3401 if (parent_node)
3402 {
3403 int parent = GetRowByItem( parent_node->GetItem() );
3404 if ( parent >= 0 )
3405 {
3406 unsigned int row = m_currentRow;
3407 SelectRow( row, false);
3408 SelectRow( parent, true );
3409 ChangeCurrentRow( parent );
3410 GetOwner()->EnsureVisible( parent, -1 );
3411 SendSelectionChangedEvent( parent_node->GetItem() );
3412 }
3413 }
3414 }
3415 break;
3416 }
3417 case WXK_RIGHT:
3418 {
3419 if (!IsExpanded( m_currentRow ))
3420 Expand( m_currentRow );
3421 else
3422 {
3423 unsigned int row = m_currentRow;
3424 SelectRow( row, false );
3425 SelectRow( row + 1, true );
3426 ChangeCurrentRow( row + 1 );
3427 GetOwner()->EnsureVisible( row + 1, -1 );
3428 SendSelectionChangedEvent( GetItemByRow(row+1) );
3429 }
3430 break;
3431 }
3432 case WXK_END:
3433 {
3434 if (!IsEmpty())
3435 OnArrowChar( GetRowCount() - 1, event );
3436 break;
3437 }
3438 case WXK_HOME:
3439 if (!IsEmpty())
3440 OnArrowChar( 0, event );
3441 break;
3442
3443 case WXK_PAGEUP:
3444 {
3445 int steps = pageSize - 1;
3446 int index = m_currentRow - steps;
3447 if (index < 0)
3448 index = 0;
3449
3450 OnArrowChar( index, event );
3451 }
3452 break;
3453
3454 case WXK_PAGEDOWN:
3455 {
3456 int steps = pageSize - 1;
3457 unsigned int index = m_currentRow + steps;
3458 unsigned int count = GetRowCount();
3459 if ( index >= count )
3460 index = count - 1;
3461
3462 OnArrowChar( index, event );
3463 }
3464 break;
3465
3466 case WXK_F2:
3467 {
3468 if(m_selection.size() == 1)
3469 {
3470 // TODO: we need to revise that when we have a concept for a 'current column'
3471 GetOwner()->StartEditor(GetItemByRow(m_selection[0]), 0);
3472 }
3473 }
3474 break;
3475
3476 default:
3477 event.Skip();
3478 }
3479 }
3480
3481 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3482 {
3483 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3484 {
3485 // let the base handle mouse wheel events.
3486 event.Skip();
3487 return;
3488 }
3489
3490 // set the focus to ourself if any of the mouse buttons are pressed
3491 if(event.ButtonDown() && !HasFocus())
3492 SetFocus();
3493
3494 int x = event.GetX();
3495 int y = event.GetY();
3496 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
3497 wxDataViewColumn *col = NULL;
3498
3499 int xpos = 0;
3500 unsigned int cols = GetOwner()->GetColumnCount();
3501 unsigned int i;
3502 for (i = 0; i < cols; i++)
3503 {
3504 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
3505 if (c->IsHidden())
3506 continue; // skip it!
3507
3508 if (x < xpos + c->GetWidth())
3509 {
3510 col = c;
3511 break;
3512 }
3513 xpos += c->GetWidth();
3514 }
3515 if (!col)
3516 {
3517 event.Skip();
3518 return;
3519 }
3520
3521 wxDataViewRenderer *cell = col->GetRenderer();
3522 unsigned int current = GetLineAt( y );
3523 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
3524 {
3525 // Unselect all if below the last row ?
3526 event.Skip();
3527 return;
3528 }
3529
3530 // Test whether the mouse is hovered on the tree item button
3531 bool hoverOverExpander = false;
3532 if ((!IsList()) && (GetOwner()->GetExpanderColumn() == col))
3533 {
3534 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3535 if( node!=NULL && node->HasChildren() )
3536 {
3537 int indent = node->GetIndentLevel();
3538 indent = GetOwner()->GetIndent()*indent;
3539
3540 // we make the rectangle we are looking in a bit bigger than the actual
3541 // visual expander so the user can hit that little thing reliably
3542 wxRect rect( xpos + indent,
3543 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
3544 m_lineHeight, m_lineHeight);
3545 if( rect.Contains(x, y) )
3546 {
3547 // So the mouse is over the expander
3548 hoverOverExpander = true;
3549 if (m_underMouse && m_underMouse != node)
3550 {
3551 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3552 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
3553 }
3554 if (m_underMouse != node)
3555 {
3556 // wxLogMessage("Do the row: %d", current);
3557 RefreshRow(current);
3558 }
3559 m_underMouse = node;
3560 }
3561 }
3562 if (node!=NULL && !node->HasChildren())
3563 delete node;
3564 }
3565 if (!hoverOverExpander)
3566 {
3567 if (m_underMouse != NULL)
3568 {
3569 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3570 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
3571 m_underMouse = NULL;
3572 }
3573 }
3574
3575 wxDataViewModel *model = GetOwner()->GetModel();
3576
3577 #if wxUSE_DRAG_AND_DROP
3578 if (event.Dragging())
3579 {
3580 if (m_dragCount == 0)
3581 {
3582 // we have to report the raw, physical coords as we want to be
3583 // able to call HitTest(event.m_pointDrag) from the user code to
3584 // get the item being dragged
3585 m_dragStart = event.GetPosition();
3586 }
3587
3588 m_dragCount++;
3589
3590 if (m_dragCount != 3)
3591 return;
3592
3593 if (event.LeftIsDown())
3594 {
3595 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
3596 &m_dragStart.x, &m_dragStart.y );
3597 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
3598 wxDataViewItem item = GetItemByRow( drag_item_row );
3599
3600 // Notify cell about drag
3601 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3602 event.SetEventObject( m_owner );
3603 event.SetItem( item );
3604 event.SetModel( model );
3605 if (!m_owner->HandleWindowEvent( event ))
3606 return;
3607
3608 if (!event.IsAllowed())
3609 return;
3610
3611 wxDataObject *obj = event.GetDataObject();
3612 if (!obj)
3613 return;
3614
3615 wxDataViewDropSource drag( this, drag_item_row );
3616 drag.SetData( *obj );
3617 /* wxDragResult res = */ drag.DoDragDrop();
3618 delete obj;
3619 }
3620 return;
3621 }
3622 else
3623 {
3624 m_dragCount = 0;
3625 }
3626 #endif // wxUSE_DRAG_AND_DROP
3627
3628 bool simulateClick = false;
3629
3630 if (event.ButtonDClick())
3631 {
3632 m_renameTimer->Stop();
3633 m_lastOnSame = false;
3634 }
3635
3636 wxDataViewItem item = GetItemByRow(current);
3637 bool ignore_other_columns =
3638 ((GetOwner()->GetExpanderColumn() != col) &&
3639 (model->IsContainer(item)) &&
3640 (!model->HasContainerColumns(item)));
3641
3642 if (event.LeftDClick())
3643 {
3644 if(hoverOverExpander)
3645 {
3646 // a double click on the expander will be converted into a "simulated" normal click
3647 simulateClick = true;
3648 }
3649 else if ( current == m_lineLastClicked )
3650 {
3651 if ((!ignore_other_columns) && (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE))
3652 {
3653 const unsigned colIdx = col->GetModelColumn();
3654
3655 cell->PrepareForItem(model, item, colIdx);
3656
3657 wxRect cell_rect( xpos, GetLineStart( current ),
3658 col->GetWidth(), GetLineHeight( current ) );
3659 cell->WXOnActivate( cell_rect, model, item, colIdx );
3660 }
3661 else
3662 {
3663 wxWindow *parent = GetParent();
3664 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
3665 le.SetItem( item );
3666 le.SetColumn( col->GetModelColumn() );
3667 le.SetDataViewColumn( col );
3668 le.SetEventObject(parent);
3669 le.SetModel(GetOwner()->GetModel());
3670
3671 parent->GetEventHandler()->ProcessEvent(le);
3672 }
3673 return;
3674 }
3675 else
3676 {
3677 // The first click was on another item, so don't interpret this as
3678 // a double click, but as a simple click instead
3679 simulateClick = true;
3680 }
3681 }
3682
3683 if (event.LeftUp() && !hoverOverExpander)
3684 {
3685 if (m_lineSelectSingleOnUp != (unsigned int)-1)
3686 {
3687 // select single line
3688 SelectAllRows( false );
3689 SelectRow( m_lineSelectSingleOnUp, true );
3690 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
3691 }
3692
3693 // If the user click the expander, we do not do editing even if the column
3694 // with expander are editable
3695 if (m_lastOnSame && !ignore_other_columns)
3696 {
3697 if ((col == m_currentCol) && (current == m_currentRow) &&
3698 (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) )
3699 {
3700 m_renameTimer->Start( 100, true );
3701 }
3702 }
3703
3704 m_lastOnSame = false;
3705 m_lineSelectSingleOnUp = (unsigned int)-1;
3706 }
3707 else if(!event.LeftUp())
3708 {
3709 // This is necessary, because after a DnD operation in
3710 // from and to ourself, the up event is swallowed by the
3711 // DnD code. So on next non-up event (which means here and
3712 // now) m_lineSelectSingleOnUp should be reset.
3713 m_lineSelectSingleOnUp = (unsigned int)-1;
3714 }
3715
3716 if (event.RightDown())
3717 {
3718 m_lineBeforeLastClicked = m_lineLastClicked;
3719 m_lineLastClicked = current;
3720
3721 // If the item is already selected, do not update the selection.
3722 // Multi-selections should not be cleared if a selected item is clicked.
3723 if (!IsRowSelected(current))
3724 {
3725 SelectAllRows(false);
3726 ChangeCurrentRow(current);
3727 SelectRow(m_currentRow,true);
3728 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
3729 }
3730 }
3731 else if (event.RightUp())
3732 {
3733 wxVariant value;
3734 model->GetValue( value, item, col->GetModelColumn() );
3735 wxWindow *parent = GetParent();
3736 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
3737 le.SetItem( item );
3738 le.SetColumn( col->GetModelColumn() );
3739 le.SetDataViewColumn( col );
3740 le.SetEventObject(parent);
3741 le.SetModel(GetOwner()->GetModel());
3742 le.SetValue(value);
3743 parent->GetEventHandler()->ProcessEvent(le);
3744 }
3745 else if (event.MiddleDown())
3746 {
3747 }
3748
3749 if((event.LeftDown() || simulateClick) && hoverOverExpander)
3750 {
3751 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
3752
3753 // hoverOverExpander being true tells us that our node must be
3754 // valid and have children.
3755 // So we don't need any extra checks.
3756 if( node->IsOpen() )
3757 Collapse(current);
3758 else
3759 Expand(current);
3760 }
3761 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
3762 {
3763 m_lineBeforeLastClicked = m_lineLastClicked;
3764 m_lineLastClicked = current;
3765
3766 unsigned int oldCurrentRow = m_currentRow;
3767 bool oldWasSelected = IsRowSelected(m_currentRow);
3768
3769 bool cmdModifierDown = event.CmdDown();
3770 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3771 {
3772 if ( IsSingleSel() || !IsRowSelected(current) )
3773 {
3774 SelectAllRows( false );
3775 ChangeCurrentRow(current);
3776 SelectRow(m_currentRow,true);
3777 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
3778 }
3779 else // multi sel & current is highlighted & no mod keys
3780 {
3781 m_lineSelectSingleOnUp = current;
3782 ChangeCurrentRow(current); // change focus
3783 }
3784 }
3785 else // multi sel & either ctrl or shift is down
3786 {
3787 if (cmdModifierDown)
3788 {
3789 ChangeCurrentRow(current);
3790 ReverseRowSelection(m_currentRow);
3791 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
3792 }
3793 else if (event.ShiftDown())
3794 {
3795 ChangeCurrentRow(current);
3796
3797 unsigned int lineFrom = oldCurrentRow,
3798 lineTo = current;
3799
3800 if ( lineTo < lineFrom )
3801 {
3802 lineTo = lineFrom;
3803 lineFrom = m_currentRow;
3804 }
3805
3806 SelectRows(lineFrom, lineTo, true);
3807 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
3808 }
3809 else // !ctrl, !shift
3810 {
3811 // test in the enclosing if should make it impossible
3812 wxFAIL_MSG( wxT("how did we get here?") );
3813 }
3814 }
3815
3816 if (m_currentRow != oldCurrentRow)
3817 RefreshRow( oldCurrentRow );
3818
3819 wxDataViewColumn *oldCurrentCol = m_currentCol;
3820
3821 // Update selection here...
3822 m_currentCol = col;
3823
3824 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
3825 (current == oldCurrentRow)) && oldWasSelected;
3826
3827 // Call LeftClick after everything else as under GTK+
3828 if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
3829 {
3830 // notify cell about click
3831 cell->PrepareForItem(model, item, col->GetModelColumn());
3832
3833 wxRect cell_rect( xpos, GetLineStart( current ),
3834 col->GetWidth(), GetLineHeight( current ) );
3835
3836 // Report position relative to the cell's custom area, i.e.
3837 // no the entire space as given by the control but the one
3838 // used by the renderer after calculation of alignment etc.
3839
3840 // adjust the rectangle ourselves to account for the alignment
3841 wxRect rectItem = cell_rect;
3842 const int align = cell->GetAlignment();
3843 if ( align != wxDVR_DEFAULT_ALIGNMENT )
3844 {
3845 const wxSize size = cell->GetSize();
3846
3847 if ( size.x >= 0 && size.x < cell_rect.width )
3848 {
3849 if ( align & wxALIGN_CENTER_HORIZONTAL )
3850 rectItem.x += (cell_rect.width - size.x)/2;
3851 else if ( align & wxALIGN_RIGHT )
3852 rectItem.x += cell_rect.width - size.x;
3853 // else: wxALIGN_LEFT is the default
3854 }
3855
3856 if ( size.y >= 0 && size.y < cell_rect.height )
3857 {
3858 if ( align & wxALIGN_CENTER_VERTICAL )
3859 rectItem.y += (cell_rect.height - size.y)/2;
3860 else if ( align & wxALIGN_BOTTOM )
3861 rectItem.y += cell_rect.height - size.y;
3862 // else: wxALIGN_TOP is the default
3863 }
3864 }
3865
3866 wxPoint pos( event.GetPosition() );
3867 pos.x -= rectItem.x;
3868 pos.y -= rectItem.y;
3869
3870 m_owner->CalcUnscrolledPosition( pos.x, pos.y, &pos.x, &pos.y );
3871
3872 /* ignore ret */ cell->WXOnLeftClick( pos, cell_rect,
3873 model, item, col->GetModelColumn());
3874 }
3875 }
3876 }
3877
3878 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3879 {
3880 m_hasFocus = true;
3881
3882 if (HasCurrentRow())
3883 Refresh();
3884
3885 event.Skip();
3886 }
3887
3888 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3889 {
3890 m_hasFocus = false;
3891
3892 if (HasCurrentRow())
3893 Refresh();
3894
3895 event.Skip();
3896 }
3897
3898 wxDataViewItem wxDataViewMainWindow::GetSelection() const
3899 {
3900 if( m_selection.GetCount() != 1 )
3901 return wxDataViewItem();
3902
3903 return GetItemByRow( m_selection.Item(0));
3904 }
3905
3906 //-----------------------------------------------------------------------------
3907 // wxDataViewCtrl
3908 //-----------------------------------------------------------------------------
3909
3910 WX_DEFINE_LIST(wxDataViewColumnList)
3911
3912 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3913 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3914 EVT_SIZE(wxDataViewCtrl::OnSize)
3915 END_EVENT_TABLE()
3916
3917 wxDataViewCtrl::~wxDataViewCtrl()
3918 {
3919 if (m_notifier)
3920 GetModel()->RemoveNotifier( m_notifier );
3921
3922 m_cols.Clear();
3923 m_colsBestWidths.clear();
3924 }
3925
3926 void wxDataViewCtrl::Init()
3927 {
3928 m_cols.DeleteContents(true);
3929 m_notifier = NULL;
3930
3931 // No sorting column at start
3932 m_sortingColumnIdx = wxNOT_FOUND;
3933
3934 m_headerArea = NULL;
3935 }
3936
3937 bool wxDataViewCtrl::Create(wxWindow *parent,
3938 wxWindowID id,
3939 const wxPoint& pos,
3940 const wxSize& size,
3941 long style,
3942 const wxValidator& validator,
3943 const wxString& name)
3944 {
3945 // if ( (style & wxBORDER_MASK) == 0)
3946 // style |= wxBORDER_SUNKEN;
3947
3948 Init();
3949
3950 if (!wxControl::Create( parent, id, pos, size,
3951 style | wxScrolledWindowStyle, validator, name))
3952 return false;
3953
3954 SetInitialSize(size);
3955
3956 #ifdef __WXMAC__
3957 MacSetClipChildren( true );
3958 #endif
3959
3960 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
3961
3962 // We use the cursor keys for moving the selection, not scrolling, so call
3963 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3964 // keyboard events forwarded to us from wxListMainWindow.
3965 DisableKeyboardScrolling();
3966
3967 if (HasFlag(wxDV_NO_HEADER))
3968 m_headerArea = NULL;
3969 else
3970 m_headerArea = new wxDataViewHeaderWindow(this);
3971
3972 SetTargetWindow( m_clientArea );
3973
3974 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
3975 if (m_headerArea)
3976 sizer->Add( m_headerArea, 0, wxGROW );
3977 sizer->Add( m_clientArea, 1, wxGROW );
3978 SetSizer( sizer );
3979
3980 return true;
3981 }
3982
3983 wxBorder wxDataViewCtrl::GetDefaultBorder() const
3984 {
3985 return wxBORDER_THEME;
3986 }
3987
3988 #ifdef __WXMSW__
3989 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
3990 WXWPARAM wParam,
3991 WXLPARAM lParam)
3992 {
3993 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
3994
3995 #ifndef __WXWINCE__
3996 // we need to process arrows ourselves for scrolling
3997 if ( nMsg == WM_GETDLGCODE )
3998 {
3999 rc |= DLGC_WANTARROWS;
4000 }
4001 #endif
4002
4003 return rc;
4004 }
4005 #endif
4006
4007 wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4008 {
4009 wxSize newsize = size;
4010 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
4011 newsize.y -= m_headerArea->GetSize().y;
4012
4013 return newsize;
4014 }
4015
4016 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4017 {
4018 // We need to override OnSize so that our scrolled
4019 // window a) does call Layout() to use sizers for
4020 // positioning the controls but b) does not query
4021 // the sizer for their size and use that for setting
4022 // the scrollable area as set that ourselves by
4023 // calling SetScrollbar() further down.
4024
4025 Layout();
4026
4027 AdjustScrollbars();
4028 }
4029
4030 void wxDataViewCtrl::SetFocus()
4031 {
4032 if (m_clientArea)
4033 m_clientArea->SetFocus();
4034 }
4035
4036 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4037 {
4038 if (!wxDataViewCtrlBase::AssociateModel( model ))
4039 return false;
4040
4041 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4042
4043 model->AddNotifier( m_notifier );
4044
4045 m_clientArea->DestroyTree();
4046
4047 m_clientArea->BuildTree(model);
4048
4049 m_clientArea->UpdateDisplay();
4050
4051 return true;
4052 }
4053
4054 #if wxUSE_DRAG_AND_DROP
4055
4056 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4057 {
4058 return m_clientArea->EnableDragSource( format );
4059 }
4060
4061 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4062 {
4063 return m_clientArea->EnableDropTarget( format );
4064 }
4065
4066 #endif // wxUSE_DRAG_AND_DROP
4067
4068 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4069 {
4070 if (!wxDataViewCtrlBase::AppendColumn(col))
4071 return false;
4072
4073 m_cols.Append( col );
4074 m_colsBestWidths.push_back(0);
4075 OnColumnsCountChanged();
4076 return true;
4077 }
4078
4079 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4080 {
4081 if (!wxDataViewCtrlBase::PrependColumn(col))
4082 return false;
4083
4084 m_cols.Insert( col );
4085 m_colsBestWidths.insert(m_colsBestWidths.begin(), 0);
4086 OnColumnsCountChanged();
4087 return true;
4088 }
4089
4090 bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4091 {
4092 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4093 return false;
4094
4095 m_cols.Insert( pos, col );
4096 m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, 0);
4097 OnColumnsCountChanged();
4098 return true;
4099 }
4100
4101 void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4102 {
4103 if ( m_headerArea )
4104 m_headerArea->UpdateColumn(idx);
4105
4106 m_clientArea->UpdateDisplay();
4107 }
4108
4109 void wxDataViewCtrl::OnColumnsCountChanged()
4110 {
4111 if (m_headerArea)
4112 m_headerArea->SetColumnCount(GetColumnCount());
4113
4114 m_clientArea->UpdateDisplay();
4115 }
4116
4117 void wxDataViewCtrl::DoSetExpanderColumn()
4118 {
4119 m_clientArea->UpdateDisplay();
4120 }
4121
4122 void wxDataViewCtrl::DoSetIndent()
4123 {
4124 m_clientArea->UpdateDisplay();
4125 }
4126
4127 unsigned int wxDataViewCtrl::GetColumnCount() const
4128 {
4129 return m_cols.GetCount();
4130 }
4131
4132 bool wxDataViewCtrl::SetRowHeight( int lineHeight )
4133 {
4134 if ( !m_clientArea )
4135 return false;
4136
4137 m_clientArea->SetRowHeight(lineHeight);
4138
4139 return true;
4140 }
4141
4142 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
4143 {
4144 return m_cols[idx];
4145 }
4146
4147 wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
4148 {
4149 // columns can't be reordered if there is no header window which allows
4150 // to do this
4151 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
4152 : pos;
4153
4154 return GetColumn(idx);
4155 }
4156
4157 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4158 {
4159 const unsigned count = m_cols.size();
4160 for ( unsigned n = 0; n < count; n++ )
4161 {
4162 if ( m_cols[n] == column )
4163 return n;
4164 }
4165
4166 return wxNOT_FOUND;
4167 }
4168
4169 unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const
4170 {
4171 if ( m_colsBestWidths[idx] != 0 )
4172 return m_colsBestWidths[idx];
4173
4174 const int count = m_clientArea->GetRowCount();
4175 wxDataViewColumn *column = GetColumn(idx);
4176 wxDataViewRenderer *renderer =
4177 const_cast<wxDataViewRenderer*>(column->GetRenderer());
4178
4179 class MaxWidthCalculator
4180 {
4181 public:
4182 MaxWidthCalculator(wxDataViewMainWindow *clientArea,
4183 wxDataViewRenderer *renderer,
4184 const wxDataViewModel *model,
4185 unsigned column)
4186 : m_width(0),
4187 m_clientArea(clientArea),
4188 m_renderer(renderer),
4189 m_model(model),
4190 m_column(column)
4191 {
4192 }
4193
4194 void UpdateWithWidth(int width)
4195 {
4196 m_width = wxMax(m_width, width);
4197 }
4198
4199 void UpdateWithRow(int row)
4200 {
4201 wxDataViewItem item = m_clientArea->GetItemByRow(row);
4202 m_renderer->PrepareForItem(m_model, item, m_column);
4203 m_width = wxMax(m_width, m_renderer->GetSize().x);
4204 }
4205
4206 int GetMaxWidth() const { return m_width; }
4207
4208 private:
4209 int m_width;
4210 wxDataViewMainWindow *m_clientArea;
4211 wxDataViewRenderer *m_renderer;
4212 const wxDataViewModel *m_model;
4213 unsigned m_column;
4214 };
4215
4216 MaxWidthCalculator calculator(m_clientArea, renderer,
4217 GetModel(), column->GetModelColumn());
4218
4219 if ( m_headerArea )
4220 {
4221 int header_width = m_headerArea->GetTextExtent(column->GetTitle()).x;
4222 // Labels on native MSW header are indented on both sides
4223 header_width +=
4224 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea);
4225 calculator.UpdateWithWidth(header_width);
4226 }
4227
4228 // The code below deserves some explanation. For very large controls, we
4229 // simply can't afford to calculate sizes for all items, it takes too
4230 // long. So the best we can do is to check the first and the last N/2
4231 // items in the control for some sufficiently large N and calculate best
4232 // sizes from that. That can result in the calculated best width being too
4233 // small for some outliers, but it's better to get slightly imperfect
4234 // result than to wait several seconds after every update. To avoid highly
4235 // visible miscalculations, we also include all currently visible items
4236 // no matter what. Finally, the value of N is determined dynamically by
4237 // measuring how much time we spent on the determining item widths so far.
4238
4239 #if wxUSE_STOPWATCH
4240 int top_part_end = count;
4241 static const long CALC_TIMEOUT = 20/*ms*/;
4242 // don't call wxStopWatch::Time() too often
4243 static const unsigned CALC_CHECK_FREQ = 100;
4244 wxStopWatch timer;
4245 #else
4246 // use some hard-coded limit, that's the best we can do without timer
4247 int top_part_end = wxMin(500, count);
4248 #endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4249
4250 int row = 0;
4251
4252 for ( row = 0; row < top_part_end; row++ )
4253 {
4254 #if wxUSE_STOPWATCH
4255 if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 &&
4256 timer.Time() > CALC_TIMEOUT )
4257 break;
4258 #endif // wxUSE_STOPWATCH
4259 calculator.UpdateWithRow(row);
4260 }
4261
4262 // row is the first unmeasured item now; that's our value of N/2
4263
4264 if ( row < count )
4265 {
4266 top_part_end = row;
4267
4268 // add bottom N/2 items now:
4269 const int bottom_part_start = wxMax(row, count - row);
4270 for ( row = bottom_part_start; row < count; row++ )
4271 {
4272 calculator.UpdateWithRow(row);
4273 }
4274
4275 // finally, include currently visible items in the calculation:
4276 const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0));
4277 int first_visible = m_clientArea->GetLineAt(origin.y);
4278 int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y);
4279
4280 first_visible = wxMax(first_visible, top_part_end);
4281 last_visible = wxMin(bottom_part_start, last_visible);
4282
4283 for ( row = first_visible; row < last_visible; row++ )
4284 {
4285 calculator.UpdateWithRow(row);
4286 }
4287
4288 wxLogTrace("dataview",
4289 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4290 top_part_end,
4291 count - bottom_part_start,
4292 wxMax(0, last_visible - first_visible),
4293 count);
4294 }
4295
4296 int max_width = calculator.GetMaxWidth();
4297 if ( max_width > 0 )
4298 max_width += 2 * PADDING_RIGHTLEFT;
4299
4300 const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx] = max_width;
4301 return max_width;
4302 }
4303
4304 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
4305 unsigned int WXUNUSED(new_pos))
4306 {
4307 // do _not_ reorder m_cols elements here, they should always be in the
4308 // order in which columns were added, we only display the columns in
4309 // different order
4310 m_clientArea->UpdateDisplay();
4311 }
4312
4313 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4314 {
4315 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4316 if (!ret)
4317 return false;
4318
4319 m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column));
4320 m_cols.Erase(ret);
4321 OnColumnsCountChanged();
4322
4323 return true;
4324 }
4325
4326 bool wxDataViewCtrl::ClearColumns()
4327 {
4328 m_cols.Clear();
4329 m_colsBestWidths.clear();
4330 OnColumnsCountChanged();
4331 return true;
4332 }
4333
4334 void wxDataViewCtrl::InvalidateColBestWidth(int idx)
4335 {
4336 m_colsBestWidths[idx] = 0;
4337
4338 if ( m_headerArea )
4339 m_headerArea->UpdateColumn(idx);
4340 }
4341
4342 void wxDataViewCtrl::InvalidateColBestWidths()
4343 {
4344 m_colsBestWidths.clear();
4345 m_colsBestWidths.resize(m_cols.size());
4346
4347 if ( m_headerArea )
4348 {
4349 const unsigned cols = m_headerArea->GetColumnCount();
4350 for ( unsigned i = 0; i < cols; i++ )
4351 m_headerArea->UpdateColumn(i);
4352 }
4353 }
4354
4355 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4356 {
4357 #if 1
4358 unsigned int len = GetColumnCount();
4359 for ( unsigned int i = 0; i < len; i++ )
4360 {
4361 wxDataViewColumn * col = GetColumnAt(i);
4362 if (column==col)
4363 return i;
4364 }
4365
4366 return wxNOT_FOUND;
4367 #else
4368 // This returns the position in pixels which is not what we want.
4369 int ret = 0,
4370 dummy = 0;
4371 unsigned int len = GetColumnCount();
4372 for ( unsigned int i = 0; i < len; i++ )
4373 {
4374 wxDataViewColumn * col = GetColumnAt(i);
4375 if (col->IsHidden())
4376 continue;
4377 ret += col->GetWidth();
4378 if (column==col)
4379 {
4380 CalcScrolledPosition( ret, dummy, &ret, &dummy );
4381 break;
4382 }
4383 }
4384 return ret;
4385 #endif
4386 }
4387
4388 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4389 {
4390 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
4391 : GetColumn(m_sortingColumnIdx);
4392 }
4393
4394 wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4395 {
4396 return GetItemByRow(m_clientArea->GetCurrentRow());
4397 }
4398
4399 void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4400 {
4401 const int row = m_clientArea->GetRowByItem(item);
4402
4403 const unsigned oldCurrent = m_clientArea->GetCurrentRow();
4404 if ( static_cast<unsigned>(row) != oldCurrent )
4405 {
4406 m_clientArea->ChangeCurrentRow(row);
4407 m_clientArea->RefreshRow(oldCurrent);
4408 m_clientArea->RefreshRow(row);
4409 }
4410 }
4411
4412 // Selection code with wxDataViewItem as parameters
4413 wxDataViewItem wxDataViewCtrl::GetSelection() const
4414 {
4415 return m_clientArea->GetSelection();
4416 }
4417
4418 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4419 {
4420 sel.Empty();
4421 wxDataViewSelection selection = m_clientArea->GetSelections();
4422 int len = selection.GetCount();
4423 for( int i = 0; i < len; i ++)
4424 {
4425 unsigned int row = selection[i];
4426 sel.Add( m_clientArea->GetItemByRow( row ) );
4427 }
4428 return len;
4429 }
4430
4431 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4432 {
4433 wxDataViewSelection selection(wxDataViewSelectionCmp);
4434
4435 wxDataViewItem last_parent;
4436
4437 int len = sel.GetCount();
4438 for( int i = 0; i < len; i ++ )
4439 {
4440 wxDataViewItem item = sel[i];
4441 wxDataViewItem parent = GetModel()->GetParent( item );
4442 if (parent)
4443 {
4444 if (parent != last_parent)
4445 ExpandAncestors(item);
4446 }
4447
4448 last_parent = parent;
4449 int row = m_clientArea->GetRowByItem( item );
4450 if( row >= 0 )
4451 selection.Add( static_cast<unsigned int>(row) );
4452 }
4453
4454 m_clientArea->SetSelections( selection );
4455 }
4456
4457 void wxDataViewCtrl::Select( const wxDataViewItem & item )
4458 {
4459 ExpandAncestors( item );
4460
4461 int row = m_clientArea->GetRowByItem( item );
4462 if( row >= 0 )
4463 {
4464 // Unselect all rows before select another in the single select mode
4465 if (m_clientArea->IsSingleSel())
4466 m_clientArea->SelectAllRows(false);
4467
4468 m_clientArea->SelectRow(row, true);
4469
4470 // Also set focus to the selected item
4471 m_clientArea->ChangeCurrentRow( row );
4472 }
4473 }
4474
4475 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
4476 {
4477 int row = m_clientArea->GetRowByItem( item );
4478 if( row >= 0 )
4479 m_clientArea->SelectRow(row, false);
4480 }
4481
4482 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
4483 {
4484 int row = m_clientArea->GetRowByItem( item );
4485 if( row >= 0 )
4486 {
4487 return m_clientArea->IsRowSelected(row);
4488 }
4489 return false;
4490 }
4491
4492 // Selection code with row number as parameter
4493 int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
4494 {
4495 sel.Empty();
4496 wxDataViewSelection selection = m_clientArea->GetSelections();
4497 int len = selection.GetCount();
4498 for( int i = 0; i < len; i ++)
4499 {
4500 unsigned int row = selection[i];
4501 sel.Add( row );
4502 }
4503 return len;
4504 }
4505
4506 void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
4507 {
4508 wxDataViewSelection selection(wxDataViewSelectionCmp);
4509 int len = sel.GetCount();
4510 for( int i = 0; i < len; i ++ )
4511 {
4512 int row = sel[i];
4513 if( row >= 0 )
4514 selection.Add( static_cast<unsigned int>(row) );
4515 }
4516 m_clientArea->SetSelections( selection );
4517 }
4518
4519 void wxDataViewCtrl::Select( int row )
4520 {
4521 if( row >= 0 )
4522 {
4523 if (m_clientArea->IsSingleSel())
4524 m_clientArea->SelectAllRows(false);
4525 m_clientArea->SelectRow( row, true );
4526 }
4527 }
4528
4529 void wxDataViewCtrl::Unselect( int row )
4530 {
4531 if( row >= 0 )
4532 m_clientArea->SelectRow(row, false);
4533 }
4534
4535 bool wxDataViewCtrl::IsSelected( int row ) const
4536 {
4537 if( row >= 0 )
4538 return m_clientArea->IsRowSelected(row);
4539 return false;
4540 }
4541
4542 void wxDataViewCtrl::SelectRange( int from, int to )
4543 {
4544 wxArrayInt sel;
4545 for( int i = from; i < to; i ++ )
4546 sel.Add( i );
4547 m_clientArea->Select(sel);
4548 }
4549
4550 void wxDataViewCtrl::UnselectRange( int from, int to )
4551 {
4552 wxDataViewSelection sel = m_clientArea->GetSelections();
4553 for( int i = from; i < to; i ++ )
4554 if( sel.Index( i ) != wxNOT_FOUND )
4555 sel.Remove( i );
4556 m_clientArea->SetSelections(sel);
4557 }
4558
4559 void wxDataViewCtrl::SelectAll()
4560 {
4561 m_clientArea->SelectAllRows(true);
4562 }
4563
4564 void wxDataViewCtrl::UnselectAll()
4565 {
4566 m_clientArea->SelectAllRows(false);
4567 }
4568
4569 void wxDataViewCtrl::EnsureVisible( int row, int column )
4570 {
4571 if( row < 0 )
4572 row = 0;
4573 if( row > (int) m_clientArea->GetRowCount() )
4574 row = m_clientArea->GetRowCount();
4575
4576 int first = m_clientArea->GetFirstVisibleRow();
4577 int last = m_clientArea->GetLastVisibleRow();
4578 if( row < first )
4579 m_clientArea->ScrollTo( row, column );
4580 else if( row > last )
4581 m_clientArea->ScrollTo( row - last + first, column );
4582 else
4583 m_clientArea->ScrollTo( first, column );
4584 }
4585
4586 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
4587 {
4588 ExpandAncestors( item );
4589
4590 m_clientArea->RecalculateDisplay();
4591
4592 int row = m_clientArea->GetRowByItem(item);
4593 if( row >= 0 )
4594 {
4595 if( column == NULL )
4596 EnsureVisible(row, -1);
4597 else
4598 EnsureVisible( row, GetColumnIndex(column) );
4599 }
4600
4601 }
4602
4603 void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
4604 wxDataViewColumn* &column ) const
4605 {
4606 m_clientArea->HitTest(point, item, column);
4607 }
4608
4609 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
4610 const wxDataViewColumn* column ) const
4611 {
4612 return m_clientArea->GetItemRect(item, column);
4613 }
4614
4615 wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
4616 {
4617 return m_clientArea->GetItemByRow( row );
4618 }
4619
4620 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
4621 {
4622 return m_clientArea->GetRowByItem( item );
4623 }
4624
4625 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4626 {
4627 ExpandAncestors( item );
4628
4629 int row = m_clientArea->GetRowByItem( item );
4630 if (row != -1)
4631 m_clientArea->Expand(row);
4632 }
4633
4634 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4635 {
4636 int row = m_clientArea->GetRowByItem( item );
4637 if (row != -1)
4638 m_clientArea->Collapse(row);
4639 }
4640
4641 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4642 {
4643 int row = m_clientArea->GetRowByItem( item );
4644 if (row != -1)
4645 return m_clientArea->IsExpanded(row);
4646 return false;
4647 }
4648
4649 void wxDataViewCtrl::StartEditor( const wxDataViewItem & item, unsigned int column )
4650 {
4651 wxDataViewColumn* col = GetColumn( column );
4652 if (!col)
4653 return;
4654
4655 wxRect itemRect = GetItemRect(item, col);
4656 wxDataViewRenderer* renderer = col->GetRenderer();
4657 if (renderer->GetMode() == wxDATAVIEW_CELL_EDITABLE)
4658 renderer->StartEditing(item, itemRect);
4659 }
4660
4661 #endif // !wxUSE_GENERICDATAVIEWCTRL
4662
4663 #endif // wxUSE_DATAVIEWCTRL