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