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