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