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"
47 #include "wx/imaglist.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
55 static const int SCROLL_UNIT_X
= 15;
57 // the cell padding on the left/right
58 static const int PADDING_RIGHTLEFT
= 3;
60 // the expander space margin
61 static const int EXPANDER_MARGIN
= 4;
63 //-----------------------------------------------------------------------------
64 // wxDataViewHeaderWindow
65 //-----------------------------------------------------------------------------
67 // on wxMSW the header window (only that part however) can be made native!
68 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
69 #define USE_NATIVE_HEADER_WINDOW
72 //Below is the compare stuff
73 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
74 static wxDataViewModel
* g_model
;
75 static int g_column
= -2;
76 static bool g_asending
= true;
78 // NB: for some reason, this class must be dllexport'ed or we get warnings from
80 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
83 wxDataViewHeaderWindowBase()
86 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
87 const wxPoint
&pos
, const wxSize
&size
,
90 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
93 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
94 wxDataViewCtrl
*GetOwner() { return m_owner
; }
96 // called on column addition/removal
97 virtual void UpdateDisplay() { /* by default, do nothing */ }
99 // returns the n-th column
100 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
103 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
110 wxDataViewCtrl
*m_owner
;
112 // sends an event generated from the n-th wxDataViewColumn
113 void SendEvent(wxEventType type
, unsigned int n
);
116 #ifdef USE_NATIVE_HEADER_WINDOW
118 #define COLUMN_WIDTH_OFFSET 2
119 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
121 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
125 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
127 const wxPoint
&pos
= wxDefaultPosition
,
128 const wxSize
&size
= wxDefaultSize
,
129 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
131 Create(parent
, id
, pos
, size
, name
);
134 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
135 const wxPoint
&pos
, const wxSize
&size
,
136 const wxString
&name
);
138 ~wxDataViewHeaderWindowMSW();
140 // called when any column setting is changed and/or changed
142 virtual void UpdateDisplay();
144 virtual void OnInternalIdle();
146 // called Refresh afterwards
147 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
149 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
152 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
154 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
156 wxSize
DoGetBestSize() const;
158 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
160 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
161 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
165 bool m_delayedUpdate
;
166 wxImageList
*m_imageList
;
169 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
172 #else // !defined(__WXMSW__)
174 #define HEADER_WINDOW_HEIGHT 25
175 #define HEADER_HORIZ_BORDER 5
176 #define HEADER_VERT_BORDER 3
177 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
179 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
182 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
184 const wxPoint
&pos
= wxDefaultPosition
,
185 const wxSize
&size
= wxDefaultSize
,
186 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
189 Create(parent
, id
, pos
, size
, name
);
192 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
193 const wxPoint
&pos
, const wxSize
&size
,
194 const wxString
&name
);
196 ~wxGenericDataViewHeaderWindow()
198 delete m_resizeCursor
;
201 virtual void UpdateDisplay() { Refresh(); }
205 void OnPaint( wxPaintEvent
&event
);
206 void OnMouse( wxMouseEvent
&event
);
207 void OnSetFocus( wxFocusEvent
&event
);
212 // vars used for column resizing:
214 wxCursor
*m_resizeCursor
;
215 const wxCursor
*m_currentCursor
;
218 bool m_dirty
; // needs refresh?
219 int m_hover
; // index of the column under the mouse
220 int m_column
; // index of the column being resized
221 int m_currentX
; // divider line position in logical (unscrolled) coords
222 int m_minX
; // minimal position beyond which the divider line
223 // can't be dragged in logical coords
225 // the pen used to draw the current column width drag line
226 // when resizing the columsn
230 // internal utilities:
234 m_currentCursor
= (wxCursor
*) NULL
;
235 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
237 m_isDragging
= false;
240 m_hover
= wxNOT_FOUND
;
241 m_column
= wxNOT_FOUND
;
245 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
246 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
249 void AdjustDC(wxDC
& dc
);
252 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
253 DECLARE_EVENT_TABLE()
256 #endif // defined(__WXMSW__)
258 //-----------------------------------------------------------------------------
259 // wxDataViewRenameTimer
260 //-----------------------------------------------------------------------------
262 class wxDataViewRenameTimer
: public wxTimer
265 wxDataViewMainWindow
*m_owner
;
268 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
272 //-----------------------------------------------------------------------------
273 // wxDataViewTreeNode
274 //-----------------------------------------------------------------------------
275 class wxDataViewTreeNode
;
276 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
277 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
279 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
280 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
282 class wxDataViewTreeNode
285 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
292 m_hasChildren
= false;
296 ~wxDataViewTreeNode()
300 wxDataViewTreeNode
* GetParent() { return m_parent
; }
301 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
302 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
303 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
305 void AddNode( wxDataViewTreeNode
* node
)
307 m_leaves
.Add( node
->GetItem().GetID() );
309 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
312 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
314 void AddLeaf( void * leaf
)
316 m_leaves
.Add( leaf
);
318 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
321 wxDataViewItem
& GetItem() { return m_item
; }
322 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
324 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
325 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
329 wxDataViewTreeNode
* node
= this;
330 while( node
->GetParent()->GetParent() != NULL
)
332 node
= node
->GetParent();
345 int len
= m_nodes
.GetCount();
347 for ( int i
= 0 ;i
< len
; i
++)
348 sum
+= m_nodes
[i
]->GetSubTreeCount();
350 sum
+= m_leaves
.GetCount();
353 ChangeSubTreeCount(-sum
);
359 ChangeSubTreeCount(sum
);
362 bool HasChildren() { return m_hasChildren
; }
363 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
365 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
366 int GetSubTreeCount() { return m_subTreeCount
; }
367 void ChangeSubTreeCount( int num
)
371 m_subTreeCount
+= num
;
373 m_parent
->ChangeSubTreeCount(num
);
380 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
381 int len
= m_nodes
.GetCount();
382 for (int i
= 0; i
< len
; i
++)
383 m_nodes
[i
]->Resort();
384 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
389 wxDataViewTreeNode
*m_parent
;
390 wxDataViewTreeNodes m_nodes
;
391 wxDataViewTreeLeaves m_leaves
;
392 wxDataViewItem m_item
;
398 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
400 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
403 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
405 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
410 //-----------------------------------------------------------------------------
411 // wxDataViewMainWindow
412 //-----------------------------------------------------------------------------
414 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
416 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
417 WX_DEFINE_LIST(ItemList
)
419 class wxDataViewMainWindow
: public wxWindow
422 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
424 const wxPoint
&pos
= wxDefaultPosition
,
425 const wxSize
&size
= wxDefaultSize
,
426 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
427 virtual ~wxDataViewMainWindow();
429 // notifications from wxDataViewModel
430 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
431 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
432 bool ItemChanged( const wxDataViewItem
&item
);
433 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
447 g_model
= GetOwner()->GetModel();
448 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
451 if (g_model
->HasDefaultCompare())
459 g_column
= col
->GetModelColumn();
460 g_asending
= col
->IsSortOrderAscending();
463 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
464 wxDataViewCtrl
*GetOwner() { return m_owner
; }
465 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
467 void OnPaint( wxPaintEvent
&event
);
468 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
469 void OnChar( wxKeyEvent
&event
);
470 void OnMouse( wxMouseEvent
&event
);
471 void OnSetFocus( wxFocusEvent
&event
);
472 void OnKillFocus( wxFocusEvent
&event
);
474 void UpdateDisplay();
475 void RecalculateDisplay();
476 void OnInternalIdle();
478 void OnRenameTimer();
480 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
481 void ScrollTo( int rows
, int column
);
483 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
484 void ChangeCurrentRow( unsigned int row
);
486 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
487 bool IsEmpty() { return GetRowCount() == 0; }
489 int GetCountPerPage() const;
490 int GetEndOfLastCol() const;
491 unsigned int GetFirstVisibleRow() const;
492 //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
493 unsigned int GetLastVisibleRow();
494 unsigned int GetRowCount() ;
496 wxDataViewItem
GetSelection() const;
497 wxDataViewSelection
GetSelections(){ return m_selection
; }
498 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
499 void Select( const wxArrayInt
& aSelections
);
500 void SelectAllRows( bool on
);
501 void SelectRow( unsigned int row
, bool on
);
502 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
503 void ReverseRowSelection( unsigned int row
);
504 bool IsRowSelected( unsigned int row
);
505 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
507 void RefreshRow( unsigned int row
);
508 void RefreshRows( unsigned int from
, unsigned int to
);
509 void RefreshRowsAfter( unsigned int firstRow
);
511 // returns the colour to be used for drawing the rules
512 wxColour
GetRuleColour() const
514 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
517 wxRect
GetLineRect( unsigned int row
) const;
519 //Some useful functions for row and item mapping
520 wxDataViewItem
GetItemByRow( unsigned int row
) const;
521 int GetRowByItem( const wxDataViewItem
& item
);
523 //Methods for building the mapping tree
524 void BuildTree( wxDataViewModel
* model
);
526 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
527 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
529 void Expand( unsigned int row
) { OnExpanding( row
); }
530 void Collapse( unsigned int row
) { OnCollapsing( row
); }
532 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
533 //We did not need this temporarily
534 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
536 int RecalculateCount() ;
538 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
539 void OnExpanding( unsigned int row
);
540 void OnCollapsing( unsigned int row
);
542 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
545 wxDataViewCtrl
*m_owner
;
549 wxDataViewColumn
*m_currentCol
;
550 unsigned int m_currentRow
;
551 wxDataViewSelection m_selection
;
553 wxDataViewRenameTimer
*m_renameTimer
;
561 // for double click logic
562 unsigned int m_lineLastClicked
,
563 m_lineBeforeLastClicked
,
564 m_lineSelectSingleOnUp
;
566 // the pen used to draw horiz/vertical rules
569 // the pen used to draw the expander and the lines
572 //This is the tree structure of the model
573 wxDataViewTreeNode
* m_root
;
575 //This is the tree node under the cursor
576 wxDataViewTreeNode
* m_underMouse
;
578 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
579 DECLARE_EVENT_TABLE()
582 // ---------------------------------------------------------
583 // wxGenericDataViewModelNotifier
584 // ---------------------------------------------------------
586 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
589 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
590 { m_mainWindow
= mainWindow
; }
592 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
593 { return m_mainWindow
->ItemAdded( parent
, item
); }
594 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
595 { return m_mainWindow
->ItemDeleted( parent
, item
); }
596 virtual bool ItemChanged( const wxDataViewItem
& item
)
597 { return m_mainWindow
->ItemChanged(item
); }
598 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
599 { return m_mainWindow
->ValueChanged( item
, col
); }
600 virtual bool Cleared()
601 { return m_mainWindow
->Cleared(); }
602 virtual void Resort()
603 { m_mainWindow
->Resort(); }
605 wxDataViewMainWindow
*m_mainWindow
;
608 // ---------------------------------------------------------
609 // wxDataViewRenderer
610 // ---------------------------------------------------------
612 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
614 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
615 wxDataViewCellMode mode
,
617 wxDataViewRendererBase( varianttype
, mode
, align
)
626 wxDataViewRenderer::~wxDataViewRenderer()
632 wxDC
*wxDataViewRenderer::GetDC()
636 if (GetOwner() == NULL
)
638 if (GetOwner()->GetOwner() == NULL
)
640 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
646 void wxDataViewRenderer::SetAlignment( int align
)
651 int wxDataViewRenderer::GetAlignment() const
656 int wxDataViewRenderer::CalculateAlignment() const
658 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
660 if (GetOwner() == NULL
)
661 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
663 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
669 // ---------------------------------------------------------
670 // wxDataViewCustomRenderer
671 // ---------------------------------------------------------
673 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
675 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
676 wxDataViewCellMode mode
, int align
) :
677 wxDataViewRenderer( varianttype
, mode
, align
)
681 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
683 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
684 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
685 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
686 view
->GetForegroundColour();
687 dc
->SetTextForeground(col
);
688 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
691 // ---------------------------------------------------------
692 // wxDataViewTextRenderer
693 // ---------------------------------------------------------
695 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
697 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
698 wxDataViewCellMode mode
, int align
) :
699 wxDataViewCustomRenderer( varianttype
, mode
, align
)
703 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
705 m_text
= value
.GetString();
710 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
715 bool wxDataViewTextRenderer::HasEditorCtrl()
720 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
721 wxRect labelRect
, const wxVariant
&value
)
723 return new wxTextCtrl( parent
, wxID_ANY
, value
,
724 wxPoint(labelRect
.x
,labelRect
.y
),
725 wxSize(labelRect
.width
,labelRect
.height
) );
728 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
730 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
731 value
= text
->GetValue();
735 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
737 RenderText( m_text
, 0, cell
, dc
, state
);
741 wxSize
wxDataViewTextRenderer::GetSize() const
743 const wxDataViewCtrl
*view
= GetView();
747 view
->GetTextExtent( m_text
, &x
, &y
);
748 return wxSize( x
, y
);
750 return wxSize(80,20);
753 // ---------------------------------------------------------
754 // wxDataViewTextRendererAttr
755 // ---------------------------------------------------------
757 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
759 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
760 wxDataViewCellMode mode
, int align
) :
761 wxDataViewTextRenderer( varianttype
, mode
, align
)
766 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
773 if (m_attr
.HasColour())
775 colour
= dc
->GetTextForeground();
776 dc
->SetTextForeground( m_attr
.GetColour() );
779 if (m_attr
.GetBold() || m_attr
.GetItalic())
781 font
= dc
->GetFont();
782 wxFont myfont
= font
;
783 if (m_attr
.GetBold())
784 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
785 if (m_attr
.GetItalic())
786 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
787 dc
->SetFont( myfont
);
791 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
796 if (m_attr
.HasColour())
797 dc
->SetTextForeground( colour
);
799 if (m_attr
.GetBold() || m_attr
.GetItalic())
807 // ---------------------------------------------------------
808 // wxDataViewBitmapRenderer
809 // ---------------------------------------------------------
811 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
813 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
814 wxDataViewCellMode mode
, int align
) :
815 wxDataViewCustomRenderer( varianttype
, mode
, align
)
819 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
821 if (value
.GetType() == wxT("wxBitmap"))
823 if (value
.GetType() == wxT("wxIcon"))
829 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
834 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
837 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
838 else if (m_icon
.Ok())
839 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
844 wxSize
wxDataViewBitmapRenderer::GetSize() const
847 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
848 else if (m_icon
.Ok())
849 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
851 return wxSize(16,16);
854 // ---------------------------------------------------------
855 // wxDataViewToggleRenderer
856 // ---------------------------------------------------------
858 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
860 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
861 wxDataViewCellMode mode
, int align
) :
862 wxDataViewCustomRenderer( varianttype
, mode
, align
)
867 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
869 m_toggle
= value
.GetBool();
874 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
879 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
881 // User wxRenderer here
884 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
886 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
891 flags
|= wxCONTROL_CHECKED
;
892 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
893 flags
|= wxCONTROL_DISABLED
;
895 wxRendererNative::Get().DrawCheckBox(
896 GetOwner()->GetOwner(),
904 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
905 wxDataViewModel
*model
,
906 const wxDataViewItem
& item
, unsigned int col
)
908 bool value
= !m_toggle
;
909 wxVariant variant
= value
;
910 model
->SetValue( variant
, item
, col
);
911 model
->ValueChanged( item
, col
);
915 wxSize
wxDataViewToggleRenderer::GetSize() const
917 return wxSize(20,20);
920 // ---------------------------------------------------------
921 // wxDataViewProgressRenderer
922 // ---------------------------------------------------------
924 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
926 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
927 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
928 wxDataViewCustomRenderer( varianttype
, mode
, align
)
934 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
938 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
940 m_value
= (long) value
;
942 if (m_value
< 0) m_value
= 0;
943 if (m_value
> 100) m_value
= 100;
948 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
950 value
= (long) m_value
;
954 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
956 double pct
= (double)m_value
/ 100.0;
958 bar
.width
= (int)(cell
.width
* pct
);
959 dc
->SetPen( *wxTRANSPARENT_PEN
);
960 dc
->SetBrush( *wxBLUE_BRUSH
);
961 dc
->DrawRectangle( bar
);
963 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
964 dc
->SetPen( *wxBLACK_PEN
);
965 dc
->DrawRectangle( cell
);
970 wxSize
wxDataViewProgressRenderer::GetSize() const
972 return wxSize(40,12);
975 // ---------------------------------------------------------
976 // wxDataViewDateRenderer
977 // ---------------------------------------------------------
979 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
981 #if wxUSE_DATE_RENDERER_POPUP
983 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
986 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
987 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
988 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
993 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
994 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
995 sizer
->Add( m_cal
, 1, wxGROW
);
1000 void OnCalendar( wxCalendarEvent
&event
);
1002 wxCalendarCtrl
*m_cal
;
1003 wxDataViewModel
*m_model
;
1005 const wxDataViewItem
& m_item
;
1008 virtual void OnDismiss()
1013 DECLARE_EVENT_TABLE()
1016 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1017 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1020 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1022 wxDateTime date
= event
.GetDate();
1023 wxVariant value
= date
;
1024 m_model
->SetValue( value
, m_item
, m_col
);
1025 m_model
->ValueChanged( m_item
, m_col
);
1029 #endif // wxUSE_DATE_RENDERER_POPUP
1031 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1033 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1034 wxDataViewCellMode mode
, int align
) :
1035 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1039 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1041 m_date
= value
.GetDateTime();
1046 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1052 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1054 wxString tmp
= m_date
.FormatDate();
1055 RenderText( tmp
, 0, cell
, dc
, state
);
1059 wxSize
wxDataViewDateRenderer::GetSize() const
1061 const wxDataViewCtrl
* view
= GetView();
1062 wxString tmp
= m_date
.FormatDate();
1064 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1065 return wxSize(x
,y
+d
);
1068 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1069 const wxDataViewItem
& item
, unsigned int col
)
1072 model
->GetValue( variant
, item
, col
);
1073 wxDateTime value
= variant
.GetDateTime();
1075 #if wxUSE_DATE_RENDERER_POPUP
1076 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1077 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1078 wxPoint pos
= wxGetMousePosition();
1081 popup
->Popup( popup
->m_cal
);
1082 #else // !wxUSE_DATE_RENDERER_POPUP
1083 wxMessageBox(value
.Format());
1084 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1088 // ---------------------------------------------------------
1089 // wxDataViewIconTextRenderer
1090 // ---------------------------------------------------------
1092 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1094 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1095 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1096 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1099 SetAlignment(align
);
1102 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1106 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1112 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1117 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1120 const wxIcon
&icon
= m_value
.GetIcon();
1123 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1124 xoffset
= icon
.GetWidth()+4;
1127 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1132 wxSize
wxDataViewIconTextRenderer::GetSize() const
1134 const wxDataViewCtrl
*view
= GetView();
1135 if (!m_value
.GetText().empty())
1138 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1140 if (m_value
.GetIcon().IsOk())
1141 x
+= m_value
.GetIcon().GetWidth() + 4;
1142 return wxSize( x
, y
);
1144 return wxSize(80,20);
1148 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1149 wxRect
WXUNUSED(labelRect
),
1150 const wxVariant
& WXUNUSED(value
))
1156 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1157 wxVariant
& WXUNUSED(value
))
1162 // ---------------------------------------------------------
1164 // ---------------------------------------------------------
1166 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1168 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1169 unsigned int model_column
,
1170 int width
, wxAlignment align
, int flags
) :
1171 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1173 SetAlignment(align
);
1177 m_autosize
= width
< 0; // TODO
1179 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1182 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1183 unsigned int model_column
,
1184 int width
, wxAlignment align
, int flags
) :
1185 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1187 SetAlignment(align
);
1190 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1193 wxDataViewColumn::~wxDataViewColumn()
1197 void wxDataViewColumn::Init( int width
)
1200 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1204 void wxDataViewColumn::SetResizeable( bool resizeable
)
1207 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1209 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1212 void wxDataViewColumn::SetHidden( bool hidden
)
1215 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1217 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1219 // tell our owner to e.g. update its scrollbars:
1221 GetOwner()->OnColumnChange();
1224 void wxDataViewColumn::SetSortable( bool sortable
)
1227 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1229 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1231 // Update header button
1233 GetOwner()->OnColumnChange();
1236 void wxDataViewColumn::SetReorderable( bool reorderable
)
1239 m_flags
|= wxDATAVIEW_COL_REORDERABLE
;
1241 m_flags
&= ~wxDATAVIEW_COL_REORDERABLE
;
1244 void wxDataViewColumn::SetSortOrder( bool ascending
)
1246 m_ascending
= ascending
;
1248 // Update header button
1250 GetOwner()->OnColumnChange();
1253 bool wxDataViewColumn::IsSortOrderAscending() const
1258 void wxDataViewColumn::SetInternalWidth( int width
)
1262 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1263 if (m_owner
&& m_owner
->m_clientArea
)
1264 m_owner
->m_clientArea
->RecalculateDisplay();
1267 void wxDataViewColumn::SetWidth( int width
)
1269 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1271 SetInternalWidth(width
);
1275 //-----------------------------------------------------------------------------
1276 // wxDataViewHeaderWindowBase
1277 //-----------------------------------------------------------------------------
1279 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1281 wxWindow
*parent
= GetParent();
1282 wxDataViewEvent
le(type
, parent
->GetId());
1284 le
.SetEventObject(parent
);
1286 le
.SetDataViewColumn(GetColumn(n
));
1287 le
.SetModel(GetOwner()->GetModel());
1289 // for events created by wxDataViewHeaderWindow the
1290 // row / value fields are not valid
1292 parent
->GetEventHandler()->ProcessEvent(le
);
1295 #ifdef USE_NATIVE_HEADER_WINDOW
1297 #ifndef HDS_DRAGDROP
1298 #define HDS_DRAGDROP 0x0040
1300 #ifndef HDS_FULLDRAG
1301 #define HDS_FULLDRAG 0x0080
1304 // implemented in msw/listctrl.cpp:
1305 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1307 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1309 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1310 const wxPoint
&pos
, const wxSize
&size
,
1311 const wxString
&name
)
1315 m_scrollOffsetX
= 0;
1316 m_delayedUpdate
= false;
1317 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this );
1319 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1320 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1321 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1324 wxSize
new_size(w
,h
);
1326 if ( !CreateControl(parent
, id
, pos
, new_size
, 0, wxDefaultValidator
, name
) )
1329 // create the native WC_HEADER window:
1330 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1331 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1334 msStyle
|= WS_VISIBLE
;
1336 m_hWnd
= CreateWindowEx(0,
1347 wxLogLastError(_T("CreateWindowEx"));
1351 m_imageList
= new wxImageList( 16, 16 );
1352 Header_SetImageList( (HWND
) m_hWnd
, m_imageList
->GetHIMAGELIST() );
1354 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1355 // to call wxDataViewHeaderWindow::MSWOnNotify
1356 SubclassWin(m_hWnd
);
1358 // the following is required to get the default win's font for
1359 // header windows and must be done befor sending the HDM_LAYOUT msg
1365 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1371 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1373 return wxSize( 80, m_buttonHeight
+2 );
1376 void wxDataViewHeaderWindowMSW::OnInternalIdle()
1378 if (m_delayedUpdate
)
1380 m_delayedUpdate
= false;
1385 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1387 // remove old columns
1388 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1389 Header_DeleteItem((HWND
)m_hWnd
, 0);
1391 m_imageList
->RemoveAll();
1393 // add the updated array of columns to the header control
1394 unsigned int cols
= GetOwner()->GetColumnCount();
1395 unsigned int added
= 0;
1396 for (unsigned int i
= 0; i
< cols
; i
++)
1398 wxDataViewColumn
*col
= GetColumn( i
);
1399 if (col
->IsHidden())
1400 continue; // don't add it!
1403 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1404 if (col
->GetBitmap().IsOk())
1406 m_imageList
->Add( col
->GetBitmap() );
1407 hdi
.mask
|= HDI_IMAGE
;
1408 hdi
.iImage
= m_imageList
->GetImageCount()-1;
1410 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1411 hdi
.cxy
= col
->GetWidth();
1412 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1413 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1414 if (col
->GetBitmap().IsOk())
1415 hdi
.fmt
|= HDF_IMAGE
;
1417 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1419 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1421 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1422 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1423 // VZ: works with 5.81
1424 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1427 // lParam is reserved for application's use:
1428 // we store there the column index to use it later in MSWOnNotify
1429 // (since columns may have been hidden)
1430 hdi
.lParam
= (LPARAM
)i
;
1432 // the native wxMSW implementation of the header window
1433 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1434 // on the right: to correct this effect we make the column
1435 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1437 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1439 switch (col
->GetAlignment())
1442 hdi
.fmt
|= HDF_LEFT
;
1444 case wxALIGN_CENTER
:
1445 case wxALIGN_CENTER_HORIZONTAL
:
1446 hdi
.fmt
|= HDF_CENTER
;
1449 hdi
.fmt
|= HDF_RIGHT
;
1453 // such alignment is not allowed for the column header!
1457 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1458 (WPARAM
)added
, (LPARAM
)&hdi
);
1463 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1467 // NOTE: we don't just return nmHDR->iItem because when there are
1468 // hidden columns, nmHDR->iItem may be different from
1469 // nmHDR->pitem->lParam
1471 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1473 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1478 item
.mask
= HDI_LPARAM
;
1479 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1481 return (unsigned int)item
.lParam
;
1484 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1486 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1488 // is it a message from the header?
1489 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1490 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1492 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1493 switch ( nmhdr
->code
)
1495 case HDN_BEGINTRACK
:
1496 // user has started to resize a column:
1497 // do we need to veto it?
1498 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1506 // user has started to reorder a column
1507 if ((nmHDR
->iItem
!= -1) && (!GetColumn(nmHDR
->iItem
)->IsReorderable()))
1514 case HDN_ENDDRAG
: // user has finished reordering a column
1516 wxDataViewColumn
*col
= GetColumn(nmHDR
->iItem
);
1517 unsigned int new_pos
= nmHDR
->pitem
->iOrder
;
1518 m_owner
->ColumnMoved( col
, new_pos
);
1519 m_delayedUpdate
= true;
1523 case HDN_ITEMCHANGING
:
1524 if (nmHDR
->pitem
!= NULL
&&
1525 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1527 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1528 if (nmHDR
->pitem
->cxy
< minWidth
)
1530 // do not allow the user to resize this column under
1531 // its minimal width:
1537 case HDN_ITEMCHANGED
: // user is resizing a column
1538 case HDN_ENDTRACK
: // user has finished resizing a column
1540 // update the width of the modified column:
1541 if (nmHDR
->pitem
!= NULL
&&
1542 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1544 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1545 unsigned int w
= nmHDR
->pitem
->cxy
;
1546 wxDataViewColumn
*col
= GetColumn(idx
);
1548 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1549 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1550 w
-= COLUMN_WIDTH_OFFSET
;
1552 if (w
>= (unsigned)col
->GetMinWidth())
1553 col
->SetInternalWidth(w
);
1559 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1560 wxDataViewModel
* model
= GetOwner()->GetModel();
1562 if(nmHDR
->iButton
== 0)
1564 wxDataViewColumn
*col
= GetColumn(idx
);
1565 if(col
->IsSortable())
1567 if(model
&& m_owner
->GetSortingColumn() == col
)
1569 bool order
= col
->IsSortOrderAscending();
1570 col
->SetSortOrder(!order
);
1574 m_owner
->SetSortingColumn(col
);
1582 wxEventType evt
= nmHDR
->iButton
== 0 ?
1583 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1584 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1585 SendEvent(evt
, idx
);
1591 // NOTE: for some reason (i.e. for a bug in Windows)
1592 // the HDN_ITEMCLICK notification is not sent on
1593 // right clicks, so we need to handle NM_RCLICK
1596 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1597 if (column
!= wxNOT_FOUND
)
1600 item
.mask
= HDI_LPARAM
;
1601 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1603 // 'idx' may be different from 'column' if there are
1604 // hidden columns...
1605 unsigned int idx
= (unsigned int)item
.lParam
;
1606 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1612 case HDN_GETDISPINFOW
:
1613 // see wxListCtrl::MSWOnNotify for more info!
1616 case HDN_ITEMDBLCLICK
:
1618 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1619 int w
= GetOwner()->GetBestColumnWidth(idx
);
1621 // update the native control:
1623 ZeroMemory(&hd
, sizeof(hd
));
1624 hd
.mask
= HDI_WIDTH
;
1626 Header_SetItem(GetHwnd(),
1627 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1630 // update the wxDataViewColumn class:
1631 GetColumn(idx
)->SetInternalWidth(w
);
1636 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1642 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1643 const wxRect
* WXUNUSED(rect
))
1645 m_scrollOffsetX
+= dx
;
1647 GetParent()->Layout();
1650 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1654 // TODO: why is there a border + 2px around it?
1655 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1658 #else // !defined(__WXMSW__)
1660 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1661 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1662 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1663 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1664 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1667 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1668 const wxPoint
&pos
, const wxSize
&size
,
1669 const wxString
&name
)
1673 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1676 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1677 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1678 SetOwnForegroundColour( attr
.colFg
);
1679 SetOwnBackgroundColour( attr
.colBg
);
1681 SetOwnFont( attr
.font
);
1683 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1684 // a wxBoxSizer and in order to avoid super-big header windows,
1685 // we need to set our height as fixed
1686 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1687 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1692 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1695 GetClientSize( &w
, &h
);
1697 wxAutoBufferedPaintDC
dc( this );
1699 dc
.SetBackground(GetBackgroundColour());
1703 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1706 m_owner
->GetViewStart( &x
, NULL
);
1708 // account for the horz scrollbar offset
1709 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1711 dc
.SetFont( GetFont() );
1713 unsigned int cols
= GetOwner()->GetColumnCount();
1716 for (i
= 0; i
< cols
; i
++)
1718 wxDataViewColumn
*col
= GetColumn( i
);
1719 if (col
->IsHidden())
1720 continue; // skip it!
1722 int cw
= col
->GetWidth();
1725 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1726 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1728 if (col
->IsSortOrderAscending())
1729 sortArrow
= wxHDR_SORT_ICON_UP
;
1731 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1735 if (m_parent
->IsEnabled())
1737 if ((int) i
== m_hover
)
1738 state
= wxCONTROL_CURRENT
;
1742 state
= (int) wxCONTROL_DISABLED
;
1745 wxRendererNative::Get().DrawHeaderButton
1749 wxRect(xpos
, 0, cw
, ch
-1),
1754 // align as required the column title:
1756 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1757 switch (col
->GetAlignment())
1760 x
+= HEADER_HORIZ_BORDER
;
1763 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1766 case wxALIGN_CENTER
:
1767 case wxALIGN_CENTER_HORIZONTAL
:
1768 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1772 // always center the title vertically:
1773 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1775 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1777 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1778 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1779 dc
.DrawText( col
->GetTitle(), x
, y
);
1780 dc
.DestroyClippingRegion();
1786 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1788 GetParent()->SetFocus();
1792 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1794 // we want to work with logical coords
1796 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1797 int y
= event
.GetY();
1801 // we don't draw the line beyond our window,
1802 // but we allow dragging it there
1804 GetClientSize( &w
, NULL
);
1805 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1808 if (event
.ButtonUp())
1810 m_isDragging
= false;
1816 m_currentX
= wxMax(m_minX
+ 7, x
);
1820 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1822 GetOwner()->Refresh();
1826 else // not dragging
1829 m_column
= wxNOT_FOUND
;
1831 bool hit_border
= false;
1833 // end of the current column
1836 // find the column where this event occured
1837 int countCol
= m_owner
->GetColumnCount();
1838 for (int column
= 0; column
< countCol
; column
++)
1840 wxDataViewColumn
*p
= GetColumn(column
);
1843 continue; // skip if not shown
1845 xpos
+= p
->GetWidth();
1847 if ((abs(x
-xpos
) < 3) && (y
< 22))
1855 // inside the column
1862 int old_hover
= m_hover
;
1864 if (event
.Leaving())
1865 m_hover
= wxNOT_FOUND
;
1866 if (old_hover
!= m_hover
)
1869 if (m_column
== wxNOT_FOUND
)
1872 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1873 if (event
.LeftDClick() && resizeable
)
1875 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1878 else if (event
.LeftDown() || event
.RightUp())
1880 if (hit_border
&& event
.LeftDown() && resizeable
)
1882 m_isDragging
= true;
1886 else // click on a column
1888 wxDataViewModel
* model
= GetOwner()->GetModel();
1889 wxEventType evt
= event
.LeftDown() ?
1890 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1891 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1892 SendEvent(evt
, m_column
);
1894 //Left click the header
1895 if(event
.LeftDown())
1897 wxDataViewColumn
*col
= GetColumn(m_column
);
1898 if(col
->IsSortable())
1900 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1901 if(model
&& sortCol
== col
)
1903 bool order
= col
->IsSortOrderAscending();
1904 col
->SetSortOrder(!order
);
1908 m_owner
->SetSortingColumn(col
);
1914 //Send the column sorted event
1915 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1919 else if (event
.Moving())
1921 if (hit_border
&& resizeable
)
1922 m_currentCursor
= m_resizeCursor
;
1924 m_currentCursor
= wxSTANDARD_CURSOR
;
1926 SetCursor(*m_currentCursor
);
1931 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1935 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1936 m_owner
->GetViewStart( &x
, NULL
);
1938 // shift the DC origin to match the position of the main window horizontal
1939 // scrollbar: this allows us to always use logical coords
1940 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1943 #endif // defined(__WXMSW__)
1945 //-----------------------------------------------------------------------------
1946 // wxDataViewRenameTimer
1947 //-----------------------------------------------------------------------------
1949 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1954 void wxDataViewRenameTimer::Notify()
1956 m_owner
->OnRenameTimer();
1959 //-----------------------------------------------------------------------------
1960 // wxDataViewMainWindow
1961 //-----------------------------------------------------------------------------
1963 //The tree building helper, declared firstly
1964 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1966 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1968 if (row1
> row2
) return 1;
1969 if (row1
== row2
) return 0;
1974 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1976 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1977 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1978 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1979 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1980 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1981 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1984 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1985 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1986 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1987 m_selection( wxDataViewSelectionCmp
)
1992 m_lastOnSame
= false;
1993 m_renameTimer
= new wxDataViewRenameTimer( this );
1995 // TODO: user better initial values/nothing selected
1996 m_currentCol
= NULL
;
1999 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
2002 m_dragStart
= wxPoint(0,0);
2003 m_lineLastClicked
= (unsigned int) -1;
2004 m_lineBeforeLastClicked
= (unsigned int) -1;
2005 m_lineSelectSingleOnUp
= (unsigned int) -1;
2009 SetBackgroundColour( *wxWHITE
);
2011 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
2013 m_penRule
= wxPen(GetRuleColour());
2015 //Here I compose a pen can draw black lines, maybe there are something system colour to use
2016 m_penExpander
= wxPen(wxColour(0,0,0));
2017 //Some new added code to deal with the tree structure
2018 m_root
= new wxDataViewTreeNode( NULL
);
2019 m_root
->SetHasChildren(true);
2021 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
2023 m_underMouse
= NULL
;
2027 wxDataViewMainWindow::~wxDataViewMainWindow()
2030 delete m_renameTimer
;
2033 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2035 wxDataViewModel
*model
= GetOwner()->GetModel();
2036 wxAutoBufferedPaintDC
dc( this );
2039 dc
.SetPen( *wxTRANSPARENT_PEN
);
2040 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
2041 dc
.SetBrush( *wxWHITE_BRUSH
);
2042 wxSize
size( GetClientSize() );
2043 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
2047 GetOwner()->PrepareDC( dc
);
2048 dc
.SetFont( GetFont() );
2050 wxRect update
= GetUpdateRegion().GetBox();
2051 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2053 // compute which items needs to be redrawn
2054 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2055 unsigned int item_count
=
2056 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2057 (int)(GetRowCount( ) - item_start
));
2058 unsigned int item_last
= item_start
+ item_count
;
2060 // compute which columns needs to be redrawn
2061 unsigned int cols
= GetOwner()->GetColumnCount();
2062 unsigned int col_start
= 0;
2063 unsigned int x_start
= 0;
2064 for (x_start
= 0; col_start
< cols
; col_start
++)
2066 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2067 if (col
->IsHidden())
2068 continue; // skip it!
2070 unsigned int w
= col
->GetWidth();
2071 if (x_start
+w
>= (unsigned int)update
.x
)
2077 unsigned int col_last
= col_start
;
2078 unsigned int x_last
= x_start
;
2079 for (; col_last
< cols
; col_last
++)
2081 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2082 if (col
->IsHidden())
2083 continue; // skip it!
2085 if (x_last
> (unsigned int)update
.GetRight())
2088 x_last
+= col
->GetWidth();
2091 // Draw horizontal rules if required
2092 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2094 dc
.SetPen(m_penRule
);
2095 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2097 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2099 int y
= i
* m_lineHeight
;
2100 dc
.DrawLine(x_start
, y
, x_last
, y
);
2104 // Draw vertical rules if required
2105 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2107 dc
.SetPen(m_penRule
);
2108 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2111 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2113 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2114 if (col
->IsHidden())
2115 continue; // skip it
2117 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2118 x
, item_last
* m_lineHeight
);
2120 x
+= col
->GetWidth();
2123 // Draw last vertical rule
2124 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2125 x
, item_last
* m_lineHeight
);
2128 // redraw the background for the items which are selected/current
2129 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2131 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2132 if (selected
|| item
== m_currentRow
)
2134 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2135 if (item
== m_currentRow
)
2136 flags
|= wxCONTROL_CURRENT
;
2138 flags
|= wxCONTROL_FOCUSED
;
2140 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2141 wxRendererNative::Get().DrawItemSelectionRect
2151 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2154 // TODO: last column for RTL support
2155 expander
= GetOwner()->GetColumn( 0 );
2156 GetOwner()->SetExpanderColumn(expander
);
2159 // redraw all cells for all rows which must be repainted and for all columns
2161 cell_rect
.x
= x_start
;
2162 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2163 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2165 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2166 wxDataViewRenderer
*cell
= col
->GetRenderer();
2167 cell_rect
.width
= col
->GetWidth();
2169 if (col
->IsHidden())
2170 continue; // skipt it!
2173 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2175 // get the cell value and set it into the renderer
2177 wxDataViewTreeNode
*node
= NULL
;
2178 wxDataViewItem dataitem
;
2182 node
= GetTreeNodeByRow(item
);
2186 dataitem
= node
->GetItem();
2188 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2193 dataitem
= wxDataViewItem( (void*) item
);
2196 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2197 cell
->SetValue( value
);
2199 if (cell
->GetWantsAttr())
2201 wxDataViewItemAttr attr
;
2202 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2204 cell
->SetAttr( attr
);
2205 cell
->SetHasAttr( ret
);
2208 // update the y offset
2209 cell_rect
.y
= item
* m_lineHeight
;
2211 //Draw the expander here.
2213 if ((m_root
) && (col
== expander
))
2215 indent
= node
->GetIndentLevel();
2217 //Calculate the indent first
2218 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2220 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2221 // change the cell_rect.x to the appropriate pos
2222 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2223 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2224 dc
.SetPen( m_penExpander
);
2225 dc
.SetBrush( wxNullBrush
);
2226 if( node
->HasChildren() )
2228 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2230 if (m_underMouse
== node
)
2232 flag
|= wxCONTROL_CURRENT
;
2234 if( node
->IsOpen() )
2235 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2237 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2239 //force the expander column to left-center align
2240 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2242 if (node
&& !node
->HasChildren())
2244 // Yes, if the node does not have any child, it must be a leaf which
2245 // mean that it is a temporarily created by GetTreeNodeByRow
2249 // cannot be bigger than allocated space
2250 wxSize size
= cell
->GetSize();
2251 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2252 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2253 // size.y = wxMin( size.y, cell_rect.height );
2254 size
.y
= cell_rect
.height
;
2256 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2257 int align
= cell
->CalculateAlignment();
2259 // horizontal alignment:
2260 item_rect
.x
= cell_rect
.x
;
2261 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2262 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2263 else if (align
& wxALIGN_RIGHT
)
2264 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2265 //else: wxALIGN_LEFT is the default
2267 // vertical alignment:
2268 item_rect
.y
= cell_rect
.y
;
2269 if (align
& wxALIGN_CENTER_VERTICAL
)
2270 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2271 else if (align
& wxALIGN_BOTTOM
)
2272 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2273 //else: wxALIGN_TOP is the default
2276 item_rect
.x
+= PADDING_RIGHTLEFT
;
2277 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2279 //Here we add the tree indent
2280 item_rect
.x
+= indent
;
2283 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2284 state
|= wxDATAVIEW_CELL_SELECTED
;
2286 // TODO: it would be much more efficient to create a clipping
2287 // region for the entire column being rendered (in the OnPaint
2288 // of wxDataViewMainWindow) instead of a single clip region for
2289 // each cell. However it would mean that each renderer should
2290 // respect the given wxRect's top & bottom coords, eventually
2291 // violating only the left & right coords - however the user can
2292 // make its own renderer and thus we cannot be sure of that.
2293 dc
.SetClippingRegion( item_rect
);
2294 cell
->Render( item_rect
, &dc
, state
);
2295 dc
.DestroyClippingRegion();
2298 cell_rect
.x
+= cell_rect
.width
;
2302 void wxDataViewMainWindow::OnRenameTimer()
2304 // We have to call this here because changes may just have
2305 // been made and no screen update taken place.
2310 unsigned int cols
= GetOwner()->GetColumnCount();
2312 for (i
= 0; i
< cols
; i
++)
2314 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2316 continue; // skip it!
2318 if (c
== m_currentCol
)
2320 xpos
+= c
->GetWidth();
2322 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2323 m_currentCol
->GetWidth(), m_lineHeight
);
2325 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2326 &labelRect
.x
, &labelRect
.y
);
2328 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2329 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2333 //------------------------------------------------------------------
2334 // Helper class for do operation on the tree node
2335 //------------------------------------------------------------------
2342 //The return value control how the tree-walker tranverse the tree
2343 // 0: Job done, stop tranverse and return
2344 // 1: Ignore the current node's subtree and continue
2345 // 2: Job not done, continue
2346 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2347 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2348 virtual int operator() ( void * n
) = 0;
2351 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2356 switch( func( node
) )
2367 wxDataViewTreeNodes nodes
= node
->GetNodes();
2368 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2370 int len_nodes
= nodes
.GetCount();
2371 int len
= leaves
.GetCount();
2372 int i
= 0, nodes_i
= 0;
2374 for( ; i
< len
; i
++ )
2376 void * n
= leaves
[i
];
2377 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2379 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2382 if( Walker( nd
, func
) )
2401 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2412 wxDataViewTreeNode
* node
;
2413 node
= FindNode(parent
);
2418 node
->SetHasChildren( true );
2420 if( g_model
->IsContainer( item
) )
2422 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2423 newnode
->SetItem(item
);
2424 newnode
->SetHasChildren( true );
2425 node
->AddNode( newnode
);
2428 node
->AddLeaf( item
.GetID() );
2430 node
->ChangeSubTreeCount(1);
2438 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2440 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2441 const wxDataViewItem
& item
)
2446 if( m_currentRow
> GetRowCount() )
2447 m_currentRow
= m_count
- 1;
2449 m_selection
.Empty();
2456 wxDataViewTreeNode
* node
= FindNode(parent
);
2458 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2459 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2462 node
->GetChildren().Remove( item
.GetID() );
2463 //Manuplate selection
2464 if( m_selection
.GetCount() > 1 )
2466 m_selection
.Empty();
2468 bool isContainer
= false;
2469 wxDataViewTreeNodes nds
= node
->GetNodes();
2470 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2472 if (nds
[i
]->GetItem() == item
)
2480 wxDataViewTreeNode
* n
= NULL
;
2481 wxDataViewTreeNodes nodes
= node
->GetNodes();
2482 int len
= nodes
.GetCount();
2483 for( int i
= 0 ; i
< len
; i
++)
2485 if( nodes
[i
]->GetItem() == item
)
2492 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2494 node
->GetNodes().Remove( n
);
2495 sub
-= n
->GetSubTreeCount();
2496 ::DestroyTreeHelper(n
);
2498 //Make the row number invalid and get a new valid one when user call GetRowCount
2500 node
->ChangeSubTreeCount(sub
);
2502 //Change the current row to the last row if the current exceed the max row number
2503 if( m_currentRow
> GetRowCount() )
2504 m_currentRow
= m_count
- 1;
2511 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2517 wxWindow
*parent
= GetParent();
2518 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2519 le
.SetEventObject(parent
);
2520 le
.SetModel(GetOwner()->GetModel());
2522 parent
->GetEventHandler()->ProcessEvent(le
);
2527 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2529 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2530 /*#define MAX_VIRTUAL_WIDTH 100000
2532 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2533 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2534 Refresh( true, &rect );
2542 wxWindow
*parent
= GetParent();
2543 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2544 le
.SetEventObject(parent
);
2545 le
.SetModel(GetOwner()->GetModel());
2548 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2549 parent
->GetEventHandler()->ProcessEvent(le
);
2554 bool wxDataViewMainWindow::Cleared()
2559 BuildTree( GetOwner()->GetModel() );
2566 void wxDataViewMainWindow::UpdateDisplay()
2571 void wxDataViewMainWindow::OnInternalIdle()
2573 wxWindow::OnInternalIdle();
2577 RecalculateDisplay();
2582 void wxDataViewMainWindow::RecalculateDisplay()
2584 wxDataViewModel
*model
= GetOwner()->GetModel();
2591 int width
= GetEndOfLastCol();
2592 int height
= GetRowCount() * m_lineHeight
;
2594 SetVirtualSize( width
, height
);
2595 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2600 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2602 wxWindow::ScrollWindow( dx
, dy
, rect
);
2604 if (GetOwner()->m_headerArea
)
2605 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2608 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2611 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2612 int sy
= rows
*m_lineHeight
/y
;
2616 wxRect rect
= GetClientRect();
2618 int x_start
= 0, x_end
= 0, w
= 0;
2620 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2621 for (x_start
= 0; colnum
< column
; colnum
++)
2623 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2624 if (col
->IsHidden())
2625 continue; // skip it!
2627 w
= col
->GetWidth();
2631 x_end
= x_start
+ w
;
2632 xe
= xx
+ rect
.width
;
2635 sx
= ( xx
+ x_end
- xe
)/x
;
2642 m_owner
->Scroll( sx
, sy
);
2645 int wxDataViewMainWindow::GetCountPerPage() const
2647 wxSize size
= GetClientSize();
2648 return size
.y
/ m_lineHeight
;
2651 int wxDataViewMainWindow::GetEndOfLastCol() const
2655 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2657 const wxDataViewColumn
*c
=
2658 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2661 width
+= c
->GetWidth();
2666 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2670 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2672 return y
/ m_lineHeight
;
2675 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2677 wxSize client_size
= GetClientSize();
2678 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2679 &client_size
.x
, &client_size
.y
);
2681 //we should deal with the pixel here
2682 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2684 return wxMin( GetRowCount()-1, row
);
2687 unsigned int wxDataViewMainWindow::GetRowCount()
2689 if ( m_count
== -1 )
2691 m_count
= RecalculateCount();
2693 GetVirtualSize( &width
, &height
);
2694 height
= m_count
* m_lineHeight
;
2696 SetVirtualSize( width
, height
);
2701 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2708 void wxDataViewMainWindow::SelectAllRows( bool on
)
2715 m_selection
.Clear();
2716 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2717 m_selection
.Add( i
);
2722 unsigned int first_visible
= GetFirstVisibleRow();
2723 unsigned int last_visible
= GetLastVisibleRow();
2725 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2727 unsigned int row
= m_selection
[i
];
2728 if ((row
>= first_visible
) && (row
<= last_visible
))
2731 m_selection
.Clear();
2735 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2737 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2741 m_selection
.Add( row
);
2749 m_selection
.Remove( row
);
2755 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2759 unsigned int tmp
= from
;
2765 for (i
= from
; i
<= to
; i
++)
2767 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2770 m_selection
.Add( i
);
2775 m_selection
.Remove( i
);
2778 RefreshRows( from
, to
);
2781 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2783 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2785 int n
= aSelections
[i
];
2787 m_selection
.Add( n
);
2792 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2794 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2795 m_selection
.Add( row
);
2797 m_selection
.Remove( row
);
2801 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2803 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2806 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2808 wxWindow
*parent
= GetParent();
2809 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2811 le
.SetEventObject(parent
);
2812 le
.SetModel(GetOwner()->GetModel());
2815 parent
->GetEventHandler()->ProcessEvent(le
);
2818 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2820 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2821 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2823 wxSize client_size
= GetClientSize();
2824 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2825 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2826 if (intersect_rect
.width
> 0)
2827 Refresh( true, &intersect_rect
);
2830 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2834 unsigned int tmp
= to
;
2839 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2840 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2842 wxSize client_size
= GetClientSize();
2843 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2844 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2845 if (intersect_rect
.width
> 0)
2846 Refresh( true, &intersect_rect
);
2849 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2851 unsigned int count
= GetRowCount();
2852 if (firstRow
> count
)
2855 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2856 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2858 wxSize client_size
= GetClientSize();
2859 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2860 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2861 if (intersect_rect
.width
> 0)
2862 Refresh( true, &intersect_rect
);
2865 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2867 wxCHECK_RET( newCurrent
< GetRowCount(),
2868 _T("invalid item index in OnArrowChar()") );
2870 // if there is no selection, we cannot move it anywhere
2871 if (!HasCurrentRow())
2874 unsigned int oldCurrent
= m_currentRow
;
2876 // in single selection we just ignore Shift as we can't select several
2878 if ( event
.ShiftDown() && !IsSingleSel() )
2880 RefreshRow( oldCurrent
);
2882 ChangeCurrentRow( newCurrent
);
2884 // select all the items between the old and the new one
2885 if ( oldCurrent
> newCurrent
)
2887 newCurrent
= oldCurrent
;
2888 oldCurrent
= m_currentRow
;
2891 SelectRows( oldCurrent
, newCurrent
, true );
2892 if (oldCurrent
!=newCurrent
)
2893 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2897 RefreshRow( oldCurrent
);
2899 // all previously selected items are unselected unless ctrl is held
2900 if ( !event
.ControlDown() )
2901 SelectAllRows(false);
2903 ChangeCurrentRow( newCurrent
);
2905 if ( !event
.ControlDown() )
2907 SelectRow( m_currentRow
, true );
2908 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2911 RefreshRow( m_currentRow
);
2914 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2917 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2921 rect
.y
= m_lineHeight
* row
;
2922 rect
.width
= GetEndOfLastCol();
2923 rect
.height
= m_lineHeight
;
2928 class RowToItemJob
: public DoJob
2931 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2932 virtual ~RowToItemJob(){};
2934 virtual int operator() ( wxDataViewTreeNode
* node
)
2937 if( current
== static_cast<int>(row
))
2939 ret
= node
->GetItem() ;
2943 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2945 current
+= node
->GetSubTreeCount();
2950 //If the current has no child node, we can find the desired item of the row number directly.
2951 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2952 if( node
->GetNodes().GetCount() == 0)
2954 int index
= static_cast<int>(row
) - current
- 1;
2955 ret
= node
->GetChildren().Item( index
);
2962 virtual int operator() ( void * n
)
2965 if( current
== static_cast<int>(row
))
2967 ret
= wxDataViewItem( n
) ;
2972 wxDataViewItem
GetResult(){ return ret
; }
2979 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2983 return wxDataViewItem( (void*) row
);
2987 RowToItemJob
job( row
, -2 );
2988 Walker( m_root
, job
);
2989 return job
.GetResult();
2993 class RowToTreeNodeJob
: public DoJob
2996 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2999 this->current
= current
;
3003 virtual ~RowToTreeNodeJob(){};
3005 virtual int operator() ( wxDataViewTreeNode
* node
)
3008 if( current
== static_cast<int>(row
))
3014 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
3016 current
+= node
->GetSubTreeCount();
3022 //If the current has no child node, we can find the desired item of the row number directly.
3023 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
3024 if( node
->GetNodes().GetCount() == 0)
3026 int index
= static_cast<int>(row
) - current
- 1;
3027 void * n
= node
->GetChildren().Item( index
);
3028 ret
= new wxDataViewTreeNode( parent
) ;
3029 ret
->SetItem( wxDataViewItem( n
));
3030 ret
->SetHasChildren(false);
3039 virtual int operator() ( void * n
)
3042 if( current
== static_cast<int>(row
))
3044 ret
= new wxDataViewTreeNode( parent
) ;
3045 ret
->SetItem( wxDataViewItem( n
));
3046 ret
->SetHasChildren(false);
3052 wxDataViewTreeNode
* GetResult(){ return ret
; }
3056 wxDataViewTreeNode
* ret
;
3057 wxDataViewTreeNode
* parent
;
3061 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
3069 RowToTreeNodeJob
job( row
, -2, m_root
);
3070 Walker( m_root
, job
);
3071 return job
.GetResult();
3075 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3077 wxWindow
*parent
= GetParent();
3078 wxDataViewEvent
le(type
, parent
->GetId());
3080 le
.SetEventObject(parent
);
3081 le
.SetModel(GetOwner()->GetModel());
3084 parent
->GetEventHandler()->ProcessEvent(le
);
3088 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3090 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3093 if( node
->HasChildren())
3095 if( !node
->IsOpen())
3097 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3098 //Check if the user prevent expanding
3099 if( e
.GetSkipped() )
3103 //Here I build the children of current node
3104 if( node
->GetChildrenNumber() == 0 )
3107 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3111 //Send the expanded event
3112 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3116 SelectRow( row
, false );
3117 SelectRow( row
+ 1, true );
3118 ChangeCurrentRow( row
+ 1 );
3119 SendSelectionChangedEvent( GetItemByRow(row
+1));
3127 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3129 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3132 wxDataViewTreeNode
* nd
= node
;
3134 if( node
->HasChildren() && node
->IsOpen() )
3136 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3137 if( e
.GetSkipped() )
3142 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3146 node
= node
->GetParent();
3149 int parent
= GetRowByItem( node
->GetItem() ) ;
3152 SelectRow( row
, false);
3153 SelectRow(parent
, true );
3154 ChangeCurrentRow( parent
);
3155 SendSelectionChangedEvent( node
->GetItem() );
3159 if( !nd
->HasChildren())
3164 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3166 wxDataViewModel
* model
= GetOwner()->GetModel();
3170 //Compose the a parent-chain of the finding item
3172 list
.DeleteContents( true );
3173 wxDataViewItem
it( item
);
3176 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3177 list
.Insert( pItem
);
3178 it
= model
->GetParent( it
);
3181 //Find the item along the parent-chain.
3182 //This algorithm is designed to speed up the node-finding method
3183 wxDataViewTreeNode
* node
= m_root
;
3184 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3186 if( node
->HasChildren() )
3188 if( node
->GetChildrenNumber() == 0 )
3191 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3194 wxDataViewTreeNodes nodes
= node
->GetNodes();
3198 for (i
= 0; i
< nodes
.GetCount(); i
++)
3200 if (nodes
[i
]->GetItem() == (**iter
))
3202 if (nodes
[i
]->GetItem() == item
)
3219 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3221 wxDataViewColumn
*col
= NULL
;
3222 unsigned int cols
= GetOwner()->GetColumnCount();
3223 unsigned int colnum
= 0;
3224 unsigned int x_start
= 0;
3226 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3227 for (x_start
= 0; colnum
< cols
; colnum
++)
3229 col
= GetOwner()->GetColumn(colnum
);
3230 if (col
->IsHidden())
3231 continue; // skip it!
3233 unsigned int w
= col
->GetWidth();
3234 if (x_start
+w
>= (unsigned int)x
)
3241 item
= GetItemByRow( y
/m_lineHeight
);
3244 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3246 int row
= GetRowByItem(item
);
3247 int y
= row
*m_lineHeight
;
3248 int h
= m_lineHeight
;
3250 wxDataViewColumn
*col
= NULL
;
3251 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3253 col
= GetOwner()->GetColumn( i
);
3254 x
+= col
->GetWidth();
3255 if( GetOwner()->GetColumn(i
+1) == column
)
3258 int w
= col
->GetWidth();
3259 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3260 return wxRect(x
, y
, w
, h
);
3263 int wxDataViewMainWindow::RecalculateCount()
3267 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3269 return list_model
->GetLastIndex() + 1;
3271 return list_model
->GetLastIndex() - 1;
3276 return m_root
->GetSubTreeCount();
3280 class ItemToRowJob
: public DoJob
3283 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3284 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3285 virtual ~ItemToRowJob(){};
3287 //Maybe binary search will help to speed up this process
3288 virtual int operator() ( wxDataViewTreeNode
* node
)
3291 if( node
->GetItem() == item
)
3296 if( node
->GetItem() == **m_iter
)
3303 ret
+= node
->GetSubTreeCount();
3309 virtual int operator() ( void * n
)
3312 if( n
== item
.GetID() )
3316 //the row number is begin from zero
3317 int GetResult(){ return ret
-1 ; }
3319 ItemList::const_iterator m_iter
;
3320 wxDataViewItem item
;
3325 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3327 wxDataViewModel
* model
= GetOwner()->GetModel();
3333 return wxPtrToUInt( item
.GetID() );
3340 //Compose the a parent-chain of the finding item
3342 wxDataViewItem
* pItem
= NULL
;
3343 list
.DeleteContents( true );
3344 wxDataViewItem
it( item
);
3347 pItem
= new wxDataViewItem( it
);
3348 list
.Insert( pItem
);
3349 it
= model
->GetParent( it
);
3351 pItem
= new wxDataViewItem( );
3352 list
.Insert( pItem
);
3354 ItemToRowJob
job( item
, list
.begin() );
3355 Walker(m_root
, job
);
3356 return job
.GetResult();
3360 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3362 if( !model
->IsContainer( item
) )
3365 wxDataViewItemArray children
;
3366 unsigned int num
= model
->GetChildren( item
, children
);
3367 unsigned int index
= 0;
3368 while( index
< num
)
3370 if( model
->IsContainer( children
[index
] ) )
3372 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3373 n
->SetItem(children
[index
]);
3374 n
->SetHasChildren( true ) ;
3379 node
->AddLeaf( children
[index
].GetID() );
3383 node
->SetSubTreeCount( num
);
3384 wxDataViewTreeNode
* n
= node
->GetParent();
3386 n
->ChangeSubTreeCount(num
);
3390 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3394 if (GetOwner()->GetModel()->IsVirtualListModel())
3400 m_root
= new wxDataViewTreeNode( NULL
);
3401 m_root
->SetHasChildren(true);
3403 //First we define a invalid item to fetch the top-level elements
3404 wxDataViewItem item
;
3406 BuildTreeHelper( model
, item
, m_root
);
3410 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3412 if( node
->GetNodeNumber() != 0 )
3414 int len
= node
->GetNodeNumber();
3416 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3417 for( ; i
< len
; i
++ )
3419 DestroyTreeHelper(nodes
[i
]);
3425 void wxDataViewMainWindow::DestroyTree()
3429 ::DestroyTreeHelper(m_root
);
3435 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3437 if ( HandleAsNavigationKey(event
) )
3440 // no item -> nothing to do
3441 if (!HasCurrentRow())
3447 // don't use m_linesPerPage directly as it might not be computed yet
3448 const int pageSize
= GetCountPerPage();
3449 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3451 switch ( event
.GetKeyCode() )
3455 if (m_currentRow
> 0)
3457 wxWindow
*parent
= GetParent();
3458 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3459 le
.SetItem( GetItemByRow(m_currentRow
) );
3460 le
.SetEventObject(parent
);
3461 le
.SetModel(GetOwner()->GetModel());
3463 parent
->GetEventHandler()->ProcessEvent(le
);
3468 if ( m_currentRow
> 0 )
3469 OnArrowChar( m_currentRow
- 1, event
);
3473 if ( m_currentRow
< GetRowCount() - 1 )
3474 OnArrowChar( m_currentRow
+ 1, event
);
3476 //Add the process for tree expanding/collapsing
3478 OnCollapsing(m_currentRow
);
3481 OnExpanding( m_currentRow
);
3485 OnArrowChar( GetRowCount() - 1, event
);
3490 OnArrowChar( 0, event
);
3495 int steps
= pageSize
- 1;
3496 int index
= m_currentRow
- steps
;
3500 OnArrowChar( index
, event
);
3506 int steps
= pageSize
- 1;
3507 unsigned int index
= m_currentRow
+ steps
;
3508 unsigned int count
= GetRowCount();
3509 if ( index
>= count
)
3512 OnArrowChar( index
, event
);
3521 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3523 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3525 // let the base handle mouse wheel events.
3530 int x
= event
.GetX();
3531 int y
= event
.GetY();
3532 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3533 wxDataViewColumn
*col
= NULL
;
3536 unsigned int cols
= GetOwner()->GetColumnCount();
3538 for (i
= 0; i
< cols
; i
++)
3540 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3542 continue; // skip it!
3544 if (x
< xpos
+ c
->GetWidth())
3549 xpos
+= c
->GetWidth();
3554 wxDataViewRenderer
*cell
= col
->GetRenderer();
3555 unsigned int current
= y
/ m_lineHeight
;
3556 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3558 // Unselect all if below the last row ?
3562 //Test whether the mouse is hovered on the tree item button
3564 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3566 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3567 if( node
!=NULL
&& node
->HasChildren() )
3569 int indent
= node
->GetIndentLevel();
3570 indent
= GetOwner()->GetIndent()*indent
;
3571 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3572 if( rect
.Contains( x
, y
) )
3574 //So the mouse is over the expander
3576 if (m_underMouse
&& m_underMouse
!= node
)
3578 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3579 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3581 if (m_underMouse
!= node
)
3583 //wxLogMessage("Do the row: %d", current);
3584 RefreshRow(current
);
3586 m_underMouse
= node
;
3589 if (node
!=NULL
&& !node
->HasChildren())
3594 if (m_underMouse
!= NULL
)
3596 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3597 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3598 m_underMouse
= NULL
;
3602 wxDataViewModel
*model
= GetOwner()->GetModel();
3604 if (event
.Dragging())
3606 if (m_dragCount
== 0)
3608 // we have to report the raw, physical coords as we want to be
3609 // able to call HitTest(event.m_pointDrag) from the user code to
3610 // get the item being dragged
3611 m_dragStart
= event
.GetPosition();
3616 if (m_dragCount
!= 3)
3619 if (event
.LeftIsDown())
3621 // Notify cell about drag
3630 bool forceClick
= false;
3632 if (event
.ButtonDClick())
3634 m_renameTimer
->Stop();
3635 m_lastOnSame
= false;
3638 wxDataViewItem item
= GetItemByRow(current
);
3639 bool ignore_other_columns
=
3640 ((GetOwner()->GetExpanderColumn() != col
) &&
3641 (model
->IsContainer(item
)) &&
3642 (!model
->HasContainerColumns(item
)));
3644 if (event
.LeftDClick())
3646 if ( current
== m_lineLastClicked
)
3648 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3651 model
->GetValue( value
, item
, col
->GetModelColumn() );
3652 cell
->SetValue( value
);
3653 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3654 col
->GetWidth(), m_lineHeight
);
3655 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3660 wxWindow
*parent
= GetParent();
3661 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3663 le
.SetEventObject(parent
);
3664 le
.SetModel(GetOwner()->GetModel());
3666 parent
->GetEventHandler()->ProcessEvent(le
);
3672 // The first click was on another item, so don't interpret this as
3673 // a double click, but as a simple click instead
3680 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3682 // select single line
3683 SelectAllRows( false );
3684 SelectRow( m_lineSelectSingleOnUp
, true );
3687 //Process the event of user clicking the expander
3688 bool expander
= false;
3689 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3691 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3692 if( node
!=NULL
&& node
->HasChildren() )
3694 int indent
= node
->GetIndentLevel();
3695 indent
= GetOwner()->GetIndent()*indent
;
3696 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3697 if( rect
.Contains( x
, y
) )
3700 if( node
->IsOpen() )
3701 OnCollapsing(current
);
3703 OnExpanding( current
);
3706 if (node
&& !node
->HasChildren())
3709 //If the user click the expander, we do not do editing even if the column with expander are editable
3710 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3712 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3713 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3715 m_renameTimer
->Start( 100, true );
3719 m_lastOnSame
= false;
3720 m_lineSelectSingleOnUp
= (unsigned int)-1;
3724 // This is necessary, because after a DnD operation in
3725 // from and to ourself, the up event is swallowed by the
3726 // DnD code. So on next non-up event (which means here and
3727 // now) m_lineSelectSingleOnUp should be reset.
3728 m_lineSelectSingleOnUp
= (unsigned int)-1;
3731 if (event
.RightDown())
3733 m_lineBeforeLastClicked
= m_lineLastClicked
;
3734 m_lineLastClicked
= current
;
3736 // If the item is already selected, do not update the selection.
3737 // Multi-selections should not be cleared if a selected item is clicked.
3738 if (!IsRowSelected(current
))
3740 SelectAllRows(false);
3741 ChangeCurrentRow(current
);
3742 SelectRow(m_currentRow
,true);
3743 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3747 model
->GetValue( value
, item
, col
->GetModelColumn() );
3748 wxWindow
*parent
= GetParent();
3749 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3751 le
.SetEventObject(parent
);
3752 le
.SetModel(GetOwner()->GetModel());
3754 parent
->GetEventHandler()->ProcessEvent(le
);
3756 else if (event
.MiddleDown())
3759 if (event
.LeftDown() || forceClick
)
3763 m_lineBeforeLastClicked
= m_lineLastClicked
;
3764 m_lineLastClicked
= current
;
3766 unsigned int oldCurrentRow
= m_currentRow
;
3767 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3769 bool cmdModifierDown
= event
.CmdDown();
3770 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3772 if ( IsSingleSel() || !IsRowSelected(current
) )
3774 SelectAllRows( false );
3775 ChangeCurrentRow(current
);
3776 SelectRow(m_currentRow
,true);
3777 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3779 else // multi sel & current is highlighted & no mod keys
3781 m_lineSelectSingleOnUp
= current
;
3782 ChangeCurrentRow(current
); // change focus
3785 else // multi sel & either ctrl or shift is down
3787 if (cmdModifierDown
)
3789 ChangeCurrentRow(current
);
3790 ReverseRowSelection(m_currentRow
);
3791 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3793 else if (event
.ShiftDown())
3795 ChangeCurrentRow(current
);
3797 unsigned int lineFrom
= oldCurrentRow
,
3800 if ( lineTo
< lineFrom
)
3803 lineFrom
= m_currentRow
;
3806 SelectRows(lineFrom
, lineTo
, true);
3807 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3809 else // !ctrl, !shift
3811 // test in the enclosing if should make it impossible
3812 wxFAIL_MSG( _T("how did we get here?") );
3816 if (m_currentRow
!= oldCurrentRow
)
3817 RefreshRow( oldCurrentRow
);
3819 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3821 // Update selection here...
3824 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3825 (current
== oldCurrentRow
)) && oldWasSelected
;
3827 // Call LeftClick after everything else as under GTK+
3828 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3830 // notify cell about right click
3832 model
->GetValue( value
, item
, col
->GetModelColumn() );
3833 cell
->SetValue( value
);
3834 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3835 col
->GetWidth(), m_lineHeight
);
3836 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3841 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3845 if (HasCurrentRow())
3851 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3855 if (HasCurrentRow())
3861 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3863 if( m_selection
.GetCount() != 1 )
3864 return wxDataViewItem();
3866 return GetItemByRow( m_selection
.Item(0));
3869 //-----------------------------------------------------------------------------
3871 //-----------------------------------------------------------------------------
3872 WX_DEFINE_LIST(wxDataViewColumnList
)
3874 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3876 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3877 EVT_SIZE(wxDataViewCtrl::OnSize
)
3880 wxDataViewCtrl::~wxDataViewCtrl()
3883 GetModel()->RemoveNotifier( m_notifier
);
3885 wxDataViewColumnList::const_iterator iter
;
3886 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3892 void wxDataViewCtrl::Init()
3897 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3898 const wxPoint
& pos
, const wxSize
& size
,
3899 long style
, const wxValidator
& validator
)
3901 if ( (style
& wxBORDER_MASK
) == 0)
3902 style
|= wxBORDER_SUNKEN
;
3904 if (!wxControl::Create( parent
, id
, pos
, size
,
3905 style
| wxScrolledWindowStyle
, validator
))
3908 SetInitialSize(size
);
3913 MacSetClipChildren( true ) ;
3916 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3918 if (HasFlag(wxDV_NO_HEADER
))
3919 m_headerArea
= NULL
;
3921 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3923 SetTargetWindow( m_clientArea
);
3925 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3927 sizer
->Add( m_headerArea
, 0, wxGROW
);
3928 sizer
->Add( m_clientArea
, 1, wxGROW
);
3935 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3939 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3942 // we need to process arrows ourselves for scrolling
3943 if ( nMsg
== WM_GETDLGCODE
)
3945 rc
|= DLGC_WANTARROWS
;
3953 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3955 // We need to override OnSize so that our scrolled
3956 // window a) does call Layout() to use sizers for
3957 // positioning the controls but b) does not query
3958 // the sizer for their size and use that for setting
3959 // the scrollable area as set that ourselves by
3960 // calling SetScrollbar() further down.
3967 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3969 if (!wxDataViewCtrlBase::AssociateModel( model
))
3972 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3974 model
->AddNotifier( m_notifier
);
3976 m_clientArea
->DestroyTree();
3978 m_clientArea
->BuildTree(model
);
3980 m_clientArea
->UpdateDisplay();
3985 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3987 if (!wxDataViewCtrlBase::AppendColumn(col
))
3990 m_cols
.Append( col
);
3995 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3997 if (!wxDataViewCtrlBase::PrependColumn(col
))
4000 m_cols
.Insert( col
);
4005 void wxDataViewCtrl::OnColumnChange()
4008 m_headerArea
->UpdateDisplay();
4010 m_clientArea
->UpdateDisplay();
4013 void wxDataViewCtrl::DoSetExpanderColumn()
4015 m_clientArea
->UpdateDisplay();
4018 void wxDataViewCtrl::DoSetIndent()
4020 m_clientArea
->UpdateDisplay();
4023 unsigned int wxDataViewCtrl::GetColumnCount() const
4025 return m_cols
.GetCount();
4028 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
4030 wxDataViewColumnList::const_iterator iter
;
4032 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
4037 if ((*iter
)->IsHidden())
4044 void wxDataViewCtrl::ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
)
4046 if (new_pos
> m_cols
.GetCount()) return;
4047 m_cols
.DeleteObject( col
);
4048 m_cols
.Insert( new_pos
, col
);
4050 m_clientArea
->UpdateDisplay();
4053 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4055 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4066 bool wxDataViewCtrl::ClearColumns()
4073 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4075 int ret
= 0, dead
= 0;
4076 int len
= GetColumnCount();
4077 for (int i
=0; i
<len
; i
++)
4079 wxDataViewColumn
* col
= GetColumn(i
);
4080 if (col
->IsHidden())
4082 ret
+= col
->GetWidth();
4085 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
4092 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4097 //Selection code with wxDataViewItem as parameters
4098 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4100 return m_clientArea
->GetSelection();
4103 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4106 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4107 int len
= selection
.GetCount();
4108 for( int i
= 0; i
< len
; i
++)
4110 unsigned int row
= selection
[i
];
4111 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4116 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4118 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4119 int len
= sel
.GetCount();
4120 for( int i
= 0; i
< len
; i
++ )
4122 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4124 selection
.Add( static_cast<unsigned int>(row
) );
4126 m_clientArea
->SetSelections( selection
);
4129 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4131 int row
= m_clientArea
->GetRowByItem( item
);
4134 //Unselect all rows before select another in the single select mode
4135 if (m_clientArea
->IsSingleSel())
4136 m_clientArea
->SelectAllRows(false);
4137 m_clientArea
->SelectRow(row
, true);
4141 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4143 int row
= m_clientArea
->GetRowByItem( item
);
4145 m_clientArea
->SelectRow(row
, false);
4148 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4150 int row
= m_clientArea
->GetRowByItem( item
);
4153 return m_clientArea
->IsRowSelected(row
);
4158 //Selection code with row number as parameter
4159 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4162 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4163 int len
= selection
.GetCount();
4164 for( int i
= 0; i
< len
; i
++)
4166 unsigned int row
= selection
[i
];
4172 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4174 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4175 int len
= sel
.GetCount();
4176 for( int i
= 0; i
< len
; i
++ )
4180 selection
.Add( static_cast<unsigned int>(row
) );
4182 m_clientArea
->SetSelections( selection
);
4185 void wxDataViewCtrl::Select( int row
)
4189 if (m_clientArea
->IsSingleSel())
4190 m_clientArea
->SelectAllRows(false);
4191 m_clientArea
->SelectRow( row
, true );
4195 void wxDataViewCtrl::Unselect( int row
)
4198 m_clientArea
->SelectRow(row
, false);
4201 bool wxDataViewCtrl::IsSelected( int row
) const
4204 return m_clientArea
->IsRowSelected(row
);
4208 void wxDataViewCtrl::SelectRange( int from
, int to
)
4211 for( int i
= from
; i
< to
; i
++ )
4213 m_clientArea
->Select(sel
);
4216 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4218 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4219 for( int i
= from
; i
< to
; i
++ )
4220 if( sel
.Index( i
) != wxNOT_FOUND
)
4222 m_clientArea
->SetSelections(sel
);
4225 void wxDataViewCtrl::SelectAll()
4227 m_clientArea
->SelectAllRows(true);
4230 void wxDataViewCtrl::UnselectAll()
4232 m_clientArea
->SelectAllRows(false);
4235 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4239 if( row
> (int) m_clientArea
->GetRowCount() )
4240 row
= m_clientArea
->GetRowCount();
4242 int first
= m_clientArea
->GetFirstVisibleRow();
4243 int last
= m_clientArea
->GetLastVisibleRow();
4245 m_clientArea
->ScrollTo( row
, column
);
4246 else if( row
> last
)
4247 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4249 m_clientArea
->ScrollTo( first
, column
);
4252 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4254 int row
= m_clientArea
->GetRowByItem(item
);
4257 if( column
== NULL
)
4258 EnsureVisible(row
, -1);
4262 int len
= GetColumnCount();
4263 for( int i
= 0; i
< len
; i
++ )
4264 if( GetColumn(i
) == column
)
4269 EnsureVisible( row
, col
);
4275 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4277 m_clientArea
->HitTest(point
, item
, column
);
4280 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4282 return m_clientArea
->GetItemRect(item
, column
);
4285 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4287 return m_clientArea
->GetItemByRow( row
);
4290 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4292 return m_clientArea
->GetRowByItem( item
);
4295 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4297 int row
= m_clientArea
->GetRowByItem( item
);
4299 m_clientArea
->Expand(row
);
4302 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4304 int row
= m_clientArea
->GetRowByItem( item
);
4306 m_clientArea
->Collapse(row
);
4310 // !wxUSE_GENERICDATAVIEWCTRL
4313 // wxUSE_DATAVIEWCTRL