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