1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Otto Wyss
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 cell padding on the top/bottom
60 static const int PADDING_TOPBOTTOM
= 1;
62 // the expander space margin
63 static const int EXPANDER_MARGIN
= 4;
65 //-----------------------------------------------------------------------------
66 // wxDataViewHeaderWindow
67 //-----------------------------------------------------------------------------
69 #define USE_NATIVE_HEADER_WINDOW 0
71 // NB: for some reason, this class must be dllexport'ed or we get warnings from
73 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
76 wxDataViewHeaderWindowBase()
79 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
80 const wxPoint
&pos
, const wxSize
&size
,
83 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
86 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
87 wxDataViewCtrl
*GetOwner() { return m_owner
; }
89 // called on column addition/removal
90 virtual void UpdateDisplay() { /* by default, do nothing */ }
92 // returns the n-th column
93 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
96 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
103 wxDataViewCtrl
*m_owner
;
105 // sends an event generated from the n-th wxDataViewColumn
106 void SendEvent(wxEventType type
, unsigned int n
);
109 // on wxMSW the header window (only that part however) can be made native!
110 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
112 #define COLUMN_WIDTH_OFFSET 2
113 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
115 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
119 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
121 const wxPoint
&pos
= wxDefaultPosition
,
122 const wxSize
&size
= wxDefaultSize
,
123 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
125 Create(parent
, id
, pos
, size
, name
);
128 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
,
130 const wxString
&name
);
132 ~wxDataViewHeaderWindowMSW();
134 // called when any column setting is changed and/or changed
136 virtual void UpdateDisplay();
138 // called when the main window gets scrolled
139 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
142 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
143 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
145 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
147 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
148 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
151 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
154 #else // !defined(__WXMSW__)
156 #define HEADER_WINDOW_HEIGHT 25
157 #define HEADER_HORIZ_BORDER 5
158 #define HEADER_VERT_BORDER 3
159 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
161 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
164 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
166 const wxPoint
&pos
= wxDefaultPosition
,
167 const wxSize
&size
= wxDefaultSize
,
168 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
171 Create(parent
, id
, pos
, size
, name
);
174 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
175 const wxPoint
&pos
, const wxSize
&size
,
176 const wxString
&name
);
178 ~wxGenericDataViewHeaderWindow()
180 delete m_resizeCursor
;
183 virtual void UpdateDisplay() { Refresh(); }
187 void OnPaint( wxPaintEvent
&event
);
188 void OnMouse( wxMouseEvent
&event
);
189 void OnSetFocus( wxFocusEvent
&event
);
194 // vars used for column resizing:
196 wxCursor
*m_resizeCursor
;
197 const wxCursor
*m_currentCursor
;
200 bool m_dirty
; // needs refresh?
201 int m_column
; // index of the column being resized
202 int m_currentX
; // divider line position in logical (unscrolled) coords
203 int m_minX
; // minimal position beyond which the divider line
204 // can't be dragged in logical coords
206 // the pen used to draw the current column width drag line
207 // when resizing the columsn
211 // internal utilities:
215 m_currentCursor
= (wxCursor
*) NULL
;
216 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
218 m_isDragging
= false;
221 m_column
= wxNOT_FOUND
;
225 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
226 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
230 void AdjustDC(wxDC
& dc
);
233 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
234 DECLARE_EVENT_TABLE()
237 #endif // defined(__WXMSW__)
239 //-----------------------------------------------------------------------------
240 // wxDataViewRenameTimer
241 //-----------------------------------------------------------------------------
243 class wxDataViewRenameTimer
: public wxTimer
246 wxDataViewMainWindow
*m_owner
;
249 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
253 //-----------------------------------------------------------------------------
254 // wxDataViewTreeNode
255 //-----------------------------------------------------------------------------
256 class wxDataViewTreeNode
;
257 WX_DEFINE_SORTED_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
258 WX_DEFINE_SORTED_ARRAY( void* , wxDataViewTreeLeaves
);
260 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
);
261 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
);
263 class wxDataViewTreeNode
266 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
267 :leaves( wxGenericTreeModelItemCmp
),
268 nodes(wxGenericTreeModelNodeCmp
)
269 { this->parent
= parent
;
277 //I don't know what I need to do in the destructure
278 ~wxDataViewTreeNode()
283 wxDataViewTreeNode
* GetParent() { return parent
; }
284 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
285 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
286 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
288 void AddNode( wxDataViewTreeNode
* node
)
291 leaves
.Add( node
->GetItem().GetID() );
293 void AddLeaf( void * leaf
) { leaves
.Add( leaf
); }
295 wxDataViewItem
& GetItem() { return item
; }
296 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
298 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
299 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
303 wxDataViewTreeNode
* node
= this;
304 while( node
->GetParent()->GetParent() != NULL
)
306 node
= node
->GetParent();
319 int len
= nodes
.GetCount();
321 for ( int i
= 0 ;i
< len
; i
++)
322 sum
+= nodes
[i
]->GetSubTreeCount();
324 sum
+= leaves
.GetCount();
327 ChangeSubTreeCount(-sum
);
333 ChangeSubTreeCount(sum
);
336 bool HasChildren() { return hasChildren
; }
337 void SetHasChildren( bool has
){ hasChildren
= has
; }
339 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
340 int GetSubTreeCount() { return subTreeCount
; }
341 void ChangeSubTreeCount( int num
)
347 parent
->ChangeSubTreeCount(num
);
352 wxDataViewTreeNodes nds
= nodes
;
353 wxDataViewTreeLeaves lvs
= leaves
;
357 int len
= nds
.GetCount();
361 for(i
= 0; i
< len
; i
++)
363 for(i
= 0; i
< len
; i
++)
367 len
= lvs
.GetCount();
370 for(int i
= 0; i
< len
; i
++)
376 wxDataViewTreeNode
* parent
;
377 wxDataViewTreeNodes nodes
;
378 wxDataViewTreeLeaves leaves
;
385 //Below is the compare stuff
386 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
387 static wxDataViewModel
* g_model
;
389 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
)
391 return g_model
->Compare( node1
->GetItem(), node2
->GetItem() );
394 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
)
396 return g_model
->Compare( id1
, id2
);
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 void SendModelEvent( wxEventType type
, const wxDataViewItem
& item
);
422 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
423 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
424 bool ItemChanged( const wxDataViewItem
&item
);
425 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
428 { g_model
= GetOwner()->GetModel(); m_root
->Resort(); UpdateDisplay(); }
430 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
431 wxDataViewCtrl
*GetOwner() { return m_owner
; }
432 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
434 void OnPaint( wxPaintEvent
&event
);
435 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
436 void OnChar( wxKeyEvent
&event
);
437 void OnMouse( wxMouseEvent
&event
);
438 void OnSetFocus( wxFocusEvent
&event
);
439 void OnKillFocus( wxFocusEvent
&event
);
441 void UpdateDisplay();
442 void RecalculateDisplay();
443 void OnInternalIdle();
445 void OnRenameTimer();
447 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
448 void ScrollTo( int rows
);
450 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
451 void ChangeCurrentRow( unsigned int row
);
453 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
454 bool IsEmpty() { return GetRowCount() == 0; }
456 int GetCountPerPage() const;
457 int GetEndOfLastCol() const;
458 unsigned int GetFirstVisibleRow() const;
459 //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
460 unsigned int GetLastVisibleRow();
461 unsigned int GetRowCount() ;
463 wxDataViewItem
GetSelection();
464 wxDataViewSelection
GetSelections(){ return m_selection
; }
465 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
466 void Select( const wxArrayInt
& aSelections
);
467 void SelectAllRows( bool on
);
468 void SelectRow( unsigned int row
, bool on
);
469 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
470 void ReverseRowSelection( unsigned int row
);
471 bool IsRowSelected( unsigned int row
);
473 void RefreshRow( unsigned int row
);
474 void RefreshRows( unsigned int from
, unsigned int to
);
475 void RefreshRowsAfter( unsigned int firstRow
);
477 // returns the colour to be used for drawing the rules
478 wxColour
GetRuleColour() const
480 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
483 //void EnsureVisible( unsigned int row );
484 wxRect
GetLineRect( unsigned int row
) const;
486 //Some useful functions for row and item mapping
487 wxDataViewItem
GetItemByRow( unsigned int row
);
488 int GetRowByItem( const wxDataViewItem
& item
);
490 //Methods for building the mapping tree
491 void BuildTree( wxDataViewModel
* model
);
494 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
495 //We did not need this temporarily
496 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
498 int RecalculateCount() ;
500 void OnExpanding( unsigned int row
);
501 void OnCollapsing( unsigned int row
);
503 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
506 wxDataViewCtrl
*m_owner
;
510 wxDataViewColumn
*m_currentCol
;
511 unsigned int m_currentRow
;
512 wxDataViewSelection m_selection
;
514 wxDataViewRenameTimer
*m_renameTimer
;
522 // for double click logic
523 unsigned int m_lineLastClicked
,
524 m_lineBeforeLastClicked
,
525 m_lineSelectSingleOnUp
;
527 // the pen used to draw horiz/vertical rules
530 // the pen used to draw the expander and the lines
533 //This is the tree structure of the model
534 wxDataViewTreeNode
* m_root
;
537 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
538 DECLARE_EVENT_TABLE()
541 // ---------------------------------------------------------
542 // wxGenericDataViewModelNotifier
543 // ---------------------------------------------------------
545 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
548 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
549 { m_mainWindow
= mainWindow
; }
551 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
552 { return m_mainWindow
->ItemAdded( parent
, item
); }
553 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
554 { return m_mainWindow
->ItemDeleted( parent
, item
); }
555 virtual bool ItemChanged( const wxDataViewItem
& item
)
556 { return m_mainWindow
->ItemChanged(item
); }
557 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
558 { return m_mainWindow
->ValueChanged( item
, col
); }
559 virtual bool Cleared()
560 { return m_mainWindow
->Cleared(); }
561 virtual void Resort()
562 { m_mainWindow
->Resort(); }
564 wxDataViewMainWindow
*m_mainWindow
;
567 // ---------------------------------------------------------
568 // wxDataViewRenderer
569 // ---------------------------------------------------------
571 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
573 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
574 wxDataViewCellMode mode
,
576 wxDataViewRendererBase( varianttype
, mode
, align
)
583 wxDataViewRenderer::~wxDataViewRenderer()
589 wxDC
*wxDataViewRenderer::GetDC()
593 if (GetOwner() == NULL
)
595 if (GetOwner()->GetOwner() == NULL
)
597 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
603 // ---------------------------------------------------------
604 // wxDataViewCustomRenderer
605 // ---------------------------------------------------------
607 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
609 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
610 wxDataViewCellMode mode
, int align
) :
611 wxDataViewRenderer( varianttype
, mode
, align
)
615 // ---------------------------------------------------------
616 // wxDataViewTextRenderer
617 // ---------------------------------------------------------
619 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
621 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
622 wxDataViewCellMode mode
, int align
) :
623 wxDataViewCustomRenderer( varianttype
, mode
, align
)
627 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
629 m_text
= value
.GetString();
634 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
639 bool wxDataViewTextRenderer::HasEditorCtrl()
644 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
645 wxRect labelRect
, const wxVariant
&value
)
647 return new wxTextCtrl( parent
, wxID_ANY
, value
,
648 wxPoint(labelRect
.x
,labelRect
.y
),
649 wxSize(labelRect
.width
,labelRect
.height
) );
652 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
654 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
655 value
= text
->GetValue();
659 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
661 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
662 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
663 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
664 view
->GetForegroundColour();
666 dc
->SetTextForeground(col
);
667 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
672 wxSize
wxDataViewTextRenderer::GetSize() const
674 const wxDataViewCtrl
*view
= GetView();
678 view
->GetTextExtent( m_text
, &x
, &y
);
679 return wxSize( x
, y
);
681 return wxSize(80,20);
684 // ---------------------------------------------------------
685 // wxDataViewBitmapRenderer
686 // ---------------------------------------------------------
688 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
690 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
691 wxDataViewCellMode mode
, int align
) :
692 wxDataViewCustomRenderer( varianttype
, mode
, align
)
696 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
698 if (value
.GetType() == wxT("wxBitmap"))
700 if (value
.GetType() == wxT("wxIcon"))
706 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
711 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
714 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
715 else if (m_icon
.Ok())
716 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
721 wxSize
wxDataViewBitmapRenderer::GetSize() const
724 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
725 else if (m_icon
.Ok())
726 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
728 return wxSize(16,16);
731 // ---------------------------------------------------------
732 // wxDataViewToggleRenderer
733 // ---------------------------------------------------------
735 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
737 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
738 wxDataViewCellMode mode
, int align
) :
739 wxDataViewCustomRenderer( varianttype
, mode
, align
)
744 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
746 m_toggle
= value
.GetBool();
751 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
756 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
758 // User wxRenderer here
761 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
763 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
768 flags
|= wxCONTROL_CHECKED
;
769 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
770 flags
|= wxCONTROL_DISABLED
;
772 wxRendererNative::Get().DrawCheckBox(
773 GetOwner()->GetOwner(),
781 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
782 wxDataViewModel
*model
,
783 const wxDataViewItem
& item
, unsigned int col
)
785 bool value
= !m_toggle
;
786 wxVariant variant
= value
;
787 model
->SetValue( variant
, item
, col
);
788 model
->ValueChanged( item
, col
);
792 wxSize
wxDataViewToggleRenderer::GetSize() const
794 return wxSize(20,20);
797 // ---------------------------------------------------------
798 // wxDataViewProgressRenderer
799 // ---------------------------------------------------------
801 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
803 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
804 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
805 wxDataViewCustomRenderer( varianttype
, mode
, align
)
811 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
815 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
817 m_value
= (long) value
;
819 if (m_value
< 0) m_value
= 0;
820 if (m_value
> 100) m_value
= 100;
825 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
827 value
= (long) m_value
;
831 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
833 double pct
= (double)m_value
/ 100.0;
835 bar
.width
= (int)(cell
.width
* pct
);
836 dc
->SetPen( *wxTRANSPARENT_PEN
);
837 dc
->SetBrush( *wxBLUE_BRUSH
);
838 dc
->DrawRectangle( bar
);
840 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
841 dc
->SetPen( *wxBLACK_PEN
);
842 dc
->DrawRectangle( cell
);
847 wxSize
wxDataViewProgressRenderer::GetSize() const
849 return wxSize(40,12);
852 // ---------------------------------------------------------
853 // wxDataViewDateRenderer
854 // ---------------------------------------------------------
856 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
858 #if wxUSE_DATE_RENDERER_POPUP
860 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
863 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
864 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
865 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
870 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
871 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
872 sizer
->Add( m_cal
, 1, wxGROW
);
877 void OnCalendar( wxCalendarEvent
&event
);
879 wxCalendarCtrl
*m_cal
;
880 wxDataViewModel
*m_model
;
882 const wxDataViewItem
& m_item
;
885 virtual void OnDismiss()
890 DECLARE_EVENT_TABLE()
893 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
894 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
897 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
899 wxDateTime date
= event
.GetDate();
900 wxVariant value
= date
;
901 m_model
->SetValue( value
, m_item
, m_col
);
902 m_model
->ValueChanged( m_item
, m_col
);
906 #endif // wxUSE_DATE_RENDERER_POPUP
908 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
910 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
911 wxDataViewCellMode mode
, int align
) :
912 wxDataViewCustomRenderer( varianttype
, mode
, align
)
916 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
918 m_date
= value
.GetDateTime();
923 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
929 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
931 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
932 wxString tmp
= m_date
.FormatDate();
933 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
938 wxSize
wxDataViewDateRenderer::GetSize() const
940 const wxDataViewCtrl
* view
= GetView();
941 wxString tmp
= m_date
.FormatDate();
943 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
944 return wxSize(x
,y
+d
);
947 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
948 const wxDataViewItem
& item
, unsigned int col
)
951 model
->GetValue( variant
, item
, col
);
952 wxDateTime value
= variant
.GetDateTime();
954 #if wxUSE_DATE_RENDERER_POPUP
955 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
956 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
957 wxPoint pos
= wxGetMousePosition();
960 popup
->Popup( popup
->m_cal
);
961 #else // !wxUSE_DATE_RENDERER_POPUP
962 wxMessageBox(value
.Format());
963 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
967 // ---------------------------------------------------------
969 // ---------------------------------------------------------
971 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
973 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
974 unsigned int model_column
,
975 int width
, wxAlignment align
, int flags
) :
976 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
982 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
985 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
986 unsigned int model_column
,
987 int width
, wxAlignment align
, int flags
) :
988 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
993 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
996 wxDataViewColumn::~wxDataViewColumn()
1000 void wxDataViewColumn::Init( int width
)
1003 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1007 void wxDataViewColumn::SetResizeable( bool resizeable
)
1010 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1012 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1015 void wxDataViewColumn::SetHidden( bool hidden
)
1018 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1020 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1022 // tell our owner to e.g. update its scrollbars:
1024 GetOwner()->OnColumnChange();
1027 void wxDataViewColumn::SetSortable( bool sortable
)
1030 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1032 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1034 // Update header button
1036 GetOwner()->OnColumnChange();
1039 void wxDataViewColumn::SetSortOrder( bool ascending
)
1041 m_ascending
= ascending
;
1043 // Update header button
1045 GetOwner()->OnColumnChange();
1048 bool wxDataViewColumn::IsSortOrderAscending() const
1053 void wxDataViewColumn::SetInternalWidth( int width
)
1057 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1058 if (m_owner
&& m_owner
->m_clientArea
)
1059 m_owner
->m_clientArea
->RecalculateDisplay();
1062 void wxDataViewColumn::SetWidth( int width
)
1064 m_owner
->m_headerArea
->UpdateDisplay();
1066 SetInternalWidth(width
);
1070 //-----------------------------------------------------------------------------
1071 // wxDataViewHeaderWindowBase
1072 //-----------------------------------------------------------------------------
1074 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1076 wxWindow
*parent
= GetParent();
1077 wxDataViewEvent
le(type
, parent
->GetId());
1079 le
.SetEventObject(parent
);
1081 le
.SetDataViewColumn(GetColumn(n
));
1082 le
.SetModel(GetOwner()->GetModel());
1084 // for events created by wxDataViewHeaderWindow the
1085 // row / value fields are not valid
1087 parent
->GetEventHandler()->ProcessEvent(le
);
1090 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1092 // implemented in msw/listctrl.cpp:
1093 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1095 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1097 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1098 const wxPoint
&pos
, const wxSize
&size
,
1099 const wxString
&name
)
1103 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1106 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1107 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1108 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1109 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1111 // create the native WC_HEADER window:
1112 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1113 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1114 m_hWnd
= CreateWindowEx(0,
1125 wxLogLastError(_T("CreateWindowEx"));
1129 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1130 // to call wxDataViewHeaderWindow::MSWOnNotify
1131 SubclassWin(m_hWnd
);
1133 // the following is required to get the default win's font for
1134 // header windows and must be done befor sending the HDM_LAYOUT msg
1141 // Retrieve the bounding rectangle of the parent window's
1142 // client area, and then request size and position values
1143 // from the header control.
1144 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1146 hdl
.prc
= &rcParent
;
1148 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1150 wxLogLastError(_T("SendMessage"));
1154 // Set the size, position, and visibility of the header control.
1155 SetWindowPos((HWND
)m_hWnd
,
1159 wp
.flags
| SWP_SHOWWINDOW
);
1161 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1162 // a wxBoxSizer and in order to avoid super-big header windows,
1163 // we need to set our height as fixed
1164 SetMinSize(wxSize(-1, wp
.cy
));
1165 SetMaxSize(wxSize(-1, wp
.cy
));
1170 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1175 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1177 // remove old columns
1178 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1179 Header_DeleteItem((HWND
)m_hWnd
, 0);
1181 // add the updated array of columns to the header control
1182 unsigned int cols
= GetOwner()->GetColumnCount();
1183 unsigned int added
= 0;
1184 wxDataViewModel
* model
= GetOwner()->GetModel();
1185 for (unsigned int i
= 0; i
< cols
; i
++)
1187 wxDataViewColumn
*col
= GetColumn( i
);
1188 if (col
->IsHidden())
1189 continue; // don't add it!
1192 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1193 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1194 hdi
.cxy
= col
->GetWidth();
1195 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1196 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1197 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1200 if(model
&& model
->GetSortingColumn() == i
)
1202 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1203 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1204 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1208 // lParam is reserved for application's use:
1209 // we store there the column index to use it later in MSWOnNotify
1210 // (since columns may have been hidden)
1211 hdi
.lParam
= (LPARAM
)i
;
1213 // the native wxMSW implementation of the header window
1214 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1215 // on the right: to correct this effect we make the column
1216 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1218 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1220 switch (col
->GetAlignment())
1223 hdi
.fmt
|= HDF_LEFT
;
1225 case wxALIGN_CENTER
:
1226 case wxALIGN_CENTER_HORIZONTAL
:
1227 hdi
.fmt
|= HDF_CENTER
;
1230 hdi
.fmt
|= HDF_RIGHT
;
1234 // such alignment is not allowed for the column header!
1238 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1239 (WPARAM
)added
, (LPARAM
)&hdi
);
1244 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1248 // NOTE: we don't just return nmHDR->iItem because when there are
1249 // hidden columns, nmHDR->iItem may be different from
1250 // nmHDR->pitem->lParam
1252 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1254 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1259 item
.mask
= HDI_LPARAM
;
1260 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1262 return (unsigned int)item
.lParam
;
1265 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1267 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1269 // is it a message from the header?
1270 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1271 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1273 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1274 switch ( nmhdr
->code
)
1276 case HDN_BEGINTRACK
:
1277 // user has started to resize a column:
1278 // do we need to veto it?
1279 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1287 // user has started to reorder a column
1290 case HDN_ITEMCHANGING
:
1291 if (nmHDR
->pitem
!= NULL
&&
1292 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1294 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1295 if (nmHDR
->pitem
->cxy
< minWidth
)
1297 // do not allow the user to resize this column under
1298 // its minimal width:
1304 case HDN_ITEMCHANGED
: // user is resizing a column
1305 case HDN_ENDTRACK
: // user has finished resizing a column
1306 case HDN_ENDDRAG
: // user has finished reordering a column
1308 // update the width of the modified column:
1309 if (nmHDR
->pitem
!= NULL
&&
1310 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1312 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1313 unsigned int w
= nmHDR
->pitem
->cxy
;
1314 wxDataViewColumn
*col
= GetColumn(idx
);
1316 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1317 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1318 w
-= COLUMN_WIDTH_OFFSET
;
1320 if (w
>= (unsigned)col
->GetMinWidth())
1321 col
->SetInternalWidth(w
);
1327 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1328 wxDataViewModel
* model
= GetOwner()->GetModel();
1330 if(nmHDR
->iButton
== 0)
1332 wxDataViewColumn
*col
= GetColumn(idx
);
1333 if(col
->IsSortable())
1335 if(model
&& model
->GetSortingColumn() == idx
)
1337 bool order
= col
->IsSortOrderAscending();
1338 col
->SetSortOrder(!order
);
1339 model
->SetSortOrderAscending(!order
);
1343 model
->SetSortingColumn(idx
);
1344 model
->SetSortOrderAscending(col
->IsSortOrderAscending());
1352 wxEventType evt
= nmHDR
->iButton
== 0 ?
1353 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1354 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1355 SendEvent(evt
, idx
);
1361 // NOTE: for some reason (i.e. for a bug in Windows)
1362 // the HDN_ITEMCLICK notification is not sent on
1363 // right clicks, so we need to handle NM_RCLICK
1366 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1367 if (column
!= wxNOT_FOUND
)
1370 item
.mask
= HDI_LPARAM
;
1371 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1373 // 'idx' may be different from 'column' if there are
1374 // hidden columns...
1375 unsigned int idx
= (unsigned int)item
.lParam
;
1376 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1382 case HDN_GETDISPINFOW
:
1383 // see wxListCtrl::MSWOnNotify for more info!
1386 case HDN_ITEMDBLCLICK
:
1388 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1389 int w
= GetOwner()->GetBestColumnWidth(idx
);
1391 // update the native control:
1393 ZeroMemory(&hd
, sizeof(hd
));
1394 hd
.mask
= HDI_WIDTH
;
1396 Header_SetItem(GetHwnd(),
1397 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1400 // update the wxDataViewColumn class:
1401 GetColumn(idx
)->SetInternalWidth(w
);
1406 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1412 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1413 const wxRect
*WXUNUSED(rect
))
1415 wxSize ourSz
= GetClientSize();
1416 wxSize ownerSz
= m_owner
->GetClientSize();
1418 // where should the (logical) origin of this window be placed?
1420 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1422 // put this window on top of our parent and
1423 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1424 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1428 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1429 int WXUNUSED(w
), int WXUNUSED(h
),
1432 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1433 // the wxDataViewCtrl window gets resized: the following dummy call
1434 // to ScrollWindow() is required in order to get this header window
1435 // correctly repainted when it's (horizontally) scrolled:
1440 #else // !defined(__WXMSW__)
1442 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1443 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1444 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1445 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1446 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1449 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1450 const wxPoint
&pos
, const wxSize
&size
,
1451 const wxString
&name
)
1455 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1458 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1459 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1460 SetOwnForegroundColour( attr
.colFg
);
1461 SetOwnBackgroundColour( attr
.colBg
);
1463 SetOwnFont( attr
.font
);
1465 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1466 // a wxBoxSizer and in order to avoid super-big header windows,
1467 // we need to set our height as fixed
1468 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1469 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1474 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1477 GetClientSize( &w
, &h
);
1479 wxAutoBufferedPaintDC
dc( this );
1481 dc
.SetBackground(GetBackgroundColour());
1485 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1488 m_owner
->GetViewStart( &x
, NULL
);
1490 // account for the horz scrollbar offset
1491 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1493 dc
.SetFont( GetFont() );
1495 unsigned int cols
= GetOwner()->GetColumnCount();
1498 for (i
= 0; i
< cols
; i
++)
1500 wxDataViewColumn
*col
= GetColumn( i
);
1501 if (col
->IsHidden())
1502 continue; // skip it!
1504 int cw
= col
->GetWidth();
1507 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1508 if (col
->IsSortable() && GetOwner()->GetModel()->GetSortingColumn() == i
)
1510 if (col
->IsSortOrderAscending())
1511 sortArrow
= wxHDR_SORT_ICON_UP
;
1513 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1516 wxRendererNative::Get().DrawHeaderButton
1520 wxRect(xpos
, 0, cw
, ch
-1),
1521 m_parent
->IsEnabled() ? 0
1522 : (int)wxCONTROL_DISABLED
,
1526 // align as required the column title:
1528 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1529 switch (col
->GetAlignment())
1532 x
+= HEADER_HORIZ_BORDER
;
1534 case wxALIGN_CENTER
:
1535 case wxALIGN_CENTER_HORIZONTAL
:
1536 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1539 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1543 // always center the title vertically:
1544 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1546 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1548 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1549 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1550 dc
.DrawText( col
->GetTitle(), x
, y
);
1551 dc
.DestroyClippingRegion();
1557 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1559 GetParent()->SetFocus();
1563 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1565 // we want to work with logical coords
1567 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1568 int y
= event
.GetY();
1572 // we don't draw the line beyond our window,
1573 // but we allow dragging it there
1575 GetClientSize( &w
, NULL
);
1576 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1579 // erase the line if it was drawn
1583 if (event
.ButtonUp())
1585 m_isDragging
= false;
1591 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1594 GetOwner()->Refresh();
1598 m_currentX
= wxMax(m_minX
+ 7, x
);
1600 // draw in the new location
1601 if (m_currentX
< w
) DrawCurrent();
1605 else // not dragging
1608 m_column
= wxNOT_FOUND
;
1610 bool hit_border
= false;
1612 // end of the current column
1615 // find the column where this event occured
1616 int countCol
= m_owner
->GetColumnCount();
1617 for (int column
= 0; column
< countCol
; column
++)
1619 wxDataViewColumn
*p
= GetColumn(column
);
1622 continue; // skip if not shown
1624 xpos
+= p
->GetWidth();
1626 if ((abs(x
-xpos
) < 3) && (y
< 22))
1634 // inside the column
1641 if (m_column
== wxNOT_FOUND
)
1644 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1645 if (event
.LeftDClick() && resizeable
)
1647 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1650 else if (event
.LeftDown() || event
.RightUp())
1652 if (hit_border
&& event
.LeftDown() && resizeable
)
1654 m_isDragging
= true;
1659 else // click on a column
1661 wxDataViewModel
* model
= GetOwner()->GetModel();
1662 wxEventType evt
= event
.LeftDown() ?
1663 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1664 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1665 SendEvent(evt
, m_column
);
1667 //Left click the header
1668 if(event
.LeftDown())
1670 wxDataViewColumn
*col
= GetColumn(m_column
);
1671 if(col
->IsSortable())
1673 unsigned int colnum
= model
->GetSortingColumn();
1674 if(model
&& static_cast<int>(colnum
) == m_column
)
1676 bool order
= col
->IsSortOrderAscending();
1677 col
->SetSortOrder(!order
);
1678 model
->SetSortOrderAscending(!order
);
1682 model
->SetSortingColumn(m_column
);
1683 model
->SetSortOrderAscending(col
->IsSortOrderAscending());
1689 //Send the column sorted event
1690 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1694 else if (event
.Moving())
1696 if (hit_border
&& resizeable
)
1697 m_currentCursor
= m_resizeCursor
;
1699 m_currentCursor
= wxSTANDARD_CURSOR
;
1701 SetCursor(*m_currentCursor
);
1706 void wxGenericDataViewHeaderWindow::DrawCurrent()
1708 int x1
= m_currentX
;
1710 ClientToScreen (&x1
, &y1
);
1712 int x2
= m_currentX
-1;
1714 ++x2
; // but why ????
1717 m_owner
->GetClientSize( NULL
, &y2
);
1718 m_owner
->ClientToScreen( &x2
, &y2
);
1721 dc
.SetLogicalFunction(wxINVERT
);
1722 dc
.SetPen(m_penCurrent
);
1723 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1725 dc
.DrawLine(x1
, y1
, x2
, y2
);
1728 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1732 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1733 m_owner
->GetViewStart( &x
, NULL
);
1735 // shift the DC origin to match the position of the main window horizontal
1736 // scrollbar: this allows us to always use logical coords
1737 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1740 #endif // defined(__WXMSW__)
1742 //-----------------------------------------------------------------------------
1743 // wxDataViewRenameTimer
1744 //-----------------------------------------------------------------------------
1746 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1751 void wxDataViewRenameTimer::Notify()
1753 m_owner
->OnRenameTimer();
1756 //-----------------------------------------------------------------------------
1757 // wxDataViewMainWindow
1758 //-----------------------------------------------------------------------------
1760 //The tree building helper, declared firstly
1761 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1763 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1765 if (row1
> row2
) return 1;
1766 if (row1
== row2
) return 0;
1771 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1773 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1774 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1775 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1776 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1777 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1778 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1781 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1782 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1783 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1784 m_selection( wxDataViewSelectionCmp
)
1789 m_lastOnSame
= false;
1790 m_renameTimer
= new wxDataViewRenameTimer( this );
1792 // TODO: user better initial values/nothing selected
1793 m_currentCol
= NULL
;
1796 // TODO: we need to calculate this smartly
1803 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1806 m_dragStart
= wxPoint(0,0);
1807 m_lineLastClicked
= (unsigned int) -1;
1808 m_lineBeforeLastClicked
= (unsigned int) -1;
1809 m_lineSelectSingleOnUp
= (unsigned int) -1;
1813 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1814 SetBackgroundColour( *wxWHITE
);
1816 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1818 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1819 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1820 //Some new added code to deal with the tree structure
1821 m_root
= new wxDataViewTreeNode( NULL
);
1822 m_root
->SetHasChildren(true);
1824 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1829 wxDataViewMainWindow::~wxDataViewMainWindow()
1832 delete m_renameTimer
;
1835 void wxDataViewMainWindow::OnRenameTimer()
1837 // We have to call this here because changes may just have
1838 // been made and no screen update taken place.
1843 unsigned int cols
= GetOwner()->GetColumnCount();
1845 for (i
= 0; i
< cols
; i
++)
1847 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1849 continue; // skip it!
1851 if (c
== m_currentCol
)
1853 xpos
+= c
->GetWidth();
1855 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1856 m_currentCol
->GetWidth(), m_lineHeight
);
1858 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1859 &labelRect
.x
, &labelRect
.y
);
1861 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1862 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1865 //------------------------------------------------------------------
1866 // Helper class for do operation on the tree node
1867 //------------------------------------------------------------------
1874 //The return value control how the tree-walker tranverse the tree
1875 // 0: Job done, stop tranverse and return
1876 // 1: Ignore the current node's subtree and continue
1877 // 2: Job not done, continue
1878 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1879 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1880 virtual int operator() ( void * n
) = 0;
1883 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1885 if( node
==NULL
|| !node
->HasChildren())
1888 switch( func( node
) )
1899 wxDataViewTreeNodes nodes
= node
->GetNodes();
1900 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1902 int len_nodes
= nodes
.GetCount();
1903 int len
= leaves
.GetCount();
1904 int i
= 0, nodes_i
= 0;
1906 for( ; i
< len
; i
++ )
1908 void * n
= leaves
[i
];
1909 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1911 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1914 if( Walker( nd
, func
) )
1933 void wxDataViewMainWindow::SendModelEvent( wxEventType type
, const wxDataViewItem
& item
)
1935 wxWindow
*parent
= GetParent();
1936 wxDataViewEvent
le(type
, parent
->GetId());
1938 le
.SetEventObject(parent
);
1939 le
.SetModel(GetOwner()->GetModel());
1942 parent
->GetEventHandler()->ProcessEvent(le
);
1945 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1947 g_model
= GetOwner()->GetModel();
1949 wxDataViewTreeNode
* node
;
1950 node
= FindNode(parent
);
1951 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, item
);
1956 node
->SetHasChildren( true );
1958 if( g_model
->IsContainer( item
) )
1960 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1961 newnode
->SetItem(item
);
1962 newnode
->SetHasChildren( true );
1963 node
->AddNode( newnode
);
1966 node
->AddLeaf( item
.GetID() );
1968 node
->ChangeSubTreeCount(1);
1976 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1978 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1979 const wxDataViewItem
& item
)
1981 g_model
= GetOwner()->GetModel();
1983 wxDataViewTreeNode
* node
;
1984 node
= FindNode(parent
);
1985 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, item
);
1987 if( node
== NULL
|| node
->GetChildren().Index( item
.GetID() ) == wxNOT_FOUND
)
1993 node
->GetChildren().Remove( item
.GetID() );
1994 if( GetOwner()->GetModel()->IsContainer( item
) )
1996 wxDataViewTreeNode
* n
= NULL
;
1997 wxDataViewTreeNodes nodes
= node
->GetNodes();
1998 int len
= nodes
.GetCount();
1999 for( int i
= 0 ; i
< len
; i
++)
2001 if( nodes
[i
]->GetItem() == item
)
2011 node
->GetNodes().Remove( n
);
2012 sub
-= n
->GetSubTreeCount();
2013 DestroyTreeHelper(n
);
2016 if( node
->GetChildrenNumber() == 0)
2017 node
->SetHasChildren( false );
2019 //Make the row number invalid and get a new valid one when user call GetRowCount
2021 node
->ChangeSubTreeCount(sub
);
2022 //Change the current row to the last row if the current exceed the max row number
2023 if( m_currentRow
> GetRowCount() )
2024 m_currentRow
= m_count
- 1;
2030 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2032 g_model
= GetOwner()->GetModel();
2035 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
,item
);
2040 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2042 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2043 /*#define MAX_VIRTUAL_WIDTH 100000
2045 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2046 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2047 Refresh( true, &rect );
2051 g_model
= GetOwner()->GetModel();
2055 wxWindow
*parent
= GetParent();
2056 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, parent
->GetId());
2057 le
.SetEventObject(parent
);
2058 le
.SetModel(GetOwner()->GetModel());
2061 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2062 parent
->GetEventHandler()->ProcessEvent(le
);
2067 bool wxDataViewMainWindow::Cleared()
2069 g_model
= GetOwner()->GetModel();
2074 wxWindow
*parent
= GetParent();
2075 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, parent
->GetId());
2076 le
.SetEventObject(parent
);
2077 le
.SetModel(GetOwner()->GetModel());
2078 parent
->GetEventHandler()->ProcessEvent(le
);
2083 void wxDataViewMainWindow::UpdateDisplay()
2088 void wxDataViewMainWindow::OnInternalIdle()
2090 wxWindow::OnInternalIdle();
2094 RecalculateDisplay();
2099 void wxDataViewMainWindow::RecalculateDisplay()
2101 wxDataViewModel
*model
= GetOwner()->GetModel();
2108 int width
= GetEndOfLastCol();
2109 int height
= GetRowCount() * m_lineHeight
;
2111 SetVirtualSize( width
, height
);
2112 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2117 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2119 wxWindow::ScrollWindow( dx
, dy
, rect
);
2121 if (GetOwner()->m_headerArea
)
2122 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2125 void wxDataViewMainWindow::ScrollTo( int rows
)
2128 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2129 int sc
= rows
*m_lineHeight
/y
;
2130 m_owner
->Scroll(0, sc
);
2133 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2135 wxDataViewModel
*model
= GetOwner()->GetModel();
2136 wxAutoBufferedPaintDC
dc( this );
2139 dc
.SetBackground(GetBackgroundColour());
2141 GetOwner()->PrepareDC( dc
);
2142 dc
.SetFont( GetFont() );
2144 wxRect update
= GetUpdateRegion().GetBox();
2145 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2147 // compute which items needs to be redrawn
2148 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2149 unsigned int item_count
=
2150 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2151 (int)(GetRowCount( )- item_start
) );
2152 unsigned int item_last
= item_start
+ item_count
;
2154 // compute which columns needs to be redrawn
2155 unsigned int cols
= GetOwner()->GetColumnCount();
2156 unsigned int col_start
= 0;
2157 unsigned int x_start
= 0;
2158 for (x_start
= 0; col_start
< cols
; col_start
++)
2160 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2161 if (col
->IsHidden())
2162 continue; // skip it!
2164 unsigned int w
= col
->GetWidth();
2165 if (x_start
+w
>= (unsigned int)update
.x
)
2171 unsigned int col_last
= col_start
;
2172 unsigned int x_last
= x_start
;
2173 for (; col_last
< cols
; col_last
++)
2175 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2176 if (col
->IsHidden())
2177 continue; // skip it!
2179 if (x_last
> (unsigned int)update
.GetRight())
2182 x_last
+= col
->GetWidth();
2185 // Draw horizontal rules if required
2186 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2188 dc
.SetPen(m_penRule
);
2189 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2191 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2193 int y
= i
* m_lineHeight
;
2194 dc
.DrawLine(x_start
, y
, x_last
, y
);
2198 // Draw vertical rules if required
2199 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2201 dc
.SetPen(m_penRule
);
2202 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2205 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2207 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2208 if (col
->IsHidden())
2209 continue; // skip it
2211 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2212 x
, item_last
* m_lineHeight
);
2214 x
+= col
->GetWidth();
2217 // Draw last vertical rule
2218 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2219 x
, item_last
* m_lineHeight
);
2222 // redraw the background for the items which are selected/current
2223 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2225 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2226 if (selected
|| item
== m_currentRow
)
2228 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2229 if (item
== m_currentRow
)
2230 flags
|= wxCONTROL_CURRENT
;
2232 flags
|= wxCONTROL_FOCUSED
;
2234 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2235 wxRendererNative::Get().DrawItemSelectionRect
2245 // redraw all cells for all rows which must be repainted and for all columns
2247 cell_rect
.x
= x_start
;
2248 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2249 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2251 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2252 wxDataViewRenderer
*cell
= col
->GetRenderer();
2253 cell_rect
.width
= col
->GetWidth();
2255 if (col
->IsHidden())
2256 continue; // skipt it!
2258 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2260 // get the cell value and set it into the renderer
2262 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2268 wxDataViewItem dataitem
= node
->GetItem();
2269 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2270 cell
->SetValue( value
);
2272 // update the y offset
2273 cell_rect
.y
= item
* m_lineHeight
;
2275 //Draw the expander here.
2276 int indent
= node
->GetIndentLevel();
2277 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
2279 //Calculate the indent first
2280 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2282 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2283 // change the cell_rect.x to the appropriate pos
2284 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2285 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2286 dc
.SetPen( m_penExpander
);
2287 dc
.SetBrush( wxNullBrush
);
2288 if( node
->HasChildren() )
2290 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2291 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2292 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2293 if( node
->IsOpen() )
2294 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2296 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2300 // I am wandering whether we should draw dot lines between tree nodes
2302 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2305 //force the expander column to left-center align
2306 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2310 // cannot be bigger than allocated space
2311 wxSize size
= cell
->GetSize();
2312 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2313 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2314 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2316 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2317 int align
= cell
->GetAlignment();
2319 // horizontal alignment:
2320 item_rect
.x
= cell_rect
.x
;
2321 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2322 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2323 else if (align
& wxALIGN_RIGHT
)
2324 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2325 //else: wxALIGN_LEFT is the default
2327 // vertical alignment:
2328 item_rect
.y
= cell_rect
.y
;
2329 if (align
& wxALIGN_CENTER_VERTICAL
)
2330 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2331 else if (align
& wxALIGN_BOTTOM
)
2332 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2333 //else: wxALIGN_TOP is the default
2336 item_rect
.x
+= PADDING_RIGHTLEFT
;
2337 item_rect
.y
+= PADDING_TOPBOTTOM
;
2338 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2339 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2341 //Here we add the tree indent
2342 item_rect
.x
+= indent
;
2345 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2346 state
|= wxDATAVIEW_CELL_SELECTED
;
2348 // TODO: it would be much more efficient to create a clipping
2349 // region for the entire column being rendered (in the OnPaint
2350 // of wxDataViewMainWindow) instead of a single clip region for
2351 // each cell. However it would mean that each renderer should
2352 // respect the given wxRect's top & bottom coords, eventually
2353 // violating only the left & right coords - however the user can
2354 // make its own renderer and thus we cannot be sure of that.
2355 dc
.SetClippingRegion( item_rect
);
2356 cell
->Render( item_rect
, &dc
, state
);
2357 dc
.DestroyClippingRegion();
2360 cell_rect
.x
+= cell_rect
.width
;
2364 int wxDataViewMainWindow::GetCountPerPage() const
2366 wxSize size
= GetClientSize();
2367 return size
.y
/ m_lineHeight
;
2370 int wxDataViewMainWindow::GetEndOfLastCol() const
2374 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2376 const wxDataViewColumn
*c
=
2377 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2380 width
+= c
->GetWidth();
2385 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2389 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2391 return y
/ m_lineHeight
;
2394 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2396 wxSize client_size
= GetClientSize();
2397 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2398 &client_size
.x
, &client_size
.y
);
2400 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2403 unsigned int wxDataViewMainWindow::GetRowCount()
2405 if ( m_count
== -1 )
2407 m_count
= RecalculateCount();
2409 GetVirtualSize( &width
, &height
);
2410 height
= m_count
* m_lineHeight
;
2412 SetVirtualSize( width
, height
);
2417 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2424 void wxDataViewMainWindow::SelectAllRows( bool on
)
2431 m_selection
.Clear();
2432 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2433 m_selection
.Add( i
);
2438 unsigned int first_visible
= GetFirstVisibleRow();
2439 unsigned int last_visible
= GetLastVisibleRow();
2441 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2443 unsigned int row
= m_selection
[i
];
2444 if ((row
>= first_visible
) && (row
<= last_visible
))
2447 m_selection
.Clear();
2451 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2453 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2457 m_selection
.Add( row
);
2465 m_selection
.Remove( row
);
2471 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2475 unsigned int tmp
= from
;
2481 for (i
= from
; i
<= to
; i
++)
2483 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2486 m_selection
.Add( i
);
2491 m_selection
.Remove( i
);
2494 RefreshRows( from
, to
);
2497 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2499 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2501 int n
= aSelections
[i
];
2503 m_selection
.Add( n
);
2508 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2510 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2511 m_selection
.Add( row
);
2513 m_selection
.Remove( row
);
2517 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2519 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2522 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2524 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2525 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2527 wxSize client_size
= GetClientSize();
2528 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2529 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2530 if (intersect_rect
.width
> 0)
2531 Refresh( true, &intersect_rect
);
2534 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2538 unsigned int tmp
= to
;
2543 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2544 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2546 wxSize client_size
= GetClientSize();
2547 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2548 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2549 if (intersect_rect
.width
> 0)
2550 Refresh( true, &intersect_rect
);
2553 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2555 unsigned int count
= GetRowCount();
2556 if (firstRow
> count
)
2559 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2560 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2562 wxSize client_size
= GetClientSize();
2563 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2564 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2565 if (intersect_rect
.width
> 0)
2566 Refresh( true, &intersect_rect
);
2569 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2571 wxCHECK_RET( newCurrent
< GetRowCount(),
2572 _T("invalid item index in OnArrowChar()") );
2574 // if there is no selection, we cannot move it anywhere
2575 if (!HasCurrentRow())
2578 unsigned int oldCurrent
= m_currentRow
;
2580 // in single selection we just ignore Shift as we can't select several
2582 if ( event
.ShiftDown() && !IsSingleSel() )
2584 RefreshRow( oldCurrent
);
2586 ChangeCurrentRow( newCurrent
);
2588 // select all the items between the old and the new one
2589 if ( oldCurrent
> newCurrent
)
2591 newCurrent
= oldCurrent
;
2592 oldCurrent
= m_currentRow
;
2595 SelectRows( oldCurrent
, newCurrent
, true );
2599 RefreshRow( oldCurrent
);
2601 // all previously selected items are unselected unless ctrl is held
2602 if ( !event
.ControlDown() )
2603 SelectAllRows(false);
2605 ChangeCurrentRow( newCurrent
);
2607 if ( !event
.ControlDown() )
2608 SelectRow( m_currentRow
, true );
2610 RefreshRow( m_currentRow
);
2613 //EnsureVisible( m_currentRow );
2616 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2620 rect
.y
= m_lineHeight
* row
;
2621 rect
.width
= GetEndOfLastCol();
2622 rect
.height
= m_lineHeight
;
2627 class RowToItemJob
: public DoJob
2630 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2631 virtual ~RowToItemJob(){};
2633 virtual int operator() ( wxDataViewTreeNode
* node
)
2636 if( current
== static_cast<int>(row
))
2638 ret
= node
->GetItem() ;
2642 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2644 current
+= node
->GetSubTreeCount();
2649 //If the current has no child node, we can find the desired item of the row number directly.
2650 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2651 if( node
->GetNodes().GetCount() == 0)
2653 int index
= static_cast<int>(row
) - current
- 1;
2654 ret
= node
->GetChildren().Item( index
);
2661 virtual int operator() ( void * n
)
2664 if( current
== static_cast<int>(row
))
2666 ret
= wxDataViewItem( n
) ;
2671 wxDataViewItem
GetResult(){ return ret
; }
2678 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2680 RowToItemJob
job( row
, -2 );
2681 Walker( m_root
, job
);
2682 return job
.GetResult();
2685 class RowToTreeNodeJob
: public DoJob
2688 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2691 this->current
= current
;
2695 virtual ~RowToTreeNodeJob(){};
2697 virtual int operator() ( wxDataViewTreeNode
* node
)
2700 if( current
== static_cast<int>(row
))
2706 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2708 current
+= node
->GetSubTreeCount();
2714 //If the current has no child node, we can find the desired item of the row number directly.
2715 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2716 if( node
->GetNodes().GetCount() == 0)
2718 int index
= static_cast<int>(row
) - current
- 1;
2719 void * n
= node
->GetChildren().Item( index
);
2720 ret
= new wxDataViewTreeNode( parent
) ;
2721 ret
->SetItem( wxDataViewItem( n
));
2722 ret
->SetHasChildren(false);
2731 virtual int operator() ( void * n
)
2734 if( current
== static_cast<int>(row
))
2736 ret
= new wxDataViewTreeNode( parent
) ;
2737 ret
->SetItem( wxDataViewItem( n
));
2738 ret
->SetHasChildren(false);
2744 wxDataViewTreeNode
* GetResult(){ return ret
; }
2748 wxDataViewTreeNode
* ret
;
2749 wxDataViewTreeNode
* parent
;
2753 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2755 RowToTreeNodeJob
job( row
, -2, m_root
);
2756 Walker( m_root
, job
);
2757 return job
.GetResult();
2760 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2762 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2765 if( node
->HasChildren())
2766 if( !node
->IsOpen())
2769 //Here I build the children of current node
2770 if( node
->GetChildrenNumber() == 0 )
2771 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2777 SelectRow( row
, false );
2778 SelectRow( row
+ 1, true );
2779 ChangeCurrentRow( row
+ 1 );
2786 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2788 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2791 wxDataViewTreeNode
* nd
= node
;
2793 if( node
->HasChildren() && node
->IsOpen() )
2798 //RefreshRows(row,GetLastVisibleRow());
2802 node
= node
->GetParent();
2805 int parent
= GetRowByItem( node
->GetItem() ) ;
2808 SelectRow( row
, false);
2809 SelectRow(parent
, true );
2810 ChangeCurrentRow( parent
);
2814 if( !nd
->HasChildren())
2819 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2821 wxDataViewModel
* model
= GetOwner()->GetModel();
2825 //Compose the a parent-chain of the finding item
2827 list
.DeleteContents( true );
2828 wxDataViewItem
it( item
);
2831 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2832 list
.Insert( pItem
);
2833 it
= model
->GetParent( it
);
2836 //Find the item along the parent-chain.
2837 //This algorithm is designed to speed up the node-finding method
2838 wxDataViewTreeNode
* node
= m_root
;
2839 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2841 if( node
->HasChildren() )
2843 if( node
->GetChildrenNumber() == 0 )
2844 BuildTreeHelper(model
, node
->GetItem(), node
);
2846 wxDataViewTreeNodes nodes
= node
->GetNodes();
2847 //The wxSortedArray search a node in binary search, so using Item() is more efficient
2848 wxDataViewTreeNode temp
;
2849 temp
.SetItem(*(n
->GetData()));
2850 int index
= nodes
.Index( &temp
);
2851 if( index
== wxNOT_FOUND
)
2853 node
= nodes
[index
];
2861 int wxDataViewMainWindow::RecalculateCount()
2863 return m_root
->GetSubTreeCount();
2866 class ItemToRowJob
: public DoJob
2869 ItemToRowJob(const wxDataViewItem
& item
, ItemList::Node
* node
)
2870 { this->item
= item
; ret
= -1 ; nd
= node
; }
2871 virtual ~ItemToRowJob(){};
2873 //Maybe binary search will help to speed up this process
2874 virtual int operator() ( wxDataViewTreeNode
* node
)
2877 if( node
->GetItem() == item
)
2882 if( nd
&& node
->GetItem() == *(nd
->GetData()))
2889 ret
+= node
->GetSubTreeCount();
2895 virtual int operator() ( void * n
)
2898 if( n
== item
.GetID() )
2902 //the row number is begin from zero
2903 int GetResult(){ return ret
-1 ; }
2905 ItemList::Node
* nd
;
2906 wxDataViewItem item
;
2911 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2913 wxDataViewModel
* model
= GetOwner()->GetModel();
2920 //Compose the a parent-chain of the finding item
2922 wxDataViewItem
* pItem
= NULL
;
2923 list
.DeleteContents( true );
2924 wxDataViewItem
it( item
);
2927 pItem
= new wxDataViewItem( it
);
2928 list
.Insert( pItem
);
2929 it
= model
->GetParent( it
);
2931 pItem
= new wxDataViewItem( );
2932 list
.Insert( pItem
);
2934 ItemToRowJob
job( item
, list
.GetFirst() );
2935 Walker(m_root
, job
);
2936 return job
.GetResult();
2939 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2941 if( !model
->IsContainer( item
) )
2944 wxDataViewItem i
= model
->GetFirstChild( item
);
2949 if( model
->IsContainer( i
) )
2951 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2953 n
->SetHasChildren( true ) ;
2958 node
->AddLeaf( i
.GetID() );
2960 i
= model
->GetNextSibling( i
);
2962 node
->SetSubTreeCount( num
);
2963 wxDataViewTreeNode
* n
= node
->GetParent();
2965 n
->ChangeSubTreeCount(num
);
2969 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2971 //First we define a invalid item to fetch the top-level elements
2972 wxDataViewItem item
;
2973 g_model
= GetOwner()->GetModel();
2974 BuildTreeHelper( model
, item
, m_root
);
2978 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2980 if( node
->GetNodeNumber() != 0 )
2982 int len
= node
->GetNodeNumber();
2984 wxDataViewTreeNodes nodes
= node
->GetNodes();
2985 for( ; i
< len
; i
++ )
2987 DestroyTreeHelper(nodes
[i
]);
2993 void wxDataViewMainWindow::DestroyTree()
2995 DestroyTreeHelper(m_root
);
2996 m_root
->SetSubTreeCount(0);
3000 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3002 if (event
.GetKeyCode() == WXK_TAB
)
3004 wxNavigationKeyEvent nevent
;
3005 nevent
.SetWindowChange( event
.ControlDown() );
3006 nevent
.SetDirection( !event
.ShiftDown() );
3007 nevent
.SetEventObject( GetParent()->GetParent() );
3008 nevent
.SetCurrentFocus( m_parent
);
3009 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3013 // no item -> nothing to do
3014 if (!HasCurrentRow())
3020 // don't use m_linesPerPage directly as it might not be computed yet
3021 const int pageSize
= GetCountPerPage();
3022 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3024 switch ( event
.GetKeyCode() )
3027 if ( m_currentRow
> 0 )
3028 OnArrowChar( m_currentRow
- 1, event
);
3032 if ( m_currentRow
< GetRowCount() - 1 )
3033 OnArrowChar( m_currentRow
+ 1, event
);
3035 //Add the process for tree expanding/collapsing
3037 OnCollapsing(m_currentRow
);
3040 OnExpanding( m_currentRow
);
3044 OnArrowChar( GetRowCount() - 1, event
);
3049 OnArrowChar( 0, event
);
3054 int steps
= pageSize
- 1;
3055 int index
= m_currentRow
- steps
;
3059 OnArrowChar( index
, event
);
3065 int steps
= pageSize
- 1;
3066 unsigned int index
= m_currentRow
+ steps
;
3067 unsigned int count
= GetRowCount();
3068 if ( index
>= count
)
3071 OnArrowChar( index
, event
);
3080 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3082 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3084 // let the base handle mouse wheel events.
3089 int x
= event
.GetX();
3090 int y
= event
.GetY();
3091 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3093 wxDataViewColumn
*col
= NULL
;
3096 unsigned int cols
= GetOwner()->GetColumnCount();
3098 for (i
= 0; i
< cols
; i
++)
3100 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3102 continue; // skip it!
3104 if (x
< xpos
+ c
->GetWidth())
3109 xpos
+= c
->GetWidth();
3113 wxDataViewRenderer
*cell
= col
->GetRenderer();
3115 unsigned int current
= y
/ m_lineHeight
;
3117 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3119 // Unselect all if below the last row ?
3123 wxDataViewModel
*model
= GetOwner()->GetModel();
3125 if (event
.Dragging())
3127 if (m_dragCount
== 0)
3129 // we have to report the raw, physical coords as we want to be
3130 // able to call HitTest(event.m_pointDrag) from the user code to
3131 // get the item being dragged
3132 m_dragStart
= event
.GetPosition();
3137 if (m_dragCount
!= 3)
3140 if (event
.LeftIsDown())
3142 // Notify cell about drag
3151 bool forceClick
= false;
3153 if (event
.ButtonDClick())
3155 m_renameTimer
->Stop();
3156 m_lastOnSame
= false;
3159 if (event
.LeftDClick())
3161 if ( current
== m_lineLastClicked
)
3163 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3165 wxDataViewItem item
= GetItemByRow(current
);
3167 model
->GetValue( value
, item
, col
->GetModelColumn() );
3168 cell
->SetValue( value
);
3169 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3170 col
->GetWidth(), m_lineHeight
);
3171 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3173 wxWindow
*parent
= GetParent();
3174 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3176 le
.SetEventObject(parent
);
3177 le
.SetColumn(col
->GetModelColumn());
3178 le
.SetDataViewColumn(col
);
3179 le
.SetModel(GetOwner()->GetModel());
3181 parent
->GetEventHandler()->ProcessEvent(le
);
3187 // The first click was on another item, so don't interpret this as
3188 // a double click, but as a simple click instead
3195 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3197 // select single line
3198 SelectAllRows( false );
3199 SelectRow( m_lineSelectSingleOnUp
, true );
3202 //Process the event of user clicking the expander
3203 bool expander
= false;
3204 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3205 if( node
!=NULL
&& node
->HasChildren() )
3207 int indent
= node
->GetIndentLevel();
3208 indent
= GetOwner()->GetIndent()*indent
;
3209 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3210 if( rect
.Contains( x
, y
) )
3213 if( node
->IsOpen() )
3214 OnCollapsing(current
);
3216 OnExpanding( current
);
3220 //If the user click the expander, we do not do editing even if the column with expander are editable
3221 if (m_lastOnSame
&& !expander
)
3223 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3224 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3226 m_renameTimer
->Start( 100, true );
3230 m_lastOnSame
= false;
3231 m_lineSelectSingleOnUp
= (unsigned int)-1;
3235 // This is necessary, because after a DnD operation in
3236 // from and to ourself, the up event is swallowed by the
3237 // DnD code. So on next non-up event (which means here and
3238 // now) m_lineSelectSingleOnUp should be reset.
3239 m_lineSelectSingleOnUp
= (unsigned int)-1;
3242 if (event
.RightDown())
3244 m_lineBeforeLastClicked
= m_lineLastClicked
;
3245 m_lineLastClicked
= current
;
3247 // If the item is already selected, do not update the selection.
3248 // Multi-selections should not be cleared if a selected item is clicked.
3249 if (!IsRowSelected(current
))
3251 SelectAllRows(false);
3252 ChangeCurrentRow(current
);
3253 SelectRow(m_currentRow
,true);
3256 // notify cell about right click
3259 // Allow generation of context menu event
3262 else if (event
.MiddleDown())
3264 // notify cell about middle click
3267 if (event
.LeftDown() || forceClick
)
3273 m_lineBeforeLastClicked
= m_lineLastClicked
;
3274 m_lineLastClicked
= current
;
3276 unsigned int oldCurrentRow
= m_currentRow
;
3277 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3279 bool cmdModifierDown
= event
.CmdDown();
3280 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3282 if ( IsSingleSel() || !IsRowSelected(current
) )
3284 SelectAllRows( false );
3286 ChangeCurrentRow(current
);
3288 SelectRow(m_currentRow
,true);
3290 else // multi sel & current is highlighted & no mod keys
3292 m_lineSelectSingleOnUp
= current
;
3293 ChangeCurrentRow(current
); // change focus
3296 else // multi sel & either ctrl or shift is down
3298 if (cmdModifierDown
)
3300 ChangeCurrentRow(current
);
3302 ReverseRowSelection(m_currentRow
);
3304 else if (event
.ShiftDown())
3306 ChangeCurrentRow(current
);
3308 unsigned int lineFrom
= oldCurrentRow
,
3311 if ( lineTo
< lineFrom
)
3314 lineFrom
= m_currentRow
;
3317 SelectRows(lineFrom
, lineTo
, true);
3319 else // !ctrl, !shift
3321 // test in the enclosing if should make it impossible
3322 wxFAIL_MSG( _T("how did we get here?") );
3326 if (m_currentRow
!= oldCurrentRow
)
3327 RefreshRow( oldCurrentRow
);
3329 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3331 // Update selection here...
3334 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3335 (current
== oldCurrentRow
)) && oldWasSelected
;
3339 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3343 if (HasCurrentRow())
3349 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3353 if (HasCurrentRow())
3359 wxDataViewItem
wxDataViewMainWindow::GetSelection()
3361 if( m_selection
.GetCount() != 1 )
3362 return wxDataViewItem();
3364 return GetItemByRow( m_selection
.Item(0));
3367 //-----------------------------------------------------------------------------
3369 //-----------------------------------------------------------------------------
3371 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3373 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3374 EVT_SIZE(wxDataViewCtrl::OnSize
)
3377 wxDataViewCtrl::~wxDataViewCtrl()
3380 GetModel()->RemoveNotifier( m_notifier
);
3383 void wxDataViewCtrl::Init()
3388 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3389 const wxPoint
& pos
, const wxSize
& size
,
3390 long style
, const wxValidator
& validator
)
3392 if (!wxControl::Create( parent
, id
, pos
, size
,
3393 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3399 MacSetClipChildren( true ) ;
3402 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3404 if (HasFlag(wxDV_NO_HEADER
))
3405 m_headerArea
= NULL
;
3407 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3409 SetTargetWindow( m_clientArea
);
3411 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3413 sizer
->Add( m_headerArea
, 0, wxGROW
);
3414 sizer
->Add( m_clientArea
, 1, wxGROW
);
3421 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3425 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3428 // we need to process arrows ourselves for scrolling
3429 if ( nMsg
== WM_GETDLGCODE
)
3431 rc
|= DLGC_WANTARROWS
;
3439 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3441 // We need to override OnSize so that our scrolled
3442 // window a) does call Layout() to use sizers for
3443 // positioning the controls but b) does not query
3444 // the sizer for their size and use that for setting
3445 // the scrollable area as set that ourselves by
3446 // calling SetScrollbar() further down.
3453 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3455 if (!wxDataViewCtrlBase::AssociateModel( model
))
3458 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3460 model
->AddNotifier( m_notifier
);
3462 m_clientArea
->BuildTree(model
);
3464 m_clientArea
->UpdateDisplay();
3469 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3471 if (!wxDataViewCtrlBase::AppendColumn(col
))
3478 void wxDataViewCtrl::OnColumnChange()
3481 m_headerArea
->UpdateDisplay();
3483 m_clientArea
->UpdateDisplay();
3486 void wxDataViewCtrl::DoSetExpanderColumn()
3488 m_clientArea
->UpdateDisplay();
3491 void wxDataViewCtrl::DoSetIndent()
3493 m_clientArea
->UpdateDisplay();
3496 //Selection code with wxDataViewItem as parameters
3497 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3500 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3501 int len
= selection
.GetCount();
3502 for( int i
= 0; i
< len
; i
++)
3504 unsigned int row
= selection
[i
];
3505 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3510 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3512 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3513 int len
= sel
.GetCount();
3514 for( int i
= 0; i
< len
; i
++ )
3516 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3518 selection
.Add( static_cast<unsigned int>(row
) );
3520 m_clientArea
->SetSelections( selection
);
3523 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3525 int row
= m_clientArea
->GetRowByItem( item
);
3527 m_clientArea
->SelectRow(row
, true);
3530 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3532 int row
= m_clientArea
->GetRowByItem( item
);
3534 m_clientArea
->SelectRow(row
, false);
3537 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3539 int row
= m_clientArea
->GetRowByItem( item
);
3542 return m_clientArea
->IsRowSelected(row
);
3547 //Selection code with row number as parameter
3548 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3551 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3552 int len
= selection
.GetCount();
3553 for( int i
= 0; i
< len
; i
++)
3555 unsigned int row
= selection
[i
];
3561 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3563 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3564 int len
= sel
.GetCount();
3565 for( int i
= 0; i
< len
; i
++ )
3569 selection
.Add( static_cast<unsigned int>(row
) );
3571 m_clientArea
->SetSelections( selection
);
3574 void wxDataViewCtrl::Select( int row
)
3577 m_clientArea
->SelectRow( row
, true );
3580 void wxDataViewCtrl::Unselect( int row
)
3583 m_clientArea
->SelectRow(row
, false);
3586 bool wxDataViewCtrl::IsSelected( int row
) const
3589 return m_clientArea
->IsRowSelected(row
);
3593 void wxDataViewCtrl::SelectRange( int from
, int to
)
3596 for( int i
= from
; i
< to
; i
++ )
3598 m_clientArea
->Select(sel
);
3601 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3603 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3604 for( int i
= from
; i
< to
; i
++ )
3605 if( sel
.Index( i
) != wxNOT_FOUND
)
3607 m_clientArea
->SetSelections(sel
);
3610 void wxDataViewCtrl::SelectAll()
3612 m_clientArea
->SelectAllRows(true);
3615 void wxDataViewCtrl::UnselectAll()
3617 m_clientArea
->SelectAllRows(false);
3620 void wxDataViewCtrl::EnsureVisible( int row
)
3622 m_clientArea
->ScrollTo( row
);
3625 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, wxDataViewColumn
*column
)
3627 int row
= m_clientArea
->GetRowByItem(item
);
3632 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3634 return m_clientArea
->GetItemByRow( row
);
3637 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3639 return m_clientArea
->GetRowByItem( item
);
3643 // !wxUSE_GENERICDATAVIEWCTRL
3646 // wxUSE_DATAVIEWCTRL