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