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