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