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 #ifndef HDS_DRAGDROP
1197 #define HDS_DRAGDROP 0x0040
1199 #ifndef HDS_FULLDRAG
1200 #define HDS_FULLDRAG 0x0080
1203 // implemented in msw/listctrl.cpp:
1204 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1206 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1208 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1209 const wxPoint
&pos
, const wxSize
&size
,
1210 const wxString
&name
)
1214 m_scrollOffsetX
= 0;
1215 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this ) + 10;
1217 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1218 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1219 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1220 h
= size
.y
== wxDefaultCoord
? m_buttonHeight
: size
.y
;
1222 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1225 // create the native WC_HEADER window:
1226 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1227 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1230 msStyle
|= WS_VISIBLE
;
1232 m_hWnd
= CreateWindowEx(0,
1243 wxLogLastError(_T("CreateWindowEx"));
1247 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1248 // to call wxDataViewHeaderWindow::MSWOnNotify
1249 SubclassWin(m_hWnd
);
1251 // the following is required to get the default win's font for
1252 // header windows and must be done befor sending the HDM_LAYOUT msg
1258 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1263 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1265 return wxSize(80, m_buttonHeight
);
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 WXUNUSED(dy
),
1502 const wxRect
* WXUNUSED(rect
))
1504 m_scrollOffsetX
+= dx
;
1506 GetParent()->Layout();
1509 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1513 // TODO: why is there a border + 2px around it?
1514 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1517 #else // !defined(__WXMSW__)
1519 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1520 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1521 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1522 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1523 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1526 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1527 const wxPoint
&pos
, const wxSize
&size
,
1528 const wxString
&name
)
1532 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1535 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1536 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1537 SetOwnForegroundColour( attr
.colFg
);
1538 SetOwnBackgroundColour( attr
.colBg
);
1540 SetOwnFont( attr
.font
);
1542 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1543 // a wxBoxSizer and in order to avoid super-big header windows,
1544 // we need to set our height as fixed
1545 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1546 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1551 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1554 GetClientSize( &w
, &h
);
1556 wxAutoBufferedPaintDC
dc( this );
1558 dc
.SetBackground(GetBackgroundColour());
1562 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1565 m_owner
->GetViewStart( &x
, NULL
);
1567 // account for the horz scrollbar offset
1568 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1570 dc
.SetFont( GetFont() );
1572 unsigned int cols
= GetOwner()->GetColumnCount();
1575 for (i
= 0; i
< cols
; i
++)
1577 wxDataViewColumn
*col
= GetColumn( i
);
1578 if (col
->IsHidden())
1579 continue; // skip it!
1581 int cw
= col
->GetWidth();
1584 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1585 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1587 if (col
->IsSortOrderAscending())
1588 sortArrow
= wxHDR_SORT_ICON_UP
;
1590 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1594 if (m_parent
->IsEnabled())
1596 if ((int) i
== m_hover
)
1597 state
= wxCONTROL_CURRENT
;
1601 state
= (int) wxCONTROL_DISABLED
;
1604 wxRendererNative::Get().DrawHeaderButton
1608 wxRect(xpos
, 0, cw
, ch
-1),
1613 // align as required the column title:
1615 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1616 switch (col
->GetAlignment())
1619 x
+= HEADER_HORIZ_BORDER
;
1622 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1625 case wxALIGN_CENTER
:
1626 case wxALIGN_CENTER_HORIZONTAL
:
1627 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1631 // always center the title vertically:
1632 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1634 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1636 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1637 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1638 dc
.DrawText( col
->GetTitle(), x
, y
);
1639 dc
.DestroyClippingRegion();
1645 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1647 GetParent()->SetFocus();
1651 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1653 // we want to work with logical coords
1655 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1656 int y
= event
.GetY();
1660 // we don't draw the line beyond our window,
1661 // but we allow dragging it there
1663 GetClientSize( &w
, NULL
);
1664 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1667 if (event
.ButtonUp())
1669 m_isDragging
= false;
1675 m_currentX
= wxMax(m_minX
+ 7, x
);
1679 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1681 GetOwner()->Refresh();
1685 else // not dragging
1688 m_column
= wxNOT_FOUND
;
1690 bool hit_border
= false;
1692 // end of the current column
1695 // find the column where this event occured
1696 int countCol
= m_owner
->GetColumnCount();
1697 for (int column
= 0; column
< countCol
; column
++)
1699 wxDataViewColumn
*p
= GetColumn(column
);
1702 continue; // skip if not shown
1704 xpos
+= p
->GetWidth();
1706 if ((abs(x
-xpos
) < 3) && (y
< 22))
1714 // inside the column
1721 int old_hover
= m_hover
;
1723 if (event
.Leaving())
1724 m_hover
= wxNOT_FOUND
;
1725 if (old_hover
!= m_hover
)
1728 if (m_column
== wxNOT_FOUND
)
1731 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1732 if (event
.LeftDClick() && resizeable
)
1734 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1737 else if (event
.LeftDown() || event
.RightUp())
1739 if (hit_border
&& event
.LeftDown() && resizeable
)
1741 m_isDragging
= true;
1745 else // click on a column
1747 wxDataViewModel
* model
= GetOwner()->GetModel();
1748 wxEventType evt
= event
.LeftDown() ?
1749 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1750 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1751 SendEvent(evt
, m_column
);
1753 //Left click the header
1754 if(event
.LeftDown())
1756 wxDataViewColumn
*col
= GetColumn(m_column
);
1757 if(col
->IsSortable())
1759 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1760 if(model
&& sortCol
== col
)
1762 bool order
= col
->IsSortOrderAscending();
1763 col
->SetSortOrder(!order
);
1767 m_owner
->SetSortingColumn(col
);
1773 //Send the column sorted event
1774 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1778 else if (event
.Moving())
1780 if (hit_border
&& resizeable
)
1781 m_currentCursor
= m_resizeCursor
;
1783 m_currentCursor
= wxSTANDARD_CURSOR
;
1785 SetCursor(*m_currentCursor
);
1790 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1794 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1795 m_owner
->GetViewStart( &x
, NULL
);
1797 // shift the DC origin to match the position of the main window horizontal
1798 // scrollbar: this allows us to always use logical coords
1799 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1802 #endif // defined(__WXMSW__)
1804 //-----------------------------------------------------------------------------
1805 // wxDataViewRenameTimer
1806 //-----------------------------------------------------------------------------
1808 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1813 void wxDataViewRenameTimer::Notify()
1815 m_owner
->OnRenameTimer();
1818 //-----------------------------------------------------------------------------
1819 // wxDataViewMainWindow
1820 //-----------------------------------------------------------------------------
1822 //The tree building helper, declared firstly
1823 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1825 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1827 if (row1
> row2
) return 1;
1828 if (row1
== row2
) return 0;
1833 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1835 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1836 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1837 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1838 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1839 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1840 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1843 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1844 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1845 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1846 m_selection( wxDataViewSelectionCmp
)
1851 m_lastOnSame
= false;
1852 m_renameTimer
= new wxDataViewRenameTimer( this );
1854 // TODO: user better initial values/nothing selected
1855 m_currentCol
= NULL
;
1858 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1861 m_dragStart
= wxPoint(0,0);
1862 m_lineLastClicked
= (unsigned int) -1;
1863 m_lineBeforeLastClicked
= (unsigned int) -1;
1864 m_lineSelectSingleOnUp
= (unsigned int) -1;
1868 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1869 SetBackgroundColour( *wxWHITE
);
1871 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1873 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1874 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1875 //Some new added code to deal with the tree structure
1876 m_root
= new wxDataViewTreeNode( NULL
);
1877 m_root
->SetHasChildren(true);
1879 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1881 m_underMouse
= NULL
;
1885 wxDataViewMainWindow::~wxDataViewMainWindow()
1888 delete m_renameTimer
;
1891 void wxDataViewMainWindow::OnRenameTimer()
1893 // We have to call this here because changes may just have
1894 // been made and no screen update taken place.
1899 unsigned int cols
= GetOwner()->GetColumnCount();
1901 for (i
= 0; i
< cols
; i
++)
1903 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1905 continue; // skip it!
1907 if (c
== m_currentCol
)
1909 xpos
+= c
->GetWidth();
1911 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1912 m_currentCol
->GetWidth(), m_lineHeight
);
1914 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1915 &labelRect
.x
, &labelRect
.y
);
1917 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1918 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1922 //------------------------------------------------------------------
1923 // Helper class for do operation on the tree node
1924 //------------------------------------------------------------------
1931 //The return value control how the tree-walker tranverse the tree
1932 // 0: Job done, stop tranverse and return
1933 // 1: Ignore the current node's subtree and continue
1934 // 2: Job not done, continue
1935 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1936 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1937 virtual int operator() ( void * n
) = 0;
1940 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1945 switch( func( node
) )
1956 wxDataViewTreeNodes nodes
= node
->GetNodes();
1957 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1959 int len_nodes
= nodes
.GetCount();
1960 int len
= leaves
.GetCount();
1961 int i
= 0, nodes_i
= 0;
1963 for( ; i
< len
; i
++ )
1965 void * n
= leaves
[i
];
1966 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1968 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1971 if( Walker( nd
, func
) )
1990 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1994 wxDataViewTreeNode
* node
;
1995 node
= FindNode(parent
);
2000 node
->SetHasChildren( true );
2002 if( g_model
->IsContainer( item
) )
2004 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2005 newnode
->SetItem(item
);
2006 newnode
->SetHasChildren( true );
2007 node
->AddNode( newnode
);
2010 node
->AddLeaf( item
.GetID() );
2012 node
->ChangeSubTreeCount(1);
2020 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2022 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2023 const wxDataViewItem
& item
)
2025 wxDataViewTreeNode
* node
= FindNode(parent
);
2027 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2028 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2031 node
->GetChildren().Remove( item
.GetID() );
2032 //Manuplate selection
2033 if( m_selection
.GetCount() > 1 )
2035 m_selection
.Empty();
2037 bool isContainer
= false;
2038 wxDataViewTreeNodes nds
= node
->GetNodes();
2039 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2041 if (nds
[i
]->GetItem() == item
)
2049 wxDataViewTreeNode
* n
= NULL
;
2050 wxDataViewTreeNodes nodes
= node
->GetNodes();
2051 int len
= nodes
.GetCount();
2052 for( int i
= 0 ; i
< len
; i
++)
2054 if( nodes
[i
]->GetItem() == item
)
2061 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2063 node
->GetNodes().Remove( n
);
2064 sub
-= n
->GetSubTreeCount();
2065 DestroyTreeHelper(n
);
2067 //Make the row number invalid and get a new valid one when user call GetRowCount
2069 node
->ChangeSubTreeCount(sub
);
2070 if( node
->GetChildrenNumber() == 0)
2072 node
->GetParent()->GetNodes().Remove( node
);
2076 //Change the current row to the last row if the current exceed the max row number
2077 if( m_currentRow
> GetRowCount() )
2078 m_currentRow
= m_count
- 1;
2085 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2091 wxWindow
*parent
= GetParent();
2092 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2093 le
.SetEventObject(parent
);
2094 le
.SetModel(GetOwner()->GetModel());
2096 parent
->GetEventHandler()->ProcessEvent(le
);
2101 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2103 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2104 /*#define MAX_VIRTUAL_WIDTH 100000
2106 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2107 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2108 Refresh( true, &rect );
2116 wxWindow
*parent
= GetParent();
2117 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2118 le
.SetEventObject(parent
);
2119 le
.SetModel(GetOwner()->GetModel());
2122 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2123 parent
->GetEventHandler()->ProcessEvent(le
);
2128 bool wxDataViewMainWindow::Cleared()
2138 void wxDataViewMainWindow::UpdateDisplay()
2143 void wxDataViewMainWindow::OnInternalIdle()
2145 wxWindow::OnInternalIdle();
2149 RecalculateDisplay();
2154 void wxDataViewMainWindow::RecalculateDisplay()
2156 wxDataViewModel
*model
= GetOwner()->GetModel();
2163 int width
= GetEndOfLastCol();
2164 int height
= GetRowCount() * m_lineHeight
;
2166 SetVirtualSize( width
, height
);
2167 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2172 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2174 wxWindow::ScrollWindow( dx
, dy
, rect
);
2176 if (GetOwner()->m_headerArea
)
2177 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2180 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2183 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2184 int sy
= rows
*m_lineHeight
/y
;
2188 wxRect rect
= GetClientRect();
2190 int x_start
= 0, x_end
= 0, w
= 0;
2192 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2193 for (x_start
= 0; colnum
< column
; colnum
++)
2195 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2196 if (col
->IsHidden())
2197 continue; // skip it!
2199 w
= col
->GetWidth();
2203 x_end
= x_start
+ w
;
2204 xe
= xx
+ rect
.width
;
2207 sx
= ( xx
+ x_end
- xe
)/x
;
2214 m_owner
->Scroll( sx
, sy
);
2217 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2219 wxDataViewModel
*model
= GetOwner()->GetModel();
2220 wxAutoBufferedPaintDC
dc( this );
2223 dc
.SetBackground(GetBackgroundColour());
2225 GetOwner()->PrepareDC( dc
);
2226 dc
.SetFont( GetFont() );
2228 wxRect update
= GetUpdateRegion().GetBox();
2229 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2231 // compute which items needs to be redrawn
2232 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2233 unsigned int item_count
=
2234 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2235 (int)(GetRowCount( )- item_start
) );
2236 unsigned int item_last
= item_start
+ item_count
;
2238 // compute which columns needs to be redrawn
2239 unsigned int cols
= GetOwner()->GetColumnCount();
2240 unsigned int col_start
= 0;
2241 unsigned int x_start
= 0;
2242 for (x_start
= 0; col_start
< cols
; col_start
++)
2244 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2245 if (col
->IsHidden())
2246 continue; // skip it!
2248 unsigned int w
= col
->GetWidth();
2249 if (x_start
+w
>= (unsigned int)update
.x
)
2255 unsigned int col_last
= col_start
;
2256 unsigned int x_last
= x_start
;
2257 for (; col_last
< cols
; col_last
++)
2259 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2260 if (col
->IsHidden())
2261 continue; // skip it!
2263 if (x_last
> (unsigned int)update
.GetRight())
2266 x_last
+= col
->GetWidth();
2269 // Draw horizontal rules if required
2270 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2272 dc
.SetPen(m_penRule
);
2273 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2275 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2277 int y
= i
* m_lineHeight
;
2278 dc
.DrawLine(x_start
, y
, x_last
, y
);
2282 // Draw vertical rules if required
2283 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2285 dc
.SetPen(m_penRule
);
2286 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2289 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2291 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2292 if (col
->IsHidden())
2293 continue; // skip it
2295 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2296 x
, item_last
* m_lineHeight
);
2298 x
+= col
->GetWidth();
2301 // Draw last vertical rule
2302 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2303 x
, item_last
* m_lineHeight
);
2306 // redraw the background for the items which are selected/current
2307 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2309 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2310 if (selected
|| item
== m_currentRow
)
2312 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2313 if (item
== m_currentRow
)
2314 flags
|= wxCONTROL_CURRENT
;
2316 flags
|= wxCONTROL_FOCUSED
;
2318 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2319 wxRendererNative::Get().DrawItemSelectionRect
2329 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2332 // TODO: last column for RTL support
2333 expander
= GetOwner()->GetColumn( 0 );
2334 GetOwner()->SetExpanderColumn(expander
);
2337 // redraw all cells for all rows which must be repainted and for all columns
2339 cell_rect
.x
= x_start
;
2340 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2341 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2343 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2344 wxDataViewRenderer
*cell
= col
->GetRenderer();
2345 cell_rect
.width
= col
->GetWidth();
2347 if (col
->IsHidden())
2348 continue; // skipt it!
2351 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2353 // get the cell value and set it into the renderer
2355 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2359 wxDataViewItem dataitem
= node
->GetItem();
2361 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2364 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2365 cell
->SetValue( value
);
2367 // update the y offset
2368 cell_rect
.y
= item
* m_lineHeight
;
2370 //Draw the expander here.
2371 int indent
= node
->GetIndentLevel();
2372 if( col
== expander
)
2374 //Calculate the indent first
2375 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2377 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2378 // change the cell_rect.x to the appropriate pos
2379 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2380 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2381 dc
.SetPen( m_penExpander
);
2382 dc
.SetBrush( wxNullBrush
);
2383 if( node
->HasChildren() )
2385 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2387 if (m_underMouse
== node
)
2389 flag
|= wxCONTROL_CURRENT
;
2391 if( node
->IsOpen() )
2392 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2394 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2398 // I am wandering whether we should draw dot lines between tree nodes
2400 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2403 //force the expander column to left-center align
2404 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2408 // cannot be bigger than allocated space
2409 wxSize size
= cell
->GetSize();
2410 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2411 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2412 // size.y = wxMin( size.y, cell_rect.height );
2413 size
.y
= cell_rect
.height
;
2415 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2416 int align
= cell
->GetAlignment();
2418 // horizontal alignment:
2419 item_rect
.x
= cell_rect
.x
;
2420 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2421 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2422 else if (align
& wxALIGN_RIGHT
)
2423 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2424 //else: wxALIGN_LEFT is the default
2426 // vertical alignment:
2427 item_rect
.y
= cell_rect
.y
;
2428 if (align
& wxALIGN_CENTER_VERTICAL
)
2429 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2430 else if (align
& wxALIGN_BOTTOM
)
2431 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2432 //else: wxALIGN_TOP is the default
2435 item_rect
.x
+= PADDING_RIGHTLEFT
;
2436 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2438 //Here we add the tree indent
2439 item_rect
.x
+= indent
;
2442 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2443 state
|= wxDATAVIEW_CELL_SELECTED
;
2445 // TODO: it would be much more efficient to create a clipping
2446 // region for the entire column being rendered (in the OnPaint
2447 // of wxDataViewMainWindow) instead of a single clip region for
2448 // each cell. However it would mean that each renderer should
2449 // respect the given wxRect's top & bottom coords, eventually
2450 // violating only the left & right coords - however the user can
2451 // make its own renderer and thus we cannot be sure of that.
2452 dc
.SetClippingRegion( item_rect
);
2453 cell
->Render( item_rect
, &dc
, state
);
2454 dc
.DestroyClippingRegion();
2457 cell_rect
.x
+= cell_rect
.width
;
2461 int wxDataViewMainWindow::GetCountPerPage() const
2463 wxSize size
= GetClientSize();
2464 return size
.y
/ m_lineHeight
;
2467 int wxDataViewMainWindow::GetEndOfLastCol() const
2471 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2473 const wxDataViewColumn
*c
=
2474 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2477 width
+= c
->GetWidth();
2482 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2486 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2488 return y
/ m_lineHeight
;
2491 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2493 wxSize client_size
= GetClientSize();
2494 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2495 &client_size
.x
, &client_size
.y
);
2497 //we should deal with the pixel here
2498 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2500 return wxMin( GetRowCount()-1, row
);
2503 unsigned int wxDataViewMainWindow::GetRowCount()
2505 if ( m_count
== -1 )
2507 m_count
= RecalculateCount();
2509 GetVirtualSize( &width
, &height
);
2510 height
= m_count
* m_lineHeight
;
2512 SetVirtualSize( width
, height
);
2517 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2524 void wxDataViewMainWindow::SelectAllRows( bool on
)
2531 m_selection
.Clear();
2532 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2533 m_selection
.Add( i
);
2538 unsigned int first_visible
= GetFirstVisibleRow();
2539 unsigned int last_visible
= GetLastVisibleRow();
2541 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2543 unsigned int row
= m_selection
[i
];
2544 if ((row
>= first_visible
) && (row
<= last_visible
))
2547 m_selection
.Clear();
2551 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2553 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2557 m_selection
.Add( row
);
2565 m_selection
.Remove( row
);
2571 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2575 unsigned int tmp
= from
;
2581 for (i
= from
; i
<= to
; i
++)
2583 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2586 m_selection
.Add( i
);
2591 m_selection
.Remove( i
);
2594 RefreshRows( from
, to
);
2597 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2599 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2601 int n
= aSelections
[i
];
2603 m_selection
.Add( n
);
2608 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2610 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2611 m_selection
.Add( row
);
2613 m_selection
.Remove( row
);
2617 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2619 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2622 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2624 wxWindow
*parent
= GetParent();
2625 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2627 le
.SetEventObject(parent
);
2628 le
.SetModel(GetOwner()->GetModel());
2631 parent
->GetEventHandler()->ProcessEvent(le
);
2634 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2636 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2637 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2639 wxSize client_size
= GetClientSize();
2640 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2641 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2642 if (intersect_rect
.width
> 0)
2643 Refresh( true, &intersect_rect
);
2646 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2650 unsigned int tmp
= to
;
2655 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2656 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2658 wxSize client_size
= GetClientSize();
2659 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2660 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2661 if (intersect_rect
.width
> 0)
2662 Refresh( true, &intersect_rect
);
2665 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2667 unsigned int count
= GetRowCount();
2668 if (firstRow
> count
)
2671 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2672 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2674 wxSize client_size
= GetClientSize();
2675 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2676 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2677 if (intersect_rect
.width
> 0)
2678 Refresh( true, &intersect_rect
);
2681 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2683 wxCHECK_RET( newCurrent
< GetRowCount(),
2684 _T("invalid item index in OnArrowChar()") );
2686 // if there is no selection, we cannot move it anywhere
2687 if (!HasCurrentRow())
2690 unsigned int oldCurrent
= m_currentRow
;
2692 // in single selection we just ignore Shift as we can't select several
2694 if ( event
.ShiftDown() && !IsSingleSel() )
2696 RefreshRow( oldCurrent
);
2698 ChangeCurrentRow( newCurrent
);
2700 // select all the items between the old and the new one
2701 if ( oldCurrent
> newCurrent
)
2703 newCurrent
= oldCurrent
;
2704 oldCurrent
= m_currentRow
;
2707 SelectRows( oldCurrent
, newCurrent
, true );
2708 if (oldCurrent
!=newCurrent
)
2709 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2713 RefreshRow( oldCurrent
);
2715 // all previously selected items are unselected unless ctrl is held
2716 if ( !event
.ControlDown() )
2717 SelectAllRows(false);
2719 ChangeCurrentRow( newCurrent
);
2721 if ( !event
.ControlDown() )
2723 SelectRow( m_currentRow
, true );
2724 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2727 RefreshRow( m_currentRow
);
2730 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2733 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2737 rect
.y
= m_lineHeight
* row
;
2738 rect
.width
= GetEndOfLastCol();
2739 rect
.height
= m_lineHeight
;
2744 class RowToItemJob
: public DoJob
2747 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2748 virtual ~RowToItemJob(){};
2750 virtual int operator() ( wxDataViewTreeNode
* node
)
2753 if( current
== static_cast<int>(row
))
2755 ret
= node
->GetItem() ;
2759 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2761 current
+= node
->GetSubTreeCount();
2766 //If the current has no child node, we can find the desired item of the row number directly.
2767 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2768 if( node
->GetNodes().GetCount() == 0)
2770 int index
= static_cast<int>(row
) - current
- 1;
2771 ret
= node
->GetChildren().Item( index
);
2778 virtual int operator() ( void * n
)
2781 if( current
== static_cast<int>(row
))
2783 ret
= wxDataViewItem( n
) ;
2788 wxDataViewItem
GetResult(){ return ret
; }
2795 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2797 RowToItemJob
job( row
, -2 );
2798 Walker( m_root
, job
);
2799 return job
.GetResult();
2802 class RowToTreeNodeJob
: public DoJob
2805 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2808 this->current
= current
;
2812 virtual ~RowToTreeNodeJob(){};
2814 virtual int operator() ( wxDataViewTreeNode
* node
)
2817 if( current
== static_cast<int>(row
))
2823 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2825 current
+= node
->GetSubTreeCount();
2831 //If the current has no child node, we can find the desired item of the row number directly.
2832 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2833 if( node
->GetNodes().GetCount() == 0)
2835 int index
= static_cast<int>(row
) - current
- 1;
2836 void * n
= node
->GetChildren().Item( index
);
2837 ret
= new wxDataViewTreeNode( parent
) ;
2838 ret
->SetItem( wxDataViewItem( n
));
2839 ret
->SetHasChildren(false);
2848 virtual int operator() ( void * n
)
2851 if( current
== static_cast<int>(row
))
2853 ret
= new wxDataViewTreeNode( parent
) ;
2854 ret
->SetItem( wxDataViewItem( n
));
2855 ret
->SetHasChildren(false);
2861 wxDataViewTreeNode
* GetResult(){ return ret
; }
2865 wxDataViewTreeNode
* ret
;
2866 wxDataViewTreeNode
* parent
;
2870 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2872 RowToTreeNodeJob
job( row
, -2, m_root
);
2873 Walker( m_root
, job
);
2874 return job
.GetResult();
2877 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2879 wxWindow
*parent
= GetParent();
2880 wxDataViewEvent
le(type
, parent
->GetId());
2882 le
.SetEventObject(parent
);
2883 le
.SetModel(GetOwner()->GetModel());
2886 parent
->GetEventHandler()->ProcessEvent(le
);
2890 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2892 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2895 if( node
->HasChildren())
2897 if( !node
->IsOpen())
2899 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2900 //Check if the user prevent expanding
2901 if( e
.GetSkipped() )
2905 //Here I build the children of current node
2906 if( node
->GetChildrenNumber() == 0 )
2909 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2913 //Send the expanded event
2914 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2918 SelectRow( row
, false );
2919 SelectRow( row
+ 1, true );
2920 ChangeCurrentRow( row
+ 1 );
2921 SendSelectionChangedEvent( GetItemByRow(row
+1));
2929 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2931 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2934 wxDataViewTreeNode
* nd
= node
;
2936 if( node
->HasChildren() && node
->IsOpen() )
2938 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2939 if( e
.GetSkipped() )
2944 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2948 node
= node
->GetParent();
2951 int parent
= GetRowByItem( node
->GetItem() ) ;
2954 SelectRow( row
, false);
2955 SelectRow(parent
, true );
2956 ChangeCurrentRow( parent
);
2957 SendSelectionChangedEvent( node
->GetItem() );
2961 if( !nd
->HasChildren())
2966 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2968 wxDataViewModel
* model
= GetOwner()->GetModel();
2972 //Compose the a parent-chain of the finding item
2974 list
.DeleteContents( true );
2975 wxDataViewItem
it( item
);
2978 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2979 list
.Insert( pItem
);
2980 it
= model
->GetParent( it
);
2983 //Find the item along the parent-chain.
2984 //This algorithm is designed to speed up the node-finding method
2985 wxDataViewTreeNode
* node
= m_root
;
2986 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
2988 if( node
->HasChildren() )
2990 if( node
->GetChildrenNumber() == 0 )
2993 BuildTreeHelper(model
, node
->GetItem(), node
);
2996 wxDataViewTreeNodes nodes
= node
->GetNodes();
2998 for (; i
< nodes
.GetCount(); i
++)
3000 if (nodes
[i
]->GetItem() == (**iter
))
3006 if (i
== nodes
.GetCount())
3015 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3017 wxDataViewColumn
*col
= NULL
;
3018 unsigned int cols
= GetOwner()->GetColumnCount();
3019 unsigned int colnum
= 0;
3020 unsigned int x_start
= 0;
3022 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3023 for (x_start
= 0; colnum
< cols
; colnum
++)
3025 col
= GetOwner()->GetColumn(colnum
);
3026 if (col
->IsHidden())
3027 continue; // skip it!
3029 unsigned int w
= col
->GetWidth();
3030 if (x_start
+w
>= (unsigned int)x
)
3037 item
= GetItemByRow( y
/m_lineHeight
);
3040 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3042 int row
= GetRowByItem(item
);
3043 int y
= row
*m_lineHeight
;
3044 int h
= m_lineHeight
;
3046 wxDataViewColumn
*col
= NULL
;
3047 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3049 col
= GetOwner()->GetColumn( i
);
3050 x
+= col
->GetWidth();
3051 if( GetOwner()->GetColumn(i
+1) == column
)
3054 int w
= col
->GetWidth();
3055 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3056 return wxRect(x
, y
, w
, h
);
3059 int wxDataViewMainWindow::RecalculateCount()
3061 return m_root
->GetSubTreeCount();
3064 class ItemToRowJob
: public DoJob
3067 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3068 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3069 virtual ~ItemToRowJob(){};
3071 //Maybe binary search will help to speed up this process
3072 virtual int operator() ( wxDataViewTreeNode
* node
)
3075 if( node
->GetItem() == item
)
3080 if( node
->GetItem() == **m_iter
)
3087 ret
+= node
->GetSubTreeCount();
3093 virtual int operator() ( void * n
)
3096 if( n
== item
.GetID() )
3100 //the row number is begin from zero
3101 int GetResult(){ return ret
-1 ; }
3103 ItemList::const_iterator m_iter
;
3104 wxDataViewItem item
;
3109 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3111 wxDataViewModel
* model
= GetOwner()->GetModel();
3118 //Compose the a parent-chain of the finding item
3120 wxDataViewItem
* pItem
= NULL
;
3121 list
.DeleteContents( true );
3122 wxDataViewItem
it( item
);
3125 pItem
= new wxDataViewItem( it
);
3126 list
.Insert( pItem
);
3127 it
= model
->GetParent( it
);
3129 pItem
= new wxDataViewItem( );
3130 list
.Insert( pItem
);
3132 ItemToRowJob
job( item
, list
.begin() );
3133 Walker(m_root
, job
);
3134 return job
.GetResult();
3137 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3139 if( !model
->IsContainer( item
) )
3142 wxDataViewItemArray children
;
3143 unsigned int num
= model
->GetChildren( item
, children
);
3144 unsigned int index
= 0;
3145 while( index
< num
)
3147 if( model
->IsContainer( children
[index
] ) )
3149 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3150 n
->SetItem(children
[index
]);
3151 n
->SetHasChildren( true ) ;
3156 node
->AddLeaf( children
[index
].GetID() );
3160 node
->SetSubTreeCount( num
);
3161 wxDataViewTreeNode
* n
= node
->GetParent();
3163 n
->ChangeSubTreeCount(num
);
3167 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3169 //First we define a invalid item to fetch the top-level elements
3170 wxDataViewItem item
;
3172 BuildTreeHelper( model
, item
, m_root
);
3176 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3178 if( node
->GetNodeNumber() != 0 )
3180 int len
= node
->GetNodeNumber();
3182 wxDataViewTreeNodes nodes
= node
->GetNodes();
3183 for( ; i
< len
; i
++ )
3185 DestroyTreeHelper(nodes
[i
]);
3191 void wxDataViewMainWindow::DestroyTree()
3193 DestroyTreeHelper(m_root
);
3194 m_root
->SetSubTreeCount(0);
3198 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3200 if (event
.GetKeyCode() == WXK_TAB
)
3202 wxNavigationKeyEvent nevent
;
3203 nevent
.SetWindowChange( event
.ControlDown() );
3204 nevent
.SetDirection( !event
.ShiftDown() );
3205 nevent
.SetEventObject( GetParent()->GetParent() );
3206 nevent
.SetCurrentFocus( m_parent
);
3207 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3211 // no item -> nothing to do
3212 if (!HasCurrentRow())
3218 // don't use m_linesPerPage directly as it might not be computed yet
3219 const int pageSize
= GetCountPerPage();
3220 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3222 switch ( event
.GetKeyCode() )
3226 if (m_currentRow
> 0)
3228 wxWindow
*parent
= GetParent();
3229 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3230 le
.SetItem( GetItemByRow(m_currentRow
) );
3231 le
.SetEventObject(parent
);
3232 le
.SetModel(GetOwner()->GetModel());
3234 parent
->GetEventHandler()->ProcessEvent(le
);
3239 if ( m_currentRow
> 0 )
3240 OnArrowChar( m_currentRow
- 1, event
);
3244 if ( m_currentRow
< GetRowCount() - 1 )
3245 OnArrowChar( m_currentRow
+ 1, event
);
3247 //Add the process for tree expanding/collapsing
3249 OnCollapsing(m_currentRow
);
3252 OnExpanding( m_currentRow
);
3256 OnArrowChar( GetRowCount() - 1, event
);
3261 OnArrowChar( 0, event
);
3266 int steps
= pageSize
- 1;
3267 int index
= m_currentRow
- steps
;
3271 OnArrowChar( index
, event
);
3277 int steps
= pageSize
- 1;
3278 unsigned int index
= m_currentRow
+ steps
;
3279 unsigned int count
= GetRowCount();
3280 if ( index
>= count
)
3283 OnArrowChar( index
, event
);
3292 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3294 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3296 // let the base handle mouse wheel events.
3301 int x
= event
.GetX();
3302 int y
= event
.GetY();
3303 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3304 wxDataViewColumn
*col
= NULL
;
3307 unsigned int cols
= GetOwner()->GetColumnCount();
3309 for (i
= 0; i
< cols
; i
++)
3311 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3313 continue; // skip it!
3315 if (x
< xpos
+ c
->GetWidth())
3320 xpos
+= c
->GetWidth();
3325 wxDataViewRenderer
*cell
= col
->GetRenderer();
3326 unsigned int current
= y
/ m_lineHeight
;
3327 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3329 // Unselect all if below the last row ?
3333 //Test whether the mouse is hovered on the tree item button
3335 if (GetOwner()->GetExpanderColumn() == col
)
3337 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3338 if( node
!=NULL
&& node
->HasChildren() )
3340 int indent
= node
->GetIndentLevel();
3341 indent
= GetOwner()->GetIndent()*indent
;
3342 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3343 if( rect
.Contains( x
, y
) )
3345 //So the mouse is over the expander
3347 if (m_underMouse
&& m_underMouse
!= node
)
3349 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3350 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3352 if (m_underMouse
!= node
)
3354 //wxLogMessage("Do the row: %d", current);
3355 RefreshRow(current
);
3357 m_underMouse
= node
;
3360 if (node
!=NULL
&& !node
->HasChildren())
3365 if (m_underMouse
!= NULL
)
3367 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3368 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3369 m_underMouse
= NULL
;
3373 wxDataViewModel
*model
= GetOwner()->GetModel();
3375 if (event
.Dragging())
3377 if (m_dragCount
== 0)
3379 // we have to report the raw, physical coords as we want to be
3380 // able to call HitTest(event.m_pointDrag) from the user code to
3381 // get the item being dragged
3382 m_dragStart
= event
.GetPosition();
3387 if (m_dragCount
!= 3)
3390 if (event
.LeftIsDown())
3392 // Notify cell about drag
3401 bool forceClick
= false;
3403 if (event
.ButtonDClick())
3405 m_renameTimer
->Stop();
3406 m_lastOnSame
= false;
3409 wxDataViewItem item
= GetItemByRow(current
);
3410 bool ignore_other_columns
=
3411 ((GetOwner()->GetExpanderColumn() != col
) &&
3412 (model
->IsContainer(item
)) &&
3413 (!model
->HasContainerColumns(item
)));
3415 if (event
.LeftDClick())
3417 if ( current
== m_lineLastClicked
)
3419 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3422 model
->GetValue( value
, item
, col
->GetModelColumn() );
3423 cell
->SetValue( value
);
3424 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3425 col
->GetWidth(), m_lineHeight
);
3426 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3431 wxWindow
*parent
= GetParent();
3432 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3434 le
.SetEventObject(parent
);
3435 le
.SetModel(GetOwner()->GetModel());
3437 parent
->GetEventHandler()->ProcessEvent(le
);
3443 // The first click was on another item, so don't interpret this as
3444 // a double click, but as a simple click instead
3451 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3453 // select single line
3454 SelectAllRows( false );
3455 SelectRow( m_lineSelectSingleOnUp
, true );
3458 //Process the event of user clicking the expander
3459 bool expander
= false;
3460 if (GetOwner()->GetExpanderColumn() == col
)
3462 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3463 if( node
!=NULL
&& node
->HasChildren() )
3465 int indent
= node
->GetIndentLevel();
3466 indent
= GetOwner()->GetIndent()*indent
;
3467 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3468 if( rect
.Contains( x
, y
) )
3471 if( node
->IsOpen() )
3472 OnCollapsing(current
);
3474 OnExpanding( current
);
3478 //If the user click the expander, we do not do editing even if the column with expander are editable
3479 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3481 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3482 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3484 m_renameTimer
->Start( 100, true );
3488 m_lastOnSame
= false;
3489 m_lineSelectSingleOnUp
= (unsigned int)-1;
3493 // This is necessary, because after a DnD operation in
3494 // from and to ourself, the up event is swallowed by the
3495 // DnD code. So on next non-up event (which means here and
3496 // now) m_lineSelectSingleOnUp should be reset.
3497 m_lineSelectSingleOnUp
= (unsigned int)-1;
3500 if (event
.RightDown())
3502 m_lineBeforeLastClicked
= m_lineLastClicked
;
3503 m_lineLastClicked
= current
;
3505 // If the item is already selected, do not update the selection.
3506 // Multi-selections should not be cleared if a selected item is clicked.
3507 if (!IsRowSelected(current
))
3509 SelectAllRows(false);
3510 ChangeCurrentRow(current
);
3511 SelectRow(m_currentRow
,true);
3512 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3515 // notify cell about right click
3518 // Allow generation of context menu event
3521 else if (event
.MiddleDown())
3523 // notify cell about middle click
3526 if (event
.LeftDown() || forceClick
)
3530 m_lineBeforeLastClicked
= m_lineLastClicked
;
3531 m_lineLastClicked
= current
;
3533 unsigned int oldCurrentRow
= m_currentRow
;
3534 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3536 bool cmdModifierDown
= event
.CmdDown();
3537 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3539 if ( IsSingleSel() || !IsRowSelected(current
) )
3541 SelectAllRows( false );
3542 ChangeCurrentRow(current
);
3543 SelectRow(m_currentRow
,true);
3544 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3546 else // multi sel & current is highlighted & no mod keys
3548 m_lineSelectSingleOnUp
= current
;
3549 ChangeCurrentRow(current
); // change focus
3552 else // multi sel & either ctrl or shift is down
3554 if (cmdModifierDown
)
3556 ChangeCurrentRow(current
);
3557 ReverseRowSelection(m_currentRow
);
3558 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3560 else if (event
.ShiftDown())
3562 ChangeCurrentRow(current
);
3564 unsigned int lineFrom
= oldCurrentRow
,
3567 if ( lineTo
< lineFrom
)
3570 lineFrom
= m_currentRow
;
3573 SelectRows(lineFrom
, lineTo
, true);
3574 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3576 else // !ctrl, !shift
3578 // test in the enclosing if should make it impossible
3579 wxFAIL_MSG( _T("how did we get here?") );
3583 if (m_currentRow
!= oldCurrentRow
)
3584 RefreshRow( oldCurrentRow
);
3586 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3588 // Update selection here...
3591 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3592 (current
== oldCurrentRow
)) && oldWasSelected
;
3596 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3600 if (HasCurrentRow())
3606 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3610 if (HasCurrentRow())
3616 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3618 if( m_selection
.GetCount() != 1 )
3619 return wxDataViewItem();
3621 return GetItemByRow( m_selection
.Item(0));
3624 //-----------------------------------------------------------------------------
3626 //-----------------------------------------------------------------------------
3627 WX_DEFINE_LIST(wxDataViewColumnList
)
3629 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3631 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3632 EVT_SIZE(wxDataViewCtrl::OnSize
)
3635 wxDataViewCtrl::~wxDataViewCtrl()
3638 GetModel()->RemoveNotifier( m_notifier
);
3641 void wxDataViewCtrl::Init()
3646 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3647 const wxPoint
& pos
, const wxSize
& size
,
3648 long style
, const wxValidator
& validator
)
3650 if (!wxControl::Create( parent
, id
, pos
, size
,
3651 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3654 SetInitialSize(size
);
3659 MacSetClipChildren( true ) ;
3662 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3664 if (HasFlag(wxDV_NO_HEADER
))
3665 m_headerArea
= NULL
;
3667 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3669 SetTargetWindow( m_clientArea
);
3671 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3673 sizer
->Add( m_headerArea
, 0, wxGROW
);
3674 sizer
->Add( m_clientArea
, 1, wxGROW
);
3681 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3685 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3688 // we need to process arrows ourselves for scrolling
3689 if ( nMsg
== WM_GETDLGCODE
)
3691 rc
|= DLGC_WANTARROWS
;
3699 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3701 // We need to override OnSize so that our scrolled
3702 // window a) does call Layout() to use sizers for
3703 // positioning the controls but b) does not query
3704 // the sizer for their size and use that for setting
3705 // the scrollable area as set that ourselves by
3706 // calling SetScrollbar() further down.
3713 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3715 if (!wxDataViewCtrlBase::AssociateModel( model
))
3718 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3720 model
->AddNotifier( m_notifier
);
3722 m_clientArea
->BuildTree(model
);
3724 m_clientArea
->UpdateDisplay();
3729 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3731 if (!wxDataViewCtrlBase::AppendColumn(col
))
3734 m_cols
.Append( col
);
3739 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3741 if (!wxDataViewCtrlBase::PrependColumn(col
))
3744 m_cols
.Insert( col
);
3749 void wxDataViewCtrl::OnColumnChange()
3752 m_headerArea
->UpdateDisplay();
3754 m_clientArea
->UpdateDisplay();
3757 void wxDataViewCtrl::DoSetExpanderColumn()
3759 m_clientArea
->UpdateDisplay();
3762 void wxDataViewCtrl::DoSetIndent()
3764 m_clientArea
->UpdateDisplay();
3767 unsigned int wxDataViewCtrl::GetColumnCount() const
3769 return m_cols
.GetCount();
3772 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3774 wxDataViewColumnList::const_iterator iter
;
3776 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3781 if ((*iter
)->IsHidden())
3788 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3790 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3801 bool wxDataViewCtrl::ClearColumns()
3808 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3810 int ret
= 0, dead
= 0;
3811 int len
= GetColumnCount();
3812 for (int i
=0; i
<len
; i
++)
3814 wxDataViewColumn
* col
= GetColumn(i
);
3815 if (col
->IsHidden())
3817 ret
+= col
->GetWidth();
3820 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3827 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3832 //Selection code with wxDataViewItem as parameters
3833 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3835 return m_clientArea
->GetSelection();
3838 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3841 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3842 int len
= selection
.GetCount();
3843 for( int i
= 0; i
< len
; i
++)
3845 unsigned int row
= selection
[i
];
3846 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3851 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3853 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3854 int len
= sel
.GetCount();
3855 for( int i
= 0; i
< len
; i
++ )
3857 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3859 selection
.Add( static_cast<unsigned int>(row
) );
3861 m_clientArea
->SetSelections( selection
);
3864 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3866 int row
= m_clientArea
->GetRowByItem( item
);
3869 //Unselect all rows before select another in the single select mode
3870 if (m_clientArea
->IsSingleSel())
3871 m_clientArea
->SelectAllRows(false);
3872 m_clientArea
->SelectRow(row
, true);
3876 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3878 int row
= m_clientArea
->GetRowByItem( item
);
3880 m_clientArea
->SelectRow(row
, false);
3883 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3885 int row
= m_clientArea
->GetRowByItem( item
);
3888 return m_clientArea
->IsRowSelected(row
);
3893 //Selection code with row number as parameter
3894 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3897 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3898 int len
= selection
.GetCount();
3899 for( int i
= 0; i
< len
; i
++)
3901 unsigned int row
= selection
[i
];
3907 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3909 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3910 int len
= sel
.GetCount();
3911 for( int i
= 0; i
< len
; i
++ )
3915 selection
.Add( static_cast<unsigned int>(row
) );
3917 m_clientArea
->SetSelections( selection
);
3920 void wxDataViewCtrl::Select( int row
)
3924 if (m_clientArea
->IsSingleSel())
3925 m_clientArea
->SelectAllRows(false);
3926 m_clientArea
->SelectRow( row
, true );
3930 void wxDataViewCtrl::Unselect( int row
)
3933 m_clientArea
->SelectRow(row
, false);
3936 bool wxDataViewCtrl::IsSelected( int row
) const
3939 return m_clientArea
->IsRowSelected(row
);
3943 void wxDataViewCtrl::SelectRange( int from
, int to
)
3946 for( int i
= from
; i
< to
; i
++ )
3948 m_clientArea
->Select(sel
);
3951 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3953 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3954 for( int i
= from
; i
< to
; i
++ )
3955 if( sel
.Index( i
) != wxNOT_FOUND
)
3957 m_clientArea
->SetSelections(sel
);
3960 void wxDataViewCtrl::SelectAll()
3962 m_clientArea
->SelectAllRows(true);
3965 void wxDataViewCtrl::UnselectAll()
3967 m_clientArea
->SelectAllRows(false);
3970 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3974 if( row
> (int) m_clientArea
->GetRowCount() )
3975 row
= m_clientArea
->GetRowCount();
3977 int first
= m_clientArea
->GetFirstVisibleRow();
3978 int last
= m_clientArea
->GetLastVisibleRow();
3980 m_clientArea
->ScrollTo( row
, column
);
3981 else if( row
> last
)
3982 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3984 m_clientArea
->ScrollTo( first
, column
);
3987 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3989 int row
= m_clientArea
->GetRowByItem(item
);
3992 if( column
== NULL
)
3993 EnsureVisible(row
, -1);
3997 int len
= GetColumnCount();
3998 for( int i
= 0; i
< len
; i
++ )
3999 if( GetColumn(i
) == column
)
4004 EnsureVisible( row
, col
);
4010 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4012 m_clientArea
->HitTest(point
, item
, column
);
4015 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4017 return m_clientArea
->GetItemRect(item
, column
);
4020 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4022 return m_clientArea
->GetItemByRow( row
);
4025 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4027 return m_clientArea
->GetRowByItem( item
);
4030 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4032 int row
= m_clientArea
->GetRowByItem( item
);
4034 m_clientArea
->Expand(row
);
4037 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4039 int row
= m_clientArea
->GetRowByItem( item
);
4041 m_clientArea
->Collapse(row
);
4045 // !wxUSE_GENERICDATAVIEWCTRL
4048 // wxUSE_DATAVIEWCTRL