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