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