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