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