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