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 // on wxMSW the header window (only that part however) can be made native!
67 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
68 #define USE_NATIVE_HEADER_WINDOW
71 //Below is the compare stuff
72 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
73 static wxDataViewModel
* g_model
;
74 static int g_column
= -2;
75 static bool g_asending
= true;
77 // NB: for some reason, this class must be dllexport'ed or we get warnings from
79 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
82 wxDataViewHeaderWindowBase()
85 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
86 const wxPoint
&pos
, const wxSize
&size
,
89 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
92 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
93 wxDataViewCtrl
*GetOwner() { return m_owner
; }
95 // called on column addition/removal
96 virtual void UpdateDisplay() { /* by default, do nothing */ }
98 // returns the n-th column
99 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
102 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
109 wxDataViewCtrl
*m_owner
;
111 // sends an event generated from the n-th wxDataViewColumn
112 void SendEvent(wxEventType type
, unsigned int n
);
115 #ifdef USE_NATIVE_HEADER_WINDOW
117 #define COLUMN_WIDTH_OFFSET 2
118 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
120 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
124 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
126 const wxPoint
&pos
= wxDefaultPosition
,
127 const wxSize
&size
= wxDefaultSize
,
128 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
130 Create(parent
, id
, pos
, size
, name
);
133 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
134 const wxPoint
&pos
, const wxSize
&size
,
135 const wxString
&name
);
137 ~wxDataViewHeaderWindowMSW();
139 // called when any column setting is changed and/or changed
141 virtual void UpdateDisplay();
143 virtual void OnInternalIdle();
145 // called Refresh afterwards
146 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
148 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
151 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
153 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
155 wxSize
DoGetBestSize() const;
157 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
159 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
160 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
164 bool m_delayedUpdate
;
167 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
170 #else // !defined(__WXMSW__)
172 #define HEADER_WINDOW_HEIGHT 25
173 #define HEADER_HORIZ_BORDER 5
174 #define HEADER_VERT_BORDER 3
175 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
177 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
180 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
182 const wxPoint
&pos
= wxDefaultPosition
,
183 const wxSize
&size
= wxDefaultSize
,
184 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
187 Create(parent
, id
, pos
, size
, name
);
190 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
191 const wxPoint
&pos
, const wxSize
&size
,
192 const wxString
&name
);
194 ~wxGenericDataViewHeaderWindow()
196 delete m_resizeCursor
;
199 virtual void UpdateDisplay() { Refresh(); }
203 void OnPaint( wxPaintEvent
&event
);
204 void OnMouse( wxMouseEvent
&event
);
205 void OnSetFocus( wxFocusEvent
&event
);
210 // vars used for column resizing:
212 wxCursor
*m_resizeCursor
;
213 const wxCursor
*m_currentCursor
;
216 bool m_dirty
; // needs refresh?
217 int m_hover
; // index of the column under the mouse
218 int m_column
; // index of the column being resized
219 int m_currentX
; // divider line position in logical (unscrolled) coords
220 int m_minX
; // minimal position beyond which the divider line
221 // can't be dragged in logical coords
223 // the pen used to draw the current column width drag line
224 // when resizing the columsn
228 // internal utilities:
232 m_currentCursor
= (wxCursor
*) NULL
;
233 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
235 m_isDragging
= false;
238 m_hover
= wxNOT_FOUND
;
239 m_column
= wxNOT_FOUND
;
243 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
244 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
247 void AdjustDC(wxDC
& dc
);
250 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
251 DECLARE_EVENT_TABLE()
254 #endif // defined(__WXMSW__)
256 //-----------------------------------------------------------------------------
257 // wxDataViewRenameTimer
258 //-----------------------------------------------------------------------------
260 class wxDataViewRenameTimer
: public wxTimer
263 wxDataViewMainWindow
*m_owner
;
266 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
270 //-----------------------------------------------------------------------------
271 // wxDataViewTreeNode
272 //-----------------------------------------------------------------------------
273 class wxDataViewTreeNode
;
274 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
275 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
277 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
278 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
280 class wxDataViewTreeNode
283 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
290 m_hasChildren
= false;
294 ~wxDataViewTreeNode()
298 wxDataViewTreeNode
* GetParent() { return m_parent
; }
299 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
300 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
301 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
303 void AddNode( wxDataViewTreeNode
* node
)
305 m_leaves
.Add( node
->GetItem().GetID() );
307 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
310 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
312 void AddLeaf( void * leaf
)
314 m_leaves
.Add( leaf
);
316 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
319 wxDataViewItem
& GetItem() { return m_item
; }
320 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
322 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
323 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
327 wxDataViewTreeNode
* node
= this;
328 while( node
->GetParent()->GetParent() != NULL
)
330 node
= node
->GetParent();
343 int len
= m_nodes
.GetCount();
345 for ( int i
= 0 ;i
< len
; i
++)
346 sum
+= m_nodes
[i
]->GetSubTreeCount();
348 sum
+= m_leaves
.GetCount();
351 ChangeSubTreeCount(-sum
);
357 ChangeSubTreeCount(sum
);
360 bool HasChildren() { return m_hasChildren
; }
361 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
363 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
364 int GetSubTreeCount() { return m_subTreeCount
; }
365 void ChangeSubTreeCount( int num
)
369 m_subTreeCount
+= num
;
371 m_parent
->ChangeSubTreeCount(num
);
378 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
379 int len
= m_nodes
.GetCount();
380 for (int i
= 0; i
< len
; i
++)
381 m_nodes
[i
]->Resort();
382 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
387 wxDataViewTreeNode
*m_parent
;
388 wxDataViewTreeNodes m_nodes
;
389 wxDataViewTreeLeaves m_leaves
;
390 wxDataViewItem m_item
;
396 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
398 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
401 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
403 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
408 //-----------------------------------------------------------------------------
409 // wxDataViewMainWindow
410 //-----------------------------------------------------------------------------
412 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
414 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
415 WX_DEFINE_LIST(ItemList
)
417 class wxDataViewMainWindow
: public wxWindow
420 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
422 const wxPoint
&pos
= wxDefaultPosition
,
423 const wxSize
&size
= wxDefaultSize
,
424 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
425 virtual ~wxDataViewMainWindow();
427 // notifications from wxDataViewModel
428 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
429 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
430 bool ItemChanged( const wxDataViewItem
&item
);
431 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
445 g_model
= GetOwner()->GetModel();
446 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
449 if (g_model
->HasDefaultCompare())
457 g_column
= col
->GetModelColumn();
458 g_asending
= col
->IsSortOrderAscending();
461 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
462 wxDataViewCtrl
*GetOwner() { return m_owner
; }
463 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
465 void OnPaint( wxPaintEvent
&event
);
466 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
467 void OnChar( wxKeyEvent
&event
);
468 void OnMouse( wxMouseEvent
&event
);
469 void OnSetFocus( wxFocusEvent
&event
);
470 void OnKillFocus( wxFocusEvent
&event
);
472 void UpdateDisplay();
473 void RecalculateDisplay();
474 void OnInternalIdle();
476 void OnRenameTimer();
478 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
479 void ScrollTo( int rows
, int column
);
481 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
482 void ChangeCurrentRow( unsigned int row
);
484 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
485 bool IsEmpty() { return GetRowCount() == 0; }
487 int GetCountPerPage() const;
488 int GetEndOfLastCol() const;
489 unsigned int GetFirstVisibleRow() const;
490 //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
491 unsigned int GetLastVisibleRow();
492 unsigned int GetRowCount() ;
494 wxDataViewItem
GetSelection() const;
495 wxDataViewSelection
GetSelections(){ return m_selection
; }
496 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
497 void Select( const wxArrayInt
& aSelections
);
498 void SelectAllRows( bool on
);
499 void SelectRow( unsigned int row
, bool on
);
500 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
501 void ReverseRowSelection( unsigned int row
);
502 bool IsRowSelected( unsigned int row
);
503 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
505 void RefreshRow( unsigned int row
);
506 void RefreshRows( unsigned int from
, unsigned int to
);
507 void RefreshRowsAfter( unsigned int firstRow
);
509 // returns the colour to be used for drawing the rules
510 wxColour
GetRuleColour() const
512 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
515 wxRect
GetLineRect( unsigned int row
) const;
517 //Some useful functions for row and item mapping
518 wxDataViewItem
GetItemByRow( unsigned int row
) const;
519 int GetRowByItem( const wxDataViewItem
& item
);
521 //Methods for building the mapping tree
522 void BuildTree( wxDataViewModel
* model
);
524 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
525 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
527 void Expand( unsigned int row
) { OnExpanding( row
); }
528 void Collapse( unsigned int row
) { OnCollapsing( row
); }
530 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
531 //We did not need this temporarily
532 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
534 int RecalculateCount() ;
536 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
537 void OnExpanding( unsigned int row
);
538 void OnCollapsing( unsigned int row
);
540 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
543 wxDataViewCtrl
*m_owner
;
547 wxDataViewColumn
*m_currentCol
;
548 unsigned int m_currentRow
;
549 wxDataViewSelection m_selection
;
551 wxDataViewRenameTimer
*m_renameTimer
;
559 // for double click logic
560 unsigned int m_lineLastClicked
,
561 m_lineBeforeLastClicked
,
562 m_lineSelectSingleOnUp
;
564 // the pen used to draw horiz/vertical rules
567 // the pen used to draw the expander and the lines
570 //This is the tree structure of the model
571 wxDataViewTreeNode
* m_root
;
573 //This is the tree node under the cursor
574 wxDataViewTreeNode
* m_underMouse
;
576 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
577 DECLARE_EVENT_TABLE()
580 // ---------------------------------------------------------
581 // wxGenericDataViewModelNotifier
582 // ---------------------------------------------------------
584 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
587 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
588 { m_mainWindow
= mainWindow
; }
590 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
591 { return m_mainWindow
->ItemAdded( parent
, item
); }
592 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
593 { return m_mainWindow
->ItemDeleted( parent
, item
); }
594 virtual bool ItemChanged( const wxDataViewItem
& item
)
595 { return m_mainWindow
->ItemChanged(item
); }
596 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
597 { return m_mainWindow
->ValueChanged( item
, col
); }
598 virtual bool Cleared()
599 { return m_mainWindow
->Cleared(); }
600 virtual void Resort()
601 { m_mainWindow
->Resort(); }
603 wxDataViewMainWindow
*m_mainWindow
;
606 // ---------------------------------------------------------
607 // wxDataViewRenderer
608 // ---------------------------------------------------------
610 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
612 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
613 wxDataViewCellMode mode
,
615 wxDataViewRendererBase( varianttype
, mode
, align
)
624 wxDataViewRenderer::~wxDataViewRenderer()
630 wxDC
*wxDataViewRenderer::GetDC()
634 if (GetOwner() == NULL
)
636 if (GetOwner()->GetOwner() == NULL
)
638 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
644 void wxDataViewRenderer::SetAlignment( int align
)
649 int wxDataViewRenderer::GetAlignment() const
654 int wxDataViewRenderer::CalculateAlignment() const
656 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
658 if (GetOwner() == NULL
)
659 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
661 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
667 // ---------------------------------------------------------
668 // wxDataViewCustomRenderer
669 // ---------------------------------------------------------
671 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
673 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
674 wxDataViewCellMode mode
, int align
) :
675 wxDataViewRenderer( varianttype
, mode
, align
)
679 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
681 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
682 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
683 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
684 view
->GetForegroundColour();
685 dc
->SetTextForeground(col
);
686 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
689 // ---------------------------------------------------------
690 // wxDataViewTextRenderer
691 // ---------------------------------------------------------
693 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
695 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
696 wxDataViewCellMode mode
, int align
) :
697 wxDataViewCustomRenderer( varianttype
, mode
, align
)
701 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
703 m_text
= value
.GetString();
708 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
713 bool wxDataViewTextRenderer::HasEditorCtrl()
718 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
719 wxRect labelRect
, const wxVariant
&value
)
721 return new wxTextCtrl( parent
, wxID_ANY
, value
,
722 wxPoint(labelRect
.x
,labelRect
.y
),
723 wxSize(labelRect
.width
,labelRect
.height
) );
726 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
728 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
729 value
= text
->GetValue();
733 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
735 RenderText( m_text
, 0, cell
, dc
, state
);
739 wxSize
wxDataViewTextRenderer::GetSize() const
741 const wxDataViewCtrl
*view
= GetView();
745 view
->GetTextExtent( m_text
, &x
, &y
);
746 return wxSize( x
, y
);
748 return wxSize(80,20);
751 // ---------------------------------------------------------
752 // wxDataViewTextRendererAttr
753 // ---------------------------------------------------------
755 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
757 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
758 wxDataViewCellMode mode
, int align
) :
759 wxDataViewTextRenderer( varianttype
, mode
, align
)
764 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
771 if (m_attr
.HasColour())
773 colour
= dc
->GetTextForeground();
774 dc
->SetTextForeground( m_attr
.GetColour() );
777 if (m_attr
.GetBold() || m_attr
.GetItalic())
779 font
= dc
->GetFont();
780 wxFont myfont
= font
;
781 if (m_attr
.GetBold())
782 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
783 if (m_attr
.GetItalic())
784 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
785 dc
->SetFont( myfont
);
789 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
794 if (m_attr
.HasColour())
795 dc
->SetTextForeground( colour
);
797 if (m_attr
.GetBold() || m_attr
.GetItalic())
805 // ---------------------------------------------------------
806 // wxDataViewBitmapRenderer
807 // ---------------------------------------------------------
809 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
811 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
812 wxDataViewCellMode mode
, int align
) :
813 wxDataViewCustomRenderer( varianttype
, mode
, align
)
817 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
819 if (value
.GetType() == wxT("wxBitmap"))
821 if (value
.GetType() == wxT("wxIcon"))
827 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
832 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
835 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
836 else if (m_icon
.Ok())
837 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
842 wxSize
wxDataViewBitmapRenderer::GetSize() const
845 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
846 else if (m_icon
.Ok())
847 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
849 return wxSize(16,16);
852 // ---------------------------------------------------------
853 // wxDataViewToggleRenderer
854 // ---------------------------------------------------------
856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
858 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
859 wxDataViewCellMode mode
, int align
) :
860 wxDataViewCustomRenderer( varianttype
, mode
, align
)
865 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
867 m_toggle
= value
.GetBool();
872 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
877 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
879 // User wxRenderer here
882 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
884 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
889 flags
|= wxCONTROL_CHECKED
;
890 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
891 flags
|= wxCONTROL_DISABLED
;
893 wxRendererNative::Get().DrawCheckBox(
894 GetOwner()->GetOwner(),
902 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
903 wxDataViewModel
*model
,
904 const wxDataViewItem
& item
, unsigned int col
)
906 bool value
= !m_toggle
;
907 wxVariant variant
= value
;
908 model
->SetValue( variant
, item
, col
);
909 model
->ValueChanged( item
, col
);
913 wxSize
wxDataViewToggleRenderer::GetSize() const
915 return wxSize(20,20);
918 // ---------------------------------------------------------
919 // wxDataViewProgressRenderer
920 // ---------------------------------------------------------
922 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
924 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
925 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
926 wxDataViewCustomRenderer( varianttype
, mode
, align
)
932 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
936 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
938 m_value
= (long) value
;
940 if (m_value
< 0) m_value
= 0;
941 if (m_value
> 100) m_value
= 100;
946 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
948 value
= (long) m_value
;
952 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
954 double pct
= (double)m_value
/ 100.0;
956 bar
.width
= (int)(cell
.width
* pct
);
957 dc
->SetPen( *wxTRANSPARENT_PEN
);
958 dc
->SetBrush( *wxBLUE_BRUSH
);
959 dc
->DrawRectangle( bar
);
961 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
962 dc
->SetPen( *wxBLACK_PEN
);
963 dc
->DrawRectangle( cell
);
968 wxSize
wxDataViewProgressRenderer::GetSize() const
970 return wxSize(40,12);
973 // ---------------------------------------------------------
974 // wxDataViewDateRenderer
975 // ---------------------------------------------------------
977 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
979 #if wxUSE_DATE_RENDERER_POPUP
981 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
984 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
985 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
986 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
991 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
992 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
993 sizer
->Add( m_cal
, 1, wxGROW
);
998 void OnCalendar( wxCalendarEvent
&event
);
1000 wxCalendarCtrl
*m_cal
;
1001 wxDataViewModel
*m_model
;
1003 const wxDataViewItem
& m_item
;
1006 virtual void OnDismiss()
1011 DECLARE_EVENT_TABLE()
1014 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1015 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1018 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1020 wxDateTime date
= event
.GetDate();
1021 wxVariant value
= date
;
1022 m_model
->SetValue( value
, m_item
, m_col
);
1023 m_model
->ValueChanged( m_item
, m_col
);
1027 #endif // wxUSE_DATE_RENDERER_POPUP
1029 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1031 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1032 wxDataViewCellMode mode
, int align
) :
1033 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1037 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1039 m_date
= value
.GetDateTime();
1044 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1050 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1052 wxString tmp
= m_date
.FormatDate();
1053 RenderText( tmp
, 0, cell
, dc
, state
);
1057 wxSize
wxDataViewDateRenderer::GetSize() const
1059 const wxDataViewCtrl
* view
= GetView();
1060 wxString tmp
= m_date
.FormatDate();
1062 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1063 return wxSize(x
,y
+d
);
1066 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1067 const wxDataViewItem
& item
, unsigned int col
)
1070 model
->GetValue( variant
, item
, col
);
1071 wxDateTime value
= variant
.GetDateTime();
1073 #if wxUSE_DATE_RENDERER_POPUP
1074 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1075 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1076 wxPoint pos
= wxGetMousePosition();
1079 popup
->Popup( popup
->m_cal
);
1080 #else // !wxUSE_DATE_RENDERER_POPUP
1081 wxMessageBox(value
.Format());
1082 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1086 // ---------------------------------------------------------
1087 // wxDataViewIconTextRenderer
1088 // ---------------------------------------------------------
1090 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1092 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1093 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1094 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1097 SetAlignment(align
);
1100 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1104 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1110 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1115 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1118 const wxIcon
&icon
= m_value
.GetIcon();
1121 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1122 xoffset
= icon
.GetWidth()+4;
1125 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1130 wxSize
wxDataViewIconTextRenderer::GetSize() const
1132 const wxDataViewCtrl
*view
= GetView();
1133 if (!m_value
.GetText().empty())
1136 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1138 if (m_value
.GetIcon().IsOk())
1139 x
+= m_value
.GetIcon().GetWidth() + 4;
1140 return wxSize( x
, y
);
1142 return wxSize(80,20);
1146 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1147 wxRect
WXUNUSED(labelRect
),
1148 const wxVariant
& WXUNUSED(value
))
1154 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1155 wxVariant
& WXUNUSED(value
))
1160 // ---------------------------------------------------------
1162 // ---------------------------------------------------------
1164 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1166 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1167 unsigned int model_column
,
1168 int width
, wxAlignment align
, int flags
) :
1169 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1171 SetAlignment(align
);
1175 m_autosize
= width
< 0; // TODO
1177 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1180 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1181 unsigned int model_column
,
1182 int width
, wxAlignment align
, int flags
) :
1183 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1185 SetAlignment(align
);
1188 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1191 wxDataViewColumn::~wxDataViewColumn()
1195 void wxDataViewColumn::Init( int width
)
1198 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1202 void wxDataViewColumn::SetResizeable( bool resizeable
)
1205 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1207 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1210 void wxDataViewColumn::SetHidden( bool hidden
)
1213 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1215 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1217 // tell our owner to e.g. update its scrollbars:
1219 GetOwner()->OnColumnChange();
1222 void wxDataViewColumn::SetSortable( bool sortable
)
1225 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1227 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1229 // Update header button
1231 GetOwner()->OnColumnChange();
1234 void wxDataViewColumn::SetReorderable( bool reorderable
)
1237 m_flags
|= wxDATAVIEW_COL_REORDERABLE
;
1239 m_flags
&= ~wxDATAVIEW_COL_REORDERABLE
;
1242 void wxDataViewColumn::SetSortOrder( bool ascending
)
1244 m_ascending
= ascending
;
1246 // Update header button
1248 GetOwner()->OnColumnChange();
1251 bool wxDataViewColumn::IsSortOrderAscending() const
1256 void wxDataViewColumn::SetInternalWidth( int width
)
1260 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1261 if (m_owner
&& m_owner
->m_clientArea
)
1262 m_owner
->m_clientArea
->RecalculateDisplay();
1265 void wxDataViewColumn::SetWidth( int width
)
1267 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1269 SetInternalWidth(width
);
1273 //-----------------------------------------------------------------------------
1274 // wxDataViewHeaderWindowBase
1275 //-----------------------------------------------------------------------------
1277 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1279 wxWindow
*parent
= GetParent();
1280 wxDataViewEvent
le(type
, parent
->GetId());
1282 le
.SetEventObject(parent
);
1284 le
.SetDataViewColumn(GetColumn(n
));
1285 le
.SetModel(GetOwner()->GetModel());
1287 // for events created by wxDataViewHeaderWindow the
1288 // row / value fields are not valid
1290 parent
->GetEventHandler()->ProcessEvent(le
);
1293 #ifdef USE_NATIVE_HEADER_WINDOW
1295 #ifndef HDS_DRAGDROP
1296 #define HDS_DRAGDROP 0x0040
1298 #ifndef HDS_FULLDRAG
1299 #define HDS_FULLDRAG 0x0080
1302 // implemented in msw/listctrl.cpp:
1303 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1305 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1307 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1308 const wxPoint
&pos
, const wxSize
&size
,
1309 const wxString
&name
)
1313 m_scrollOffsetX
= 0;
1314 m_delayedUpdate
= false;
1315 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this );
1317 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1318 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1319 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1322 wxSize
new_size(w
,h
);
1324 if ( !CreateControl(parent
, id
, pos
, new_size
, 0, wxDefaultValidator
, name
) )
1327 // create the native WC_HEADER window:
1328 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1329 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1332 msStyle
|= WS_VISIBLE
;
1334 m_hWnd
= CreateWindowEx(0,
1345 wxLogLastError(_T("CreateWindowEx"));
1349 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1350 // to call wxDataViewHeaderWindow::MSWOnNotify
1351 SubclassWin(m_hWnd
);
1353 // the following is required to get the default win's font for
1354 // header windows and must be done befor sending the HDM_LAYOUT msg
1360 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1365 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1367 return wxSize( 80, m_buttonHeight
+2 );
1370 void wxDataViewHeaderWindowMSW::OnInternalIdle()
1372 if (m_delayedUpdate
)
1374 m_delayedUpdate
= false;
1379 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1381 // remove old columns
1382 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1383 Header_DeleteItem((HWND
)m_hWnd
, 0);
1385 // add the updated array of columns to the header control
1386 unsigned int cols
= GetOwner()->GetColumnCount();
1387 unsigned int added
= 0;
1388 for (unsigned int i
= 0; i
< cols
; i
++)
1390 wxDataViewColumn
*col
= GetColumn( i
);
1391 if (col
->IsHidden())
1392 continue; // don't add it!
1395 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1396 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1397 hdi
.cxy
= col
->GetWidth();
1398 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1399 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1400 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1402 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1404 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1405 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1406 // VZ: works with 5.81
1407 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1410 // lParam is reserved for application's use:
1411 // we store there the column index to use it later in MSWOnNotify
1412 // (since columns may have been hidden)
1413 hdi
.lParam
= (LPARAM
)i
;
1415 // the native wxMSW implementation of the header window
1416 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1417 // on the right: to correct this effect we make the column
1418 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1420 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1422 switch (col
->GetAlignment())
1425 hdi
.fmt
|= HDF_LEFT
;
1427 case wxALIGN_CENTER
:
1428 case wxALIGN_CENTER_HORIZONTAL
:
1429 hdi
.fmt
|= HDF_CENTER
;
1432 hdi
.fmt
|= HDF_RIGHT
;
1436 // such alignment is not allowed for the column header!
1440 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1441 (WPARAM
)added
, (LPARAM
)&hdi
);
1446 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1450 // NOTE: we don't just return nmHDR->iItem because when there are
1451 // hidden columns, nmHDR->iItem may be different from
1452 // nmHDR->pitem->lParam
1454 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1456 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1461 item
.mask
= HDI_LPARAM
;
1462 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1464 return (unsigned int)item
.lParam
;
1467 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1469 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1471 // is it a message from the header?
1472 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1473 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1475 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1476 switch ( nmhdr
->code
)
1478 case HDN_BEGINTRACK
:
1479 // user has started to resize a column:
1480 // do we need to veto it?
1481 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1489 // user has started to reorder a column
1490 if ((nmHDR
->iItem
!= -1) && (!GetColumn(nmHDR
->iItem
)->IsReorderable()))
1497 case HDN_ENDDRAG
: // user has finished reordering a column
1499 wxDataViewColumn
*col
= GetColumn(nmHDR
->iItem
);
1500 unsigned int new_pos
= nmHDR
->pitem
->iOrder
;
1501 m_owner
->ColumnMoved( col
, new_pos
);
1502 m_delayedUpdate
= true;
1506 case HDN_ITEMCHANGING
:
1507 if (nmHDR
->pitem
!= NULL
&&
1508 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1510 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1511 if (nmHDR
->pitem
->cxy
< minWidth
)
1513 // do not allow the user to resize this column under
1514 // its minimal width:
1520 case HDN_ITEMCHANGED
: // user is resizing a column
1521 case HDN_ENDTRACK
: // user has finished resizing a column
1523 // update the width of the modified column:
1524 if (nmHDR
->pitem
!= NULL
&&
1525 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1527 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1528 unsigned int w
= nmHDR
->pitem
->cxy
;
1529 wxDataViewColumn
*col
= GetColumn(idx
);
1531 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1532 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1533 w
-= COLUMN_WIDTH_OFFSET
;
1535 if (w
>= (unsigned)col
->GetMinWidth())
1536 col
->SetInternalWidth(w
);
1542 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1543 wxDataViewModel
* model
= GetOwner()->GetModel();
1545 if(nmHDR
->iButton
== 0)
1547 wxDataViewColumn
*col
= GetColumn(idx
);
1548 if(col
->IsSortable())
1550 if(model
&& m_owner
->GetSortingColumn() == col
)
1552 bool order
= col
->IsSortOrderAscending();
1553 col
->SetSortOrder(!order
);
1557 m_owner
->SetSortingColumn(col
);
1565 wxEventType evt
= nmHDR
->iButton
== 0 ?
1566 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1567 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1568 SendEvent(evt
, idx
);
1574 // NOTE: for some reason (i.e. for a bug in Windows)
1575 // the HDN_ITEMCLICK notification is not sent on
1576 // right clicks, so we need to handle NM_RCLICK
1579 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1580 if (column
!= wxNOT_FOUND
)
1583 item
.mask
= HDI_LPARAM
;
1584 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1586 // 'idx' may be different from 'column' if there are
1587 // hidden columns...
1588 unsigned int idx
= (unsigned int)item
.lParam
;
1589 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1595 case HDN_GETDISPINFOW
:
1596 // see wxListCtrl::MSWOnNotify for more info!
1599 case HDN_ITEMDBLCLICK
:
1601 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1602 int w
= GetOwner()->GetBestColumnWidth(idx
);
1604 // update the native control:
1606 ZeroMemory(&hd
, sizeof(hd
));
1607 hd
.mask
= HDI_WIDTH
;
1609 Header_SetItem(GetHwnd(),
1610 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1613 // update the wxDataViewColumn class:
1614 GetColumn(idx
)->SetInternalWidth(w
);
1619 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1625 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1626 const wxRect
* WXUNUSED(rect
))
1628 m_scrollOffsetX
+= dx
;
1630 GetParent()->Layout();
1633 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1637 // TODO: why is there a border + 2px around it?
1638 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1641 #else // !defined(__WXMSW__)
1643 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1644 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1645 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1646 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1647 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1650 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1651 const wxPoint
&pos
, const wxSize
&size
,
1652 const wxString
&name
)
1656 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1659 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1660 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1661 SetOwnForegroundColour( attr
.colFg
);
1662 SetOwnBackgroundColour( attr
.colBg
);
1664 SetOwnFont( attr
.font
);
1666 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1667 // a wxBoxSizer and in order to avoid super-big header windows,
1668 // we need to set our height as fixed
1669 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1670 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1675 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1678 GetClientSize( &w
, &h
);
1680 wxAutoBufferedPaintDC
dc( this );
1682 dc
.SetBackground(GetBackgroundColour());
1686 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1689 m_owner
->GetViewStart( &x
, NULL
);
1691 // account for the horz scrollbar offset
1692 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1694 dc
.SetFont( GetFont() );
1696 unsigned int cols
= GetOwner()->GetColumnCount();
1699 for (i
= 0; i
< cols
; i
++)
1701 wxDataViewColumn
*col
= GetColumn( i
);
1702 if (col
->IsHidden())
1703 continue; // skip it!
1705 int cw
= col
->GetWidth();
1708 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1709 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1711 if (col
->IsSortOrderAscending())
1712 sortArrow
= wxHDR_SORT_ICON_UP
;
1714 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1718 if (m_parent
->IsEnabled())
1720 if ((int) i
== m_hover
)
1721 state
= wxCONTROL_CURRENT
;
1725 state
= (int) wxCONTROL_DISABLED
;
1728 wxRendererNative::Get().DrawHeaderButton
1732 wxRect(xpos
, 0, cw
, ch
-1),
1737 // align as required the column title:
1739 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1740 switch (col
->GetAlignment())
1743 x
+= HEADER_HORIZ_BORDER
;
1746 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1749 case wxALIGN_CENTER
:
1750 case wxALIGN_CENTER_HORIZONTAL
:
1751 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1755 // always center the title vertically:
1756 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1758 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1760 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1761 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1762 dc
.DrawText( col
->GetTitle(), x
, y
);
1763 dc
.DestroyClippingRegion();
1769 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1771 GetParent()->SetFocus();
1775 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1777 // we want to work with logical coords
1779 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1780 int y
= event
.GetY();
1784 // we don't draw the line beyond our window,
1785 // but we allow dragging it there
1787 GetClientSize( &w
, NULL
);
1788 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1791 if (event
.ButtonUp())
1793 m_isDragging
= false;
1799 m_currentX
= wxMax(m_minX
+ 7, x
);
1803 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1805 GetOwner()->Refresh();
1809 else // not dragging
1812 m_column
= wxNOT_FOUND
;
1814 bool hit_border
= false;
1816 // end of the current column
1819 // find the column where this event occured
1820 int countCol
= m_owner
->GetColumnCount();
1821 for (int column
= 0; column
< countCol
; column
++)
1823 wxDataViewColumn
*p
= GetColumn(column
);
1826 continue; // skip if not shown
1828 xpos
+= p
->GetWidth();
1830 if ((abs(x
-xpos
) < 3) && (y
< 22))
1838 // inside the column
1845 int old_hover
= m_hover
;
1847 if (event
.Leaving())
1848 m_hover
= wxNOT_FOUND
;
1849 if (old_hover
!= m_hover
)
1852 if (m_column
== wxNOT_FOUND
)
1855 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1856 if (event
.LeftDClick() && resizeable
)
1858 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1861 else if (event
.LeftDown() || event
.RightUp())
1863 if (hit_border
&& event
.LeftDown() && resizeable
)
1865 m_isDragging
= true;
1869 else // click on a column
1871 wxDataViewModel
* model
= GetOwner()->GetModel();
1872 wxEventType evt
= event
.LeftDown() ?
1873 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1874 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1875 SendEvent(evt
, m_column
);
1877 //Left click the header
1878 if(event
.LeftDown())
1880 wxDataViewColumn
*col
= GetColumn(m_column
);
1881 if(col
->IsSortable())
1883 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1884 if(model
&& sortCol
== col
)
1886 bool order
= col
->IsSortOrderAscending();
1887 col
->SetSortOrder(!order
);
1891 m_owner
->SetSortingColumn(col
);
1897 //Send the column sorted event
1898 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1902 else if (event
.Moving())
1904 if (hit_border
&& resizeable
)
1905 m_currentCursor
= m_resizeCursor
;
1907 m_currentCursor
= wxSTANDARD_CURSOR
;
1909 SetCursor(*m_currentCursor
);
1914 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1918 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1919 m_owner
->GetViewStart( &x
, NULL
);
1921 // shift the DC origin to match the position of the main window horizontal
1922 // scrollbar: this allows us to always use logical coords
1923 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1926 #endif // defined(__WXMSW__)
1928 //-----------------------------------------------------------------------------
1929 // wxDataViewRenameTimer
1930 //-----------------------------------------------------------------------------
1932 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1937 void wxDataViewRenameTimer::Notify()
1939 m_owner
->OnRenameTimer();
1942 //-----------------------------------------------------------------------------
1943 // wxDataViewMainWindow
1944 //-----------------------------------------------------------------------------
1946 //The tree building helper, declared firstly
1947 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1949 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1951 if (row1
> row2
) return 1;
1952 if (row1
== row2
) return 0;
1957 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1959 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1960 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1961 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1962 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1963 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1964 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1967 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1968 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1969 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1970 m_selection( wxDataViewSelectionCmp
)
1975 m_lastOnSame
= false;
1976 m_renameTimer
= new wxDataViewRenameTimer( this );
1978 // TODO: user better initial values/nothing selected
1979 m_currentCol
= NULL
;
1982 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1985 m_dragStart
= wxPoint(0,0);
1986 m_lineLastClicked
= (unsigned int) -1;
1987 m_lineBeforeLastClicked
= (unsigned int) -1;
1988 m_lineSelectSingleOnUp
= (unsigned int) -1;
1992 SetBackgroundColour( *wxWHITE
);
1994 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1996 m_penRule
= wxPen(GetRuleColour());
1998 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1999 m_penExpander
= wxPen(wxColour(0,0,0));
2000 //Some new added code to deal with the tree structure
2001 m_root
= new wxDataViewTreeNode( NULL
);
2002 m_root
->SetHasChildren(true);
2004 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
2006 m_underMouse
= NULL
;
2010 wxDataViewMainWindow::~wxDataViewMainWindow()
2013 delete m_renameTimer
;
2016 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2018 wxDataViewModel
*model
= GetOwner()->GetModel();
2019 wxAutoBufferedPaintDC
dc( this );
2022 dc
.SetPen( *wxTRANSPARENT_PEN
);
2023 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
2024 dc
.SetBrush( *wxWHITE_BRUSH
);
2025 wxSize
size( GetClientSize() );
2026 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
2030 GetOwner()->PrepareDC( dc
);
2031 dc
.SetFont( GetFont() );
2033 wxRect update
= GetUpdateRegion().GetBox();
2034 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2036 // compute which items needs to be redrawn
2037 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2038 unsigned int item_count
=
2039 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2040 (int)(GetRowCount( ) - item_start
));
2041 unsigned int item_last
= item_start
+ item_count
;
2043 // compute which columns needs to be redrawn
2044 unsigned int cols
= GetOwner()->GetColumnCount();
2045 unsigned int col_start
= 0;
2046 unsigned int x_start
= 0;
2047 for (x_start
= 0; col_start
< cols
; col_start
++)
2049 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2050 if (col
->IsHidden())
2051 continue; // skip it!
2053 unsigned int w
= col
->GetWidth();
2054 if (x_start
+w
>= (unsigned int)update
.x
)
2060 unsigned int col_last
= col_start
;
2061 unsigned int x_last
= x_start
;
2062 for (; col_last
< cols
; col_last
++)
2064 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2065 if (col
->IsHidden())
2066 continue; // skip it!
2068 if (x_last
> (unsigned int)update
.GetRight())
2071 x_last
+= col
->GetWidth();
2074 // Draw horizontal rules if required
2075 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2077 dc
.SetPen(m_penRule
);
2078 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2080 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2082 int y
= i
* m_lineHeight
;
2083 dc
.DrawLine(x_start
, y
, x_last
, y
);
2087 // Draw vertical rules if required
2088 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2090 dc
.SetPen(m_penRule
);
2091 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2094 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2096 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2097 if (col
->IsHidden())
2098 continue; // skip it
2100 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2101 x
, item_last
* m_lineHeight
);
2103 x
+= col
->GetWidth();
2106 // Draw last vertical rule
2107 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2108 x
, item_last
* m_lineHeight
);
2111 // redraw the background for the items which are selected/current
2112 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2114 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2115 if (selected
|| item
== m_currentRow
)
2117 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2118 if (item
== m_currentRow
)
2119 flags
|= wxCONTROL_CURRENT
;
2121 flags
|= wxCONTROL_FOCUSED
;
2123 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2124 wxRendererNative::Get().DrawItemSelectionRect
2134 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2137 // TODO: last column for RTL support
2138 expander
= GetOwner()->GetColumn( 0 );
2139 GetOwner()->SetExpanderColumn(expander
);
2142 // redraw all cells for all rows which must be repainted and for all columns
2144 cell_rect
.x
= x_start
;
2145 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2146 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2148 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2149 wxDataViewRenderer
*cell
= col
->GetRenderer();
2150 cell_rect
.width
= col
->GetWidth();
2152 if (col
->IsHidden())
2153 continue; // skipt it!
2156 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2158 // get the cell value and set it into the renderer
2160 wxDataViewTreeNode
*node
= NULL
;
2161 wxDataViewItem dataitem
;
2165 node
= GetTreeNodeByRow(item
);
2169 dataitem
= node
->GetItem();
2171 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2176 dataitem
= wxDataViewItem( (void*) item
);
2179 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2180 cell
->SetValue( value
);
2182 if (cell
->GetWantsAttr())
2184 wxDataViewItemAttr attr
;
2185 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2187 cell
->SetAttr( attr
);
2188 cell
->SetHasAttr( ret
);
2191 // update the y offset
2192 cell_rect
.y
= item
* m_lineHeight
;
2194 //Draw the expander here.
2196 if ((m_root
) && (col
== expander
))
2198 indent
= node
->GetIndentLevel();
2200 //Calculate the indent first
2201 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2203 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2204 // change the cell_rect.x to the appropriate pos
2205 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2206 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2207 dc
.SetPen( m_penExpander
);
2208 dc
.SetBrush( wxNullBrush
);
2209 if( node
->HasChildren() )
2211 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2213 if (m_underMouse
== node
)
2215 flag
|= wxCONTROL_CURRENT
;
2217 if( node
->IsOpen() )
2218 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2220 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2222 //force the expander column to left-center align
2223 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2225 if (node
&& !node
->HasChildren())
2227 // Yes, if the node does not have any child, it must be a leaf which
2228 // mean that it is a temporarily created by GetTreeNodeByRow
2232 // cannot be bigger than allocated space
2233 wxSize size
= cell
->GetSize();
2234 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2235 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2236 // size.y = wxMin( size.y, cell_rect.height );
2237 size
.y
= cell_rect
.height
;
2239 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2240 int align
= cell
->CalculateAlignment();
2242 // horizontal alignment:
2243 item_rect
.x
= cell_rect
.x
;
2244 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2245 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2246 else if (align
& wxALIGN_RIGHT
)
2247 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2248 //else: wxALIGN_LEFT is the default
2250 // vertical alignment:
2251 item_rect
.y
= cell_rect
.y
;
2252 if (align
& wxALIGN_CENTER_VERTICAL
)
2253 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2254 else if (align
& wxALIGN_BOTTOM
)
2255 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2256 //else: wxALIGN_TOP is the default
2259 item_rect
.x
+= PADDING_RIGHTLEFT
;
2260 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2262 //Here we add the tree indent
2263 item_rect
.x
+= indent
;
2266 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2267 state
|= wxDATAVIEW_CELL_SELECTED
;
2269 // TODO: it would be much more efficient to create a clipping
2270 // region for the entire column being rendered (in the OnPaint
2271 // of wxDataViewMainWindow) instead of a single clip region for
2272 // each cell. However it would mean that each renderer should
2273 // respect the given wxRect's top & bottom coords, eventually
2274 // violating only the left & right coords - however the user can
2275 // make its own renderer and thus we cannot be sure of that.
2276 dc
.SetClippingRegion( item_rect
);
2277 cell
->Render( item_rect
, &dc
, state
);
2278 dc
.DestroyClippingRegion();
2281 cell_rect
.x
+= cell_rect
.width
;
2285 void wxDataViewMainWindow::OnRenameTimer()
2287 // We have to call this here because changes may just have
2288 // been made and no screen update taken place.
2293 unsigned int cols
= GetOwner()->GetColumnCount();
2295 for (i
= 0; i
< cols
; i
++)
2297 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2299 continue; // skip it!
2301 if (c
== m_currentCol
)
2303 xpos
+= c
->GetWidth();
2305 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2306 m_currentCol
->GetWidth(), m_lineHeight
);
2308 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2309 &labelRect
.x
, &labelRect
.y
);
2311 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2312 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2316 //------------------------------------------------------------------
2317 // Helper class for do operation on the tree node
2318 //------------------------------------------------------------------
2325 //The return value control how the tree-walker tranverse the tree
2326 // 0: Job done, stop tranverse and return
2327 // 1: Ignore the current node's subtree and continue
2328 // 2: Job not done, continue
2329 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2330 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2331 virtual int operator() ( void * n
) = 0;
2334 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2339 switch( func( node
) )
2350 wxDataViewTreeNodes nodes
= node
->GetNodes();
2351 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2353 int len_nodes
= nodes
.GetCount();
2354 int len
= leaves
.GetCount();
2355 int i
= 0, nodes_i
= 0;
2357 for( ; i
< len
; i
++ )
2359 void * n
= leaves
[i
];
2360 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2362 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2365 if( Walker( nd
, func
) )
2384 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2395 wxDataViewTreeNode
* node
;
2396 node
= FindNode(parent
);
2401 node
->SetHasChildren( true );
2403 if( g_model
->IsContainer( item
) )
2405 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2406 newnode
->SetItem(item
);
2407 newnode
->SetHasChildren( true );
2408 node
->AddNode( newnode
);
2411 node
->AddLeaf( item
.GetID() );
2413 node
->ChangeSubTreeCount(1);
2421 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2423 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2424 const wxDataViewItem
& item
)
2429 if( m_currentRow
> GetRowCount() )
2430 m_currentRow
= m_count
- 1;
2432 m_selection
.Empty();
2439 wxDataViewTreeNode
* node
= FindNode(parent
);
2441 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2442 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2445 node
->GetChildren().Remove( item
.GetID() );
2446 //Manuplate selection
2447 if( m_selection
.GetCount() > 1 )
2449 m_selection
.Empty();
2451 bool isContainer
= false;
2452 wxDataViewTreeNodes nds
= node
->GetNodes();
2453 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2455 if (nds
[i
]->GetItem() == item
)
2463 wxDataViewTreeNode
* n
= NULL
;
2464 wxDataViewTreeNodes nodes
= node
->GetNodes();
2465 int len
= nodes
.GetCount();
2466 for( int i
= 0 ; i
< len
; i
++)
2468 if( nodes
[i
]->GetItem() == item
)
2475 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2477 node
->GetNodes().Remove( n
);
2478 sub
-= n
->GetSubTreeCount();
2479 ::DestroyTreeHelper(n
);
2481 //Make the row number invalid and get a new valid one when user call GetRowCount
2483 node
->ChangeSubTreeCount(sub
);
2485 //Change the current row to the last row if the current exceed the max row number
2486 if( m_currentRow
> GetRowCount() )
2487 m_currentRow
= m_count
- 1;
2494 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2500 wxWindow
*parent
= GetParent();
2501 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2502 le
.SetEventObject(parent
);
2503 le
.SetModel(GetOwner()->GetModel());
2505 parent
->GetEventHandler()->ProcessEvent(le
);
2510 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2512 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2513 /*#define MAX_VIRTUAL_WIDTH 100000
2515 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2516 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2517 Refresh( true, &rect );
2525 wxWindow
*parent
= GetParent();
2526 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2527 le
.SetEventObject(parent
);
2528 le
.SetModel(GetOwner()->GetModel());
2531 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2532 parent
->GetEventHandler()->ProcessEvent(le
);
2537 bool wxDataViewMainWindow::Cleared()
2542 BuildTree( GetOwner()->GetModel() );
2549 void wxDataViewMainWindow::UpdateDisplay()
2554 void wxDataViewMainWindow::OnInternalIdle()
2556 wxWindow::OnInternalIdle();
2560 RecalculateDisplay();
2565 void wxDataViewMainWindow::RecalculateDisplay()
2567 wxDataViewModel
*model
= GetOwner()->GetModel();
2574 int width
= GetEndOfLastCol();
2575 int height
= GetRowCount() * m_lineHeight
;
2577 SetVirtualSize( width
, height
);
2578 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2583 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2585 wxWindow::ScrollWindow( dx
, dy
, rect
);
2587 if (GetOwner()->m_headerArea
)
2588 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2591 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2594 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2595 int sy
= rows
*m_lineHeight
/y
;
2599 wxRect rect
= GetClientRect();
2601 int x_start
= 0, x_end
= 0, w
= 0;
2603 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2604 for (x_start
= 0; colnum
< column
; colnum
++)
2606 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2607 if (col
->IsHidden())
2608 continue; // skip it!
2610 w
= col
->GetWidth();
2614 x_end
= x_start
+ w
;
2615 xe
= xx
+ rect
.width
;
2618 sx
= ( xx
+ x_end
- xe
)/x
;
2625 m_owner
->Scroll( sx
, sy
);
2628 int wxDataViewMainWindow::GetCountPerPage() const
2630 wxSize size
= GetClientSize();
2631 return size
.y
/ m_lineHeight
;
2634 int wxDataViewMainWindow::GetEndOfLastCol() const
2638 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2640 const wxDataViewColumn
*c
=
2641 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2644 width
+= c
->GetWidth();
2649 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2653 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2655 return y
/ m_lineHeight
;
2658 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2660 wxSize client_size
= GetClientSize();
2661 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2662 &client_size
.x
, &client_size
.y
);
2664 //we should deal with the pixel here
2665 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2667 return wxMin( GetRowCount()-1, row
);
2670 unsigned int wxDataViewMainWindow::GetRowCount()
2672 if ( m_count
== -1 )
2674 m_count
= RecalculateCount();
2676 GetVirtualSize( &width
, &height
);
2677 height
= m_count
* m_lineHeight
;
2679 SetVirtualSize( width
, height
);
2684 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2691 void wxDataViewMainWindow::SelectAllRows( bool on
)
2698 m_selection
.Clear();
2699 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2700 m_selection
.Add( i
);
2705 unsigned int first_visible
= GetFirstVisibleRow();
2706 unsigned int last_visible
= GetLastVisibleRow();
2708 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2710 unsigned int row
= m_selection
[i
];
2711 if ((row
>= first_visible
) && (row
<= last_visible
))
2714 m_selection
.Clear();
2718 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2720 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2724 m_selection
.Add( row
);
2732 m_selection
.Remove( row
);
2738 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2742 unsigned int tmp
= from
;
2748 for (i
= from
; i
<= to
; i
++)
2750 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2753 m_selection
.Add( i
);
2758 m_selection
.Remove( i
);
2761 RefreshRows( from
, to
);
2764 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2766 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2768 int n
= aSelections
[i
];
2770 m_selection
.Add( n
);
2775 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2777 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2778 m_selection
.Add( row
);
2780 m_selection
.Remove( row
);
2784 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2786 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2789 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2791 wxWindow
*parent
= GetParent();
2792 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2794 le
.SetEventObject(parent
);
2795 le
.SetModel(GetOwner()->GetModel());
2798 parent
->GetEventHandler()->ProcessEvent(le
);
2801 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2803 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2804 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2806 wxSize client_size
= GetClientSize();
2807 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2808 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2809 if (intersect_rect
.width
> 0)
2810 Refresh( true, &intersect_rect
);
2813 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2817 unsigned int tmp
= to
;
2822 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2823 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2825 wxSize client_size
= GetClientSize();
2826 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2827 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2828 if (intersect_rect
.width
> 0)
2829 Refresh( true, &intersect_rect
);
2832 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2834 unsigned int count
= GetRowCount();
2835 if (firstRow
> count
)
2838 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2839 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2841 wxSize client_size
= GetClientSize();
2842 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2843 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2844 if (intersect_rect
.width
> 0)
2845 Refresh( true, &intersect_rect
);
2848 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2850 wxCHECK_RET( newCurrent
< GetRowCount(),
2851 _T("invalid item index in OnArrowChar()") );
2853 // if there is no selection, we cannot move it anywhere
2854 if (!HasCurrentRow())
2857 unsigned int oldCurrent
= m_currentRow
;
2859 // in single selection we just ignore Shift as we can't select several
2861 if ( event
.ShiftDown() && !IsSingleSel() )
2863 RefreshRow( oldCurrent
);
2865 ChangeCurrentRow( newCurrent
);
2867 // select all the items between the old and the new one
2868 if ( oldCurrent
> newCurrent
)
2870 newCurrent
= oldCurrent
;
2871 oldCurrent
= m_currentRow
;
2874 SelectRows( oldCurrent
, newCurrent
, true );
2875 if (oldCurrent
!=newCurrent
)
2876 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2880 RefreshRow( oldCurrent
);
2882 // all previously selected items are unselected unless ctrl is held
2883 if ( !event
.ControlDown() )
2884 SelectAllRows(false);
2886 ChangeCurrentRow( newCurrent
);
2888 if ( !event
.ControlDown() )
2890 SelectRow( m_currentRow
, true );
2891 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2894 RefreshRow( m_currentRow
);
2897 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2900 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2904 rect
.y
= m_lineHeight
* row
;
2905 rect
.width
= GetEndOfLastCol();
2906 rect
.height
= m_lineHeight
;
2911 class RowToItemJob
: public DoJob
2914 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2915 virtual ~RowToItemJob(){};
2917 virtual int operator() ( wxDataViewTreeNode
* node
)
2920 if( current
== static_cast<int>(row
))
2922 ret
= node
->GetItem() ;
2926 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2928 current
+= node
->GetSubTreeCount();
2933 //If the current has no child node, we can find the desired item of the row number directly.
2934 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2935 if( node
->GetNodes().GetCount() == 0)
2937 int index
= static_cast<int>(row
) - current
- 1;
2938 ret
= node
->GetChildren().Item( index
);
2945 virtual int operator() ( void * n
)
2948 if( current
== static_cast<int>(row
))
2950 ret
= wxDataViewItem( n
) ;
2955 wxDataViewItem
GetResult(){ return ret
; }
2962 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2966 return wxDataViewItem( (void*) row
);
2970 RowToItemJob
job( row
, -2 );
2971 Walker( m_root
, job
);
2972 return job
.GetResult();
2976 class RowToTreeNodeJob
: public DoJob
2979 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2982 this->current
= current
;
2986 virtual ~RowToTreeNodeJob(){};
2988 virtual int operator() ( wxDataViewTreeNode
* node
)
2991 if( current
== static_cast<int>(row
))
2997 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2999 current
+= node
->GetSubTreeCount();
3005 //If the current has no child node, we can find the desired item of the row number directly.
3006 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
3007 if( node
->GetNodes().GetCount() == 0)
3009 int index
= static_cast<int>(row
) - current
- 1;
3010 void * n
= node
->GetChildren().Item( index
);
3011 ret
= new wxDataViewTreeNode( parent
) ;
3012 ret
->SetItem( wxDataViewItem( n
));
3013 ret
->SetHasChildren(false);
3022 virtual int operator() ( void * n
)
3025 if( current
== static_cast<int>(row
))
3027 ret
= new wxDataViewTreeNode( parent
) ;
3028 ret
->SetItem( wxDataViewItem( n
));
3029 ret
->SetHasChildren(false);
3035 wxDataViewTreeNode
* GetResult(){ return ret
; }
3039 wxDataViewTreeNode
* ret
;
3040 wxDataViewTreeNode
* parent
;
3044 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
3052 RowToTreeNodeJob
job( row
, -2, m_root
);
3053 Walker( m_root
, job
);
3054 return job
.GetResult();
3058 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3060 wxWindow
*parent
= GetParent();
3061 wxDataViewEvent
le(type
, parent
->GetId());
3063 le
.SetEventObject(parent
);
3064 le
.SetModel(GetOwner()->GetModel());
3067 parent
->GetEventHandler()->ProcessEvent(le
);
3071 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3073 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3076 if( node
->HasChildren())
3078 if( !node
->IsOpen())
3080 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3081 //Check if the user prevent expanding
3082 if( e
.GetSkipped() )
3086 //Here I build the children of current node
3087 if( node
->GetChildrenNumber() == 0 )
3090 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3094 //Send the expanded event
3095 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3099 SelectRow( row
, false );
3100 SelectRow( row
+ 1, true );
3101 ChangeCurrentRow( row
+ 1 );
3102 SendSelectionChangedEvent( GetItemByRow(row
+1));
3110 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3112 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3115 wxDataViewTreeNode
* nd
= node
;
3117 if( node
->HasChildren() && node
->IsOpen() )
3119 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3120 if( e
.GetSkipped() )
3125 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3129 node
= node
->GetParent();
3132 int parent
= GetRowByItem( node
->GetItem() ) ;
3135 SelectRow( row
, false);
3136 SelectRow(parent
, true );
3137 ChangeCurrentRow( parent
);
3138 SendSelectionChangedEvent( node
->GetItem() );
3142 if( !nd
->HasChildren())
3147 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3149 wxDataViewModel
* model
= GetOwner()->GetModel();
3153 //Compose the a parent-chain of the finding item
3155 list
.DeleteContents( true );
3156 wxDataViewItem
it( item
);
3159 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3160 list
.Insert( pItem
);
3161 it
= model
->GetParent( it
);
3164 //Find the item along the parent-chain.
3165 //This algorithm is designed to speed up the node-finding method
3166 wxDataViewTreeNode
* node
= m_root
;
3167 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3169 if( node
->HasChildren() )
3171 if( node
->GetChildrenNumber() == 0 )
3174 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3177 wxDataViewTreeNodes nodes
= node
->GetNodes();
3181 for (i
= 0; i
< nodes
.GetCount(); i
++)
3183 if (nodes
[i
]->GetItem() == (**iter
))
3185 if (nodes
[i
]->GetItem() == item
)
3202 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3204 wxDataViewColumn
*col
= NULL
;
3205 unsigned int cols
= GetOwner()->GetColumnCount();
3206 unsigned int colnum
= 0;
3207 unsigned int x_start
= 0;
3209 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3210 for (x_start
= 0; colnum
< cols
; colnum
++)
3212 col
= GetOwner()->GetColumn(colnum
);
3213 if (col
->IsHidden())
3214 continue; // skip it!
3216 unsigned int w
= col
->GetWidth();
3217 if (x_start
+w
>= (unsigned int)x
)
3224 item
= GetItemByRow( y
/m_lineHeight
);
3227 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3229 int row
= GetRowByItem(item
);
3230 int y
= row
*m_lineHeight
;
3231 int h
= m_lineHeight
;
3233 wxDataViewColumn
*col
= NULL
;
3234 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3236 col
= GetOwner()->GetColumn( i
);
3237 x
+= col
->GetWidth();
3238 if( GetOwner()->GetColumn(i
+1) == column
)
3241 int w
= col
->GetWidth();
3242 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3243 return wxRect(x
, y
, w
, h
);
3246 int wxDataViewMainWindow::RecalculateCount()
3250 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3252 return list_model
->GetLastIndex() + 1;
3254 return list_model
->GetLastIndex() - 1;
3259 return m_root
->GetSubTreeCount();
3263 class ItemToRowJob
: public DoJob
3266 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3267 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3268 virtual ~ItemToRowJob(){};
3270 //Maybe binary search will help to speed up this process
3271 virtual int operator() ( wxDataViewTreeNode
* node
)
3274 if( node
->GetItem() == item
)
3279 if( node
->GetItem() == **m_iter
)
3286 ret
+= node
->GetSubTreeCount();
3292 virtual int operator() ( void * n
)
3295 if( n
== item
.GetID() )
3299 //the row number is begin from zero
3300 int GetResult(){ return ret
-1 ; }
3302 ItemList::const_iterator m_iter
;
3303 wxDataViewItem item
;
3308 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3310 wxDataViewModel
* model
= GetOwner()->GetModel();
3316 return wxPtrToUInt( item
.GetID() );
3323 //Compose the a parent-chain of the finding item
3325 wxDataViewItem
* pItem
= NULL
;
3326 list
.DeleteContents( true );
3327 wxDataViewItem
it( item
);
3330 pItem
= new wxDataViewItem( it
);
3331 list
.Insert( pItem
);
3332 it
= model
->GetParent( it
);
3334 pItem
= new wxDataViewItem( );
3335 list
.Insert( pItem
);
3337 ItemToRowJob
job( item
, list
.begin() );
3338 Walker(m_root
, job
);
3339 return job
.GetResult();
3343 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3345 if( !model
->IsContainer( item
) )
3348 wxDataViewItemArray children
;
3349 unsigned int num
= model
->GetChildren( item
, children
);
3350 unsigned int index
= 0;
3351 while( index
< num
)
3353 if( model
->IsContainer( children
[index
] ) )
3355 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3356 n
->SetItem(children
[index
]);
3357 n
->SetHasChildren( true ) ;
3362 node
->AddLeaf( children
[index
].GetID() );
3366 node
->SetSubTreeCount( num
);
3367 wxDataViewTreeNode
* n
= node
->GetParent();
3369 n
->ChangeSubTreeCount(num
);
3373 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3377 if (GetOwner()->GetModel()->IsVirtualListModel())
3383 m_root
= new wxDataViewTreeNode( NULL
);
3384 m_root
->SetHasChildren(true);
3386 //First we define a invalid item to fetch the top-level elements
3387 wxDataViewItem item
;
3389 BuildTreeHelper( model
, item
, m_root
);
3393 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3395 if( node
->GetNodeNumber() != 0 )
3397 int len
= node
->GetNodeNumber();
3399 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3400 for( ; i
< len
; i
++ )
3402 DestroyTreeHelper(nodes
[i
]);
3408 void wxDataViewMainWindow::DestroyTree()
3412 ::DestroyTreeHelper(m_root
);
3418 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3420 if ( HandleAsNavigationKey(event
) )
3423 // no item -> nothing to do
3424 if (!HasCurrentRow())
3430 // don't use m_linesPerPage directly as it might not be computed yet
3431 const int pageSize
= GetCountPerPage();
3432 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3434 switch ( event
.GetKeyCode() )
3438 if (m_currentRow
> 0)
3440 wxWindow
*parent
= GetParent();
3441 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3442 le
.SetItem( GetItemByRow(m_currentRow
) );
3443 le
.SetEventObject(parent
);
3444 le
.SetModel(GetOwner()->GetModel());
3446 parent
->GetEventHandler()->ProcessEvent(le
);
3451 if ( m_currentRow
> 0 )
3452 OnArrowChar( m_currentRow
- 1, event
);
3456 if ( m_currentRow
< GetRowCount() - 1 )
3457 OnArrowChar( m_currentRow
+ 1, event
);
3459 //Add the process for tree expanding/collapsing
3461 OnCollapsing(m_currentRow
);
3464 OnExpanding( m_currentRow
);
3468 OnArrowChar( GetRowCount() - 1, event
);
3473 OnArrowChar( 0, event
);
3478 int steps
= pageSize
- 1;
3479 int index
= m_currentRow
- steps
;
3483 OnArrowChar( index
, event
);
3489 int steps
= pageSize
- 1;
3490 unsigned int index
= m_currentRow
+ steps
;
3491 unsigned int count
= GetRowCount();
3492 if ( index
>= count
)
3495 OnArrowChar( index
, event
);
3504 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3506 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3508 // let the base handle mouse wheel events.
3513 int x
= event
.GetX();
3514 int y
= event
.GetY();
3515 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3516 wxDataViewColumn
*col
= NULL
;
3519 unsigned int cols
= GetOwner()->GetColumnCount();
3521 for (i
= 0; i
< cols
; i
++)
3523 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3525 continue; // skip it!
3527 if (x
< xpos
+ c
->GetWidth())
3532 xpos
+= c
->GetWidth();
3537 wxDataViewRenderer
*cell
= col
->GetRenderer();
3538 unsigned int current
= y
/ m_lineHeight
;
3539 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3541 // Unselect all if below the last row ?
3545 //Test whether the mouse is hovered on the tree item button
3547 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3549 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3550 if( node
!=NULL
&& node
->HasChildren() )
3552 int indent
= node
->GetIndentLevel();
3553 indent
= GetOwner()->GetIndent()*indent
;
3554 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3555 if( rect
.Contains( x
, y
) )
3557 //So the mouse is over the expander
3559 if (m_underMouse
&& m_underMouse
!= node
)
3561 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3562 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3564 if (m_underMouse
!= node
)
3566 //wxLogMessage("Do the row: %d", current);
3567 RefreshRow(current
);
3569 m_underMouse
= node
;
3572 if (node
!=NULL
&& !node
->HasChildren())
3577 if (m_underMouse
!= NULL
)
3579 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3580 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3581 m_underMouse
= NULL
;
3585 wxDataViewModel
*model
= GetOwner()->GetModel();
3587 if (event
.Dragging())
3589 if (m_dragCount
== 0)
3591 // we have to report the raw, physical coords as we want to be
3592 // able to call HitTest(event.m_pointDrag) from the user code to
3593 // get the item being dragged
3594 m_dragStart
= event
.GetPosition();
3599 if (m_dragCount
!= 3)
3602 if (event
.LeftIsDown())
3604 // Notify cell about drag
3613 bool forceClick
= false;
3615 if (event
.ButtonDClick())
3617 m_renameTimer
->Stop();
3618 m_lastOnSame
= false;
3621 wxDataViewItem item
= GetItemByRow(current
);
3622 bool ignore_other_columns
=
3623 ((GetOwner()->GetExpanderColumn() != col
) &&
3624 (model
->IsContainer(item
)) &&
3625 (!model
->HasContainerColumns(item
)));
3627 if (event
.LeftDClick())
3629 if ( current
== m_lineLastClicked
)
3631 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3634 model
->GetValue( value
, item
, col
->GetModelColumn() );
3635 cell
->SetValue( value
);
3636 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3637 col
->GetWidth(), m_lineHeight
);
3638 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3643 wxWindow
*parent
= GetParent();
3644 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3646 le
.SetEventObject(parent
);
3647 le
.SetModel(GetOwner()->GetModel());
3649 parent
->GetEventHandler()->ProcessEvent(le
);
3655 // The first click was on another item, so don't interpret this as
3656 // a double click, but as a simple click instead
3663 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3665 // select single line
3666 SelectAllRows( false );
3667 SelectRow( m_lineSelectSingleOnUp
, true );
3670 //Process the event of user clicking the expander
3671 bool expander
= false;
3672 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3674 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3675 if( node
!=NULL
&& node
->HasChildren() )
3677 int indent
= node
->GetIndentLevel();
3678 indent
= GetOwner()->GetIndent()*indent
;
3679 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3680 if( rect
.Contains( x
, y
) )
3683 if( node
->IsOpen() )
3684 OnCollapsing(current
);
3686 OnExpanding( current
);
3689 if (node
&& !node
->HasChildren())
3692 //If the user click the expander, we do not do editing even if the column with expander are editable
3693 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3695 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3696 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3698 m_renameTimer
->Start( 100, true );
3702 m_lastOnSame
= false;
3703 m_lineSelectSingleOnUp
= (unsigned int)-1;
3707 // This is necessary, because after a DnD operation in
3708 // from and to ourself, the up event is swallowed by the
3709 // DnD code. So on next non-up event (which means here and
3710 // now) m_lineSelectSingleOnUp should be reset.
3711 m_lineSelectSingleOnUp
= (unsigned int)-1;
3714 if (event
.RightDown())
3716 m_lineBeforeLastClicked
= m_lineLastClicked
;
3717 m_lineLastClicked
= current
;
3719 // If the item is already selected, do not update the selection.
3720 // Multi-selections should not be cleared if a selected item is clicked.
3721 if (!IsRowSelected(current
))
3723 SelectAllRows(false);
3724 ChangeCurrentRow(current
);
3725 SelectRow(m_currentRow
,true);
3726 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3730 model
->GetValue( value
, item
, col
->GetModelColumn() );
3731 wxWindow
*parent
= GetParent();
3732 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3734 le
.SetEventObject(parent
);
3735 le
.SetModel(GetOwner()->GetModel());
3737 parent
->GetEventHandler()->ProcessEvent(le
);
3739 else if (event
.MiddleDown())
3742 if (event
.LeftDown() || forceClick
)
3746 m_lineBeforeLastClicked
= m_lineLastClicked
;
3747 m_lineLastClicked
= current
;
3749 unsigned int oldCurrentRow
= m_currentRow
;
3750 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3752 bool cmdModifierDown
= event
.CmdDown();
3753 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3755 if ( IsSingleSel() || !IsRowSelected(current
) )
3757 SelectAllRows( false );
3758 ChangeCurrentRow(current
);
3759 SelectRow(m_currentRow
,true);
3760 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3762 else // multi sel & current is highlighted & no mod keys
3764 m_lineSelectSingleOnUp
= current
;
3765 ChangeCurrentRow(current
); // change focus
3768 else // multi sel & either ctrl or shift is down
3770 if (cmdModifierDown
)
3772 ChangeCurrentRow(current
);
3773 ReverseRowSelection(m_currentRow
);
3774 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3776 else if (event
.ShiftDown())
3778 ChangeCurrentRow(current
);
3780 unsigned int lineFrom
= oldCurrentRow
,
3783 if ( lineTo
< lineFrom
)
3786 lineFrom
= m_currentRow
;
3789 SelectRows(lineFrom
, lineTo
, true);
3790 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3792 else // !ctrl, !shift
3794 // test in the enclosing if should make it impossible
3795 wxFAIL_MSG( _T("how did we get here?") );
3799 if (m_currentRow
!= oldCurrentRow
)
3800 RefreshRow( oldCurrentRow
);
3802 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3804 // Update selection here...
3807 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3808 (current
== oldCurrentRow
)) && oldWasSelected
;
3810 // Call LeftClick after everything else as under GTK+
3811 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3813 // notify cell about right click
3815 model
->GetValue( value
, item
, col
->GetModelColumn() );
3816 cell
->SetValue( value
);
3817 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3818 col
->GetWidth(), m_lineHeight
);
3819 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3824 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3828 if (HasCurrentRow())
3834 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3838 if (HasCurrentRow())
3844 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3846 if( m_selection
.GetCount() != 1 )
3847 return wxDataViewItem();
3849 return GetItemByRow( m_selection
.Item(0));
3852 //-----------------------------------------------------------------------------
3854 //-----------------------------------------------------------------------------
3855 WX_DEFINE_LIST(wxDataViewColumnList
)
3857 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3859 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3860 EVT_SIZE(wxDataViewCtrl::OnSize
)
3863 wxDataViewCtrl::~wxDataViewCtrl()
3866 GetModel()->RemoveNotifier( m_notifier
);
3868 wxDataViewColumnList::const_iterator iter
;
3869 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3875 void wxDataViewCtrl::Init()
3880 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3881 const wxPoint
& pos
, const wxSize
& size
,
3882 long style
, const wxValidator
& validator
)
3884 if ( (style
& wxBORDER_MASK
) == 0)
3885 style
|= wxBORDER_SUNKEN
;
3887 if (!wxControl::Create( parent
, id
, pos
, size
,
3888 style
| wxScrolledWindowStyle
, validator
))
3891 SetInitialSize(size
);
3896 MacSetClipChildren( true ) ;
3899 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3901 if (HasFlag(wxDV_NO_HEADER
))
3902 m_headerArea
= NULL
;
3904 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3906 SetTargetWindow( m_clientArea
);
3908 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3910 sizer
->Add( m_headerArea
, 0, wxGROW
);
3911 sizer
->Add( m_clientArea
, 1, wxGROW
);
3918 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3922 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3925 // we need to process arrows ourselves for scrolling
3926 if ( nMsg
== WM_GETDLGCODE
)
3928 rc
|= DLGC_WANTARROWS
;
3936 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3938 // We need to override OnSize so that our scrolled
3939 // window a) does call Layout() to use sizers for
3940 // positioning the controls but b) does not query
3941 // the sizer for their size and use that for setting
3942 // the scrollable area as set that ourselves by
3943 // calling SetScrollbar() further down.
3950 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3952 if (!wxDataViewCtrlBase::AssociateModel( model
))
3955 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3957 model
->AddNotifier( m_notifier
);
3959 m_clientArea
->DestroyTree();
3961 m_clientArea
->BuildTree(model
);
3963 m_clientArea
->UpdateDisplay();
3968 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3970 if (!wxDataViewCtrlBase::AppendColumn(col
))
3973 m_cols
.Append( col
);
3978 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3980 if (!wxDataViewCtrlBase::PrependColumn(col
))
3983 m_cols
.Insert( col
);
3988 void wxDataViewCtrl::OnColumnChange()
3991 m_headerArea
->UpdateDisplay();
3993 m_clientArea
->UpdateDisplay();
3996 void wxDataViewCtrl::DoSetExpanderColumn()
3998 m_clientArea
->UpdateDisplay();
4001 void wxDataViewCtrl::DoSetIndent()
4003 m_clientArea
->UpdateDisplay();
4006 unsigned int wxDataViewCtrl::GetColumnCount() const
4008 return m_cols
.GetCount();
4011 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4013 wxDataViewColumnList::const_iterator iter
;
4015 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
4020 if ((*iter
)->IsHidden())
4027 void wxDataViewCtrl::ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
)
4029 if (new_pos
> m_cols
.GetCount()) return;
4030 m_cols
.DeleteObject( col
);
4031 m_cols
.Insert( new_pos
, col
);
4033 m_clientArea
->UpdateDisplay();
4036 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4038 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4049 bool wxDataViewCtrl::ClearColumns()
4056 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4058 int ret
= 0, dead
= 0;
4059 int len
= GetColumnCount();
4060 for (int i
=0; i
<len
; i
++)
4062 wxDataViewColumn
* col
= GetColumn(i
);
4063 if (col
->IsHidden())
4065 ret
+= col
->GetWidth();
4068 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
4075 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4080 //Selection code with wxDataViewItem as parameters
4081 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4083 return m_clientArea
->GetSelection();
4086 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4089 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4090 int len
= selection
.GetCount();
4091 for( int i
= 0; i
< len
; i
++)
4093 unsigned int row
= selection
[i
];
4094 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4099 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4101 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4102 int len
= sel
.GetCount();
4103 for( int i
= 0; i
< len
; i
++ )
4105 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4107 selection
.Add( static_cast<unsigned int>(row
) );
4109 m_clientArea
->SetSelections( selection
);
4112 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4114 int row
= m_clientArea
->GetRowByItem( item
);
4117 //Unselect all rows before select another in the single select mode
4118 if (m_clientArea
->IsSingleSel())
4119 m_clientArea
->SelectAllRows(false);
4120 m_clientArea
->SelectRow(row
, true);
4124 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4126 int row
= m_clientArea
->GetRowByItem( item
);
4128 m_clientArea
->SelectRow(row
, false);
4131 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4133 int row
= m_clientArea
->GetRowByItem( item
);
4136 return m_clientArea
->IsRowSelected(row
);
4141 //Selection code with row number as parameter
4142 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4145 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4146 int len
= selection
.GetCount();
4147 for( int i
= 0; i
< len
; i
++)
4149 unsigned int row
= selection
[i
];
4155 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4157 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4158 int len
= sel
.GetCount();
4159 for( int i
= 0; i
< len
; i
++ )
4163 selection
.Add( static_cast<unsigned int>(row
) );
4165 m_clientArea
->SetSelections( selection
);
4168 void wxDataViewCtrl::Select( int row
)
4172 if (m_clientArea
->IsSingleSel())
4173 m_clientArea
->SelectAllRows(false);
4174 m_clientArea
->SelectRow( row
, true );
4178 void wxDataViewCtrl::Unselect( int row
)
4181 m_clientArea
->SelectRow(row
, false);
4184 bool wxDataViewCtrl::IsSelected( int row
) const
4187 return m_clientArea
->IsRowSelected(row
);
4191 void wxDataViewCtrl::SelectRange( int from
, int to
)
4194 for( int i
= from
; i
< to
; i
++ )
4196 m_clientArea
->Select(sel
);
4199 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4201 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4202 for( int i
= from
; i
< to
; i
++ )
4203 if( sel
.Index( i
) != wxNOT_FOUND
)
4205 m_clientArea
->SetSelections(sel
);
4208 void wxDataViewCtrl::SelectAll()
4210 m_clientArea
->SelectAllRows(true);
4213 void wxDataViewCtrl::UnselectAll()
4215 m_clientArea
->SelectAllRows(false);
4218 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4222 if( row
> (int) m_clientArea
->GetRowCount() )
4223 row
= m_clientArea
->GetRowCount();
4225 int first
= m_clientArea
->GetFirstVisibleRow();
4226 int last
= m_clientArea
->GetLastVisibleRow();
4228 m_clientArea
->ScrollTo( row
, column
);
4229 else if( row
> last
)
4230 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4232 m_clientArea
->ScrollTo( first
, column
);
4235 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4237 int row
= m_clientArea
->GetRowByItem(item
);
4240 if( column
== NULL
)
4241 EnsureVisible(row
, -1);
4245 int len
= GetColumnCount();
4246 for( int i
= 0; i
< len
; i
++ )
4247 if( GetColumn(i
) == column
)
4252 EnsureVisible( row
, col
);
4258 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4260 m_clientArea
->HitTest(point
, item
, column
);
4263 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4265 return m_clientArea
->GetItemRect(item
, column
);
4268 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4270 return m_clientArea
->GetItemByRow( row
);
4273 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4275 return m_clientArea
->GetRowByItem( item
);
4278 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4280 int row
= m_clientArea
->GetRowByItem( item
);
4282 m_clientArea
->Expand(row
);
4285 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4287 int row
= m_clientArea
->GetRowByItem( item
);
4289 m_clientArea
->Collapse(row
);
4293 // !wxUSE_GENERICDATAVIEWCTRL
4296 // wxUSE_DATAVIEWCTRL