1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 #define USE_NATIVE_HEADER_WINDOW 1
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called Refresh afterwards
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
147 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
149 wxSize
DoGetBestSize() const;
151 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
153 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
154 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
160 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
163 #else // !defined(__WXMSW__)
165 #define HEADER_WINDOW_HEIGHT 25
166 #define HEADER_HORIZ_BORDER 5
167 #define HEADER_VERT_BORDER 3
168 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
170 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
173 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
175 const wxPoint
&pos
= wxDefaultPosition
,
176 const wxSize
&size
= wxDefaultSize
,
177 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
180 Create(parent
, id
, pos
, size
, name
);
183 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
184 const wxPoint
&pos
, const wxSize
&size
,
185 const wxString
&name
);
187 ~wxGenericDataViewHeaderWindow()
189 delete m_resizeCursor
;
192 virtual void UpdateDisplay() { Refresh(); }
196 void OnPaint( wxPaintEvent
&event
);
197 void OnMouse( wxMouseEvent
&event
);
198 void OnSetFocus( wxFocusEvent
&event
);
203 // vars used for column resizing:
205 wxCursor
*m_resizeCursor
;
206 const wxCursor
*m_currentCursor
;
209 bool m_dirty
; // needs refresh?
210 int m_hover
; // index of the column under the mouse
211 int m_column
; // index of the column being resized
212 int m_currentX
; // divider line position in logical (unscrolled) coords
213 int m_minX
; // minimal position beyond which the divider line
214 // can't be dragged in logical coords
216 // the pen used to draw the current column width drag line
217 // when resizing the columsn
221 // internal utilities:
225 m_currentCursor
= (wxCursor
*) NULL
;
226 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
228 m_isDragging
= false;
231 m_hover
= wxNOT_FOUND
;
232 m_column
= wxNOT_FOUND
;
236 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
237 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
240 void AdjustDC(wxDC
& dc
);
243 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
244 DECLARE_EVENT_TABLE()
247 #endif // defined(__WXMSW__)
249 //-----------------------------------------------------------------------------
250 // wxDataViewRenameTimer
251 //-----------------------------------------------------------------------------
253 class wxDataViewRenameTimer
: public wxTimer
256 wxDataViewMainWindow
*m_owner
;
259 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
263 //-----------------------------------------------------------------------------
264 // wxDataViewTreeNode
265 //-----------------------------------------------------------------------------
266 class wxDataViewTreeNode
;
267 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
268 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
270 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
271 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
273 class wxDataViewTreeNode
276 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
283 m_hasChildren
= false;
287 ~wxDataViewTreeNode()
291 wxDataViewTreeNode
* GetParent() { return m_parent
; }
292 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
293 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
294 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
296 void AddNode( wxDataViewTreeNode
* node
)
298 m_leaves
.Add( node
->GetItem().GetID() );
300 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
303 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
305 void AddLeaf( void * leaf
)
307 m_leaves
.Add( leaf
);
309 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
312 wxDataViewItem
& GetItem() { return m_item
; }
313 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
315 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
316 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
320 wxDataViewTreeNode
* node
= this;
321 while( node
->GetParent()->GetParent() != NULL
)
323 node
= node
->GetParent();
336 int len
= m_nodes
.GetCount();
338 for ( int i
= 0 ;i
< len
; i
++)
339 sum
+= m_nodes
[i
]->GetSubTreeCount();
341 sum
+= m_leaves
.GetCount();
344 ChangeSubTreeCount(-sum
);
350 ChangeSubTreeCount(sum
);
353 bool HasChildren() { return m_hasChildren
; }
354 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
356 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
357 int GetSubTreeCount() { return m_subTreeCount
; }
358 void ChangeSubTreeCount( int num
)
362 m_subTreeCount
+= num
;
364 m_parent
->ChangeSubTreeCount(num
);
371 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
372 int len
= m_nodes
.GetCount();
373 for (int i
= 0; i
< len
; i
++)
374 m_nodes
[i
]->Resort();
375 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
380 wxDataViewTreeNode
*m_parent
;
381 wxDataViewTreeNodes m_nodes
;
382 wxDataViewTreeLeaves m_leaves
;
383 wxDataViewItem m_item
;
389 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
391 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
394 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
396 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
401 //-----------------------------------------------------------------------------
402 // wxDataViewMainWindow
403 //-----------------------------------------------------------------------------
405 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
407 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
408 WX_DEFINE_LIST(ItemList
)
410 class wxDataViewMainWindow
: public wxWindow
413 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
415 const wxPoint
&pos
= wxDefaultPosition
,
416 const wxSize
&size
= wxDefaultSize
,
417 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
418 virtual ~wxDataViewMainWindow();
420 // notifications from wxDataViewModel
421 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
422 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
423 bool ItemChanged( const wxDataViewItem
&item
);
424 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
435 g_model
= GetOwner()->GetModel();
436 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
439 if (g_model
->HasDefaultCompare())
447 g_column
= col
->GetModelColumn();
448 g_asending
= col
->IsSortOrderAscending();
451 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
452 wxDataViewCtrl
*GetOwner() { return m_owner
; }
453 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
455 void OnPaint( wxPaintEvent
&event
);
456 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
457 void OnChar( wxKeyEvent
&event
);
458 void OnMouse( wxMouseEvent
&event
);
459 void OnSetFocus( wxFocusEvent
&event
);
460 void OnKillFocus( wxFocusEvent
&event
);
462 void UpdateDisplay();
463 void RecalculateDisplay();
464 void OnInternalIdle();
466 void OnRenameTimer();
468 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
469 void ScrollTo( int rows
, int column
);
471 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
472 void ChangeCurrentRow( unsigned int row
);
474 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
475 bool IsEmpty() { return GetRowCount() == 0; }
477 int GetCountPerPage() const;
478 int GetEndOfLastCol() const;
479 unsigned int GetFirstVisibleRow() const;
480 //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
481 unsigned int GetLastVisibleRow();
482 unsigned int GetRowCount() ;
484 wxDataViewItem
GetSelection() const;
485 wxDataViewSelection
GetSelections(){ return m_selection
; }
486 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
487 void Select( const wxArrayInt
& aSelections
);
488 void SelectAllRows( bool on
);
489 void SelectRow( unsigned int row
, bool on
);
490 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
491 void ReverseRowSelection( unsigned int row
);
492 bool IsRowSelected( unsigned int row
);
493 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
495 void RefreshRow( unsigned int row
);
496 void RefreshRows( unsigned int from
, unsigned int to
);
497 void RefreshRowsAfter( unsigned int firstRow
);
499 // returns the colour to be used for drawing the rules
500 wxColour
GetRuleColour() const
502 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
505 wxRect
GetLineRect( unsigned int row
) const;
507 //Some useful functions for row and item mapping
508 wxDataViewItem
GetItemByRow( unsigned int row
) const;
509 int GetRowByItem( const wxDataViewItem
& item
);
511 //Methods for building the mapping tree
512 void BuildTree( wxDataViewModel
* model
);
514 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
515 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
517 void Expand( unsigned int row
) { OnExpanding( row
); }
518 void Collapse( unsigned int row
) { OnCollapsing( row
); }
520 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
521 //We did not need this temporarily
522 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
524 int RecalculateCount() ;
526 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
527 void OnExpanding( unsigned int row
);
528 void OnCollapsing( unsigned int row
);
530 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
533 wxDataViewCtrl
*m_owner
;
537 wxDataViewColumn
*m_currentCol
;
538 unsigned int m_currentRow
;
539 wxDataViewSelection m_selection
;
541 wxDataViewRenameTimer
*m_renameTimer
;
549 // for double click logic
550 unsigned int m_lineLastClicked
,
551 m_lineBeforeLastClicked
,
552 m_lineSelectSingleOnUp
;
554 // the pen used to draw horiz/vertical rules
557 // the pen used to draw the expander and the lines
560 //This is the tree structure of the model
561 wxDataViewTreeNode
* m_root
;
563 //This is the tree node under the cursor
564 wxDataViewTreeNode
* m_underMouse
;
566 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
567 DECLARE_EVENT_TABLE()
570 // ---------------------------------------------------------
571 // wxGenericDataViewModelNotifier
572 // ---------------------------------------------------------
574 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
577 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
578 { m_mainWindow
= mainWindow
; }
580 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
581 { return m_mainWindow
->ItemAdded( parent
, item
); }
582 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
583 { return m_mainWindow
->ItemDeleted( parent
, item
); }
584 virtual bool ItemChanged( const wxDataViewItem
& item
)
585 { return m_mainWindow
->ItemChanged(item
); }
586 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
587 { return m_mainWindow
->ValueChanged( item
, col
); }
588 virtual bool Cleared()
589 { return m_mainWindow
->Cleared(); }
590 virtual void Resort()
591 { m_mainWindow
->Resort(); }
593 wxDataViewMainWindow
*m_mainWindow
;
596 // ---------------------------------------------------------
597 // wxDataViewRenderer
598 // ---------------------------------------------------------
600 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
602 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
603 wxDataViewCellMode mode
,
605 wxDataViewRendererBase( varianttype
, mode
, align
)
612 wxDataViewRenderer::~wxDataViewRenderer()
618 wxDC
*wxDataViewRenderer::GetDC()
622 if (GetOwner() == NULL
)
624 if (GetOwner()->GetOwner() == NULL
)
626 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
632 // ---------------------------------------------------------
633 // wxDataViewCustomRenderer
634 // ---------------------------------------------------------
636 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
638 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
639 wxDataViewCellMode mode
, int align
) :
640 wxDataViewRenderer( varianttype
, mode
, align
)
644 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
646 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
647 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
648 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
649 view
->GetForegroundColour();
650 dc
->SetTextForeground(col
);
651 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
654 // ---------------------------------------------------------
655 // wxDataViewTextRenderer
656 // ---------------------------------------------------------
658 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
660 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
661 wxDataViewCellMode mode
, int align
) :
662 wxDataViewCustomRenderer( varianttype
, mode
, align
)
666 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
668 m_text
= value
.GetString();
673 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
678 bool wxDataViewTextRenderer::HasEditorCtrl()
683 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
684 wxRect labelRect
, const wxVariant
&value
)
686 return new wxTextCtrl( parent
, wxID_ANY
, value
,
687 wxPoint(labelRect
.x
,labelRect
.y
),
688 wxSize(labelRect
.width
,labelRect
.height
) );
691 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
693 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
694 value
= text
->GetValue();
698 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
700 RenderText( m_text
, 0, cell
, dc
, state
);
704 wxSize
wxDataViewTextRenderer::GetSize() const
706 const wxDataViewCtrl
*view
= GetView();
710 view
->GetTextExtent( m_text
, &x
, &y
);
711 return wxSize( x
, y
);
713 return wxSize(80,20);
716 // ---------------------------------------------------------
717 // wxDataViewBitmapRenderer
718 // ---------------------------------------------------------
720 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
722 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
723 wxDataViewCellMode mode
, int align
) :
724 wxDataViewCustomRenderer( varianttype
, mode
, align
)
728 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
730 if (value
.GetType() == wxT("wxBitmap"))
732 if (value
.GetType() == wxT("wxIcon"))
738 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
743 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
746 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
747 else if (m_icon
.Ok())
748 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
753 wxSize
wxDataViewBitmapRenderer::GetSize() const
756 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
757 else if (m_icon
.Ok())
758 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
760 return wxSize(16,16);
763 // ---------------------------------------------------------
764 // wxDataViewToggleRenderer
765 // ---------------------------------------------------------
767 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
769 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
770 wxDataViewCellMode mode
, int align
) :
771 wxDataViewCustomRenderer( varianttype
, mode
, align
)
776 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
778 m_toggle
= value
.GetBool();
783 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
788 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
790 // User wxRenderer here
793 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
795 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
800 flags
|= wxCONTROL_CHECKED
;
801 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
802 flags
|= wxCONTROL_DISABLED
;
804 wxRendererNative::Get().DrawCheckBox(
805 GetOwner()->GetOwner(),
813 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
814 wxDataViewModel
*model
,
815 const wxDataViewItem
& item
, unsigned int col
)
817 bool value
= !m_toggle
;
818 wxVariant variant
= value
;
819 model
->SetValue( variant
, item
, col
);
820 model
->ValueChanged( item
, col
);
824 wxSize
wxDataViewToggleRenderer::GetSize() const
826 return wxSize(20,20);
829 // ---------------------------------------------------------
830 // wxDataViewProgressRenderer
831 // ---------------------------------------------------------
833 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
835 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
836 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
837 wxDataViewCustomRenderer( varianttype
, mode
, align
)
843 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
847 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
849 m_value
= (long) value
;
851 if (m_value
< 0) m_value
= 0;
852 if (m_value
> 100) m_value
= 100;
857 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
859 value
= (long) m_value
;
863 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
865 double pct
= (double)m_value
/ 100.0;
867 bar
.width
= (int)(cell
.width
* pct
);
868 dc
->SetPen( *wxTRANSPARENT_PEN
);
869 dc
->SetBrush( *wxBLUE_BRUSH
);
870 dc
->DrawRectangle( bar
);
872 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
873 dc
->SetPen( *wxBLACK_PEN
);
874 dc
->DrawRectangle( cell
);
879 wxSize
wxDataViewProgressRenderer::GetSize() const
881 return wxSize(40,12);
884 // ---------------------------------------------------------
885 // wxDataViewDateRenderer
886 // ---------------------------------------------------------
888 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
890 #if wxUSE_DATE_RENDERER_POPUP
892 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
895 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
896 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
897 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
902 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
903 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
904 sizer
->Add( m_cal
, 1, wxGROW
);
909 void OnCalendar( wxCalendarEvent
&event
);
911 wxCalendarCtrl
*m_cal
;
912 wxDataViewModel
*m_model
;
914 const wxDataViewItem
& m_item
;
917 virtual void OnDismiss()
922 DECLARE_EVENT_TABLE()
925 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
926 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
929 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
931 wxDateTime date
= event
.GetDate();
932 wxVariant value
= date
;
933 m_model
->SetValue( value
, m_item
, m_col
);
934 m_model
->ValueChanged( m_item
, m_col
);
938 #endif // wxUSE_DATE_RENDERER_POPUP
940 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
942 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
943 wxDataViewCellMode mode
, int align
) :
944 wxDataViewCustomRenderer( varianttype
, mode
, align
)
948 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
950 m_date
= value
.GetDateTime();
955 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
961 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
963 wxString tmp
= m_date
.FormatDate();
964 RenderText( tmp
, 0, cell
, dc
, state
);
968 wxSize
wxDataViewDateRenderer::GetSize() const
970 const wxDataViewCtrl
* view
= GetView();
971 wxString tmp
= m_date
.FormatDate();
973 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
974 return wxSize(x
,y
+d
);
977 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
978 const wxDataViewItem
& item
, unsigned int col
)
981 model
->GetValue( variant
, item
, col
);
982 wxDateTime value
= variant
.GetDateTime();
984 #if wxUSE_DATE_RENDERER_POPUP
985 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
986 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
987 wxPoint pos
= wxGetMousePosition();
990 popup
->Popup( popup
->m_cal
);
991 #else // !wxUSE_DATE_RENDERER_POPUP
992 wxMessageBox(value
.Format());
993 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
997 // ---------------------------------------------------------
998 // wxDataViewIconTextRenderer
999 // ---------------------------------------------------------
1001 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1003 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1004 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1005 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1008 SetAlignment(align
);
1011 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1015 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1021 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1026 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1029 const wxIcon
&icon
= m_value
.GetIcon();
1032 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1033 xoffset
= icon
.GetWidth()+4;
1036 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1041 wxSize
wxDataViewIconTextRenderer::GetSize() const
1043 const wxDataViewCtrl
*view
= GetView();
1044 if (!m_value
.GetText().empty())
1047 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1049 if (m_value
.GetIcon().IsOk())
1050 x
+= m_value
.GetIcon().GetWidth() + 4;
1051 return wxSize( x
, y
);
1053 return wxSize(80,20);
1057 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1058 wxRect
WXUNUSED(labelRect
),
1059 const wxVariant
& WXUNUSED(value
))
1065 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1066 wxVariant
& WXUNUSED(value
))
1071 // ---------------------------------------------------------
1073 // ---------------------------------------------------------
1075 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1077 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1078 unsigned int model_column
,
1079 int width
, wxAlignment align
, int flags
) :
1080 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1082 SetAlignment(align
);
1086 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1089 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1090 unsigned int model_column
,
1091 int width
, wxAlignment align
, int flags
) :
1092 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1094 SetAlignment(align
);
1097 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1100 wxDataViewColumn::~wxDataViewColumn()
1104 void wxDataViewColumn::Init( int width
)
1107 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1111 void wxDataViewColumn::SetResizeable( bool resizeable
)
1114 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1116 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1119 void wxDataViewColumn::SetHidden( bool hidden
)
1122 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1124 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1126 // tell our owner to e.g. update its scrollbars:
1128 GetOwner()->OnColumnChange();
1131 void wxDataViewColumn::SetSortable( bool sortable
)
1134 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1136 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1138 // Update header button
1140 GetOwner()->OnColumnChange();
1143 void wxDataViewColumn::SetSortOrder( bool ascending
)
1145 m_ascending
= ascending
;
1147 // Update header button
1149 GetOwner()->OnColumnChange();
1152 bool wxDataViewColumn::IsSortOrderAscending() const
1157 void wxDataViewColumn::SetInternalWidth( int width
)
1161 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1162 if (m_owner
&& m_owner
->m_clientArea
)
1163 m_owner
->m_clientArea
->RecalculateDisplay();
1166 void wxDataViewColumn::SetWidth( int width
)
1168 m_owner
->m_headerArea
->UpdateDisplay();
1170 SetInternalWidth(width
);
1174 //-----------------------------------------------------------------------------
1175 // wxDataViewHeaderWindowBase
1176 //-----------------------------------------------------------------------------
1178 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1180 wxWindow
*parent
= GetParent();
1181 wxDataViewEvent
le(type
, parent
->GetId());
1183 le
.SetEventObject(parent
);
1185 le
.SetDataViewColumn(GetColumn(n
));
1186 le
.SetModel(GetOwner()->GetModel());
1188 // for events created by wxDataViewHeaderWindow the
1189 // row / value fields are not valid
1191 parent
->GetEventHandler()->ProcessEvent(le
);
1194 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1196 // implemented in msw/listctrl.cpp:
1197 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1199 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1201 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1202 const wxPoint
&pos
, const wxSize
&size
,
1203 const wxString
&name
)
1207 m_scrollOffsetX
= 0;
1208 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this );
1210 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1211 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1212 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1213 h
= size
.y
== wxDefaultCoord
? m_buttonHeight
: size
.y
;
1215 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1218 // create the native WC_HEADER window:
1219 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1220 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1223 msStyle
|= WS_VISIBLE
;
1225 m_hWnd
= CreateWindowEx(0,
1236 wxLogLastError(_T("CreateWindowEx"));
1240 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1241 // to call wxDataViewHeaderWindow::MSWOnNotify
1242 SubclassWin(m_hWnd
);
1244 // the following is required to get the default win's font for
1245 // header windows and must be done befor sending the HDM_LAYOUT msg
1251 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1256 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1258 return wxSize(80, m_buttonHeight
);
1262 #define HDF_SORTUP 0x0400
1264 #ifndef HDF_SORTDOWN
1265 #define HDF_SORTDOWN 0x0200
1268 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1270 // remove old columns
1271 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1272 Header_DeleteItem((HWND
)m_hWnd
, 0);
1274 // add the updated array of columns to the header control
1275 unsigned int cols
= GetOwner()->GetColumnCount();
1276 unsigned int added
= 0;
1277 for (unsigned int i
= 0; i
< cols
; i
++)
1279 wxDataViewColumn
*col
= GetColumn( i
);
1280 if (col
->IsHidden())
1281 continue; // don't add it!
1284 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1285 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1286 hdi
.cxy
= col
->GetWidth();
1287 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1288 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1289 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1291 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1293 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1294 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1295 // VZ: works with 5.81
1296 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1299 // lParam is reserved for application's use:
1300 // we store there the column index to use it later in MSWOnNotify
1301 // (since columns may have been hidden)
1302 hdi
.lParam
= (LPARAM
)i
;
1304 // the native wxMSW implementation of the header window
1305 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1306 // on the right: to correct this effect we make the column
1307 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1309 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1311 switch (col
->GetAlignment())
1314 hdi
.fmt
|= HDF_LEFT
;
1316 case wxALIGN_CENTER
:
1317 case wxALIGN_CENTER_HORIZONTAL
:
1318 hdi
.fmt
|= HDF_CENTER
;
1321 hdi
.fmt
|= HDF_RIGHT
;
1325 // such alignment is not allowed for the column header!
1329 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1330 (WPARAM
)added
, (LPARAM
)&hdi
);
1335 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1339 // NOTE: we don't just return nmHDR->iItem because when there are
1340 // hidden columns, nmHDR->iItem may be different from
1341 // nmHDR->pitem->lParam
1343 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1345 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1350 item
.mask
= HDI_LPARAM
;
1351 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1353 return (unsigned int)item
.lParam
;
1356 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1358 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1360 // is it a message from the header?
1361 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1362 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1364 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1365 switch ( nmhdr
->code
)
1367 case HDN_BEGINTRACK
:
1368 // user has started to resize a column:
1369 // do we need to veto it?
1370 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1378 // user has started to reorder a column
1381 case HDN_ITEMCHANGING
:
1382 if (nmHDR
->pitem
!= NULL
&&
1383 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1385 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1386 if (nmHDR
->pitem
->cxy
< minWidth
)
1388 // do not allow the user to resize this column under
1389 // its minimal width:
1395 case HDN_ITEMCHANGED
: // user is resizing a column
1396 case HDN_ENDTRACK
: // user has finished resizing a column
1397 case HDN_ENDDRAG
: // user has finished reordering a column
1399 // update the width of the modified column:
1400 if (nmHDR
->pitem
!= NULL
&&
1401 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1403 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1404 unsigned int w
= nmHDR
->pitem
->cxy
;
1405 wxDataViewColumn
*col
= GetColumn(idx
);
1407 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1408 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1409 w
-= COLUMN_WIDTH_OFFSET
;
1411 if (w
>= (unsigned)col
->GetMinWidth())
1412 col
->SetInternalWidth(w
);
1418 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1419 wxDataViewModel
* model
= GetOwner()->GetModel();
1421 if(nmHDR
->iButton
== 0)
1423 wxDataViewColumn
*col
= GetColumn(idx
);
1424 if(col
->IsSortable())
1426 if(model
&& m_owner
->GetSortingColumn() == col
)
1428 bool order
= col
->IsSortOrderAscending();
1429 col
->SetSortOrder(!order
);
1433 m_owner
->SetSortingColumn(col
);
1441 wxEventType evt
= nmHDR
->iButton
== 0 ?
1442 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1443 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1444 SendEvent(evt
, idx
);
1450 // NOTE: for some reason (i.e. for a bug in Windows)
1451 // the HDN_ITEMCLICK notification is not sent on
1452 // right clicks, so we need to handle NM_RCLICK
1455 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1456 if (column
!= wxNOT_FOUND
)
1459 item
.mask
= HDI_LPARAM
;
1460 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1462 // 'idx' may be different from 'column' if there are
1463 // hidden columns...
1464 unsigned int idx
= (unsigned int)item
.lParam
;
1465 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1471 case HDN_GETDISPINFOW
:
1472 // see wxListCtrl::MSWOnNotify for more info!
1475 case HDN_ITEMDBLCLICK
:
1477 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1478 int w
= GetOwner()->GetBestColumnWidth(idx
);
1480 // update the native control:
1482 ZeroMemory(&hd
, sizeof(hd
));
1483 hd
.mask
= HDI_WIDTH
;
1485 Header_SetItem(GetHwnd(),
1486 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1489 // update the wxDataViewColumn class:
1490 GetColumn(idx
)->SetInternalWidth(w
);
1495 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1501 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
1503 m_scrollOffsetX
+= dx
;
1505 GetParent()->Layout();
1508 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1512 // TODO: why is there a border + 2px around it?
1513 wxControl::DoSetSize( x
+m_scrollOffsetX
-2, y
-2, w
-m_scrollOffsetX
+4, h
+4, f
);
1516 #else // !defined(__WXMSW__)
1518 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1519 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1520 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1521 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1522 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1525 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1526 const wxPoint
&pos
, const wxSize
&size
,
1527 const wxString
&name
)
1531 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1534 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1535 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1536 SetOwnForegroundColour( attr
.colFg
);
1537 SetOwnBackgroundColour( attr
.colBg
);
1539 SetOwnFont( attr
.font
);
1541 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1542 // a wxBoxSizer and in order to avoid super-big header windows,
1543 // we need to set our height as fixed
1544 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1545 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1550 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1553 GetClientSize( &w
, &h
);
1555 wxAutoBufferedPaintDC
dc( this );
1557 dc
.SetBackground(GetBackgroundColour());
1561 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1564 m_owner
->GetViewStart( &x
, NULL
);
1566 // account for the horz scrollbar offset
1567 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1569 dc
.SetFont( GetFont() );
1571 unsigned int cols
= GetOwner()->GetColumnCount();
1574 for (i
= 0; i
< cols
; i
++)
1576 wxDataViewColumn
*col
= GetColumn( i
);
1577 if (col
->IsHidden())
1578 continue; // skip it!
1580 int cw
= col
->GetWidth();
1583 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1584 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1586 if (col
->IsSortOrderAscending())
1587 sortArrow
= wxHDR_SORT_ICON_UP
;
1589 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1593 if (m_parent
->IsEnabled())
1595 if ((int) i
== m_hover
)
1596 state
= wxCONTROL_CURRENT
;
1600 state
= (int) wxCONTROL_DISABLED
;
1603 wxRendererNative::Get().DrawHeaderButton
1607 wxRect(xpos
, 0, cw
, ch
-1),
1612 // align as required the column title:
1614 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1615 switch (col
->GetAlignment())
1618 x
+= HEADER_HORIZ_BORDER
;
1621 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1624 case wxALIGN_CENTER
:
1625 case wxALIGN_CENTER_HORIZONTAL
:
1626 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1630 // always center the title vertically:
1631 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1633 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1635 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1636 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1637 dc
.DrawText( col
->GetTitle(), x
, y
);
1638 dc
.DestroyClippingRegion();
1644 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1646 GetParent()->SetFocus();
1650 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1652 // we want to work with logical coords
1654 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1655 int y
= event
.GetY();
1659 // we don't draw the line beyond our window,
1660 // but we allow dragging it there
1662 GetClientSize( &w
, NULL
);
1663 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1666 if (event
.ButtonUp())
1668 m_isDragging
= false;
1674 m_currentX
= wxMax(m_minX
+ 7, x
);
1678 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1680 GetOwner()->Refresh();
1684 else // not dragging
1687 m_column
= wxNOT_FOUND
;
1689 bool hit_border
= false;
1691 // end of the current column
1694 // find the column where this event occured
1695 int countCol
= m_owner
->GetColumnCount();
1696 for (int column
= 0; column
< countCol
; column
++)
1698 wxDataViewColumn
*p
= GetColumn(column
);
1701 continue; // skip if not shown
1703 xpos
+= p
->GetWidth();
1705 if ((abs(x
-xpos
) < 3) && (y
< 22))
1713 // inside the column
1720 int old_hover
= m_hover
;
1722 if (event
.Leaving())
1723 m_hover
= wxNOT_FOUND
;
1724 if (old_hover
!= m_hover
)
1727 if (m_column
== wxNOT_FOUND
)
1730 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1731 if (event
.LeftDClick() && resizeable
)
1733 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1736 else if (event
.LeftDown() || event
.RightUp())
1738 if (hit_border
&& event
.LeftDown() && resizeable
)
1740 m_isDragging
= true;
1744 else // click on a column
1746 wxDataViewModel
* model
= GetOwner()->GetModel();
1747 wxEventType evt
= event
.LeftDown() ?
1748 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1749 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1750 SendEvent(evt
, m_column
);
1752 //Left click the header
1753 if(event
.LeftDown())
1755 wxDataViewColumn
*col
= GetColumn(m_column
);
1756 if(col
->IsSortable())
1758 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1759 if(model
&& sortCol
== col
)
1761 bool order
= col
->IsSortOrderAscending();
1762 col
->SetSortOrder(!order
);
1766 m_owner
->SetSortingColumn(col
);
1772 //Send the column sorted event
1773 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1777 else if (event
.Moving())
1779 if (hit_border
&& resizeable
)
1780 m_currentCursor
= m_resizeCursor
;
1782 m_currentCursor
= wxSTANDARD_CURSOR
;
1784 SetCursor(*m_currentCursor
);
1789 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1793 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1794 m_owner
->GetViewStart( &x
, NULL
);
1796 // shift the DC origin to match the position of the main window horizontal
1797 // scrollbar: this allows us to always use logical coords
1798 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1801 #endif // defined(__WXMSW__)
1803 //-----------------------------------------------------------------------------
1804 // wxDataViewRenameTimer
1805 //-----------------------------------------------------------------------------
1807 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1812 void wxDataViewRenameTimer::Notify()
1814 m_owner
->OnRenameTimer();
1817 //-----------------------------------------------------------------------------
1818 // wxDataViewMainWindow
1819 //-----------------------------------------------------------------------------
1821 //The tree building helper, declared firstly
1822 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1824 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1826 if (row1
> row2
) return 1;
1827 if (row1
== row2
) return 0;
1832 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1834 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1835 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1836 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1837 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1838 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1839 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1842 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1843 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1844 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1845 m_selection( wxDataViewSelectionCmp
)
1850 m_lastOnSame
= false;
1851 m_renameTimer
= new wxDataViewRenameTimer( this );
1853 // TODO: user better initial values/nothing selected
1854 m_currentCol
= NULL
;
1857 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1860 m_dragStart
= wxPoint(0,0);
1861 m_lineLastClicked
= (unsigned int) -1;
1862 m_lineBeforeLastClicked
= (unsigned int) -1;
1863 m_lineSelectSingleOnUp
= (unsigned int) -1;
1867 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1868 SetBackgroundColour( *wxWHITE
);
1870 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1872 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1873 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1874 //Some new added code to deal with the tree structure
1875 m_root
= new wxDataViewTreeNode( NULL
);
1876 m_root
->SetHasChildren(true);
1878 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1880 m_underMouse
= NULL
;
1884 wxDataViewMainWindow::~wxDataViewMainWindow()
1887 delete m_renameTimer
;
1890 void wxDataViewMainWindow::OnRenameTimer()
1892 // We have to call this here because changes may just have
1893 // been made and no screen update taken place.
1898 unsigned int cols
= GetOwner()->GetColumnCount();
1900 for (i
= 0; i
< cols
; i
++)
1902 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1904 continue; // skip it!
1906 if (c
== m_currentCol
)
1908 xpos
+= c
->GetWidth();
1910 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1911 m_currentCol
->GetWidth(), m_lineHeight
);
1913 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1914 &labelRect
.x
, &labelRect
.y
);
1916 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1917 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1921 //------------------------------------------------------------------
1922 // Helper class for do operation on the tree node
1923 //------------------------------------------------------------------
1930 //The return value control how the tree-walker tranverse the tree
1931 // 0: Job done, stop tranverse and return
1932 // 1: Ignore the current node's subtree and continue
1933 // 2: Job not done, continue
1934 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1935 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1936 virtual int operator() ( void * n
) = 0;
1939 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1944 switch( func( node
) )
1955 wxDataViewTreeNodes nodes
= node
->GetNodes();
1956 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1958 int len_nodes
= nodes
.GetCount();
1959 int len
= leaves
.GetCount();
1960 int i
= 0, nodes_i
= 0;
1962 for( ; i
< len
; i
++ )
1964 void * n
= leaves
[i
];
1965 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1967 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1970 if( Walker( nd
, func
) )
1989 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1993 wxDataViewTreeNode
* node
;
1994 node
= FindNode(parent
);
1999 node
->SetHasChildren( true );
2001 if( g_model
->IsContainer( item
) )
2003 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2004 newnode
->SetItem(item
);
2005 newnode
->SetHasChildren( true );
2006 node
->AddNode( newnode
);
2009 node
->AddLeaf( item
.GetID() );
2011 node
->ChangeSubTreeCount(1);
2019 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2021 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2022 const wxDataViewItem
& item
)
2024 wxDataViewTreeNode
* node
= FindNode(parent
);
2026 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2027 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2030 node
->GetChildren().Remove( item
.GetID() );
2031 //Manuplate selection
2032 if( m_selection
.GetCount() > 1 )
2034 m_selection
.Empty();
2036 bool isContainer
= false;
2037 wxDataViewTreeNodes nds
= node
->GetNodes();
2038 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2040 if (nds
[i
]->GetItem() == item
)
2048 wxDataViewTreeNode
* n
= NULL
;
2049 wxDataViewTreeNodes nodes
= node
->GetNodes();
2050 int len
= nodes
.GetCount();
2051 for( int i
= 0 ; i
< len
; i
++)
2053 if( nodes
[i
]->GetItem() == item
)
2060 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2062 node
->GetNodes().Remove( n
);
2063 sub
-= n
->GetSubTreeCount();
2064 DestroyTreeHelper(n
);
2066 //Make the row number invalid and get a new valid one when user call GetRowCount
2068 node
->ChangeSubTreeCount(sub
);
2069 if( node
->GetChildrenNumber() == 0)
2071 node
->GetParent()->GetNodes().Remove( node
);
2075 //Change the current row to the last row if the current exceed the max row number
2076 if( m_currentRow
> GetRowCount() )
2077 m_currentRow
= m_count
- 1;
2084 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2090 wxWindow
*parent
= GetParent();
2091 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2092 le
.SetEventObject(parent
);
2093 le
.SetModel(GetOwner()->GetModel());
2095 parent
->GetEventHandler()->ProcessEvent(le
);
2100 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2102 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2103 /*#define MAX_VIRTUAL_WIDTH 100000
2105 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2106 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2107 Refresh( true, &rect );
2115 wxWindow
*parent
= GetParent();
2116 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2117 le
.SetEventObject(parent
);
2118 le
.SetModel(GetOwner()->GetModel());
2121 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2122 parent
->GetEventHandler()->ProcessEvent(le
);
2127 bool wxDataViewMainWindow::Cleared()
2137 void wxDataViewMainWindow::UpdateDisplay()
2142 void wxDataViewMainWindow::OnInternalIdle()
2144 wxWindow::OnInternalIdle();
2148 RecalculateDisplay();
2153 void wxDataViewMainWindow::RecalculateDisplay()
2155 wxDataViewModel
*model
= GetOwner()->GetModel();
2162 int width
= GetEndOfLastCol();
2163 int height
= GetRowCount() * m_lineHeight
;
2165 SetVirtualSize( width
, height
);
2166 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2171 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2173 wxWindow::ScrollWindow( dx
, dy
, rect
);
2175 if (GetOwner()->m_headerArea
)
2176 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2179 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2182 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2183 int sy
= rows
*m_lineHeight
/y
;
2187 wxRect rect
= GetClientRect();
2189 int x_start
= 0, x_end
= 0, w
= 0;
2191 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2192 for (x_start
= 0; colnum
< column
; colnum
++)
2194 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2195 if (col
->IsHidden())
2196 continue; // skip it!
2198 w
= col
->GetWidth();
2202 x_end
= x_start
+ w
;
2203 xe
= xx
+ rect
.width
;
2206 sx
= ( xx
+ x_end
- xe
)/x
;
2213 m_owner
->Scroll( sx
, sy
);
2216 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2218 wxDataViewModel
*model
= GetOwner()->GetModel();
2219 wxAutoBufferedPaintDC
dc( this );
2222 dc
.SetBackground(GetBackgroundColour());
2224 GetOwner()->PrepareDC( dc
);
2225 dc
.SetFont( GetFont() );
2227 wxRect update
= GetUpdateRegion().GetBox();
2228 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2230 // compute which items needs to be redrawn
2231 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2232 unsigned int item_count
=
2233 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2234 (int)(GetRowCount( )- item_start
) );
2235 unsigned int item_last
= item_start
+ item_count
;
2237 // compute which columns needs to be redrawn
2238 unsigned int cols
= GetOwner()->GetColumnCount();
2239 unsigned int col_start
= 0;
2240 unsigned int x_start
= 0;
2241 for (x_start
= 0; col_start
< cols
; col_start
++)
2243 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2244 if (col
->IsHidden())
2245 continue; // skip it!
2247 unsigned int w
= col
->GetWidth();
2248 if (x_start
+w
>= (unsigned int)update
.x
)
2254 unsigned int col_last
= col_start
;
2255 unsigned int x_last
= x_start
;
2256 for (; col_last
< cols
; col_last
++)
2258 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2259 if (col
->IsHidden())
2260 continue; // skip it!
2262 if (x_last
> (unsigned int)update
.GetRight())
2265 x_last
+= col
->GetWidth();
2268 // Draw horizontal rules if required
2269 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2271 dc
.SetPen(m_penRule
);
2272 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2274 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2276 int y
= i
* m_lineHeight
;
2277 dc
.DrawLine(x_start
, y
, x_last
, y
);
2281 // Draw vertical rules if required
2282 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2284 dc
.SetPen(m_penRule
);
2285 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2288 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2290 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2291 if (col
->IsHidden())
2292 continue; // skip it
2294 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2295 x
, item_last
* m_lineHeight
);
2297 x
+= col
->GetWidth();
2300 // Draw last vertical rule
2301 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2302 x
, item_last
* m_lineHeight
);
2305 // redraw the background for the items which are selected/current
2306 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2308 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2309 if (selected
|| item
== m_currentRow
)
2311 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2312 if (item
== m_currentRow
)
2313 flags
|= wxCONTROL_CURRENT
;
2315 flags
|= wxCONTROL_FOCUSED
;
2317 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2318 wxRendererNative::Get().DrawItemSelectionRect
2328 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2331 // TODO: last column for RTL support
2332 expander
= GetOwner()->GetColumn( 0 );
2333 GetOwner()->SetExpanderColumn(expander
);
2336 // redraw all cells for all rows which must be repainted and for all columns
2338 cell_rect
.x
= x_start
;
2339 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2340 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2342 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2343 wxDataViewRenderer
*cell
= col
->GetRenderer();
2344 cell_rect
.width
= col
->GetWidth();
2346 if (col
->IsHidden())
2347 continue; // skipt it!
2350 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2352 // get the cell value and set it into the renderer
2354 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2358 wxDataViewItem dataitem
= node
->GetItem();
2360 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2363 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2364 cell
->SetValue( value
);
2366 // update the y offset
2367 cell_rect
.y
= item
* m_lineHeight
;
2369 //Draw the expander here.
2370 int indent
= node
->GetIndentLevel();
2371 if( col
== expander
)
2373 //Calculate the indent first
2374 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2376 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2377 // change the cell_rect.x to the appropriate pos
2378 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2379 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2380 dc
.SetPen( m_penExpander
);
2381 dc
.SetBrush( wxNullBrush
);
2382 if( node
->HasChildren() )
2384 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2386 if (m_underMouse
== node
)
2388 flag
|= wxCONTROL_CURRENT
;
2390 if( node
->IsOpen() )
2391 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2393 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2397 // I am wandering whether we should draw dot lines between tree nodes
2399 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2402 //force the expander column to left-center align
2403 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2407 // cannot be bigger than allocated space
2408 wxSize size
= cell
->GetSize();
2409 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2410 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2411 // size.y = wxMin( size.y, cell_rect.height );
2412 size
.y
= cell_rect
.height
;
2414 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2415 int align
= cell
->GetAlignment();
2417 // horizontal alignment:
2418 item_rect
.x
= cell_rect
.x
;
2419 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2420 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2421 else if (align
& wxALIGN_RIGHT
)
2422 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2423 //else: wxALIGN_LEFT is the default
2425 // vertical alignment:
2426 item_rect
.y
= cell_rect
.y
;
2427 if (align
& wxALIGN_CENTER_VERTICAL
)
2428 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2429 else if (align
& wxALIGN_BOTTOM
)
2430 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2431 //else: wxALIGN_TOP is the default
2434 item_rect
.x
+= PADDING_RIGHTLEFT
;
2435 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2437 //Here we add the tree indent
2438 item_rect
.x
+= indent
;
2441 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2442 state
|= wxDATAVIEW_CELL_SELECTED
;
2444 // TODO: it would be much more efficient to create a clipping
2445 // region for the entire column being rendered (in the OnPaint
2446 // of wxDataViewMainWindow) instead of a single clip region for
2447 // each cell. However it would mean that each renderer should
2448 // respect the given wxRect's top & bottom coords, eventually
2449 // violating only the left & right coords - however the user can
2450 // make its own renderer and thus we cannot be sure of that.
2451 dc
.SetClippingRegion( item_rect
);
2452 cell
->Render( item_rect
, &dc
, state
);
2453 dc
.DestroyClippingRegion();
2456 cell_rect
.x
+= cell_rect
.width
;
2460 int wxDataViewMainWindow::GetCountPerPage() const
2462 wxSize size
= GetClientSize();
2463 return size
.y
/ m_lineHeight
;
2466 int wxDataViewMainWindow::GetEndOfLastCol() const
2470 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2472 const wxDataViewColumn
*c
=
2473 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2476 width
+= c
->GetWidth();
2481 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2485 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2487 return y
/ m_lineHeight
;
2490 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2492 wxSize client_size
= GetClientSize();
2493 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2494 &client_size
.x
, &client_size
.y
);
2496 //we should deal with the pixel here
2497 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2499 return wxMin( GetRowCount()-1, row
);
2502 unsigned int wxDataViewMainWindow::GetRowCount()
2504 if ( m_count
== -1 )
2506 m_count
= RecalculateCount();
2508 GetVirtualSize( &width
, &height
);
2509 height
= m_count
* m_lineHeight
;
2511 SetVirtualSize( width
, height
);
2516 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2523 void wxDataViewMainWindow::SelectAllRows( bool on
)
2530 m_selection
.Clear();
2531 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2532 m_selection
.Add( i
);
2537 unsigned int first_visible
= GetFirstVisibleRow();
2538 unsigned int last_visible
= GetLastVisibleRow();
2540 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2542 unsigned int row
= m_selection
[i
];
2543 if ((row
>= first_visible
) && (row
<= last_visible
))
2546 m_selection
.Clear();
2550 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2552 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2556 m_selection
.Add( row
);
2564 m_selection
.Remove( row
);
2570 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2574 unsigned int tmp
= from
;
2580 for (i
= from
; i
<= to
; i
++)
2582 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2585 m_selection
.Add( i
);
2590 m_selection
.Remove( i
);
2593 RefreshRows( from
, to
);
2596 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2598 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2600 int n
= aSelections
[i
];
2602 m_selection
.Add( n
);
2607 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2609 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2610 m_selection
.Add( row
);
2612 m_selection
.Remove( row
);
2616 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2618 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2621 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2623 wxWindow
*parent
= GetParent();
2624 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2626 le
.SetEventObject(parent
);
2627 le
.SetModel(GetOwner()->GetModel());
2630 parent
->GetEventHandler()->ProcessEvent(le
);
2633 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2635 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2636 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2638 wxSize client_size
= GetClientSize();
2639 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2640 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2641 if (intersect_rect
.width
> 0)
2642 Refresh( true, &intersect_rect
);
2645 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2649 unsigned int tmp
= to
;
2654 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2655 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2657 wxSize client_size
= GetClientSize();
2658 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2659 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2660 if (intersect_rect
.width
> 0)
2661 Refresh( true, &intersect_rect
);
2664 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2666 unsigned int count
= GetRowCount();
2667 if (firstRow
> count
)
2670 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2671 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2673 wxSize client_size
= GetClientSize();
2674 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2675 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2676 if (intersect_rect
.width
> 0)
2677 Refresh( true, &intersect_rect
);
2680 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2682 wxCHECK_RET( newCurrent
< GetRowCount(),
2683 _T("invalid item index in OnArrowChar()") );
2685 // if there is no selection, we cannot move it anywhere
2686 if (!HasCurrentRow())
2689 unsigned int oldCurrent
= m_currentRow
;
2691 // in single selection we just ignore Shift as we can't select several
2693 if ( event
.ShiftDown() && !IsSingleSel() )
2695 RefreshRow( oldCurrent
);
2697 ChangeCurrentRow( newCurrent
);
2699 // select all the items between the old and the new one
2700 if ( oldCurrent
> newCurrent
)
2702 newCurrent
= oldCurrent
;
2703 oldCurrent
= m_currentRow
;
2706 SelectRows( oldCurrent
, newCurrent
, true );
2707 if (oldCurrent
!=newCurrent
)
2708 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2712 RefreshRow( oldCurrent
);
2714 // all previously selected items are unselected unless ctrl is held
2715 if ( !event
.ControlDown() )
2716 SelectAllRows(false);
2718 ChangeCurrentRow( newCurrent
);
2720 if ( !event
.ControlDown() )
2722 SelectRow( m_currentRow
, true );
2723 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2726 RefreshRow( m_currentRow
);
2729 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2732 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2736 rect
.y
= m_lineHeight
* row
;
2737 rect
.width
= GetEndOfLastCol();
2738 rect
.height
= m_lineHeight
;
2743 class RowToItemJob
: public DoJob
2746 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2747 virtual ~RowToItemJob(){};
2749 virtual int operator() ( wxDataViewTreeNode
* node
)
2752 if( current
== static_cast<int>(row
))
2754 ret
= node
->GetItem() ;
2758 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2760 current
+= node
->GetSubTreeCount();
2765 //If the current has no child node, we can find the desired item of the row number directly.
2766 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2767 if( node
->GetNodes().GetCount() == 0)
2769 int index
= static_cast<int>(row
) - current
- 1;
2770 ret
= node
->GetChildren().Item( index
);
2777 virtual int operator() ( void * n
)
2780 if( current
== static_cast<int>(row
))
2782 ret
= wxDataViewItem( n
) ;
2787 wxDataViewItem
GetResult(){ return ret
; }
2794 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2796 RowToItemJob
job( row
, -2 );
2797 Walker( m_root
, job
);
2798 return job
.GetResult();
2801 class RowToTreeNodeJob
: public DoJob
2804 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2807 this->current
= current
;
2811 virtual ~RowToTreeNodeJob(){};
2813 virtual int operator() ( wxDataViewTreeNode
* node
)
2816 if( current
== static_cast<int>(row
))
2822 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2824 current
+= node
->GetSubTreeCount();
2830 //If the current has no child node, we can find the desired item of the row number directly.
2831 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2832 if( node
->GetNodes().GetCount() == 0)
2834 int index
= static_cast<int>(row
) - current
- 1;
2835 void * n
= node
->GetChildren().Item( index
);
2836 ret
= new wxDataViewTreeNode( parent
) ;
2837 ret
->SetItem( wxDataViewItem( n
));
2838 ret
->SetHasChildren(false);
2847 virtual int operator() ( void * n
)
2850 if( current
== static_cast<int>(row
))
2852 ret
= new wxDataViewTreeNode( parent
) ;
2853 ret
->SetItem( wxDataViewItem( n
));
2854 ret
->SetHasChildren(false);
2860 wxDataViewTreeNode
* GetResult(){ return ret
; }
2864 wxDataViewTreeNode
* ret
;
2865 wxDataViewTreeNode
* parent
;
2869 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2871 RowToTreeNodeJob
job( row
, -2, m_root
);
2872 Walker( m_root
, job
);
2873 return job
.GetResult();
2876 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2878 wxWindow
*parent
= GetParent();
2879 wxDataViewEvent
le(type
, parent
->GetId());
2881 le
.SetEventObject(parent
);
2882 le
.SetModel(GetOwner()->GetModel());
2885 parent
->GetEventHandler()->ProcessEvent(le
);
2889 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2891 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2894 if( node
->HasChildren())
2896 if( !node
->IsOpen())
2898 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2899 //Check if the user prevent expanding
2900 if( e
.GetSkipped() )
2904 //Here I build the children of current node
2905 if( node
->GetChildrenNumber() == 0 )
2908 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2912 //Send the expanded event
2913 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2917 SelectRow( row
, false );
2918 SelectRow( row
+ 1, true );
2919 ChangeCurrentRow( row
+ 1 );
2920 SendSelectionChangedEvent( GetItemByRow(row
+1));
2928 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2930 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2933 wxDataViewTreeNode
* nd
= node
;
2935 if( node
->HasChildren() && node
->IsOpen() )
2937 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2938 if( e
.GetSkipped() )
2943 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2947 node
= node
->GetParent();
2950 int parent
= GetRowByItem( node
->GetItem() ) ;
2953 SelectRow( row
, false);
2954 SelectRow(parent
, true );
2955 ChangeCurrentRow( parent
);
2956 SendSelectionChangedEvent( node
->GetItem() );
2960 if( !nd
->HasChildren())
2965 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2967 wxDataViewModel
* model
= GetOwner()->GetModel();
2971 //Compose the a parent-chain of the finding item
2973 list
.DeleteContents( true );
2974 wxDataViewItem
it( item
);
2977 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2978 list
.Insert( pItem
);
2979 it
= model
->GetParent( it
);
2982 //Find the item along the parent-chain.
2983 //This algorithm is designed to speed up the node-finding method
2984 wxDataViewTreeNode
* node
= m_root
;
2985 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
2987 if( node
->HasChildren() )
2989 if( node
->GetChildrenNumber() == 0 )
2992 BuildTreeHelper(model
, node
->GetItem(), node
);
2995 wxDataViewTreeNodes nodes
= node
->GetNodes();
2997 for (; i
< nodes
.GetCount(); i
++)
2999 if (nodes
[i
]->GetItem() == (**iter
))
3005 if (i
== nodes
.GetCount())
3014 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3016 wxDataViewColumn
*col
= NULL
;
3017 unsigned int cols
= GetOwner()->GetColumnCount();
3018 unsigned int colnum
= 0;
3019 unsigned int x_start
= 0;
3021 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3022 for (x_start
= 0; colnum
< cols
; colnum
++)
3024 col
= GetOwner()->GetColumn(colnum
);
3025 if (col
->IsHidden())
3026 continue; // skip it!
3028 unsigned int w
= col
->GetWidth();
3029 if (x_start
+w
>= (unsigned int)x
)
3036 item
= GetItemByRow( y
/m_lineHeight
);
3039 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3041 int row
= GetRowByItem(item
);
3042 int y
= row
*m_lineHeight
;
3043 int h
= m_lineHeight
;
3045 wxDataViewColumn
*col
= NULL
;
3046 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3048 col
= GetOwner()->GetColumn( i
);
3049 x
+= col
->GetWidth();
3050 if( GetOwner()->GetColumn(i
+1) == column
)
3053 int w
= col
->GetWidth();
3054 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3055 return wxRect(x
, y
, w
, h
);
3058 int wxDataViewMainWindow::RecalculateCount()
3060 return m_root
->GetSubTreeCount();
3063 class ItemToRowJob
: public DoJob
3066 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3067 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3068 virtual ~ItemToRowJob(){};
3070 //Maybe binary search will help to speed up this process
3071 virtual int operator() ( wxDataViewTreeNode
* node
)
3074 if( node
->GetItem() == item
)
3079 if( node
->GetItem() == **m_iter
)
3086 ret
+= node
->GetSubTreeCount();
3092 virtual int operator() ( void * n
)
3095 if( n
== item
.GetID() )
3099 //the row number is begin from zero
3100 int GetResult(){ return ret
-1 ; }
3102 ItemList::const_iterator m_iter
;
3103 wxDataViewItem item
;
3108 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3110 wxDataViewModel
* model
= GetOwner()->GetModel();
3117 //Compose the a parent-chain of the finding item
3119 wxDataViewItem
* pItem
= NULL
;
3120 list
.DeleteContents( true );
3121 wxDataViewItem
it( item
);
3124 pItem
= new wxDataViewItem( it
);
3125 list
.Insert( pItem
);
3126 it
= model
->GetParent( it
);
3128 pItem
= new wxDataViewItem( );
3129 list
.Insert( pItem
);
3131 ItemToRowJob
job( item
, list
.begin() );
3132 Walker(m_root
, job
);
3133 return job
.GetResult();
3136 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3138 if( !model
->IsContainer( item
) )
3141 wxDataViewItemArray children
;
3142 unsigned int num
= model
->GetChildren( item
, children
);
3143 unsigned int index
= 0;
3144 while( index
< num
)
3146 if( model
->IsContainer( children
[index
] ) )
3148 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3149 n
->SetItem(children
[index
]);
3150 n
->SetHasChildren( true ) ;
3155 node
->AddLeaf( children
[index
].GetID() );
3159 node
->SetSubTreeCount( num
);
3160 wxDataViewTreeNode
* n
= node
->GetParent();
3162 n
->ChangeSubTreeCount(num
);
3166 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3168 //First we define a invalid item to fetch the top-level elements
3169 wxDataViewItem item
;
3171 BuildTreeHelper( model
, item
, m_root
);
3175 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3177 if( node
->GetNodeNumber() != 0 )
3179 int len
= node
->GetNodeNumber();
3181 wxDataViewTreeNodes nodes
= node
->GetNodes();
3182 for( ; i
< len
; i
++ )
3184 DestroyTreeHelper(nodes
[i
]);
3190 void wxDataViewMainWindow::DestroyTree()
3192 DestroyTreeHelper(m_root
);
3193 m_root
->SetSubTreeCount(0);
3197 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3199 if (event
.GetKeyCode() == WXK_TAB
)
3201 wxNavigationKeyEvent nevent
;
3202 nevent
.SetWindowChange( event
.ControlDown() );
3203 nevent
.SetDirection( !event
.ShiftDown() );
3204 nevent
.SetEventObject( GetParent()->GetParent() );
3205 nevent
.SetCurrentFocus( m_parent
);
3206 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3210 // no item -> nothing to do
3211 if (!HasCurrentRow())
3217 // don't use m_linesPerPage directly as it might not be computed yet
3218 const int pageSize
= GetCountPerPage();
3219 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3221 switch ( event
.GetKeyCode() )
3225 if (m_currentRow
> 0)
3227 wxWindow
*parent
= GetParent();
3228 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3229 le
.SetItem( GetItemByRow(m_currentRow
) );
3230 le
.SetEventObject(parent
);
3231 le
.SetModel(GetOwner()->GetModel());
3233 parent
->GetEventHandler()->ProcessEvent(le
);
3238 if ( m_currentRow
> 0 )
3239 OnArrowChar( m_currentRow
- 1, event
);
3243 if ( m_currentRow
< GetRowCount() - 1 )
3244 OnArrowChar( m_currentRow
+ 1, event
);
3246 //Add the process for tree expanding/collapsing
3248 OnCollapsing(m_currentRow
);
3251 OnExpanding( m_currentRow
);
3255 OnArrowChar( GetRowCount() - 1, event
);
3260 OnArrowChar( 0, event
);
3265 int steps
= pageSize
- 1;
3266 int index
= m_currentRow
- steps
;
3270 OnArrowChar( index
, event
);
3276 int steps
= pageSize
- 1;
3277 unsigned int index
= m_currentRow
+ steps
;
3278 unsigned int count
= GetRowCount();
3279 if ( index
>= count
)
3282 OnArrowChar( index
, event
);
3291 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3293 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3295 // let the base handle mouse wheel events.
3300 int x
= event
.GetX();
3301 int y
= event
.GetY();
3302 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3303 wxDataViewColumn
*col
= NULL
;
3306 unsigned int cols
= GetOwner()->GetColumnCount();
3308 for (i
= 0; i
< cols
; i
++)
3310 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3312 continue; // skip it!
3314 if (x
< xpos
+ c
->GetWidth())
3319 xpos
+= c
->GetWidth();
3324 wxDataViewRenderer
*cell
= col
->GetRenderer();
3325 unsigned int current
= y
/ m_lineHeight
;
3326 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3328 // Unselect all if below the last row ?
3332 //Test whether the mouse is hovered on the tree item button
3334 if (GetOwner()->GetExpanderColumn() == col
)
3336 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3337 if( node
!=NULL
&& node
->HasChildren() )
3339 int indent
= node
->GetIndentLevel();
3340 indent
= GetOwner()->GetIndent()*indent
;
3341 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3342 if( rect
.Contains( x
, y
) )
3344 //So the mouse is over the expander
3346 if (m_underMouse
&& m_underMouse
!= node
)
3348 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3349 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3351 if (m_underMouse
!= node
)
3353 //wxLogMessage("Do the row: %d", current);
3354 RefreshRow(current
);
3356 m_underMouse
= node
;
3359 if (node
!=NULL
&& !node
->HasChildren())
3364 if (m_underMouse
!= NULL
)
3366 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3367 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3368 m_underMouse
= NULL
;
3372 wxDataViewModel
*model
= GetOwner()->GetModel();
3374 if (event
.Dragging())
3376 if (m_dragCount
== 0)
3378 // we have to report the raw, physical coords as we want to be
3379 // able to call HitTest(event.m_pointDrag) from the user code to
3380 // get the item being dragged
3381 m_dragStart
= event
.GetPosition();
3386 if (m_dragCount
!= 3)
3389 if (event
.LeftIsDown())
3391 // Notify cell about drag
3400 bool forceClick
= false;
3402 if (event
.ButtonDClick())
3404 m_renameTimer
->Stop();
3405 m_lastOnSame
= false;
3408 wxDataViewItem item
= GetItemByRow(current
);
3409 bool ignore_other_columns
=
3410 ((GetOwner()->GetExpanderColumn() != col
) &&
3411 (model
->IsContainer(item
)) &&
3412 (!model
->HasContainerColumns(item
)));
3414 if (event
.LeftDClick())
3416 if ( current
== m_lineLastClicked
)
3418 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3421 model
->GetValue( value
, item
, col
->GetModelColumn() );
3422 cell
->SetValue( value
);
3423 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3424 col
->GetWidth(), m_lineHeight
);
3425 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3430 wxWindow
*parent
= GetParent();
3431 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3433 le
.SetEventObject(parent
);
3434 le
.SetModel(GetOwner()->GetModel());
3436 parent
->GetEventHandler()->ProcessEvent(le
);
3442 // The first click was on another item, so don't interpret this as
3443 // a double click, but as a simple click instead
3450 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3452 // select single line
3453 SelectAllRows( false );
3454 SelectRow( m_lineSelectSingleOnUp
, true );
3457 //Process the event of user clicking the expander
3458 bool expander
= false;
3459 if (GetOwner()->GetExpanderColumn() == col
)
3461 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3462 if( node
!=NULL
&& node
->HasChildren() )
3464 int indent
= node
->GetIndentLevel();
3465 indent
= GetOwner()->GetIndent()*indent
;
3466 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3467 if( rect
.Contains( x
, y
) )
3470 if( node
->IsOpen() )
3471 OnCollapsing(current
);
3473 OnExpanding( current
);
3477 //If the user click the expander, we do not do editing even if the column with expander are editable
3478 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3480 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3481 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3483 m_renameTimer
->Start( 100, true );
3487 m_lastOnSame
= false;
3488 m_lineSelectSingleOnUp
= (unsigned int)-1;
3492 // This is necessary, because after a DnD operation in
3493 // from and to ourself, the up event is swallowed by the
3494 // DnD code. So on next non-up event (which means here and
3495 // now) m_lineSelectSingleOnUp should be reset.
3496 m_lineSelectSingleOnUp
= (unsigned int)-1;
3499 if (event
.RightDown())
3501 m_lineBeforeLastClicked
= m_lineLastClicked
;
3502 m_lineLastClicked
= current
;
3504 // If the item is already selected, do not update the selection.
3505 // Multi-selections should not be cleared if a selected item is clicked.
3506 if (!IsRowSelected(current
))
3508 SelectAllRows(false);
3509 ChangeCurrentRow(current
);
3510 SelectRow(m_currentRow
,true);
3511 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3514 // notify cell about right click
3517 // Allow generation of context menu event
3520 else if (event
.MiddleDown())
3522 // notify cell about middle click
3525 if (event
.LeftDown() || forceClick
)
3529 m_lineBeforeLastClicked
= m_lineLastClicked
;
3530 m_lineLastClicked
= current
;
3532 unsigned int oldCurrentRow
= m_currentRow
;
3533 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3535 bool cmdModifierDown
= event
.CmdDown();
3536 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3538 if ( IsSingleSel() || !IsRowSelected(current
) )
3540 SelectAllRows( false );
3541 ChangeCurrentRow(current
);
3542 SelectRow(m_currentRow
,true);
3543 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3545 else // multi sel & current is highlighted & no mod keys
3547 m_lineSelectSingleOnUp
= current
;
3548 ChangeCurrentRow(current
); // change focus
3551 else // multi sel & either ctrl or shift is down
3553 if (cmdModifierDown
)
3555 ChangeCurrentRow(current
);
3556 ReverseRowSelection(m_currentRow
);
3557 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3559 else if (event
.ShiftDown())
3561 ChangeCurrentRow(current
);
3563 unsigned int lineFrom
= oldCurrentRow
,
3566 if ( lineTo
< lineFrom
)
3569 lineFrom
= m_currentRow
;
3572 SelectRows(lineFrom
, lineTo
, true);
3573 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3575 else // !ctrl, !shift
3577 // test in the enclosing if should make it impossible
3578 wxFAIL_MSG( _T("how did we get here?") );
3582 if (m_currentRow
!= oldCurrentRow
)
3583 RefreshRow( oldCurrentRow
);
3585 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3587 // Update selection here...
3590 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3591 (current
== oldCurrentRow
)) && oldWasSelected
;
3595 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3599 if (HasCurrentRow())
3605 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3609 if (HasCurrentRow())
3615 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3617 if( m_selection
.GetCount() != 1 )
3618 return wxDataViewItem();
3620 return GetItemByRow( m_selection
.Item(0));
3623 //-----------------------------------------------------------------------------
3625 //-----------------------------------------------------------------------------
3626 WX_DEFINE_LIST(wxDataViewColumnList
)
3628 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3630 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3631 EVT_SIZE(wxDataViewCtrl::OnSize
)
3634 wxDataViewCtrl::~wxDataViewCtrl()
3637 GetModel()->RemoveNotifier( m_notifier
);
3640 void wxDataViewCtrl::Init()
3645 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3646 const wxPoint
& pos
, const wxSize
& size
,
3647 long style
, const wxValidator
& validator
)
3649 if (!wxControl::Create( parent
, id
, pos
, size
,
3650 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3653 SetInitialSize(size
);
3658 MacSetClipChildren( true ) ;
3661 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3663 if (HasFlag(wxDV_NO_HEADER
))
3664 m_headerArea
= NULL
;
3666 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3668 SetTargetWindow( m_clientArea
);
3670 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3672 sizer
->Add( m_headerArea
, 0, wxGROW
);
3673 sizer
->Add( m_clientArea
, 1, wxGROW
);
3680 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3684 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3687 // we need to process arrows ourselves for scrolling
3688 if ( nMsg
== WM_GETDLGCODE
)
3690 rc
|= DLGC_WANTARROWS
;
3698 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3700 // We need to override OnSize so that our scrolled
3701 // window a) does call Layout() to use sizers for
3702 // positioning the controls but b) does not query
3703 // the sizer for their size and use that for setting
3704 // the scrollable area as set that ourselves by
3705 // calling SetScrollbar() further down.
3712 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3714 if (!wxDataViewCtrlBase::AssociateModel( model
))
3717 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3719 model
->AddNotifier( m_notifier
);
3721 m_clientArea
->BuildTree(model
);
3723 m_clientArea
->UpdateDisplay();
3728 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3730 if (!wxDataViewCtrlBase::AppendColumn(col
))
3733 m_cols
.Append( col
);
3738 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3740 if (!wxDataViewCtrlBase::PrependColumn(col
))
3743 m_cols
.Insert( col
);
3748 void wxDataViewCtrl::OnColumnChange()
3751 m_headerArea
->UpdateDisplay();
3753 m_clientArea
->UpdateDisplay();
3756 void wxDataViewCtrl::DoSetExpanderColumn()
3758 m_clientArea
->UpdateDisplay();
3761 void wxDataViewCtrl::DoSetIndent()
3763 m_clientArea
->UpdateDisplay();
3766 unsigned int wxDataViewCtrl::GetColumnCount() const
3768 return m_cols
.GetCount();
3771 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3773 wxDataViewColumnList::const_iterator iter
;
3775 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3780 if ((*iter
)->IsHidden())
3787 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3789 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3800 bool wxDataViewCtrl::ClearColumns()
3807 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3809 int ret
= 0, dead
= 0;
3810 int len
= GetColumnCount();
3811 for (int i
=0; i
<len
; i
++)
3813 wxDataViewColumn
* col
= GetColumn(i
);
3814 if (col
->IsHidden())
3816 ret
+= col
->GetWidth();
3819 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3826 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3831 //Selection code with wxDataViewItem as parameters
3832 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3834 return m_clientArea
->GetSelection();
3837 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3840 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3841 int len
= selection
.GetCount();
3842 for( int i
= 0; i
< len
; i
++)
3844 unsigned int row
= selection
[i
];
3845 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3850 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3852 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3853 int len
= sel
.GetCount();
3854 for( int i
= 0; i
< len
; i
++ )
3856 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3858 selection
.Add( static_cast<unsigned int>(row
) );
3860 m_clientArea
->SetSelections( selection
);
3863 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3865 int row
= m_clientArea
->GetRowByItem( item
);
3868 //Unselect all rows before select another in the single select mode
3869 if (m_clientArea
->IsSingleSel())
3870 m_clientArea
->SelectAllRows(false);
3871 m_clientArea
->SelectRow(row
, true);
3875 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3877 int row
= m_clientArea
->GetRowByItem( item
);
3879 m_clientArea
->SelectRow(row
, false);
3882 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3884 int row
= m_clientArea
->GetRowByItem( item
);
3887 return m_clientArea
->IsRowSelected(row
);
3892 //Selection code with row number as parameter
3893 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3896 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3897 int len
= selection
.GetCount();
3898 for( int i
= 0; i
< len
; i
++)
3900 unsigned int row
= selection
[i
];
3906 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3908 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3909 int len
= sel
.GetCount();
3910 for( int i
= 0; i
< len
; i
++ )
3914 selection
.Add( static_cast<unsigned int>(row
) );
3916 m_clientArea
->SetSelections( selection
);
3919 void wxDataViewCtrl::Select( int row
)
3923 if (m_clientArea
->IsSingleSel())
3924 m_clientArea
->SelectAllRows(false);
3925 m_clientArea
->SelectRow( row
, true );
3929 void wxDataViewCtrl::Unselect( int row
)
3932 m_clientArea
->SelectRow(row
, false);
3935 bool wxDataViewCtrl::IsSelected( int row
) const
3938 return m_clientArea
->IsRowSelected(row
);
3942 void wxDataViewCtrl::SelectRange( int from
, int to
)
3945 for( int i
= from
; i
< to
; i
++ )
3947 m_clientArea
->Select(sel
);
3950 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3952 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3953 for( int i
= from
; i
< to
; i
++ )
3954 if( sel
.Index( i
) != wxNOT_FOUND
)
3956 m_clientArea
->SetSelections(sel
);
3959 void wxDataViewCtrl::SelectAll()
3961 m_clientArea
->SelectAllRows(true);
3964 void wxDataViewCtrl::UnselectAll()
3966 m_clientArea
->SelectAllRows(false);
3969 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3973 if( row
> (int) m_clientArea
->GetRowCount() )
3974 row
= m_clientArea
->GetRowCount();
3976 int first
= m_clientArea
->GetFirstVisibleRow();
3977 int last
= m_clientArea
->GetLastVisibleRow();
3979 m_clientArea
->ScrollTo( row
, column
);
3980 else if( row
> last
)
3981 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3983 m_clientArea
->ScrollTo( first
, column
);
3986 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3988 int row
= m_clientArea
->GetRowByItem(item
);
3991 if( column
== NULL
)
3992 EnsureVisible(row
, -1);
3996 int len
= GetColumnCount();
3997 for( int i
= 0; i
< len
; i
++ )
3998 if( GetColumn(i
) == column
)
4003 EnsureVisible( row
, col
);
4009 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4011 m_clientArea
->HitTest(point
, item
, column
);
4014 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4016 return m_clientArea
->GetItemRect(item
, column
);
4019 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4021 return m_clientArea
->GetItemByRow( row
);
4024 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4026 return m_clientArea
->GetRowByItem( item
);
4029 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4031 int row
= m_clientArea
->GetRowByItem( item
);
4033 m_clientArea
->Expand(row
);
4036 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4038 int row
= m_clientArea
->GetRowByItem( item
);
4040 m_clientArea
->Collapse(row
);
4044 // !wxUSE_GENERICDATAVIEWCTRL
4047 // wxUSE_DATAVIEWCTRL