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