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