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
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
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"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 #define USE_NATIVE_HEADER_WINDOW 1
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called Refresh afterwards
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
144 virtual bool CanApplyThemeBorder() const { return false; }
147 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
149 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
151 wxSize
DoGetBestSize() const;
153 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
155 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
156 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
162 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
165 #else // !defined(__WXMSW__)
167 #define HEADER_WINDOW_HEIGHT 25
168 #define HEADER_HORIZ_BORDER 5
169 #define HEADER_VERT_BORDER 3
170 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
172 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
175 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
177 const wxPoint
&pos
= wxDefaultPosition
,
178 const wxSize
&size
= wxDefaultSize
,
179 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
182 Create(parent
, id
, pos
, size
, name
);
185 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
186 const wxPoint
&pos
, const wxSize
&size
,
187 const wxString
&name
);
189 ~wxGenericDataViewHeaderWindow()
191 delete m_resizeCursor
;
194 virtual void UpdateDisplay() { Refresh(); }
198 void OnPaint( wxPaintEvent
&event
);
199 void OnMouse( wxMouseEvent
&event
);
200 void OnSetFocus( wxFocusEvent
&event
);
205 // vars used for column resizing:
207 wxCursor
*m_resizeCursor
;
208 const wxCursor
*m_currentCursor
;
211 bool m_dirty
; // needs refresh?
212 int m_hover
; // index of the column under the mouse
213 int m_column
; // index of the column being resized
214 int m_currentX
; // divider line position in logical (unscrolled) coords
215 int m_minX
; // minimal position beyond which the divider line
216 // can't be dragged in logical coords
218 // the pen used to draw the current column width drag line
219 // when resizing the columsn
223 // internal utilities:
227 m_currentCursor
= (wxCursor
*) NULL
;
228 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
230 m_isDragging
= false;
233 m_hover
= wxNOT_FOUND
;
234 m_column
= wxNOT_FOUND
;
238 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
239 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
242 void AdjustDC(wxDC
& dc
);
245 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
246 DECLARE_EVENT_TABLE()
249 #endif // defined(__WXMSW__)
251 //-----------------------------------------------------------------------------
252 // wxDataViewRenameTimer
253 //-----------------------------------------------------------------------------
255 class wxDataViewRenameTimer
: public wxTimer
258 wxDataViewMainWindow
*m_owner
;
261 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
265 //-----------------------------------------------------------------------------
266 // wxDataViewTreeNode
267 //-----------------------------------------------------------------------------
268 class wxDataViewTreeNode
;
269 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
270 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
272 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
273 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
275 class wxDataViewTreeNode
278 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
285 m_hasChildren
= false;
289 ~wxDataViewTreeNode()
293 wxDataViewTreeNode
* GetParent() { return m_parent
; }
294 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
295 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
296 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
298 void AddNode( wxDataViewTreeNode
* node
)
300 m_leaves
.Add( node
->GetItem().GetID() );
302 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
305 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
307 void AddLeaf( void * leaf
)
309 m_leaves
.Add( leaf
);
311 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
314 wxDataViewItem
& GetItem() { return m_item
; }
315 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
317 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
318 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
322 wxDataViewTreeNode
* node
= this;
323 while( node
->GetParent()->GetParent() != NULL
)
325 node
= node
->GetParent();
338 int len
= m_nodes
.GetCount();
340 for ( int i
= 0 ;i
< len
; i
++)
341 sum
+= m_nodes
[i
]->GetSubTreeCount();
343 sum
+= m_leaves
.GetCount();
346 ChangeSubTreeCount(-sum
);
352 ChangeSubTreeCount(sum
);
355 bool HasChildren() { return m_hasChildren
; }
356 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
358 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
359 int GetSubTreeCount() { return m_subTreeCount
; }
360 void ChangeSubTreeCount( int num
)
364 m_subTreeCount
+= num
;
366 m_parent
->ChangeSubTreeCount(num
);
373 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
374 int len
= m_nodes
.GetCount();
375 for (int i
= 0; i
< len
; i
++)
376 m_nodes
[i
]->Resort();
377 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
382 wxDataViewTreeNode
*m_parent
;
383 wxDataViewTreeNodes m_nodes
;
384 wxDataViewTreeLeaves m_leaves
;
385 wxDataViewItem m_item
;
391 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
393 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
396 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
398 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
403 //-----------------------------------------------------------------------------
404 // wxDataViewMainWindow
405 //-----------------------------------------------------------------------------
407 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
409 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
410 WX_DEFINE_LIST(ItemList
)
412 class wxDataViewMainWindow
: public wxWindow
415 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
417 const wxPoint
&pos
= wxDefaultPosition
,
418 const wxSize
&size
= wxDefaultSize
,
419 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
420 virtual ~wxDataViewMainWindow();
422 // notifications from wxDataViewModel
423 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
424 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
425 bool ItemChanged( const wxDataViewItem
&item
);
426 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
440 g_model
= GetOwner()->GetModel();
441 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
444 if (g_model
->HasDefaultCompare())
452 g_column
= col
->GetModelColumn();
453 g_asending
= col
->IsSortOrderAscending();
456 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
457 wxDataViewCtrl
*GetOwner() { return m_owner
; }
458 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
460 void OnPaint( wxPaintEvent
&event
);
461 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
462 void OnChar( wxKeyEvent
&event
);
463 void OnMouse( wxMouseEvent
&event
);
464 void OnSetFocus( wxFocusEvent
&event
);
465 void OnKillFocus( wxFocusEvent
&event
);
467 void UpdateDisplay();
468 void RecalculateDisplay();
469 void OnInternalIdle();
471 void OnRenameTimer();
473 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
474 void ScrollTo( int rows
, int column
);
476 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
477 void ChangeCurrentRow( unsigned int row
);
479 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
480 bool IsEmpty() { return GetRowCount() == 0; }
482 int GetCountPerPage() const;
483 int GetEndOfLastCol() const;
484 unsigned int GetFirstVisibleRow() const;
485 //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
486 unsigned int GetLastVisibleRow();
487 unsigned int GetRowCount() ;
489 wxDataViewItem
GetSelection() const;
490 wxDataViewSelection
GetSelections(){ return m_selection
; }
491 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
492 void Select( const wxArrayInt
& aSelections
);
493 void SelectAllRows( bool on
);
494 void SelectRow( unsigned int row
, bool on
);
495 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
496 void ReverseRowSelection( unsigned int row
);
497 bool IsRowSelected( unsigned int row
);
498 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
500 void RefreshRow( unsigned int row
);
501 void RefreshRows( unsigned int from
, unsigned int to
);
502 void RefreshRowsAfter( unsigned int firstRow
);
504 // returns the colour to be used for drawing the rules
505 wxColour
GetRuleColour() const
507 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
510 wxRect
GetLineRect( unsigned int row
) const;
512 //Some useful functions for row and item mapping
513 wxDataViewItem
GetItemByRow( unsigned int row
) const;
514 int GetRowByItem( const wxDataViewItem
& item
);
516 //Methods for building the mapping tree
517 void BuildTree( wxDataViewModel
* model
);
519 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
520 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
522 void Expand( unsigned int row
) { OnExpanding( row
); }
523 void Collapse( unsigned int row
) { OnCollapsing( row
); }
525 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
526 //We did not need this temporarily
527 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
529 int RecalculateCount() ;
531 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
532 void OnExpanding( unsigned int row
);
533 void OnCollapsing( unsigned int row
);
535 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
538 wxDataViewCtrl
*m_owner
;
542 wxDataViewColumn
*m_currentCol
;
543 unsigned int m_currentRow
;
544 wxDataViewSelection m_selection
;
546 wxDataViewRenameTimer
*m_renameTimer
;
554 // for double click logic
555 unsigned int m_lineLastClicked
,
556 m_lineBeforeLastClicked
,
557 m_lineSelectSingleOnUp
;
559 // the pen used to draw horiz/vertical rules
562 // the pen used to draw the expander and the lines
565 //This is the tree structure of the model
566 wxDataViewTreeNode
* m_root
;
568 //This is the tree node under the cursor
569 wxDataViewTreeNode
* m_underMouse
;
571 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
572 DECLARE_EVENT_TABLE()
575 // ---------------------------------------------------------
576 // wxGenericDataViewModelNotifier
577 // ---------------------------------------------------------
579 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
582 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
583 { m_mainWindow
= mainWindow
; }
585 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
586 { return m_mainWindow
->ItemAdded( parent
, item
); }
587 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
588 { return m_mainWindow
->ItemDeleted( parent
, item
); }
589 virtual bool ItemChanged( const wxDataViewItem
& item
)
590 { return m_mainWindow
->ItemChanged(item
); }
591 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
592 { return m_mainWindow
->ValueChanged( item
, col
); }
593 virtual bool Cleared()
594 { return m_mainWindow
->Cleared(); }
595 virtual void Resort()
596 { m_mainWindow
->Resort(); }
598 wxDataViewMainWindow
*m_mainWindow
;
601 // ---------------------------------------------------------
602 // wxDataViewRenderer
603 // ---------------------------------------------------------
605 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
607 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
608 wxDataViewCellMode mode
,
610 wxDataViewRendererBase( varianttype
, mode
, align
)
619 wxDataViewRenderer::~wxDataViewRenderer()
625 wxDC
*wxDataViewRenderer::GetDC()
629 if (GetOwner() == NULL
)
631 if (GetOwner()->GetOwner() == NULL
)
633 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
639 // ---------------------------------------------------------
640 // wxDataViewCustomRenderer
641 // ---------------------------------------------------------
643 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
645 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
646 wxDataViewCellMode mode
, int align
) :
647 wxDataViewRenderer( varianttype
, mode
, align
)
651 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
653 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
654 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
655 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
656 view
->GetForegroundColour();
657 dc
->SetTextForeground(col
);
658 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
661 // ---------------------------------------------------------
662 // wxDataViewTextRenderer
663 // ---------------------------------------------------------
665 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
667 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
668 wxDataViewCellMode mode
, int align
) :
669 wxDataViewCustomRenderer( varianttype
, mode
, align
)
673 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
675 m_text
= value
.GetString();
680 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
685 bool wxDataViewTextRenderer::HasEditorCtrl()
690 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
691 wxRect labelRect
, const wxVariant
&value
)
693 return new wxTextCtrl( parent
, wxID_ANY
, value
,
694 wxPoint(labelRect
.x
,labelRect
.y
),
695 wxSize(labelRect
.width
,labelRect
.height
) );
698 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
700 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
701 value
= text
->GetValue();
705 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
707 RenderText( m_text
, 0, cell
, dc
, state
);
711 wxSize
wxDataViewTextRenderer::GetSize() const
713 const wxDataViewCtrl
*view
= GetView();
717 view
->GetTextExtent( m_text
, &x
, &y
);
718 return wxSize( x
, y
);
720 return wxSize(80,20);
723 // ---------------------------------------------------------
724 // wxDataViewTextRendererAttr
725 // ---------------------------------------------------------
727 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
729 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
730 wxDataViewCellMode mode
, int align
) :
731 wxDataViewTextRenderer( varianttype
, mode
, align
)
736 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
743 if (m_attr
.HasColour())
745 colour
= dc
->GetTextForeground();
746 dc
->SetTextForeground( m_attr
.GetColour() );
749 if (m_attr
.GetBold() || m_attr
.GetItalic())
751 font
= dc
->GetFont();
752 wxFont myfont
= font
;
753 if (m_attr
.GetBold())
754 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
755 if (m_attr
.GetItalic())
756 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
757 dc
->SetFont( myfont
);
761 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
766 if (m_attr
.HasColour())
767 dc
->SetTextForeground( colour
);
769 if (m_attr
.GetBold() || m_attr
.GetItalic())
777 // ---------------------------------------------------------
778 // wxDataViewBitmapRenderer
779 // ---------------------------------------------------------
781 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
783 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
784 wxDataViewCellMode mode
, int align
) :
785 wxDataViewCustomRenderer( varianttype
, mode
, align
)
789 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
791 if (value
.GetType() == wxT("wxBitmap"))
793 if (value
.GetType() == wxT("wxIcon"))
799 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
804 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
807 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
808 else if (m_icon
.Ok())
809 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
814 wxSize
wxDataViewBitmapRenderer::GetSize() const
817 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
818 else if (m_icon
.Ok())
819 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
821 return wxSize(16,16);
824 // ---------------------------------------------------------
825 // wxDataViewToggleRenderer
826 // ---------------------------------------------------------
828 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
830 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
831 wxDataViewCellMode mode
, int align
) :
832 wxDataViewCustomRenderer( varianttype
, mode
, align
)
837 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
839 m_toggle
= value
.GetBool();
844 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
849 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
851 // User wxRenderer here
854 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
856 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
861 flags
|= wxCONTROL_CHECKED
;
862 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
863 flags
|= wxCONTROL_DISABLED
;
865 wxRendererNative::Get().DrawCheckBox(
866 GetOwner()->GetOwner(),
874 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
875 wxDataViewModel
*model
,
876 const wxDataViewItem
& item
, unsigned int col
)
878 bool value
= !m_toggle
;
879 wxVariant variant
= value
;
880 model
->SetValue( variant
, item
, col
);
881 model
->ValueChanged( item
, col
);
885 wxSize
wxDataViewToggleRenderer::GetSize() const
887 return wxSize(20,20);
890 // ---------------------------------------------------------
891 // wxDataViewProgressRenderer
892 // ---------------------------------------------------------
894 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
896 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
897 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
898 wxDataViewCustomRenderer( varianttype
, mode
, align
)
904 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
908 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
910 m_value
= (long) value
;
912 if (m_value
< 0) m_value
= 0;
913 if (m_value
> 100) m_value
= 100;
918 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
920 value
= (long) m_value
;
924 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
926 double pct
= (double)m_value
/ 100.0;
928 bar
.width
= (int)(cell
.width
* pct
);
929 dc
->SetPen( *wxTRANSPARENT_PEN
);
930 dc
->SetBrush( *wxBLUE_BRUSH
);
931 dc
->DrawRectangle( bar
);
933 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
934 dc
->SetPen( *wxBLACK_PEN
);
935 dc
->DrawRectangle( cell
);
940 wxSize
wxDataViewProgressRenderer::GetSize() const
942 return wxSize(40,12);
945 // ---------------------------------------------------------
946 // wxDataViewDateRenderer
947 // ---------------------------------------------------------
949 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
951 #if wxUSE_DATE_RENDERER_POPUP
953 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
956 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
957 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
958 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
963 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
964 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
965 sizer
->Add( m_cal
, 1, wxGROW
);
970 void OnCalendar( wxCalendarEvent
&event
);
972 wxCalendarCtrl
*m_cal
;
973 wxDataViewModel
*m_model
;
975 const wxDataViewItem
& m_item
;
978 virtual void OnDismiss()
983 DECLARE_EVENT_TABLE()
986 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
987 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
990 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
992 wxDateTime date
= event
.GetDate();
993 wxVariant value
= date
;
994 m_model
->SetValue( value
, m_item
, m_col
);
995 m_model
->ValueChanged( m_item
, m_col
);
999 #endif // wxUSE_DATE_RENDERER_POPUP
1001 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1003 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1004 wxDataViewCellMode mode
, int align
) :
1005 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1009 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1011 m_date
= value
.GetDateTime();
1016 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1022 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1024 wxString tmp
= m_date
.FormatDate();
1025 RenderText( tmp
, 0, cell
, dc
, state
);
1029 wxSize
wxDataViewDateRenderer::GetSize() const
1031 const wxDataViewCtrl
* view
= GetView();
1032 wxString tmp
= m_date
.FormatDate();
1034 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1035 return wxSize(x
,y
+d
);
1038 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1039 const wxDataViewItem
& item
, unsigned int col
)
1042 model
->GetValue( variant
, item
, col
);
1043 wxDateTime value
= variant
.GetDateTime();
1045 #if wxUSE_DATE_RENDERER_POPUP
1046 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1047 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1048 wxPoint pos
= wxGetMousePosition();
1051 popup
->Popup( popup
->m_cal
);
1052 #else // !wxUSE_DATE_RENDERER_POPUP
1053 wxMessageBox(value
.Format());
1054 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1058 // ---------------------------------------------------------
1059 // wxDataViewIconTextRenderer
1060 // ---------------------------------------------------------
1062 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1064 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1065 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1066 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1069 SetAlignment(align
);
1072 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1076 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1082 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1087 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1090 const wxIcon
&icon
= m_value
.GetIcon();
1093 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1094 xoffset
= icon
.GetWidth()+4;
1097 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1102 wxSize
wxDataViewIconTextRenderer::GetSize() const
1104 const wxDataViewCtrl
*view
= GetView();
1105 if (!m_value
.GetText().empty())
1108 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1110 if (m_value
.GetIcon().IsOk())
1111 x
+= m_value
.GetIcon().GetWidth() + 4;
1112 return wxSize( x
, y
);
1114 return wxSize(80,20);
1118 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1119 wxRect
WXUNUSED(labelRect
),
1120 const wxVariant
& WXUNUSED(value
))
1126 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1127 wxVariant
& WXUNUSED(value
))
1132 // ---------------------------------------------------------
1134 // ---------------------------------------------------------
1136 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1138 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1139 unsigned int model_column
,
1140 int width
, wxAlignment align
, int flags
) :
1141 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1143 SetAlignment(align
);
1147 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1150 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1151 unsigned int model_column
,
1152 int width
, wxAlignment align
, int flags
) :
1153 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1155 SetAlignment(align
);
1158 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1161 wxDataViewColumn::~wxDataViewColumn()
1165 void wxDataViewColumn::Init( int width
)
1168 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1172 void wxDataViewColumn::SetResizeable( bool resizeable
)
1175 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1177 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1180 void wxDataViewColumn::SetHidden( bool hidden
)
1183 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1185 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1187 // tell our owner to e.g. update its scrollbars:
1189 GetOwner()->OnColumnChange();
1192 void wxDataViewColumn::SetSortable( bool sortable
)
1195 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1197 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1199 // Update header button
1201 GetOwner()->OnColumnChange();
1204 void wxDataViewColumn::SetReorderable( bool reorderable
)
1207 m_flags
|= wxDATAVIEW_COL_REORDERABLE
;
1209 m_flags
&= ~wxDATAVIEW_COL_REORDERABLE
;
1212 void wxDataViewColumn::SetSortOrder( bool ascending
)
1214 m_ascending
= ascending
;
1216 // Update header button
1218 GetOwner()->OnColumnChange();
1221 bool wxDataViewColumn::IsSortOrderAscending() const
1226 void wxDataViewColumn::SetInternalWidth( int width
)
1230 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1231 if (m_owner
&& m_owner
->m_clientArea
)
1232 m_owner
->m_clientArea
->RecalculateDisplay();
1235 void wxDataViewColumn::SetWidth( int width
)
1237 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1239 SetInternalWidth(width
);
1243 //-----------------------------------------------------------------------------
1244 // wxDataViewHeaderWindowBase
1245 //-----------------------------------------------------------------------------
1247 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1249 wxWindow
*parent
= GetParent();
1250 wxDataViewEvent
le(type
, parent
->GetId());
1252 le
.SetEventObject(parent
);
1254 le
.SetDataViewColumn(GetColumn(n
));
1255 le
.SetModel(GetOwner()->GetModel());
1257 // for events created by wxDataViewHeaderWindow the
1258 // row / value fields are not valid
1260 parent
->GetEventHandler()->ProcessEvent(le
);
1263 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1265 #ifndef HDS_DRAGDROP
1266 #define HDS_DRAGDROP 0x0040
1268 #ifndef HDS_FULLDRAG
1269 #define HDS_FULLDRAG 0x0080
1272 // implemented in msw/listctrl.cpp:
1273 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1275 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1277 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1278 const wxPoint
&pos
, const wxSize
&size
,
1279 const wxString
&name
)
1283 m_scrollOffsetX
= 0;
1284 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this );
1286 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1287 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1288 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1291 wxSize
new_size(w
,h
);
1293 if ( !CreateControl(parent
, id
, pos
, new_size
, 0, wxDefaultValidator
, name
) )
1296 // create the native WC_HEADER window:
1297 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1298 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1301 msStyle
|= WS_VISIBLE
;
1303 m_hWnd
= CreateWindowEx(0,
1314 wxLogLastError(_T("CreateWindowEx"));
1318 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1319 // to call wxDataViewHeaderWindow::MSWOnNotify
1320 SubclassWin(m_hWnd
);
1322 // the following is required to get the default win's font for
1323 // header windows and must be done befor sending the HDM_LAYOUT msg
1329 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1334 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1336 return wxSize(80, m_buttonHeight
);
1339 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1341 // remove old columns
1342 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1343 Header_DeleteItem((HWND
)m_hWnd
, 0);
1345 // add the updated array of columns to the header control
1346 unsigned int cols
= GetOwner()->GetColumnCount();
1347 unsigned int added
= 0;
1348 for (unsigned int i
= 0; i
< cols
; i
++)
1350 wxDataViewColumn
*col
= GetColumn( i
);
1351 if (col
->IsHidden())
1352 continue; // don't add it!
1355 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1356 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1357 hdi
.cxy
= col
->GetWidth();
1358 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1359 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1360 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1362 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1364 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1365 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1366 // VZ: works with 5.81
1367 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1370 // lParam is reserved for application's use:
1371 // we store there the column index to use it later in MSWOnNotify
1372 // (since columns may have been hidden)
1373 hdi
.lParam
= (LPARAM
)i
;
1375 // the native wxMSW implementation of the header window
1376 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1377 // on the right: to correct this effect we make the column
1378 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1380 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1382 switch (col
->GetAlignment())
1385 hdi
.fmt
|= HDF_LEFT
;
1387 case wxALIGN_CENTER
:
1388 case wxALIGN_CENTER_HORIZONTAL
:
1389 hdi
.fmt
|= HDF_CENTER
;
1392 hdi
.fmt
|= HDF_RIGHT
;
1396 // such alignment is not allowed for the column header!
1400 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1401 (WPARAM
)added
, (LPARAM
)&hdi
);
1406 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1410 // NOTE: we don't just return nmHDR->iItem because when there are
1411 // hidden columns, nmHDR->iItem may be different from
1412 // nmHDR->pitem->lParam
1414 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1416 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1421 item
.mask
= HDI_LPARAM
;
1422 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1424 return (unsigned int)item
.lParam
;
1427 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1429 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1431 // is it a message from the header?
1432 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1433 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1435 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1436 switch ( nmhdr
->code
)
1438 case HDN_BEGINTRACK
:
1439 // user has started to resize a column:
1440 // do we need to veto it?
1441 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1449 // user has started to reorder a column
1450 if (!GetColumn(nmHDR
->iItem
)->IsReorderable())
1457 case HDN_ENDDRAG
: // user has finished reordering a column
1459 // TODO: How to query the new position here?
1463 case HDN_ITEMCHANGING
:
1464 if (nmHDR
->pitem
!= NULL
&&
1465 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1467 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1468 if (nmHDR
->pitem
->cxy
< minWidth
)
1470 // do not allow the user to resize this column under
1471 // its minimal width:
1477 case HDN_ITEMCHANGED
: // user is resizing a column
1478 case HDN_ENDTRACK
: // user has finished resizing a column
1480 // update the width of the modified column:
1481 if (nmHDR
->pitem
!= NULL
&&
1482 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1484 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1485 unsigned int w
= nmHDR
->pitem
->cxy
;
1486 wxDataViewColumn
*col
= GetColumn(idx
);
1488 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1489 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1490 w
-= COLUMN_WIDTH_OFFSET
;
1492 if (w
>= (unsigned)col
->GetMinWidth())
1493 col
->SetInternalWidth(w
);
1499 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1500 wxDataViewModel
* model
= GetOwner()->GetModel();
1502 if(nmHDR
->iButton
== 0)
1504 wxDataViewColumn
*col
= GetColumn(idx
);
1505 if(col
->IsSortable())
1507 if(model
&& m_owner
->GetSortingColumn() == col
)
1509 bool order
= col
->IsSortOrderAscending();
1510 col
->SetSortOrder(!order
);
1514 m_owner
->SetSortingColumn(col
);
1522 wxEventType evt
= nmHDR
->iButton
== 0 ?
1523 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1524 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1525 SendEvent(evt
, idx
);
1531 // NOTE: for some reason (i.e. for a bug in Windows)
1532 // the HDN_ITEMCLICK notification is not sent on
1533 // right clicks, so we need to handle NM_RCLICK
1536 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1537 if (column
!= wxNOT_FOUND
)
1540 item
.mask
= HDI_LPARAM
;
1541 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1543 // 'idx' may be different from 'column' if there are
1544 // hidden columns...
1545 unsigned int idx
= (unsigned int)item
.lParam
;
1546 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1552 case HDN_GETDISPINFOW
:
1553 // see wxListCtrl::MSWOnNotify for more info!
1556 case HDN_ITEMDBLCLICK
:
1558 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1559 int w
= GetOwner()->GetBestColumnWidth(idx
);
1561 // update the native control:
1563 ZeroMemory(&hd
, sizeof(hd
));
1564 hd
.mask
= HDI_WIDTH
;
1566 Header_SetItem(GetHwnd(),
1567 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1570 // update the wxDataViewColumn class:
1571 GetColumn(idx
)->SetInternalWidth(w
);
1576 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1582 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1583 const wxRect
* WXUNUSED(rect
))
1585 m_scrollOffsetX
+= dx
;
1587 GetParent()->Layout();
1590 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1594 // TODO: why is there a border + 2px around it?
1595 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1598 #else // !defined(__WXMSW__)
1600 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1601 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1602 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1603 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1604 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1607 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1608 const wxPoint
&pos
, const wxSize
&size
,
1609 const wxString
&name
)
1613 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1616 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1617 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1618 SetOwnForegroundColour( attr
.colFg
);
1619 SetOwnBackgroundColour( attr
.colBg
);
1621 SetOwnFont( attr
.font
);
1623 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1624 // a wxBoxSizer and in order to avoid super-big header windows,
1625 // we need to set our height as fixed
1626 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1627 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1632 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1635 GetClientSize( &w
, &h
);
1637 wxAutoBufferedPaintDC
dc( this );
1639 dc
.SetBackground(GetBackgroundColour());
1643 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1646 m_owner
->GetViewStart( &x
, NULL
);
1648 // account for the horz scrollbar offset
1649 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1651 dc
.SetFont( GetFont() );
1653 unsigned int cols
= GetOwner()->GetColumnCount();
1656 for (i
= 0; i
< cols
; i
++)
1658 wxDataViewColumn
*col
= GetColumn( i
);
1659 if (col
->IsHidden())
1660 continue; // skip it!
1662 int cw
= col
->GetWidth();
1665 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1666 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1668 if (col
->IsSortOrderAscending())
1669 sortArrow
= wxHDR_SORT_ICON_UP
;
1671 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1675 if (m_parent
->IsEnabled())
1677 if ((int) i
== m_hover
)
1678 state
= wxCONTROL_CURRENT
;
1682 state
= (int) wxCONTROL_DISABLED
;
1685 wxRendererNative::Get().DrawHeaderButton
1689 wxRect(xpos
, 0, cw
, ch
-1),
1694 // align as required the column title:
1696 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1697 switch (col
->GetAlignment())
1700 x
+= HEADER_HORIZ_BORDER
;
1703 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1706 case wxALIGN_CENTER
:
1707 case wxALIGN_CENTER_HORIZONTAL
:
1708 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1712 // always center the title vertically:
1713 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1715 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1717 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1718 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1719 dc
.DrawText( col
->GetTitle(), x
, y
);
1720 dc
.DestroyClippingRegion();
1726 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1728 GetParent()->SetFocus();
1732 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1734 // we want to work with logical coords
1736 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1737 int y
= event
.GetY();
1741 // we don't draw the line beyond our window,
1742 // but we allow dragging it there
1744 GetClientSize( &w
, NULL
);
1745 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1748 if (event
.ButtonUp())
1750 m_isDragging
= false;
1756 m_currentX
= wxMax(m_minX
+ 7, x
);
1760 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1762 GetOwner()->Refresh();
1766 else // not dragging
1769 m_column
= wxNOT_FOUND
;
1771 bool hit_border
= false;
1773 // end of the current column
1776 // find the column where this event occured
1777 int countCol
= m_owner
->GetColumnCount();
1778 for (int column
= 0; column
< countCol
; column
++)
1780 wxDataViewColumn
*p
= GetColumn(column
);
1783 continue; // skip if not shown
1785 xpos
+= p
->GetWidth();
1787 if ((abs(x
-xpos
) < 3) && (y
< 22))
1795 // inside the column
1802 int old_hover
= m_hover
;
1804 if (event
.Leaving())
1805 m_hover
= wxNOT_FOUND
;
1806 if (old_hover
!= m_hover
)
1809 if (m_column
== wxNOT_FOUND
)
1812 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1813 if (event
.LeftDClick() && resizeable
)
1815 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1818 else if (event
.LeftDown() || event
.RightUp())
1820 if (hit_border
&& event
.LeftDown() && resizeable
)
1822 m_isDragging
= true;
1826 else // click on a column
1828 wxDataViewModel
* model
= GetOwner()->GetModel();
1829 wxEventType evt
= event
.LeftDown() ?
1830 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1831 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1832 SendEvent(evt
, m_column
);
1834 //Left click the header
1835 if(event
.LeftDown())
1837 wxDataViewColumn
*col
= GetColumn(m_column
);
1838 if(col
->IsSortable())
1840 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1841 if(model
&& sortCol
== col
)
1843 bool order
= col
->IsSortOrderAscending();
1844 col
->SetSortOrder(!order
);
1848 m_owner
->SetSortingColumn(col
);
1854 //Send the column sorted event
1855 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1859 else if (event
.Moving())
1861 if (hit_border
&& resizeable
)
1862 m_currentCursor
= m_resizeCursor
;
1864 m_currentCursor
= wxSTANDARD_CURSOR
;
1866 SetCursor(*m_currentCursor
);
1871 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1875 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1876 m_owner
->GetViewStart( &x
, NULL
);
1878 // shift the DC origin to match the position of the main window horizontal
1879 // scrollbar: this allows us to always use logical coords
1880 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1883 #endif // defined(__WXMSW__)
1885 //-----------------------------------------------------------------------------
1886 // wxDataViewRenameTimer
1887 //-----------------------------------------------------------------------------
1889 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1894 void wxDataViewRenameTimer::Notify()
1896 m_owner
->OnRenameTimer();
1899 //-----------------------------------------------------------------------------
1900 // wxDataViewMainWindow
1901 //-----------------------------------------------------------------------------
1903 //The tree building helper, declared firstly
1904 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1906 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1908 if (row1
> row2
) return 1;
1909 if (row1
== row2
) return 0;
1914 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1916 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1917 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1918 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1919 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1920 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1921 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1924 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1925 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1926 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1927 m_selection( wxDataViewSelectionCmp
)
1932 m_lastOnSame
= false;
1933 m_renameTimer
= new wxDataViewRenameTimer( this );
1935 // TODO: user better initial values/nothing selected
1936 m_currentCol
= NULL
;
1939 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1942 m_dragStart
= wxPoint(0,0);
1943 m_lineLastClicked
= (unsigned int) -1;
1944 m_lineBeforeLastClicked
= (unsigned int) -1;
1945 m_lineSelectSingleOnUp
= (unsigned int) -1;
1949 SetBackgroundColour( *wxWHITE
);
1951 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1953 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1954 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1955 //Some new added code to deal with the tree structure
1956 m_root
= new wxDataViewTreeNode( NULL
);
1957 m_root
->SetHasChildren(true);
1959 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1961 m_underMouse
= NULL
;
1965 wxDataViewMainWindow::~wxDataViewMainWindow()
1968 delete m_renameTimer
;
1971 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1973 wxDataViewModel
*model
= GetOwner()->GetModel();
1974 wxAutoBufferedPaintDC
dc( this );
1977 dc
.SetPen( *wxTRANSPARENT_PEN
);
1978 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1979 dc
.SetBrush( *wxWHITE_BRUSH
);
1980 wxSize
size( GetClientSize() );
1981 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1985 GetOwner()->PrepareDC( dc
);
1986 dc
.SetFont( GetFont() );
1988 wxRect update
= GetUpdateRegion().GetBox();
1989 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1991 // compute which items needs to be redrawn
1992 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1993 unsigned int item_count
=
1994 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1995 (int)(GetRowCount( ) - item_start
));
1996 unsigned int item_last
= item_start
+ item_count
;
1998 // compute which columns needs to be redrawn
1999 unsigned int cols
= GetOwner()->GetColumnCount();
2000 unsigned int col_start
= 0;
2001 unsigned int x_start
= 0;
2002 for (x_start
= 0; col_start
< cols
; col_start
++)
2004 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2005 if (col
->IsHidden())
2006 continue; // skip it!
2008 unsigned int w
= col
->GetWidth();
2009 if (x_start
+w
>= (unsigned int)update
.x
)
2015 unsigned int col_last
= col_start
;
2016 unsigned int x_last
= x_start
;
2017 for (; col_last
< cols
; col_last
++)
2019 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2020 if (col
->IsHidden())
2021 continue; // skip it!
2023 if (x_last
> (unsigned int)update
.GetRight())
2026 x_last
+= col
->GetWidth();
2029 // Draw horizontal rules if required
2030 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2032 dc
.SetPen(m_penRule
);
2033 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2035 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2037 int y
= i
* m_lineHeight
;
2038 dc
.DrawLine(x_start
, y
, x_last
, y
);
2042 // Draw vertical rules if required
2043 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2045 dc
.SetPen(m_penRule
);
2046 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2049 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2051 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2052 if (col
->IsHidden())
2053 continue; // skip it
2055 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2056 x
, item_last
* m_lineHeight
);
2058 x
+= col
->GetWidth();
2061 // Draw last vertical rule
2062 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2063 x
, item_last
* m_lineHeight
);
2066 // redraw the background for the items which are selected/current
2067 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2069 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2070 if (selected
|| item
== m_currentRow
)
2072 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2073 if (item
== m_currentRow
)
2074 flags
|= wxCONTROL_CURRENT
;
2076 flags
|= wxCONTROL_FOCUSED
;
2078 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2079 wxRendererNative::Get().DrawItemSelectionRect
2089 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2092 // TODO: last column for RTL support
2093 expander
= GetOwner()->GetColumn( 0 );
2094 GetOwner()->SetExpanderColumn(expander
);
2097 // redraw all cells for all rows which must be repainted and for all columns
2099 cell_rect
.x
= x_start
;
2100 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2101 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2103 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2104 wxDataViewRenderer
*cell
= col
->GetRenderer();
2105 cell_rect
.width
= col
->GetWidth();
2107 if (col
->IsHidden())
2108 continue; // skipt it!
2111 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2113 // get the cell value and set it into the renderer
2115 wxDataViewTreeNode
*node
= NULL
;
2116 wxDataViewItem dataitem
;
2120 node
= GetTreeNodeByRow(item
);
2124 dataitem
= node
->GetItem();
2126 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2131 dataitem
= wxDataViewItem( (void*) item
);
2134 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2135 cell
->SetValue( value
);
2137 if (cell
->GetWantsAttr())
2139 wxDataViewItemAttr attr
;
2140 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2142 cell
->SetAttr( attr
);
2143 cell
->SetHasAttr( ret
);
2146 // update the y offset
2147 cell_rect
.y
= item
* m_lineHeight
;
2149 //Draw the expander here.
2151 if ((m_root
) && (col
== expander
))
2153 indent
= node
->GetIndentLevel();
2155 //Calculate the indent first
2156 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2158 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2159 // change the cell_rect.x to the appropriate pos
2160 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2161 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2162 dc
.SetPen( m_penExpander
);
2163 dc
.SetBrush( wxNullBrush
);
2164 if( node
->HasChildren() )
2166 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2168 if (m_underMouse
== node
)
2170 flag
|= wxCONTROL_CURRENT
;
2172 if( node
->IsOpen() )
2173 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2175 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2179 // I am wondering whether we should draw dot lines between tree nodes
2182 // Yes, if the node does not have any child, it must be a leaf which
2183 // mean that it is a temporarily created by GetTreeNodeByRow
2186 //force the expander column to left-center align
2187 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2191 // cannot be bigger than allocated space
2192 wxSize size
= cell
->GetSize();
2193 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2194 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2195 // size.y = wxMin( size.y, cell_rect.height );
2196 size
.y
= cell_rect
.height
;
2198 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2199 int align
= cell
->GetAlignment();
2201 // horizontal alignment:
2202 item_rect
.x
= cell_rect
.x
;
2203 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2204 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2205 else if (align
& wxALIGN_RIGHT
)
2206 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2207 //else: wxALIGN_LEFT is the default
2209 // vertical alignment:
2210 item_rect
.y
= cell_rect
.y
;
2211 if (align
& wxALIGN_CENTER_VERTICAL
)
2212 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2213 else if (align
& wxALIGN_BOTTOM
)
2214 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2215 //else: wxALIGN_TOP is the default
2218 item_rect
.x
+= PADDING_RIGHTLEFT
;
2219 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2221 //Here we add the tree indent
2222 item_rect
.x
+= indent
;
2225 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2226 state
|= wxDATAVIEW_CELL_SELECTED
;
2228 // TODO: it would be much more efficient to create a clipping
2229 // region for the entire column being rendered (in the OnPaint
2230 // of wxDataViewMainWindow) instead of a single clip region for
2231 // each cell. However it would mean that each renderer should
2232 // respect the given wxRect's top & bottom coords, eventually
2233 // violating only the left & right coords - however the user can
2234 // make its own renderer and thus we cannot be sure of that.
2235 dc
.SetClippingRegion( item_rect
);
2236 cell
->Render( item_rect
, &dc
, state
);
2237 dc
.DestroyClippingRegion();
2240 cell_rect
.x
+= cell_rect
.width
;
2244 void wxDataViewMainWindow::OnRenameTimer()
2246 // We have to call this here because changes may just have
2247 // been made and no screen update taken place.
2252 unsigned int cols
= GetOwner()->GetColumnCount();
2254 for (i
= 0; i
< cols
; i
++)
2256 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2258 continue; // skip it!
2260 if (c
== m_currentCol
)
2262 xpos
+= c
->GetWidth();
2264 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2265 m_currentCol
->GetWidth(), m_lineHeight
);
2267 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2268 &labelRect
.x
, &labelRect
.y
);
2270 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2271 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2275 //------------------------------------------------------------------
2276 // Helper class for do operation on the tree node
2277 //------------------------------------------------------------------
2284 //The return value control how the tree-walker tranverse the tree
2285 // 0: Job done, stop tranverse and return
2286 // 1: Ignore the current node's subtree and continue
2287 // 2: Job not done, continue
2288 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2289 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2290 virtual int operator() ( void * n
) = 0;
2293 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2298 switch( func( node
) )
2309 wxDataViewTreeNodes nodes
= node
->GetNodes();
2310 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2312 int len_nodes
= nodes
.GetCount();
2313 int len
= leaves
.GetCount();
2314 int i
= 0, nodes_i
= 0;
2316 for( ; i
< len
; i
++ )
2318 void * n
= leaves
[i
];
2319 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2321 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2324 if( Walker( nd
, func
) )
2343 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2354 wxDataViewTreeNode
* node
;
2355 node
= FindNode(parent
);
2360 node
->SetHasChildren( true );
2362 if( g_model
->IsContainer( item
) )
2364 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2365 newnode
->SetItem(item
);
2366 newnode
->SetHasChildren( true );
2367 node
->AddNode( newnode
);
2370 node
->AddLeaf( item
.GetID() );
2372 node
->ChangeSubTreeCount(1);
2380 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2382 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2383 const wxDataViewItem
& item
)
2388 if( m_currentRow
> GetRowCount() )
2389 m_currentRow
= m_count
- 1;
2391 m_selection
.Empty();
2398 wxDataViewTreeNode
* node
= FindNode(parent
);
2400 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2401 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2404 node
->GetChildren().Remove( item
.GetID() );
2405 //Manuplate selection
2406 if( m_selection
.GetCount() > 1 )
2408 m_selection
.Empty();
2410 bool isContainer
= false;
2411 wxDataViewTreeNodes nds
= node
->GetNodes();
2412 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2414 if (nds
[i
]->GetItem() == item
)
2422 wxDataViewTreeNode
* n
= NULL
;
2423 wxDataViewTreeNodes nodes
= node
->GetNodes();
2424 int len
= nodes
.GetCount();
2425 for( int i
= 0 ; i
< len
; i
++)
2427 if( nodes
[i
]->GetItem() == item
)
2434 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2436 node
->GetNodes().Remove( n
);
2437 sub
-= n
->GetSubTreeCount();
2438 DestroyTreeHelper(n
);
2440 //Make the row number invalid and get a new valid one when user call GetRowCount
2442 node
->ChangeSubTreeCount(sub
);
2443 if( node
->GetChildrenNumber() == 0)
2445 node
->GetParent()->GetNodes().Remove( node
);
2449 //Change the current row to the last row if the current exceed the max row number
2450 if( m_currentRow
> GetRowCount() )
2451 m_currentRow
= m_count
- 1;
2458 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2464 wxWindow
*parent
= GetParent();
2465 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2466 le
.SetEventObject(parent
);
2467 le
.SetModel(GetOwner()->GetModel());
2469 parent
->GetEventHandler()->ProcessEvent(le
);
2474 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2476 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2477 /*#define MAX_VIRTUAL_WIDTH 100000
2479 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2480 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2481 Refresh( true, &rect );
2489 wxWindow
*parent
= GetParent();
2490 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2491 le
.SetEventObject(parent
);
2492 le
.SetModel(GetOwner()->GetModel());
2495 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2496 parent
->GetEventHandler()->ProcessEvent(le
);
2501 bool wxDataViewMainWindow::Cleared()
2506 BuildTree( GetOwner()->GetModel() );
2513 void wxDataViewMainWindow::UpdateDisplay()
2518 void wxDataViewMainWindow::OnInternalIdle()
2520 wxWindow::OnInternalIdle();
2524 RecalculateDisplay();
2529 void wxDataViewMainWindow::RecalculateDisplay()
2531 wxDataViewModel
*model
= GetOwner()->GetModel();
2538 int width
= GetEndOfLastCol();
2539 int height
= GetRowCount() * m_lineHeight
;
2541 SetVirtualSize( width
, height
);
2542 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2547 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2549 wxWindow::ScrollWindow( dx
, dy
, rect
);
2551 if (GetOwner()->m_headerArea
)
2552 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2555 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2558 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2559 int sy
= rows
*m_lineHeight
/y
;
2563 wxRect rect
= GetClientRect();
2565 int x_start
= 0, x_end
= 0, w
= 0;
2567 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2568 for (x_start
= 0; colnum
< column
; colnum
++)
2570 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2571 if (col
->IsHidden())
2572 continue; // skip it!
2574 w
= col
->GetWidth();
2578 x_end
= x_start
+ w
;
2579 xe
= xx
+ rect
.width
;
2582 sx
= ( xx
+ x_end
- xe
)/x
;
2589 m_owner
->Scroll( sx
, sy
);
2592 int wxDataViewMainWindow::GetCountPerPage() const
2594 wxSize size
= GetClientSize();
2595 return size
.y
/ m_lineHeight
;
2598 int wxDataViewMainWindow::GetEndOfLastCol() const
2602 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2604 const wxDataViewColumn
*c
=
2605 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2608 width
+= c
->GetWidth();
2613 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2617 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2619 return y
/ m_lineHeight
;
2622 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2624 wxSize client_size
= GetClientSize();
2625 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2626 &client_size
.x
, &client_size
.y
);
2628 //we should deal with the pixel here
2629 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2631 return wxMin( GetRowCount()-1, row
);
2634 unsigned int wxDataViewMainWindow::GetRowCount()
2636 if ( m_count
== -1 )
2638 m_count
= RecalculateCount();
2640 GetVirtualSize( &width
, &height
);
2641 height
= m_count
* m_lineHeight
;
2643 SetVirtualSize( width
, height
);
2648 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2655 void wxDataViewMainWindow::SelectAllRows( bool on
)
2662 m_selection
.Clear();
2663 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2664 m_selection
.Add( i
);
2669 unsigned int first_visible
= GetFirstVisibleRow();
2670 unsigned int last_visible
= GetLastVisibleRow();
2672 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2674 unsigned int row
= m_selection
[i
];
2675 if ((row
>= first_visible
) && (row
<= last_visible
))
2678 m_selection
.Clear();
2682 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2684 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2688 m_selection
.Add( row
);
2696 m_selection
.Remove( row
);
2702 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2706 unsigned int tmp
= from
;
2712 for (i
= from
; i
<= to
; i
++)
2714 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2717 m_selection
.Add( i
);
2722 m_selection
.Remove( i
);
2725 RefreshRows( from
, to
);
2728 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2730 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2732 int n
= aSelections
[i
];
2734 m_selection
.Add( n
);
2739 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2741 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2742 m_selection
.Add( row
);
2744 m_selection
.Remove( row
);
2748 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2750 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2753 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2755 wxWindow
*parent
= GetParent();
2756 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2758 le
.SetEventObject(parent
);
2759 le
.SetModel(GetOwner()->GetModel());
2762 parent
->GetEventHandler()->ProcessEvent(le
);
2765 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2767 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2768 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2770 wxSize client_size
= GetClientSize();
2771 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2772 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2773 if (intersect_rect
.width
> 0)
2774 Refresh( true, &intersect_rect
);
2777 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2781 unsigned int tmp
= to
;
2786 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2787 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2789 wxSize client_size
= GetClientSize();
2790 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2791 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2792 if (intersect_rect
.width
> 0)
2793 Refresh( true, &intersect_rect
);
2796 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2798 unsigned int count
= GetRowCount();
2799 if (firstRow
> count
)
2802 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2803 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2805 wxSize client_size
= GetClientSize();
2806 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2807 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2808 if (intersect_rect
.width
> 0)
2809 Refresh( true, &intersect_rect
);
2812 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2814 wxCHECK_RET( newCurrent
< GetRowCount(),
2815 _T("invalid item index in OnArrowChar()") );
2817 // if there is no selection, we cannot move it anywhere
2818 if (!HasCurrentRow())
2821 unsigned int oldCurrent
= m_currentRow
;
2823 // in single selection we just ignore Shift as we can't select several
2825 if ( event
.ShiftDown() && !IsSingleSel() )
2827 RefreshRow( oldCurrent
);
2829 ChangeCurrentRow( newCurrent
);
2831 // select all the items between the old and the new one
2832 if ( oldCurrent
> newCurrent
)
2834 newCurrent
= oldCurrent
;
2835 oldCurrent
= m_currentRow
;
2838 SelectRows( oldCurrent
, newCurrent
, true );
2839 if (oldCurrent
!=newCurrent
)
2840 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2844 RefreshRow( oldCurrent
);
2846 // all previously selected items are unselected unless ctrl is held
2847 if ( !event
.ControlDown() )
2848 SelectAllRows(false);
2850 ChangeCurrentRow( newCurrent
);
2852 if ( !event
.ControlDown() )
2854 SelectRow( m_currentRow
, true );
2855 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2858 RefreshRow( m_currentRow
);
2861 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2864 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2868 rect
.y
= m_lineHeight
* row
;
2869 rect
.width
= GetEndOfLastCol();
2870 rect
.height
= m_lineHeight
;
2875 class RowToItemJob
: public DoJob
2878 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2879 virtual ~RowToItemJob(){};
2881 virtual int operator() ( wxDataViewTreeNode
* node
)
2884 if( current
== static_cast<int>(row
))
2886 ret
= node
->GetItem() ;
2890 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2892 current
+= node
->GetSubTreeCount();
2897 //If the current has no child node, we can find the desired item of the row number directly.
2898 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2899 if( node
->GetNodes().GetCount() == 0)
2901 int index
= static_cast<int>(row
) - current
- 1;
2902 ret
= node
->GetChildren().Item( index
);
2909 virtual int operator() ( void * n
)
2912 if( current
== static_cast<int>(row
))
2914 ret
= wxDataViewItem( n
) ;
2919 wxDataViewItem
GetResult(){ return ret
; }
2926 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2930 return wxDataViewItem( (void*) row
);
2934 RowToItemJob
job( row
, -2 );
2935 Walker( m_root
, job
);
2936 return job
.GetResult();
2940 class RowToTreeNodeJob
: public DoJob
2943 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2946 this->current
= current
;
2950 virtual ~RowToTreeNodeJob(){};
2952 virtual int operator() ( wxDataViewTreeNode
* node
)
2955 if( current
== static_cast<int>(row
))
2961 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2963 current
+= node
->GetSubTreeCount();
2969 //If the current has no child node, we can find the desired item of the row number directly.
2970 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2971 if( node
->GetNodes().GetCount() == 0)
2973 int index
= static_cast<int>(row
) - current
- 1;
2974 void * n
= node
->GetChildren().Item( index
);
2975 ret
= new wxDataViewTreeNode( parent
) ;
2976 ret
->SetItem( wxDataViewItem( n
));
2977 ret
->SetHasChildren(false);
2986 virtual int operator() ( void * n
)
2989 if( current
== static_cast<int>(row
))
2991 ret
= new wxDataViewTreeNode( parent
) ;
2992 ret
->SetItem( wxDataViewItem( n
));
2993 ret
->SetHasChildren(false);
2999 wxDataViewTreeNode
* GetResult(){ return ret
; }
3003 wxDataViewTreeNode
* ret
;
3004 wxDataViewTreeNode
* parent
;
3008 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
3016 RowToTreeNodeJob
job( row
, -2, m_root
);
3017 Walker( m_root
, job
);
3018 return job
.GetResult();
3022 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3024 wxWindow
*parent
= GetParent();
3025 wxDataViewEvent
le(type
, parent
->GetId());
3027 le
.SetEventObject(parent
);
3028 le
.SetModel(GetOwner()->GetModel());
3031 parent
->GetEventHandler()->ProcessEvent(le
);
3035 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3037 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3040 if( node
->HasChildren())
3042 if( !node
->IsOpen())
3044 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3045 //Check if the user prevent expanding
3046 if( e
.GetSkipped() )
3050 //Here I build the children of current node
3051 if( node
->GetChildrenNumber() == 0 )
3054 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3058 //Send the expanded event
3059 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3063 SelectRow( row
, false );
3064 SelectRow( row
+ 1, true );
3065 ChangeCurrentRow( row
+ 1 );
3066 SendSelectionChangedEvent( GetItemByRow(row
+1));
3074 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3076 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3079 wxDataViewTreeNode
* nd
= node
;
3081 if( node
->HasChildren() && node
->IsOpen() )
3083 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3084 if( e
.GetSkipped() )
3089 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3093 node
= node
->GetParent();
3096 int parent
= GetRowByItem( node
->GetItem() ) ;
3099 SelectRow( row
, false);
3100 SelectRow(parent
, true );
3101 ChangeCurrentRow( parent
);
3102 SendSelectionChangedEvent( node
->GetItem() );
3106 if( !nd
->HasChildren())
3111 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3113 wxDataViewModel
* model
= GetOwner()->GetModel();
3117 //Compose the a parent-chain of the finding item
3119 list
.DeleteContents( true );
3120 wxDataViewItem
it( item
);
3123 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3124 list
.Insert( pItem
);
3125 it
= model
->GetParent( it
);
3128 //Find the item along the parent-chain.
3129 //This algorithm is designed to speed up the node-finding method
3130 wxDataViewTreeNode
* node
= m_root
;
3131 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3133 if( node
->HasChildren() )
3135 if( node
->GetChildrenNumber() == 0 )
3138 BuildTreeHelper(model
, node
->GetItem(), node
);
3141 wxDataViewTreeNodes nodes
= node
->GetNodes();
3143 for (; i
< nodes
.GetCount(); i
++)
3145 if (nodes
[i
]->GetItem() == (**iter
))
3151 if (i
== nodes
.GetCount())
3160 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3162 wxDataViewColumn
*col
= NULL
;
3163 unsigned int cols
= GetOwner()->GetColumnCount();
3164 unsigned int colnum
= 0;
3165 unsigned int x_start
= 0;
3167 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3168 for (x_start
= 0; colnum
< cols
; colnum
++)
3170 col
= GetOwner()->GetColumn(colnum
);
3171 if (col
->IsHidden())
3172 continue; // skip it!
3174 unsigned int w
= col
->GetWidth();
3175 if (x_start
+w
>= (unsigned int)x
)
3182 item
= GetItemByRow( y
/m_lineHeight
);
3185 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3187 int row
= GetRowByItem(item
);
3188 int y
= row
*m_lineHeight
;
3189 int h
= m_lineHeight
;
3191 wxDataViewColumn
*col
= NULL
;
3192 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3194 col
= GetOwner()->GetColumn( i
);
3195 x
+= col
->GetWidth();
3196 if( GetOwner()->GetColumn(i
+1) == column
)
3199 int w
= col
->GetWidth();
3200 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3201 return wxRect(x
, y
, w
, h
);
3204 int wxDataViewMainWindow::RecalculateCount()
3208 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3210 return list_model
->GetLastIndex() + 1;
3212 return list_model
->GetLastIndex() - 1;
3217 return m_root
->GetSubTreeCount();
3221 class ItemToRowJob
: public DoJob
3224 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3225 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3226 virtual ~ItemToRowJob(){};
3228 //Maybe binary search will help to speed up this process
3229 virtual int operator() ( wxDataViewTreeNode
* node
)
3232 if( node
->GetItem() == item
)
3237 if( node
->GetItem() == **m_iter
)
3244 ret
+= node
->GetSubTreeCount();
3250 virtual int operator() ( void * n
)
3253 if( n
== item
.GetID() )
3257 //the row number is begin from zero
3258 int GetResult(){ return ret
-1 ; }
3260 ItemList::const_iterator m_iter
;
3261 wxDataViewItem item
;
3266 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3268 wxDataViewModel
* model
= GetOwner()->GetModel();
3274 return wxPtrToUInt( item
.GetID() );
3281 //Compose the a parent-chain of the finding item
3283 wxDataViewItem
* pItem
= NULL
;
3284 list
.DeleteContents( true );
3285 wxDataViewItem
it( item
);
3288 pItem
= new wxDataViewItem( it
);
3289 list
.Insert( pItem
);
3290 it
= model
->GetParent( it
);
3292 pItem
= new wxDataViewItem( );
3293 list
.Insert( pItem
);
3295 ItemToRowJob
job( item
, list
.begin() );
3296 Walker(m_root
, job
);
3297 return job
.GetResult();
3301 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3303 if( !model
->IsContainer( item
) )
3306 wxDataViewItemArray children
;
3307 unsigned int num
= model
->GetChildren( item
, children
);
3308 unsigned int index
= 0;
3309 while( index
< num
)
3311 if( model
->IsContainer( children
[index
] ) )
3313 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3314 n
->SetItem(children
[index
]);
3315 n
->SetHasChildren( true ) ;
3320 node
->AddLeaf( children
[index
].GetID() );
3324 node
->SetSubTreeCount( num
);
3325 wxDataViewTreeNode
* n
= node
->GetParent();
3327 n
->ChangeSubTreeCount(num
);
3331 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3335 if (GetOwner()->GetModel()->IsIndexListModel())
3341 m_root
= new wxDataViewTreeNode( NULL
);
3342 m_root
->SetHasChildren(true);
3344 //First we define a invalid item to fetch the top-level elements
3345 wxDataViewItem item
;
3347 BuildTreeHelper( model
, item
, m_root
);
3351 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3353 if( node
->GetNodeNumber() != 0 )
3355 int len
= node
->GetNodeNumber();
3357 wxDataViewTreeNodes nodes
= node
->GetNodes();
3358 for( ; i
< len
; i
++ )
3360 DestroyTreeHelper(nodes
[i
]);
3366 void wxDataViewMainWindow::DestroyTree()
3370 DestroyTreeHelper(m_root
);
3376 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3378 if (event
.GetKeyCode() == WXK_TAB
)
3380 wxNavigationKeyEvent nevent
;
3381 nevent
.SetWindowChange( event
.ControlDown() );
3382 nevent
.SetDirection( !event
.ShiftDown() );
3383 nevent
.SetEventObject( GetParent()->GetParent() );
3384 nevent
.SetCurrentFocus( m_parent
);
3385 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3389 // no item -> nothing to do
3390 if (!HasCurrentRow())
3396 // don't use m_linesPerPage directly as it might not be computed yet
3397 const int pageSize
= GetCountPerPage();
3398 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3400 switch ( event
.GetKeyCode() )
3404 if (m_currentRow
> 0)
3406 wxWindow
*parent
= GetParent();
3407 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3408 le
.SetItem( GetItemByRow(m_currentRow
) );
3409 le
.SetEventObject(parent
);
3410 le
.SetModel(GetOwner()->GetModel());
3412 parent
->GetEventHandler()->ProcessEvent(le
);
3417 if ( m_currentRow
> 0 )
3418 OnArrowChar( m_currentRow
- 1, event
);
3422 if ( m_currentRow
< GetRowCount() - 1 )
3423 OnArrowChar( m_currentRow
+ 1, event
);
3425 //Add the process for tree expanding/collapsing
3427 OnCollapsing(m_currentRow
);
3430 OnExpanding( m_currentRow
);
3434 OnArrowChar( GetRowCount() - 1, event
);
3439 OnArrowChar( 0, event
);
3444 int steps
= pageSize
- 1;
3445 int index
= m_currentRow
- steps
;
3449 OnArrowChar( index
, event
);
3455 int steps
= pageSize
- 1;
3456 unsigned int index
= m_currentRow
+ steps
;
3457 unsigned int count
= GetRowCount();
3458 if ( index
>= count
)
3461 OnArrowChar( index
, event
);
3470 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3472 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3474 // let the base handle mouse wheel events.
3479 int x
= event
.GetX();
3480 int y
= event
.GetY();
3481 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3482 wxDataViewColumn
*col
= NULL
;
3485 unsigned int cols
= GetOwner()->GetColumnCount();
3487 for (i
= 0; i
< cols
; i
++)
3489 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3491 continue; // skip it!
3493 if (x
< xpos
+ c
->GetWidth())
3498 xpos
+= c
->GetWidth();
3503 wxDataViewRenderer
*cell
= col
->GetRenderer();
3504 unsigned int current
= y
/ m_lineHeight
;
3505 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3507 // Unselect all if below the last row ?
3511 //Test whether the mouse is hovered on the tree item button
3513 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3515 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3516 if( node
!=NULL
&& node
->HasChildren() )
3518 int indent
= node
->GetIndentLevel();
3519 indent
= GetOwner()->GetIndent()*indent
;
3520 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3521 if( rect
.Contains( x
, y
) )
3523 //So the mouse is over the expander
3525 if (m_underMouse
&& m_underMouse
!= node
)
3527 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3528 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3530 if (m_underMouse
!= node
)
3532 //wxLogMessage("Do the row: %d", current);
3533 RefreshRow(current
);
3535 m_underMouse
= node
;
3538 if (node
!=NULL
&& !node
->HasChildren())
3543 if (m_underMouse
!= NULL
)
3545 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3546 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3547 m_underMouse
= NULL
;
3551 wxDataViewModel
*model
= GetOwner()->GetModel();
3553 if (event
.Dragging())
3555 if (m_dragCount
== 0)
3557 // we have to report the raw, physical coords as we want to be
3558 // able to call HitTest(event.m_pointDrag) from the user code to
3559 // get the item being dragged
3560 m_dragStart
= event
.GetPosition();
3565 if (m_dragCount
!= 3)
3568 if (event
.LeftIsDown())
3570 // Notify cell about drag
3579 bool forceClick
= false;
3581 if (event
.ButtonDClick())
3583 m_renameTimer
->Stop();
3584 m_lastOnSame
= false;
3587 wxDataViewItem item
= GetItemByRow(current
);
3588 bool ignore_other_columns
=
3589 ((GetOwner()->GetExpanderColumn() != col
) &&
3590 (model
->IsContainer(item
)) &&
3591 (!model
->HasContainerColumns(item
)));
3593 if (event
.LeftDClick())
3595 if ( current
== m_lineLastClicked
)
3597 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3600 model
->GetValue( value
, item
, col
->GetModelColumn() );
3601 cell
->SetValue( value
);
3602 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3603 col
->GetWidth(), m_lineHeight
);
3604 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3609 wxWindow
*parent
= GetParent();
3610 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3612 le
.SetEventObject(parent
);
3613 le
.SetModel(GetOwner()->GetModel());
3615 parent
->GetEventHandler()->ProcessEvent(le
);
3621 // The first click was on another item, so don't interpret this as
3622 // a double click, but as a simple click instead
3629 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3631 // select single line
3632 SelectAllRows( false );
3633 SelectRow( m_lineSelectSingleOnUp
, true );
3636 //Process the event of user clicking the expander
3637 bool expander
= false;
3638 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3640 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3641 if( node
!=NULL
&& node
->HasChildren() )
3643 int indent
= node
->GetIndentLevel();
3644 indent
= GetOwner()->GetIndent()*indent
;
3645 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3646 if( rect
.Contains( x
, y
) )
3649 if( node
->IsOpen() )
3650 OnCollapsing(current
);
3652 OnExpanding( current
);
3656 //If the user click the expander, we do not do editing even if the column with expander are editable
3657 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3659 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3660 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3662 m_renameTimer
->Start( 100, true );
3666 m_lastOnSame
= false;
3667 m_lineSelectSingleOnUp
= (unsigned int)-1;
3671 // This is necessary, because after a DnD operation in
3672 // from and to ourself, the up event is swallowed by the
3673 // DnD code. So on next non-up event (which means here and
3674 // now) m_lineSelectSingleOnUp should be reset.
3675 m_lineSelectSingleOnUp
= (unsigned int)-1;
3678 if (event
.RightDown())
3680 m_lineBeforeLastClicked
= m_lineLastClicked
;
3681 m_lineLastClicked
= current
;
3683 // If the item is already selected, do not update the selection.
3684 // Multi-selections should not be cleared if a selected item is clicked.
3685 if (!IsRowSelected(current
))
3687 SelectAllRows(false);
3688 ChangeCurrentRow(current
);
3689 SelectRow(m_currentRow
,true);
3690 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3693 // notify cell about right click
3696 // Allow generation of context menu event
3699 else if (event
.MiddleDown())
3701 // notify cell about middle click
3704 if (event
.LeftDown() || forceClick
)
3708 m_lineBeforeLastClicked
= m_lineLastClicked
;
3709 m_lineLastClicked
= current
;
3711 unsigned int oldCurrentRow
= m_currentRow
;
3712 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3714 bool cmdModifierDown
= event
.CmdDown();
3715 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3717 if ( IsSingleSel() || !IsRowSelected(current
) )
3719 SelectAllRows( false );
3720 ChangeCurrentRow(current
);
3721 SelectRow(m_currentRow
,true);
3722 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3724 else // multi sel & current is highlighted & no mod keys
3726 m_lineSelectSingleOnUp
= current
;
3727 ChangeCurrentRow(current
); // change focus
3730 else // multi sel & either ctrl or shift is down
3732 if (cmdModifierDown
)
3734 ChangeCurrentRow(current
);
3735 ReverseRowSelection(m_currentRow
);
3736 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3738 else if (event
.ShiftDown())
3740 ChangeCurrentRow(current
);
3742 unsigned int lineFrom
= oldCurrentRow
,
3745 if ( lineTo
< lineFrom
)
3748 lineFrom
= m_currentRow
;
3751 SelectRows(lineFrom
, lineTo
, true);
3752 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3754 else // !ctrl, !shift
3756 // test in the enclosing if should make it impossible
3757 wxFAIL_MSG( _T("how did we get here?") );
3761 if (m_currentRow
!= oldCurrentRow
)
3762 RefreshRow( oldCurrentRow
);
3764 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3766 // Update selection here...
3769 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3770 (current
== oldCurrentRow
)) && oldWasSelected
;
3774 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3778 if (HasCurrentRow())
3784 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3788 if (HasCurrentRow())
3794 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3796 if( m_selection
.GetCount() != 1 )
3797 return wxDataViewItem();
3799 return GetItemByRow( m_selection
.Item(0));
3802 //-----------------------------------------------------------------------------
3804 //-----------------------------------------------------------------------------
3805 WX_DEFINE_LIST(wxDataViewColumnList
)
3807 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3809 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3810 EVT_SIZE(wxDataViewCtrl::OnSize
)
3813 wxDataViewCtrl::~wxDataViewCtrl()
3816 GetModel()->RemoveNotifier( m_notifier
);
3819 void wxDataViewCtrl::Init()
3824 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3825 const wxPoint
& pos
, const wxSize
& size
,
3826 long style
, const wxValidator
& validator
)
3828 if (!wxControl::Create( parent
, id
, pos
, size
,
3829 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3832 SetInitialSize(size
);
3837 MacSetClipChildren( true ) ;
3840 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3842 if (HasFlag(wxDV_NO_HEADER
))
3843 m_headerArea
= NULL
;
3845 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3847 SetTargetWindow( m_clientArea
);
3849 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3851 sizer
->Add( m_headerArea
, 0, wxGROW
);
3852 sizer
->Add( m_clientArea
, 1, wxGROW
);
3859 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3863 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3866 // we need to process arrows ourselves for scrolling
3867 if ( nMsg
== WM_GETDLGCODE
)
3869 rc
|= DLGC_WANTARROWS
;
3877 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3879 // We need to override OnSize so that our scrolled
3880 // window a) does call Layout() to use sizers for
3881 // positioning the controls but b) does not query
3882 // the sizer for their size and use that for setting
3883 // the scrollable area as set that ourselves by
3884 // calling SetScrollbar() further down.
3891 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3893 if (!wxDataViewCtrlBase::AssociateModel( model
))
3896 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3898 model
->AddNotifier( m_notifier
);
3900 m_clientArea
->DestroyTree();
3902 m_clientArea
->BuildTree(model
);
3904 m_clientArea
->UpdateDisplay();
3909 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3911 if (!wxDataViewCtrlBase::AppendColumn(col
))
3914 m_cols
.Append( col
);
3919 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3921 if (!wxDataViewCtrlBase::PrependColumn(col
))
3924 m_cols
.Insert( col
);
3929 void wxDataViewCtrl::OnColumnChange()
3932 m_headerArea
->UpdateDisplay();
3934 m_clientArea
->UpdateDisplay();
3937 void wxDataViewCtrl::DoSetExpanderColumn()
3939 m_clientArea
->UpdateDisplay();
3942 void wxDataViewCtrl::DoSetIndent()
3944 m_clientArea
->UpdateDisplay();
3947 unsigned int wxDataViewCtrl::GetColumnCount() const
3949 return m_cols
.GetCount();
3952 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3954 wxDataViewColumnList::const_iterator iter
;
3956 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3961 if ((*iter
)->IsHidden())
3968 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3970 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3981 bool wxDataViewCtrl::ClearColumns()
3988 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3990 int ret
= 0, dead
= 0;
3991 int len
= GetColumnCount();
3992 for (int i
=0; i
<len
; i
++)
3994 wxDataViewColumn
* col
= GetColumn(i
);
3995 if (col
->IsHidden())
3997 ret
+= col
->GetWidth();
4000 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
4007 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4012 //Selection code with wxDataViewItem as parameters
4013 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4015 return m_clientArea
->GetSelection();
4018 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4021 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4022 int len
= selection
.GetCount();
4023 for( int i
= 0; i
< len
; i
++)
4025 unsigned int row
= selection
[i
];
4026 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4031 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4033 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4034 int len
= sel
.GetCount();
4035 for( int i
= 0; i
< len
; i
++ )
4037 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4039 selection
.Add( static_cast<unsigned int>(row
) );
4041 m_clientArea
->SetSelections( selection
);
4044 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4046 int row
= m_clientArea
->GetRowByItem( item
);
4049 //Unselect all rows before select another in the single select mode
4050 if (m_clientArea
->IsSingleSel())
4051 m_clientArea
->SelectAllRows(false);
4052 m_clientArea
->SelectRow(row
, true);
4056 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4058 int row
= m_clientArea
->GetRowByItem( item
);
4060 m_clientArea
->SelectRow(row
, false);
4063 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4065 int row
= m_clientArea
->GetRowByItem( item
);
4068 return m_clientArea
->IsRowSelected(row
);
4073 //Selection code with row number as parameter
4074 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4077 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4078 int len
= selection
.GetCount();
4079 for( int i
= 0; i
< len
; i
++)
4081 unsigned int row
= selection
[i
];
4087 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4089 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4090 int len
= sel
.GetCount();
4091 for( int i
= 0; i
< len
; i
++ )
4095 selection
.Add( static_cast<unsigned int>(row
) );
4097 m_clientArea
->SetSelections( selection
);
4100 void wxDataViewCtrl::Select( int row
)
4104 if (m_clientArea
->IsSingleSel())
4105 m_clientArea
->SelectAllRows(false);
4106 m_clientArea
->SelectRow( row
, true );
4110 void wxDataViewCtrl::Unselect( int row
)
4113 m_clientArea
->SelectRow(row
, false);
4116 bool wxDataViewCtrl::IsSelected( int row
) const
4119 return m_clientArea
->IsRowSelected(row
);
4123 void wxDataViewCtrl::SelectRange( int from
, int to
)
4126 for( int i
= from
; i
< to
; i
++ )
4128 m_clientArea
->Select(sel
);
4131 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4133 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4134 for( int i
= from
; i
< to
; i
++ )
4135 if( sel
.Index( i
) != wxNOT_FOUND
)
4137 m_clientArea
->SetSelections(sel
);
4140 void wxDataViewCtrl::SelectAll()
4142 m_clientArea
->SelectAllRows(true);
4145 void wxDataViewCtrl::UnselectAll()
4147 m_clientArea
->SelectAllRows(false);
4150 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4154 if( row
> (int) m_clientArea
->GetRowCount() )
4155 row
= m_clientArea
->GetRowCount();
4157 int first
= m_clientArea
->GetFirstVisibleRow();
4158 int last
= m_clientArea
->GetLastVisibleRow();
4160 m_clientArea
->ScrollTo( row
, column
);
4161 else if( row
> last
)
4162 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4164 m_clientArea
->ScrollTo( first
, column
);
4167 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4169 int row
= m_clientArea
->GetRowByItem(item
);
4172 if( column
== NULL
)
4173 EnsureVisible(row
, -1);
4177 int len
= GetColumnCount();
4178 for( int i
= 0; i
< len
; i
++ )
4179 if( GetColumn(i
) == column
)
4184 EnsureVisible( row
, col
);
4190 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4192 m_clientArea
->HitTest(point
, item
, column
);
4195 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4197 return m_clientArea
->GetItemRect(item
, column
);
4200 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4202 return m_clientArea
->GetItemByRow( row
);
4205 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4207 return m_clientArea
->GetRowByItem( item
);
4210 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4212 int row
= m_clientArea
->GetRowByItem( item
);
4214 m_clientArea
->Expand(row
);
4217 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4219 int row
= m_clientArea
->GetRowByItem( item
);
4221 m_clientArea
->Collapse(row
);
4225 // !wxUSE_GENERICDATAVIEWCTRL
4228 // wxUSE_DATAVIEWCTRL