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 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
;
388 static unsigned int g_column
;
389 static bool g_asending
;
391 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
)
393 return g_model
->Compare( node1
->GetItem(), node2
->GetItem(), g_column
, g_asending
);
396 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
)
398 return g_model
->Compare( id1
, id2
, g_column
, g_asending
);
403 //-----------------------------------------------------------------------------
404 // wxDataViewMainWindow
405 //-----------------------------------------------------------------------------
407 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
409 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
410 WX_DEFINE_LIST(ItemList
);
412 class wxDataViewMainWindow
: public wxWindow
415 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
417 const wxPoint
&pos
= wxDefaultPosition
,
418 const wxSize
&size
= wxDefaultSize
,
419 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
420 virtual ~wxDataViewMainWindow();
422 // notifications from wxDataViewModel
423 void SendModelEvent( wxEventType type
, const wxDataViewItem
& item
);
424 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
425 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
426 bool ItemChanged( const wxDataViewItem
&item
);
427 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
438 g_model
= GetOwner()->GetModel();
439 g_column
= GetOwner()->GetSortingColumn();
440 wxDataViewColumn
* col
= GetOwner()->GetColumn(g_column
);
446 g_asending
= col
->IsSortOrderAscending();
449 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
450 wxDataViewCtrl
*GetOwner() { return m_owner
; }
451 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
453 void OnPaint( wxPaintEvent
&event
);
454 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
455 void OnChar( wxKeyEvent
&event
);
456 void OnMouse( wxMouseEvent
&event
);
457 void OnSetFocus( wxFocusEvent
&event
);
458 void OnKillFocus( wxFocusEvent
&event
);
460 void UpdateDisplay();
461 void RecalculateDisplay();
462 void OnInternalIdle();
464 void OnRenameTimer();
466 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
467 void ScrollTo( int rows
, int column
);
469 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
470 void ChangeCurrentRow( unsigned int row
);
472 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
473 bool IsEmpty() { return GetRowCount() == 0; }
475 int GetCountPerPage() const;
476 int GetEndOfLastCol() const;
477 unsigned int GetFirstVisibleRow() const;
478 //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
479 unsigned int GetLastVisibleRow();
480 unsigned int GetRowCount() ;
482 wxDataViewItem
GetSelection() const;
483 wxDataViewSelection
GetSelections(){ return m_selection
; }
484 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
485 void Select( const wxArrayInt
& aSelections
);
486 void SelectAllRows( bool on
);
487 void SelectRow( unsigned int row
, bool on
);
488 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
489 void ReverseRowSelection( unsigned int row
);
490 bool IsRowSelected( unsigned int row
);
492 void RefreshRow( unsigned int row
);
493 void RefreshRows( unsigned int from
, unsigned int to
);
494 void RefreshRowsAfter( unsigned int firstRow
);
496 // returns the colour to be used for drawing the rules
497 wxColour
GetRuleColour() const
499 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
502 //void EnsureVisible( unsigned int row );
503 wxRect
GetLineRect( unsigned int row
) const;
505 //Some useful functions for row and item mapping
506 wxDataViewItem
GetItemByRow( unsigned int row
) const;
507 int GetRowByItem( const wxDataViewItem
& item
);
509 //Methods for building the mapping tree
510 void BuildTree( wxDataViewModel
* model
);
512 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
513 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
515 void Expand( unsigned int row
) { OnExpanding( row
); }
516 void Collapse( unsigned int row
) { OnCollapsing( row
); }
518 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
519 //We did not need this temporarily
520 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
522 int RecalculateCount() ;
524 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
525 void OnExpanding( unsigned int row
);
526 void OnCollapsing( unsigned int row
);
528 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
531 wxDataViewCtrl
*m_owner
;
535 wxDataViewColumn
*m_currentCol
;
536 unsigned int m_currentRow
;
537 wxDataViewSelection m_selection
;
539 wxDataViewRenameTimer
*m_renameTimer
;
547 // for double click logic
548 unsigned int m_lineLastClicked
,
549 m_lineBeforeLastClicked
,
550 m_lineSelectSingleOnUp
;
552 // the pen used to draw horiz/vertical rules
555 // the pen used to draw the expander and the lines
558 //This is the tree structure of the model
559 wxDataViewTreeNode
* m_root
;
562 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
563 DECLARE_EVENT_TABLE()
566 // ---------------------------------------------------------
567 // wxGenericDataViewModelNotifier
568 // ---------------------------------------------------------
570 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
573 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
574 { m_mainWindow
= mainWindow
; }
576 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
577 { return m_mainWindow
->ItemAdded( parent
, item
); }
578 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
579 { return m_mainWindow
->ItemDeleted( parent
, item
); }
580 virtual bool ItemChanged( const wxDataViewItem
& item
)
581 { return m_mainWindow
->ItemChanged(item
); }
582 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
583 { return m_mainWindow
->ValueChanged( item
, col
); }
584 virtual bool Cleared()
585 { return m_mainWindow
->Cleared(); }
586 virtual void Resort()
587 { m_mainWindow
->Resort(); }
589 wxDataViewMainWindow
*m_mainWindow
;
592 // ---------------------------------------------------------
593 // wxDataViewRenderer
594 // ---------------------------------------------------------
596 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
598 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
599 wxDataViewCellMode mode
,
601 wxDataViewRendererBase( varianttype
, mode
, align
)
608 wxDataViewRenderer::~wxDataViewRenderer()
614 wxDC
*wxDataViewRenderer::GetDC()
618 if (GetOwner() == NULL
)
620 if (GetOwner()->GetOwner() == NULL
)
622 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
628 // ---------------------------------------------------------
629 // wxDataViewCustomRenderer
630 // ---------------------------------------------------------
632 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
634 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
635 wxDataViewCellMode mode
, int align
) :
636 wxDataViewRenderer( varianttype
, mode
, align
)
640 // ---------------------------------------------------------
641 // wxDataViewTextRenderer
642 // ---------------------------------------------------------
644 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
646 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
647 wxDataViewCellMode mode
, int align
) :
648 wxDataViewCustomRenderer( varianttype
, mode
, align
)
652 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
654 m_text
= value
.GetString();
659 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
664 bool wxDataViewTextRenderer::HasEditorCtrl()
669 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
670 wxRect labelRect
, const wxVariant
&value
)
672 return new wxTextCtrl( parent
, wxID_ANY
, value
,
673 wxPoint(labelRect
.x
,labelRect
.y
),
674 wxSize(labelRect
.width
,labelRect
.height
) );
677 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
679 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
680 value
= text
->GetValue();
684 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
686 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
687 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
688 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
689 view
->GetForegroundColour();
691 dc
->SetTextForeground(col
);
692 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
697 wxSize
wxDataViewTextRenderer::GetSize() const
699 const wxDataViewCtrl
*view
= GetView();
703 view
->GetTextExtent( m_text
, &x
, &y
);
704 return wxSize( x
, y
);
706 return wxSize(80,20);
709 // ---------------------------------------------------------
710 // wxDataViewBitmapRenderer
711 // ---------------------------------------------------------
713 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
715 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
716 wxDataViewCellMode mode
, int align
) :
717 wxDataViewCustomRenderer( varianttype
, mode
, align
)
721 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
723 if (value
.GetType() == wxT("wxBitmap"))
725 if (value
.GetType() == wxT("wxIcon"))
731 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
736 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
739 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
740 else if (m_icon
.Ok())
741 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
746 wxSize
wxDataViewBitmapRenderer::GetSize() const
749 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
750 else if (m_icon
.Ok())
751 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
753 return wxSize(16,16);
756 // ---------------------------------------------------------
757 // wxDataViewToggleRenderer
758 // ---------------------------------------------------------
760 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
762 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
763 wxDataViewCellMode mode
, int align
) :
764 wxDataViewCustomRenderer( varianttype
, mode
, align
)
769 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
771 m_toggle
= value
.GetBool();
776 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
781 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
783 // User wxRenderer here
786 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
788 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
793 flags
|= wxCONTROL_CHECKED
;
794 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
795 flags
|= wxCONTROL_DISABLED
;
797 wxRendererNative::Get().DrawCheckBox(
798 GetOwner()->GetOwner(),
806 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
807 wxDataViewModel
*model
,
808 const wxDataViewItem
& item
, unsigned int col
)
810 bool value
= !m_toggle
;
811 wxVariant variant
= value
;
812 model
->SetValue( variant
, item
, col
);
813 model
->ValueChanged( item
, col
);
817 wxSize
wxDataViewToggleRenderer::GetSize() const
819 return wxSize(20,20);
822 // ---------------------------------------------------------
823 // wxDataViewProgressRenderer
824 // ---------------------------------------------------------
826 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
828 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
829 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
830 wxDataViewCustomRenderer( varianttype
, mode
, align
)
836 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
840 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
842 m_value
= (long) value
;
844 if (m_value
< 0) m_value
= 0;
845 if (m_value
> 100) m_value
= 100;
850 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
852 value
= (long) m_value
;
856 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
858 double pct
= (double)m_value
/ 100.0;
860 bar
.width
= (int)(cell
.width
* pct
);
861 dc
->SetPen( *wxTRANSPARENT_PEN
);
862 dc
->SetBrush( *wxBLUE_BRUSH
);
863 dc
->DrawRectangle( bar
);
865 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
866 dc
->SetPen( *wxBLACK_PEN
);
867 dc
->DrawRectangle( cell
);
872 wxSize
wxDataViewProgressRenderer::GetSize() const
874 return wxSize(40,12);
877 // ---------------------------------------------------------
878 // wxDataViewDateRenderer
879 // ---------------------------------------------------------
881 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
883 #if wxUSE_DATE_RENDERER_POPUP
885 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
888 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
889 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
890 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
895 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
896 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
897 sizer
->Add( m_cal
, 1, wxGROW
);
902 void OnCalendar( wxCalendarEvent
&event
);
904 wxCalendarCtrl
*m_cal
;
905 wxDataViewModel
*m_model
;
907 const wxDataViewItem
& m_item
;
910 virtual void OnDismiss()
915 DECLARE_EVENT_TABLE()
918 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
919 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
922 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
924 wxDateTime date
= event
.GetDate();
925 wxVariant value
= date
;
926 m_model
->SetValue( value
, m_item
, m_col
);
927 m_model
->ValueChanged( m_item
, m_col
);
931 #endif // wxUSE_DATE_RENDERER_POPUP
933 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
935 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
936 wxDataViewCellMode mode
, int align
) :
937 wxDataViewCustomRenderer( varianttype
, mode
, align
)
941 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
943 m_date
= value
.GetDateTime();
948 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
954 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
956 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
957 wxString tmp
= m_date
.FormatDate();
958 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
963 wxSize
wxDataViewDateRenderer::GetSize() const
965 const wxDataViewCtrl
* view
= GetView();
966 wxString tmp
= m_date
.FormatDate();
968 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
969 return wxSize(x
,y
+d
);
972 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
973 const wxDataViewItem
& item
, unsigned int col
)
976 model
->GetValue( variant
, item
, col
);
977 wxDateTime value
= variant
.GetDateTime();
979 #if wxUSE_DATE_RENDERER_POPUP
980 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
981 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
982 wxPoint pos
= wxGetMousePosition();
985 popup
->Popup( popup
->m_cal
);
986 #else // !wxUSE_DATE_RENDERER_POPUP
987 wxMessageBox(value
.Format());
988 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
992 // ---------------------------------------------------------
994 // ---------------------------------------------------------
996 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
998 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
999 unsigned int model_column
,
1000 int width
, wxAlignment align
, int flags
) :
1001 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1003 SetAlignment(align
);
1007 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1010 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1011 unsigned int model_column
,
1012 int width
, wxAlignment align
, int flags
) :
1013 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1015 SetAlignment(align
);
1018 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1021 wxDataViewColumn::~wxDataViewColumn()
1025 void wxDataViewColumn::Init( int width
)
1028 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1032 void wxDataViewColumn::SetResizeable( bool resizeable
)
1035 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1037 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1040 void wxDataViewColumn::SetHidden( bool hidden
)
1043 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1045 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1047 // tell our owner to e.g. update its scrollbars:
1049 GetOwner()->OnColumnChange();
1052 void wxDataViewColumn::SetSortable( bool sortable
)
1055 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1057 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1059 // Update header button
1061 GetOwner()->OnColumnChange();
1064 void wxDataViewColumn::SetSortOrder( bool ascending
)
1066 m_ascending
= ascending
;
1068 // Update header button
1070 GetOwner()->OnColumnChange();
1073 bool wxDataViewColumn::IsSortOrderAscending() const
1078 void wxDataViewColumn::SetInternalWidth( int width
)
1082 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1083 if (m_owner
&& m_owner
->m_clientArea
)
1084 m_owner
->m_clientArea
->RecalculateDisplay();
1087 void wxDataViewColumn::SetWidth( int width
)
1089 m_owner
->m_headerArea
->UpdateDisplay();
1091 SetInternalWidth(width
);
1095 //-----------------------------------------------------------------------------
1096 // wxDataViewHeaderWindowBase
1097 //-----------------------------------------------------------------------------
1099 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1101 wxWindow
*parent
= GetParent();
1102 wxDataViewEvent
le(type
, parent
->GetId());
1104 le
.SetEventObject(parent
);
1106 le
.SetDataViewColumn(GetColumn(n
));
1107 le
.SetModel(GetOwner()->GetModel());
1109 // for events created by wxDataViewHeaderWindow the
1110 // row / value fields are not valid
1112 parent
->GetEventHandler()->ProcessEvent(le
);
1115 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1117 // implemented in msw/listctrl.cpp:
1118 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1120 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1122 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1123 const wxPoint
&pos
, const wxSize
&size
,
1124 const wxString
&name
)
1128 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1131 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1132 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1133 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1134 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1136 // create the native WC_HEADER window:
1137 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1138 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1139 m_hWnd
= CreateWindowEx(0,
1150 wxLogLastError(_T("CreateWindowEx"));
1154 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1155 // to call wxDataViewHeaderWindow::MSWOnNotify
1156 SubclassWin(m_hWnd
);
1158 // the following is required to get the default win's font for
1159 // header windows and must be done befor sending the HDM_LAYOUT msg
1166 // Retrieve the bounding rectangle of the parent window's
1167 // client area, and then request size and position values
1168 // from the header control.
1169 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1171 hdl
.prc
= &rcParent
;
1173 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1175 wxLogLastError(_T("SendMessage"));
1179 // Set the size, position, and visibility of the header control.
1180 SetWindowPos((HWND
)m_hWnd
,
1184 wp
.flags
| SWP_SHOWWINDOW
);
1186 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1187 // a wxBoxSizer and in order to avoid super-big header windows,
1188 // we need to set our height as fixed
1189 SetMinSize(wxSize(-1, wp
.cy
));
1190 SetMaxSize(wxSize(-1, wp
.cy
));
1195 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1200 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1202 // remove old columns
1203 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1204 Header_DeleteItem((HWND
)m_hWnd
, 0);
1206 // add the updated array of columns to the header control
1207 unsigned int cols
= GetOwner()->GetColumnCount();
1208 unsigned int added
= 0;
1209 wxDataViewModel
* model
= GetOwner()->GetModel();
1210 for (unsigned int i
= 0; i
< cols
; i
++)
1212 wxDataViewColumn
*col
= GetColumn( i
);
1213 if (col
->IsHidden())
1214 continue; // don't add it!
1217 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1218 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1219 hdi
.cxy
= col
->GetWidth();
1220 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1221 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1222 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1225 if(model
&& m_owner
->GetSortingColumn() == i
)
1227 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1228 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1229 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1233 // lParam is reserved for application's use:
1234 // we store there the column index to use it later in MSWOnNotify
1235 // (since columns may have been hidden)
1236 hdi
.lParam
= (LPARAM
)i
;
1238 // the native wxMSW implementation of the header window
1239 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1240 // on the right: to correct this effect we make the column
1241 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1243 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1245 switch (col
->GetAlignment())
1248 hdi
.fmt
|= HDF_LEFT
;
1250 case wxALIGN_CENTER
:
1251 case wxALIGN_CENTER_HORIZONTAL
:
1252 hdi
.fmt
|= HDF_CENTER
;
1255 hdi
.fmt
|= HDF_RIGHT
;
1259 // such alignment is not allowed for the column header!
1263 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1264 (WPARAM
)added
, (LPARAM
)&hdi
);
1269 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1273 // NOTE: we don't just return nmHDR->iItem because when there are
1274 // hidden columns, nmHDR->iItem may be different from
1275 // nmHDR->pitem->lParam
1277 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1279 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1284 item
.mask
= HDI_LPARAM
;
1285 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1287 return (unsigned int)item
.lParam
;
1290 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1292 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1294 // is it a message from the header?
1295 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1296 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1298 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1299 switch ( nmhdr
->code
)
1301 case HDN_BEGINTRACK
:
1302 // user has started to resize a column:
1303 // do we need to veto it?
1304 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1312 // user has started to reorder a column
1315 case HDN_ITEMCHANGING
:
1316 if (nmHDR
->pitem
!= NULL
&&
1317 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1319 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1320 if (nmHDR
->pitem
->cxy
< minWidth
)
1322 // do not allow the user to resize this column under
1323 // its minimal width:
1329 case HDN_ITEMCHANGED
: // user is resizing a column
1330 case HDN_ENDTRACK
: // user has finished resizing a column
1331 case HDN_ENDDRAG
: // user has finished reordering a column
1333 // update the width of the modified column:
1334 if (nmHDR
->pitem
!= NULL
&&
1335 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1337 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1338 unsigned int w
= nmHDR
->pitem
->cxy
;
1339 wxDataViewColumn
*col
= GetColumn(idx
);
1341 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1342 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1343 w
-= COLUMN_WIDTH_OFFSET
;
1345 if (w
>= (unsigned)col
->GetMinWidth())
1346 col
->SetInternalWidth(w
);
1352 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1353 wxDataViewModel
* model
= GetOwner()->GetModel();
1355 if(nmHDR
->iButton
== 0)
1357 wxDataViewColumn
*col
= GetColumn(idx
);
1358 if(col
->IsSortable())
1360 if(model
&& m_owner
->GetSortingColumn() == idx
)
1362 bool order
= col
->IsSortOrderAscending();
1363 col
->SetSortOrder(!order
);
1367 m_owner
->SetSortingColumn(idx
);
1375 wxEventType evt
= nmHDR
->iButton
== 0 ?
1376 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1377 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1378 SendEvent(evt
, idx
);
1384 // NOTE: for some reason (i.e. for a bug in Windows)
1385 // the HDN_ITEMCLICK notification is not sent on
1386 // right clicks, so we need to handle NM_RCLICK
1389 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1390 if (column
!= wxNOT_FOUND
)
1393 item
.mask
= HDI_LPARAM
;
1394 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1396 // 'idx' may be different from 'column' if there are
1397 // hidden columns...
1398 unsigned int idx
= (unsigned int)item
.lParam
;
1399 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1405 case HDN_GETDISPINFOW
:
1406 // see wxListCtrl::MSWOnNotify for more info!
1409 case HDN_ITEMDBLCLICK
:
1411 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1412 int w
= GetOwner()->GetBestColumnWidth(idx
);
1414 // update the native control:
1416 ZeroMemory(&hd
, sizeof(hd
));
1417 hd
.mask
= HDI_WIDTH
;
1419 Header_SetItem(GetHwnd(),
1420 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1423 // update the wxDataViewColumn class:
1424 GetColumn(idx
)->SetInternalWidth(w
);
1429 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1435 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1436 const wxRect
*WXUNUSED(rect
))
1438 wxSize ourSz
= GetClientSize();
1439 wxSize ownerSz
= m_owner
->GetClientSize();
1441 // where should the (logical) origin of this window be placed?
1443 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1445 // put this window on top of our parent and
1446 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1447 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1451 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1452 int WXUNUSED(w
), int WXUNUSED(h
),
1455 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1456 // the wxDataViewCtrl window gets resized: the following dummy call
1457 // to ScrollWindow() is required in order to get this header window
1458 // correctly repainted when it's (horizontally) scrolled:
1463 #else // !defined(__WXMSW__)
1465 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1466 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1467 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1468 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1469 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1472 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1473 const wxPoint
&pos
, const wxSize
&size
,
1474 const wxString
&name
)
1478 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1481 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1482 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1483 SetOwnForegroundColour( attr
.colFg
);
1484 SetOwnBackgroundColour( attr
.colBg
);
1486 SetOwnFont( attr
.font
);
1488 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1489 // a wxBoxSizer and in order to avoid super-big header windows,
1490 // we need to set our height as fixed
1491 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1492 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1497 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1500 GetClientSize( &w
, &h
);
1502 wxAutoBufferedPaintDC
dc( this );
1504 dc
.SetBackground(GetBackgroundColour());
1508 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1511 m_owner
->GetViewStart( &x
, NULL
);
1513 // account for the horz scrollbar offset
1514 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1516 dc
.SetFont( GetFont() );
1518 unsigned int cols
= GetOwner()->GetColumnCount();
1521 for (i
= 0; i
< cols
; i
++)
1523 wxDataViewColumn
*col
= GetColumn( i
);
1524 if (col
->IsHidden())
1525 continue; // skip it!
1527 int cw
= col
->GetWidth();
1530 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1531 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == i
)
1533 if (col
->IsSortOrderAscending())
1534 sortArrow
= wxHDR_SORT_ICON_UP
;
1536 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1539 wxRendererNative::Get().DrawHeaderButton
1543 wxRect(xpos
, 0, cw
, ch
-1),
1544 m_parent
->IsEnabled() ? 0
1545 : (int)wxCONTROL_DISABLED
,
1549 // align as required the column title:
1551 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1552 switch (col
->GetAlignment())
1555 x
+= HEADER_HORIZ_BORDER
;
1557 case wxALIGN_CENTER
:
1558 case wxALIGN_CENTER_HORIZONTAL
:
1559 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1562 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1566 // always center the title vertically:
1567 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1569 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1571 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1572 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1573 dc
.DrawText( col
->GetTitle(), x
, y
);
1574 dc
.DestroyClippingRegion();
1580 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1582 GetParent()->SetFocus();
1586 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1588 // we want to work with logical coords
1590 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1591 int y
= event
.GetY();
1595 // we don't draw the line beyond our window,
1596 // but we allow dragging it there
1598 GetClientSize( &w
, NULL
);
1599 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1602 // erase the line if it was drawn
1606 if (event
.ButtonUp())
1608 m_isDragging
= false;
1614 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1617 GetOwner()->Refresh();
1621 m_currentX
= wxMax(m_minX
+ 7, x
);
1623 // draw in the new location
1624 if (m_currentX
< w
) DrawCurrent();
1628 else // not dragging
1631 m_column
= wxNOT_FOUND
;
1633 bool hit_border
= false;
1635 // end of the current column
1638 // find the column where this event occured
1639 int countCol
= m_owner
->GetColumnCount();
1640 for (int column
= 0; column
< countCol
; column
++)
1642 wxDataViewColumn
*p
= GetColumn(column
);
1645 continue; // skip if not shown
1647 xpos
+= p
->GetWidth();
1649 if ((abs(x
-xpos
) < 3) && (y
< 22))
1657 // inside the column
1664 if (m_column
== wxNOT_FOUND
)
1667 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1668 if (event
.LeftDClick() && resizeable
)
1670 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1673 else if (event
.LeftDown() || event
.RightUp())
1675 if (hit_border
&& event
.LeftDown() && resizeable
)
1677 m_isDragging
= true;
1682 else // click on a column
1684 wxDataViewModel
* model
= GetOwner()->GetModel();
1685 wxEventType evt
= event
.LeftDown() ?
1686 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1687 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1688 SendEvent(evt
, m_column
);
1690 //Left click the header
1691 if(event
.LeftDown())
1693 wxDataViewColumn
*col
= GetColumn(m_column
);
1694 if(col
->IsSortable())
1696 unsigned int colnum
= m_owner
->GetSortingColumn();
1697 if(model
&& static_cast<int>(colnum
) == m_column
)
1699 bool order
= col
->IsSortOrderAscending();
1700 col
->SetSortOrder(!order
);
1704 m_owner
->SetSortingColumn(m_column
);
1710 //Send the column sorted event
1711 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1715 else if (event
.Moving())
1717 if (hit_border
&& resizeable
)
1718 m_currentCursor
= m_resizeCursor
;
1720 m_currentCursor
= wxSTANDARD_CURSOR
;
1722 SetCursor(*m_currentCursor
);
1727 void wxGenericDataViewHeaderWindow::DrawCurrent()
1729 int x1
= m_currentX
;
1731 ClientToScreen (&x1
, &y1
);
1733 int x2
= m_currentX
-1;
1735 ++x2
; // but why ????
1738 m_owner
->GetClientSize( NULL
, &y2
);
1739 m_owner
->ClientToScreen( &x2
, &y2
);
1742 dc
.SetLogicalFunction(wxINVERT
);
1743 dc
.SetPen(m_penCurrent
);
1744 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1746 dc
.DrawLine(x1
, y1
, x2
, y2
);
1749 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1753 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1754 m_owner
->GetViewStart( &x
, NULL
);
1756 // shift the DC origin to match the position of the main window horizontal
1757 // scrollbar: this allows us to always use logical coords
1758 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1761 #endif // defined(__WXMSW__)
1763 //-----------------------------------------------------------------------------
1764 // wxDataViewRenameTimer
1765 //-----------------------------------------------------------------------------
1767 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1772 void wxDataViewRenameTimer::Notify()
1774 m_owner
->OnRenameTimer();
1777 //-----------------------------------------------------------------------------
1778 // wxDataViewMainWindow
1779 //-----------------------------------------------------------------------------
1781 //The tree building helper, declared firstly
1782 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1784 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1786 if (row1
> row2
) return 1;
1787 if (row1
== row2
) return 0;
1792 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1794 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1795 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1796 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1797 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1798 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1799 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1802 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1803 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1804 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1805 m_selection( wxDataViewSelectionCmp
)
1810 m_lastOnSame
= false;
1811 m_renameTimer
= new wxDataViewRenameTimer( this );
1813 // TODO: user better initial values/nothing selected
1814 m_currentCol
= NULL
;
1817 // TODO: we need to calculate this smartly
1824 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1827 m_dragStart
= wxPoint(0,0);
1828 m_lineLastClicked
= (unsigned int) -1;
1829 m_lineBeforeLastClicked
= (unsigned int) -1;
1830 m_lineSelectSingleOnUp
= (unsigned int) -1;
1834 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1835 SetBackgroundColour( *wxWHITE
);
1837 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1839 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1840 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1841 //Some new added code to deal with the tree structure
1842 m_root
= new wxDataViewTreeNode( NULL
);
1843 m_root
->SetHasChildren(true);
1845 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1850 wxDataViewMainWindow::~wxDataViewMainWindow()
1853 delete m_renameTimer
;
1856 void wxDataViewMainWindow::OnRenameTimer()
1858 // We have to call this here because changes may just have
1859 // been made and no screen update taken place.
1864 unsigned int cols
= GetOwner()->GetColumnCount();
1866 for (i
= 0; i
< cols
; i
++)
1868 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1870 continue; // skip it!
1872 if (c
== m_currentCol
)
1874 xpos
+= c
->GetWidth();
1876 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1877 m_currentCol
->GetWidth(), m_lineHeight
);
1879 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1880 &labelRect
.x
, &labelRect
.y
);
1882 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1883 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1887 //------------------------------------------------------------------
1888 // Helper class for do operation on the tree node
1889 //------------------------------------------------------------------
1896 //The return value control how the tree-walker tranverse the tree
1897 // 0: Job done, stop tranverse and return
1898 // 1: Ignore the current node's subtree and continue
1899 // 2: Job not done, continue
1900 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1901 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1902 virtual int operator() ( void * n
) = 0;
1905 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1907 if( node
==NULL
|| !node
->HasChildren())
1910 switch( func( node
) )
1921 wxDataViewTreeNodes nodes
= node
->GetNodes();
1922 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1924 int len_nodes
= nodes
.GetCount();
1925 int len
= leaves
.GetCount();
1926 int i
= 0, nodes_i
= 0;
1928 for( ; i
< len
; i
++ )
1930 void * n
= leaves
[i
];
1931 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1933 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1936 if( Walker( nd
, func
) )
1955 void wxDataViewMainWindow::SendModelEvent( wxEventType type
, const wxDataViewItem
& item
)
1957 wxWindow
*parent
= GetParent();
1958 wxDataViewEvent
le(type
, parent
->GetId());
1960 le
.SetEventObject(parent
);
1961 le
.SetModel(GetOwner()->GetModel());
1964 parent
->GetEventHandler()->ProcessEvent(le
);
1967 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1971 wxDataViewTreeNode
* node
;
1972 node
= FindNode(parent
);
1973 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, item
);
1978 node
->SetHasChildren( true );
1980 if( g_model
->IsContainer( item
) )
1982 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1983 newnode
->SetItem(item
);
1984 newnode
->SetHasChildren( true );
1985 node
->AddNode( newnode
);
1988 node
->AddLeaf( item
.GetID() );
1990 node
->ChangeSubTreeCount(1);
1998 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2000 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2001 const wxDataViewItem
& item
)
2005 wxDataViewTreeNode
* node
;
2006 node
= FindNode(parent
);
2008 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, item
);
2009 if( node
== NULL
|| node
->GetChildren().Index( item
.GetID() ) == wxNOT_FOUND
)
2015 node
->GetChildren().Remove( item
.GetID() );
2016 //Manuplate selection
2017 if( m_selection
.GetCount() > 1 )
2019 int row
= m_selection
[0];
2020 m_selection
.Empty();
2021 m_selection
.Add(row
);
2024 if( GetOwner()->GetModel()->IsContainer( item
) )
2026 wxDataViewTreeNode
* n
= NULL
;
2027 wxDataViewTreeNodes nodes
= node
->GetNodes();
2028 int len
= nodes
.GetCount();
2029 for( int i
= 0 ; i
< len
; i
++)
2031 if( nodes
[i
]->GetItem() == item
)
2041 node
->GetNodes().Remove( n
);
2042 sub
-= n
->GetSubTreeCount();
2043 DestroyTreeHelper(n
);
2046 if( node
->GetChildrenNumber() == 0)
2047 node
->SetHasChildren( false );
2049 //Make the row number invalid and get a new valid one when user call GetRowCount
2051 node
->ChangeSubTreeCount(sub
);
2052 //Change the current row to the last row if the current exceed the max row number
2053 if( m_currentRow
> GetRowCount() )
2054 m_currentRow
= m_count
- 1;
2060 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2065 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
,item
);
2070 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2072 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2073 /*#define MAX_VIRTUAL_WIDTH 100000
2075 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2076 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2077 Refresh( true, &rect );
2085 wxWindow
*parent
= GetParent();
2086 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, parent
->GetId());
2087 le
.SetEventObject(parent
);
2088 le
.SetModel(GetOwner()->GetModel());
2091 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2092 parent
->GetEventHandler()->ProcessEvent(le
);
2097 bool wxDataViewMainWindow::Cleared()
2104 wxWindow
*parent
= GetParent();
2105 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, parent
->GetId());
2106 le
.SetEventObject(parent
);
2107 le
.SetModel(GetOwner()->GetModel());
2108 parent
->GetEventHandler()->ProcessEvent(le
);
2113 void wxDataViewMainWindow::UpdateDisplay()
2118 void wxDataViewMainWindow::OnInternalIdle()
2120 wxWindow::OnInternalIdle();
2124 RecalculateDisplay();
2129 void wxDataViewMainWindow::RecalculateDisplay()
2131 wxDataViewModel
*model
= GetOwner()->GetModel();
2138 int width
= GetEndOfLastCol();
2139 int height
= GetRowCount() * m_lineHeight
;
2141 SetVirtualSize( width
, height
);
2142 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2147 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2149 wxWindow::ScrollWindow( dx
, dy
, rect
);
2151 if (GetOwner()->m_headerArea
)
2152 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2155 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2158 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2159 int sy
= rows
*m_lineHeight
/y
;
2163 wxRect rect
= GetClientRect();
2164 unsigned int colnum
= 0;
2165 unsigned int x_start
= 0, x_end
= 0, w
= 0;
2167 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2168 for (x_start
= 0; colnum
< column
; colnum
++)
2170 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2171 if (col
->IsHidden())
2172 continue; // skip it!
2174 w
= col
->GetWidth();
2178 x_end
= x_start
+ w
;
2179 xe
= xx
+ rect
.width
;
2182 sx
= ( xx
+ x_end
- xe
)/x
;
2189 m_owner
->Scroll( sx
, sy
);
2192 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2194 wxDataViewModel
*model
= GetOwner()->GetModel();
2195 wxAutoBufferedPaintDC
dc( this );
2198 dc
.SetBackground(GetBackgroundColour());
2200 GetOwner()->PrepareDC( dc
);
2201 dc
.SetFont( GetFont() );
2203 wxRect update
= GetUpdateRegion().GetBox();
2204 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2206 // compute which items needs to be redrawn
2207 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2208 unsigned int item_count
=
2209 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2210 (int)(GetRowCount( )- item_start
) );
2211 unsigned int item_last
= item_start
+ item_count
;
2213 // compute which columns needs to be redrawn
2214 unsigned int cols
= GetOwner()->GetColumnCount();
2215 unsigned int col_start
= 0;
2216 unsigned int x_start
= 0;
2217 for (x_start
= 0; col_start
< cols
; col_start
++)
2219 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2220 if (col
->IsHidden())
2221 continue; // skip it!
2223 unsigned int w
= col
->GetWidth();
2224 if (x_start
+w
>= (unsigned int)update
.x
)
2230 unsigned int col_last
= col_start
;
2231 unsigned int x_last
= x_start
;
2232 for (; col_last
< cols
; col_last
++)
2234 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2235 if (col
->IsHidden())
2236 continue; // skip it!
2238 if (x_last
> (unsigned int)update
.GetRight())
2241 x_last
+= col
->GetWidth();
2244 // Draw horizontal rules if required
2245 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2247 dc
.SetPen(m_penRule
);
2248 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2250 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2252 int y
= i
* m_lineHeight
;
2253 dc
.DrawLine(x_start
, y
, x_last
, y
);
2257 // Draw vertical rules if required
2258 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2260 dc
.SetPen(m_penRule
);
2261 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2264 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2266 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2267 if (col
->IsHidden())
2268 continue; // skip it
2270 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2271 x
, item_last
* m_lineHeight
);
2273 x
+= col
->GetWidth();
2276 // Draw last vertical rule
2277 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2278 x
, item_last
* m_lineHeight
);
2281 // redraw the background for the items which are selected/current
2282 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2284 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2285 if (selected
|| item
== m_currentRow
)
2287 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2288 if (item
== m_currentRow
)
2289 flags
|= wxCONTROL_CURRENT
;
2291 flags
|= wxCONTROL_FOCUSED
;
2293 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2294 wxRendererNative::Get().DrawItemSelectionRect
2304 // redraw all cells for all rows which must be repainted and for all columns
2306 cell_rect
.x
= x_start
;
2307 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2308 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2310 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2311 wxDataViewRenderer
*cell
= col
->GetRenderer();
2312 cell_rect
.width
= col
->GetWidth();
2314 if (col
->IsHidden())
2315 continue; // skipt it!
2317 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2319 // get the cell value and set it into the renderer
2321 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2327 wxDataViewItem dataitem
= node
->GetItem();
2328 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2329 cell
->SetValue( value
);
2331 // update the y offset
2332 cell_rect
.y
= item
* m_lineHeight
;
2334 //Draw the expander here.
2335 int indent
= node
->GetIndentLevel();
2336 if( col
== GetOwner()->GetExpanderColumn() )
2338 //Calculate the indent first
2339 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2341 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2342 // change the cell_rect.x to the appropriate pos
2343 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2344 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2345 dc
.SetPen( m_penExpander
);
2346 dc
.SetBrush( wxNullBrush
);
2347 if( node
->HasChildren() )
2349 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2350 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2351 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2352 if( node
->IsOpen() )
2353 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2355 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2359 // I am wandering whether we should draw dot lines between tree nodes
2361 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2364 //force the expander column to left-center align
2365 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2369 // cannot be bigger than allocated space
2370 wxSize size
= cell
->GetSize();
2371 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2372 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2373 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2375 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2376 int align
= cell
->GetAlignment();
2378 // horizontal alignment:
2379 item_rect
.x
= cell_rect
.x
;
2380 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2381 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2382 else if (align
& wxALIGN_RIGHT
)
2383 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2384 //else: wxALIGN_LEFT is the default
2386 // vertical alignment:
2387 item_rect
.y
= cell_rect
.y
;
2388 if (align
& wxALIGN_CENTER_VERTICAL
)
2389 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2390 else if (align
& wxALIGN_BOTTOM
)
2391 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2392 //else: wxALIGN_TOP is the default
2395 item_rect
.x
+= PADDING_RIGHTLEFT
;
2396 item_rect
.y
+= PADDING_TOPBOTTOM
;
2397 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2398 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2400 //Here we add the tree indent
2401 item_rect
.x
+= indent
;
2404 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2405 state
|= wxDATAVIEW_CELL_SELECTED
;
2407 // TODO: it would be much more efficient to create a clipping
2408 // region for the entire column being rendered (in the OnPaint
2409 // of wxDataViewMainWindow) instead of a single clip region for
2410 // each cell. However it would mean that each renderer should
2411 // respect the given wxRect's top & bottom coords, eventually
2412 // violating only the left & right coords - however the user can
2413 // make its own renderer and thus we cannot be sure of that.
2414 dc
.SetClippingRegion( item_rect
);
2415 cell
->Render( item_rect
, &dc
, state
);
2416 dc
.DestroyClippingRegion();
2419 cell_rect
.x
+= cell_rect
.width
;
2423 int wxDataViewMainWindow::GetCountPerPage() const
2425 wxSize size
= GetClientSize();
2426 return size
.y
/ m_lineHeight
;
2429 int wxDataViewMainWindow::GetEndOfLastCol() const
2433 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2435 const wxDataViewColumn
*c
=
2436 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2439 width
+= c
->GetWidth();
2444 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2448 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2450 return y
/ m_lineHeight
;
2453 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2455 wxSize client_size
= GetClientSize();
2456 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2457 &client_size
.x
, &client_size
.y
);
2459 //we should deal with the pixel here
2460 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2461 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2464 return wxMin( GetRowCount()-1, row
);
2467 unsigned int wxDataViewMainWindow::GetRowCount()
2469 if ( m_count
== -1 )
2471 m_count
= RecalculateCount();
2473 GetVirtualSize( &width
, &height
);
2474 height
= m_count
* m_lineHeight
;
2476 SetVirtualSize( width
, height
);
2481 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2488 void wxDataViewMainWindow::SelectAllRows( bool on
)
2495 m_selection
.Clear();
2496 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2497 m_selection
.Add( i
);
2502 unsigned int first_visible
= GetFirstVisibleRow();
2503 unsigned int last_visible
= GetLastVisibleRow();
2505 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2507 unsigned int row
= m_selection
[i
];
2508 if ((row
>= first_visible
) && (row
<= last_visible
))
2511 m_selection
.Clear();
2515 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2517 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2521 m_selection
.Add( row
);
2529 m_selection
.Remove( row
);
2535 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2539 unsigned int tmp
= from
;
2545 for (i
= from
; i
<= to
; i
++)
2547 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2550 m_selection
.Add( i
);
2555 m_selection
.Remove( i
);
2558 RefreshRows( from
, to
);
2561 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2563 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2565 int n
= aSelections
[i
];
2567 m_selection
.Add( n
);
2572 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2574 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2575 m_selection
.Add( row
);
2577 m_selection
.Remove( row
);
2581 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2583 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2586 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2588 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2589 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2591 wxSize client_size
= GetClientSize();
2592 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2593 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2594 if (intersect_rect
.width
> 0)
2595 Refresh( true, &intersect_rect
);
2598 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2602 unsigned int tmp
= to
;
2607 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2608 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2610 wxSize client_size
= GetClientSize();
2611 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2612 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2613 if (intersect_rect
.width
> 0)
2614 Refresh( true, &intersect_rect
);
2617 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2619 unsigned int count
= GetRowCount();
2620 if (firstRow
> count
)
2623 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2624 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2626 wxSize client_size
= GetClientSize();
2627 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2628 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2629 if (intersect_rect
.width
> 0)
2630 Refresh( true, &intersect_rect
);
2633 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2635 wxCHECK_RET( newCurrent
< GetRowCount(),
2636 _T("invalid item index in OnArrowChar()") );
2638 // if there is no selection, we cannot move it anywhere
2639 if (!HasCurrentRow())
2642 unsigned int oldCurrent
= m_currentRow
;
2644 // in single selection we just ignore Shift as we can't select several
2646 if ( event
.ShiftDown() && !IsSingleSel() )
2648 RefreshRow( oldCurrent
);
2650 ChangeCurrentRow( newCurrent
);
2652 // select all the items between the old and the new one
2653 if ( oldCurrent
> newCurrent
)
2655 newCurrent
= oldCurrent
;
2656 oldCurrent
= m_currentRow
;
2659 SelectRows( oldCurrent
, newCurrent
, true );
2663 RefreshRow( oldCurrent
);
2665 // all previously selected items are unselected unless ctrl is held
2666 if ( !event
.ControlDown() )
2667 SelectAllRows(false);
2669 ChangeCurrentRow( newCurrent
);
2671 if ( !event
.ControlDown() )
2672 SelectRow( m_currentRow
, true );
2674 RefreshRow( m_currentRow
);
2677 //EnsureVisible( m_currentRow );
2680 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2684 rect
.y
= m_lineHeight
* row
;
2685 rect
.width
= GetEndOfLastCol();
2686 rect
.height
= m_lineHeight
;
2691 class RowToItemJob
: public DoJob
2694 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2695 virtual ~RowToItemJob(){};
2697 virtual int operator() ( wxDataViewTreeNode
* node
)
2700 if( current
== static_cast<int>(row
))
2702 ret
= node
->GetItem() ;
2706 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2708 current
+= node
->GetSubTreeCount();
2713 //If the current has no child node, we can find the desired item of the row number directly.
2714 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2715 if( node
->GetNodes().GetCount() == 0)
2717 int index
= static_cast<int>(row
) - current
- 1;
2718 ret
= node
->GetChildren().Item( index
);
2725 virtual int operator() ( void * n
)
2728 if( current
== static_cast<int>(row
))
2730 ret
= wxDataViewItem( n
) ;
2735 wxDataViewItem
GetResult(){ return ret
; }
2742 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2744 RowToItemJob
job( row
, -2 );
2745 Walker( m_root
, job
);
2746 return job
.GetResult();
2749 class RowToTreeNodeJob
: public DoJob
2752 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2755 this->current
= current
;
2759 virtual ~RowToTreeNodeJob(){};
2761 virtual int operator() ( wxDataViewTreeNode
* node
)
2764 if( current
== static_cast<int>(row
))
2770 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2772 current
+= node
->GetSubTreeCount();
2778 //If the current has no child node, we can find the desired item of the row number directly.
2779 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2780 if( node
->GetNodes().GetCount() == 0)
2782 int index
= static_cast<int>(row
) - current
- 1;
2783 void * n
= node
->GetChildren().Item( index
);
2784 ret
= new wxDataViewTreeNode( parent
) ;
2785 ret
->SetItem( wxDataViewItem( n
));
2786 ret
->SetHasChildren(false);
2795 virtual int operator() ( void * n
)
2798 if( current
== static_cast<int>(row
))
2800 ret
= new wxDataViewTreeNode( parent
) ;
2801 ret
->SetItem( wxDataViewItem( n
));
2802 ret
->SetHasChildren(false);
2808 wxDataViewTreeNode
* GetResult(){ return ret
; }
2812 wxDataViewTreeNode
* ret
;
2813 wxDataViewTreeNode
* parent
;
2817 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2819 RowToTreeNodeJob
job( row
, -2, m_root
);
2820 Walker( m_root
, job
);
2821 return job
.GetResult();
2824 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2826 wxWindow
*parent
= GetParent();
2827 wxDataViewEvent
le(type
, parent
->GetId());
2829 le
.SetEventObject(parent
);
2830 le
.SetModel(GetOwner()->GetModel());
2833 parent
->GetEventHandler()->ProcessEvent(le
);
2837 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2839 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2842 if( node
->HasChildren())
2844 if( !node
->IsOpen())
2846 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2847 //Check if the user prevent expanding
2848 if( e
.GetSkipped() )
2852 //Here I build the children of current node
2853 if( node
->GetChildrenNumber() == 0 )
2854 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2857 //Send the expanded event
2858 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2862 SelectRow( row
, false );
2863 SelectRow( row
+ 1, true );
2864 ChangeCurrentRow( row
+ 1 );
2872 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2874 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2877 wxDataViewTreeNode
* nd
= node
;
2879 if( node
->HasChildren() && node
->IsOpen() )
2881 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2882 if( e
.GetSkipped() )
2887 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2891 node
= node
->GetParent();
2894 int parent
= GetRowByItem( node
->GetItem() ) ;
2897 SelectRow( row
, false);
2898 SelectRow(parent
, true );
2899 ChangeCurrentRow( parent
);
2903 if( !nd
->HasChildren())
2908 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2910 wxDataViewModel
* model
= GetOwner()->GetModel();
2914 //Compose the a parent-chain of the finding item
2916 list
.DeleteContents( true );
2917 wxDataViewItem
it( item
);
2920 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2921 list
.Insert( pItem
);
2922 it
= model
->GetParent( it
);
2925 //Find the item along the parent-chain.
2926 //This algorithm is designed to speed up the node-finding method
2927 wxDataViewTreeNode
* node
= m_root
;
2928 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2930 if( node
->HasChildren() )
2932 if( node
->GetChildrenNumber() == 0 )
2933 BuildTreeHelper(model
, node
->GetItem(), node
);
2935 wxDataViewTreeNodes nodes
= node
->GetNodes();
2936 //The wxSortedArray search a node in binary search, so using Item() is more efficient
2937 wxDataViewTreeNode temp
;
2938 temp
.SetItem(*(n
->GetData()));
2939 int index
= nodes
.Index( &temp
);
2940 if( index
== wxNOT_FOUND
)
2942 node
= nodes
[index
];
2950 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2952 wxDataViewColumn
*col
= NULL
;
2953 unsigned int cols
= GetOwner()->GetColumnCount();
2954 unsigned int colnum
= 0;
2955 unsigned int x_start
= 0;
2957 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2958 for (x_start
= 0; colnum
< cols
; colnum
++)
2960 col
= GetOwner()->GetColumn(colnum
);
2961 if (col
->IsHidden())
2962 continue; // skip it!
2964 unsigned int w
= col
->GetWidth();
2965 if (x_start
+w
>= (unsigned int)x
)
2972 item
= GetItemByRow( y
/m_lineHeight
);
2975 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
2977 int row
= GetRowByItem(item
);
2978 int y
= row
*m_lineHeight
;
2979 int h
= m_lineHeight
;
2981 wxDataViewColumn
*col
= NULL
;
2982 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
2984 col
= GetOwner()->GetColumn( i
);
2985 x
+= col
->GetWidth();
2986 if( GetOwner()->GetColumn(i
+1) == column
)
2989 int w
= col
->GetWidth();
2990 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
2991 return wxRect(x
, y
, w
, h
);
2994 int wxDataViewMainWindow::RecalculateCount()
2996 return m_root
->GetSubTreeCount();
2999 class ItemToRowJob
: public DoJob
3002 ItemToRowJob(const wxDataViewItem
& item
, ItemList::Node
* node
)
3003 { this->item
= item
; ret
= -1 ; nd
= node
; }
3004 virtual ~ItemToRowJob(){};
3006 //Maybe binary search will help to speed up this process
3007 virtual int operator() ( wxDataViewTreeNode
* node
)
3010 if( node
->GetItem() == item
)
3015 if( nd
&& node
->GetItem() == *(nd
->GetData()))
3022 ret
+= node
->GetSubTreeCount();
3028 virtual int operator() ( void * n
)
3031 if( n
== item
.GetID() )
3035 //the row number is begin from zero
3036 int GetResult(){ return ret
-1 ; }
3038 ItemList::Node
* nd
;
3039 wxDataViewItem item
;
3044 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3046 wxDataViewModel
* model
= GetOwner()->GetModel();
3053 //Compose the a parent-chain of the finding item
3055 wxDataViewItem
* pItem
= NULL
;
3056 list
.DeleteContents( true );
3057 wxDataViewItem
it( item
);
3060 pItem
= new wxDataViewItem( it
);
3061 list
.Insert( pItem
);
3062 it
= model
->GetParent( it
);
3064 pItem
= new wxDataViewItem( );
3065 list
.Insert( pItem
);
3067 ItemToRowJob
job( item
, list
.GetFirst() );
3068 Walker(m_root
, job
);
3069 return job
.GetResult();
3072 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3074 if( !model
->IsContainer( item
) )
3077 wxDataViewItem i
= model
->GetFirstChild( item
);
3082 if( model
->IsContainer( i
) )
3084 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3086 n
->SetHasChildren( true ) ;
3091 node
->AddLeaf( i
.GetID() );
3093 i
= model
->GetNextSibling( i
);
3095 node
->SetSubTreeCount( num
);
3096 wxDataViewTreeNode
* n
= node
->GetParent();
3098 n
->ChangeSubTreeCount(num
);
3102 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3104 //First we define a invalid item to fetch the top-level elements
3105 wxDataViewItem item
;
3107 BuildTreeHelper( model
, item
, m_root
);
3111 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3113 if( node
->GetNodeNumber() != 0 )
3115 int len
= node
->GetNodeNumber();
3117 wxDataViewTreeNodes nodes
= node
->GetNodes();
3118 for( ; i
< len
; i
++ )
3120 DestroyTreeHelper(nodes
[i
]);
3126 void wxDataViewMainWindow::DestroyTree()
3128 DestroyTreeHelper(m_root
);
3129 m_root
->SetSubTreeCount(0);
3133 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3135 if (event
.GetKeyCode() == WXK_TAB
)
3137 wxNavigationKeyEvent nevent
;
3138 nevent
.SetWindowChange( event
.ControlDown() );
3139 nevent
.SetDirection( !event
.ShiftDown() );
3140 nevent
.SetEventObject( GetParent()->GetParent() );
3141 nevent
.SetCurrentFocus( m_parent
);
3142 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3146 // no item -> nothing to do
3147 if (!HasCurrentRow())
3153 // don't use m_linesPerPage directly as it might not be computed yet
3154 const int pageSize
= GetCountPerPage();
3155 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3157 switch ( event
.GetKeyCode() )
3160 if ( m_currentRow
> 0 )
3161 OnArrowChar( m_currentRow
- 1, event
);
3165 if ( m_currentRow
< GetRowCount() - 1 )
3166 OnArrowChar( m_currentRow
+ 1, event
);
3168 //Add the process for tree expanding/collapsing
3170 OnCollapsing(m_currentRow
);
3173 OnExpanding( m_currentRow
);
3177 OnArrowChar( GetRowCount() - 1, event
);
3182 OnArrowChar( 0, event
);
3187 int steps
= pageSize
- 1;
3188 int index
= m_currentRow
- steps
;
3192 OnArrowChar( index
, event
);
3198 int steps
= pageSize
- 1;
3199 unsigned int index
= m_currentRow
+ steps
;
3200 unsigned int count
= GetRowCount();
3201 if ( index
>= count
)
3204 OnArrowChar( index
, event
);
3213 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3215 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3217 // let the base handle mouse wheel events.
3222 int x
= event
.GetX();
3223 int y
= event
.GetY();
3224 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3226 wxDataViewColumn
*col
= NULL
;
3229 unsigned int cols
= GetOwner()->GetColumnCount();
3231 for (i
= 0; i
< cols
; i
++)
3233 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3235 continue; // skip it!
3237 if (x
< xpos
+ c
->GetWidth())
3242 xpos
+= c
->GetWidth();
3246 wxDataViewRenderer
*cell
= col
->GetRenderer();
3248 unsigned int current
= y
/ m_lineHeight
;
3250 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3252 // Unselect all if below the last row ?
3256 wxDataViewModel
*model
= GetOwner()->GetModel();
3258 if (event
.Dragging())
3260 if (m_dragCount
== 0)
3262 // we have to report the raw, physical coords as we want to be
3263 // able to call HitTest(event.m_pointDrag) from the user code to
3264 // get the item being dragged
3265 m_dragStart
= event
.GetPosition();
3270 if (m_dragCount
!= 3)
3273 if (event
.LeftIsDown())
3275 // Notify cell about drag
3284 bool forceClick
= false;
3286 if (event
.ButtonDClick())
3288 m_renameTimer
->Stop();
3289 m_lastOnSame
= false;
3292 if (event
.LeftDClick())
3294 if ( current
== m_lineLastClicked
)
3296 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3298 wxDataViewItem item
= GetItemByRow(current
);
3300 model
->GetValue( value
, item
, col
->GetModelColumn() );
3301 cell
->SetValue( value
);
3302 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3303 col
->GetWidth(), m_lineHeight
);
3304 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3306 wxWindow
*parent
= GetParent();
3307 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3309 le
.SetEventObject(parent
);
3310 le
.SetColumn(col
->GetModelColumn());
3311 le
.SetDataViewColumn(col
);
3312 le
.SetModel(GetOwner()->GetModel());
3314 parent
->GetEventHandler()->ProcessEvent(le
);
3320 // The first click was on another item, so don't interpret this as
3321 // a double click, but as a simple click instead
3328 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3330 // select single line
3331 SelectAllRows( false );
3332 SelectRow( m_lineSelectSingleOnUp
, true );
3335 //Process the event of user clicking the expander
3336 bool expander
= false;
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
) )
3346 if( node
->IsOpen() )
3347 OnCollapsing(current
);
3349 OnExpanding( current
);
3353 //If the user click the expander, we do not do editing even if the column with expander are editable
3354 if (m_lastOnSame
&& !expander
)
3356 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3357 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3359 m_renameTimer
->Start( 100, true );
3363 m_lastOnSame
= false;
3364 m_lineSelectSingleOnUp
= (unsigned int)-1;
3368 // This is necessary, because after a DnD operation in
3369 // from and to ourself, the up event is swallowed by the
3370 // DnD code. So on next non-up event (which means here and
3371 // now) m_lineSelectSingleOnUp should be reset.
3372 m_lineSelectSingleOnUp
= (unsigned int)-1;
3375 if (event
.RightDown())
3377 m_lineBeforeLastClicked
= m_lineLastClicked
;
3378 m_lineLastClicked
= current
;
3380 // If the item is already selected, do not update the selection.
3381 // Multi-selections should not be cleared if a selected item is clicked.
3382 if (!IsRowSelected(current
))
3384 SelectAllRows(false);
3385 ChangeCurrentRow(current
);
3386 SelectRow(m_currentRow
,true);
3389 // notify cell about right click
3392 // Allow generation of context menu event
3395 else if (event
.MiddleDown())
3397 // notify cell about middle click
3400 if (event
.LeftDown() || forceClick
)
3406 m_lineBeforeLastClicked
= m_lineLastClicked
;
3407 m_lineLastClicked
= current
;
3409 unsigned int oldCurrentRow
= m_currentRow
;
3410 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3412 bool cmdModifierDown
= event
.CmdDown();
3413 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3415 if ( IsSingleSel() || !IsRowSelected(current
) )
3417 SelectAllRows( false );
3419 ChangeCurrentRow(current
);
3421 SelectRow(m_currentRow
,true);
3423 else // multi sel & current is highlighted & no mod keys
3425 m_lineSelectSingleOnUp
= current
;
3426 ChangeCurrentRow(current
); // change focus
3429 else // multi sel & either ctrl or shift is down
3431 if (cmdModifierDown
)
3433 ChangeCurrentRow(current
);
3435 ReverseRowSelection(m_currentRow
);
3437 else if (event
.ShiftDown())
3439 ChangeCurrentRow(current
);
3441 unsigned int lineFrom
= oldCurrentRow
,
3444 if ( lineTo
< lineFrom
)
3447 lineFrom
= m_currentRow
;
3450 SelectRows(lineFrom
, lineTo
, true);
3452 else // !ctrl, !shift
3454 // test in the enclosing if should make it impossible
3455 wxFAIL_MSG( _T("how did we get here?") );
3459 if (m_currentRow
!= oldCurrentRow
)
3460 RefreshRow( oldCurrentRow
);
3462 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3464 // Update selection here...
3467 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3468 (current
== oldCurrentRow
)) && oldWasSelected
;
3472 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3476 if (HasCurrentRow())
3482 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3486 if (HasCurrentRow())
3492 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3494 if( m_selection
.GetCount() != 1 )
3495 return wxDataViewItem();
3497 return GetItemByRow( m_selection
.Item(0));
3500 //-----------------------------------------------------------------------------
3502 //-----------------------------------------------------------------------------
3503 WX_DEFINE_LIST(wxDataViewColumnList
);
3505 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3507 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3508 EVT_SIZE(wxDataViewCtrl::OnSize
)
3511 wxDataViewCtrl::~wxDataViewCtrl()
3514 GetModel()->RemoveNotifier( m_notifier
);
3517 void wxDataViewCtrl::Init()
3522 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3523 const wxPoint
& pos
, const wxSize
& size
,
3524 long style
, const wxValidator
& validator
)
3526 if (!wxControl::Create( parent
, id
, pos
, size
,
3527 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3533 MacSetClipChildren( true ) ;
3536 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3538 if (HasFlag(wxDV_NO_HEADER
))
3539 m_headerArea
= NULL
;
3541 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3543 SetTargetWindow( m_clientArea
);
3545 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3547 sizer
->Add( m_headerArea
, 0, wxGROW
);
3548 sizer
->Add( m_clientArea
, 1, wxGROW
);
3555 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3559 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3562 // we need to process arrows ourselves for scrolling
3563 if ( nMsg
== WM_GETDLGCODE
)
3565 rc
|= DLGC_WANTARROWS
;
3573 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3575 // We need to override OnSize so that our scrolled
3576 // window a) does call Layout() to use sizers for
3577 // positioning the controls but b) does not query
3578 // the sizer for their size and use that for setting
3579 // the scrollable area as set that ourselves by
3580 // calling SetScrollbar() further down.
3587 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3589 if (!wxDataViewCtrlBase::AssociateModel( model
))
3592 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3594 model
->AddNotifier( m_notifier
);
3596 m_clientArea
->BuildTree(model
);
3598 m_clientArea
->UpdateDisplay();
3603 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3605 if (!wxDataViewCtrlBase::AppendColumn(col
))
3608 m_cols
.Append( col
);
3613 void wxDataViewCtrl::OnColumnChange()
3616 m_headerArea
->UpdateDisplay();
3618 m_clientArea
->UpdateDisplay();
3621 void wxDataViewCtrl::DoSetExpanderColumn()
3623 m_clientArea
->UpdateDisplay();
3626 void wxDataViewCtrl::DoSetIndent()
3628 m_clientArea
->UpdateDisplay();
3631 unsigned int wxDataViewCtrl::GetColumnCount() const
3633 return m_cols
.GetCount();
3636 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3638 wxDataViewColumnList::const_iterator iter
;
3640 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3645 if ((*iter
)->IsHidden())
3652 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3654 wxDataViewColumnList::Node
* ret
= m_cols
.Find( column
);
3665 bool wxDataViewCtrl::ClearColumns()
3672 //Selection code with wxDataViewItem as parameters
3673 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3675 return m_clientArea
->GetSelection();
3678 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3681 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3682 int len
= selection
.GetCount();
3683 for( int i
= 0; i
< len
; i
++)
3685 unsigned int row
= selection
[i
];
3686 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3691 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3693 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3694 int len
= sel
.GetCount();
3695 for( int i
= 0; i
< len
; i
++ )
3697 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3699 selection
.Add( static_cast<unsigned int>(row
) );
3701 m_clientArea
->SetSelections( selection
);
3704 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3706 int row
= m_clientArea
->GetRowByItem( item
);
3708 m_clientArea
->SelectRow(row
, true);
3711 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3713 int row
= m_clientArea
->GetRowByItem( item
);
3715 m_clientArea
->SelectRow(row
, false);
3718 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3720 int row
= m_clientArea
->GetRowByItem( item
);
3723 return m_clientArea
->IsRowSelected(row
);
3728 //Selection code with row number as parameter
3729 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3732 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3733 int len
= selection
.GetCount();
3734 for( int i
= 0; i
< len
; i
++)
3736 unsigned int row
= selection
[i
];
3742 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3744 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3745 int len
= sel
.GetCount();
3746 for( int i
= 0; i
< len
; i
++ )
3750 selection
.Add( static_cast<unsigned int>(row
) );
3752 m_clientArea
->SetSelections( selection
);
3755 void wxDataViewCtrl::Select( int row
)
3758 m_clientArea
->SelectRow( row
, true );
3761 void wxDataViewCtrl::Unselect( int row
)
3764 m_clientArea
->SelectRow(row
, false);
3767 bool wxDataViewCtrl::IsSelected( int row
) const
3770 return m_clientArea
->IsRowSelected(row
);
3774 void wxDataViewCtrl::SelectRange( int from
, int to
)
3777 for( int i
= from
; i
< to
; i
++ )
3779 m_clientArea
->Select(sel
);
3782 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3784 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3785 for( int i
= from
; i
< to
; i
++ )
3786 if( sel
.Index( i
) != wxNOT_FOUND
)
3788 m_clientArea
->SetSelections(sel
);
3791 void wxDataViewCtrl::SelectAll()
3793 m_clientArea
->SelectAllRows(true);
3796 void wxDataViewCtrl::UnselectAll()
3798 m_clientArea
->SelectAllRows(false);
3801 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3805 if( row
> m_clientArea
->GetRowCount() )
3806 row
= m_clientArea
->GetRowCount();
3808 int first
= m_clientArea
->GetFirstVisibleRow();
3809 int last
= m_clientArea
->GetLastVisibleRow();
3811 m_clientArea
->ScrollTo( row
, column
);
3812 else if( row
> last
)
3813 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3815 m_clientArea
->ScrollTo( first
, column
);
3818 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3820 int row
= m_clientArea
->GetRowByItem(item
);
3823 if( column
== NULL
)
3824 return EnsureVisible(row
, -1);
3828 int len
= GetColumnCount();
3829 for( int i
= 0; i
< len
; i
++ )
3830 if( GetColumn(i
) == column
)
3835 EnsureVisible( row
, col
);
3841 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3843 m_clientArea
->HitTest(point
, item
, column
);
3846 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3848 return m_clientArea
->GetItemRect(item
, column
);
3851 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3853 return m_clientArea
->GetItemByRow( row
);
3856 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3858 return m_clientArea
->GetRowByItem( item
);
3861 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3863 int row
= m_clientArea
->GetRowByItem( item
);
3865 m_clientArea
->Expand(row
);
3868 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
3870 int row
= m_clientArea
->GetRowByItem( item
);
3872 m_clientArea
->Collapse(row
);
3876 // !wxUSE_GENERICDATAVIEWCTRL
3879 // wxUSE_DATAVIEWCTRL