1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 // on wxMSW the header window (only that part however) can be made native!
67 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
68 #define USE_NATIVE_HEADER_WINDOW
71 //Below is the compare stuff
72 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
73 static wxDataViewModel
* g_model
;
74 static int g_column
= -2;
75 static bool g_asending
= true;
77 // NB: for some reason, this class must be dllexport'ed or we get warnings from
79 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
82 wxDataViewHeaderWindowBase()
85 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
86 const wxPoint
&pos
, const wxSize
&size
,
89 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
92 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
93 wxDataViewCtrl
*GetOwner() { return m_owner
; }
95 // called on column addition/removal
96 virtual void UpdateDisplay() { /* by default, do nothing */ }
98 // returns the n-th column
99 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
102 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
109 wxDataViewCtrl
*m_owner
;
111 // sends an event generated from the n-th wxDataViewColumn
112 void SendEvent(wxEventType type
, unsigned int n
);
115 #ifdef USE_NATIVE_HEADER_WINDOW
117 #define COLUMN_WIDTH_OFFSET 2
118 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
120 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
124 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
126 const wxPoint
&pos
= wxDefaultPosition
,
127 const wxSize
&size
= wxDefaultSize
,
128 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
130 Create(parent
, id
, pos
, size
, name
);
133 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
134 const wxPoint
&pos
, const wxSize
&size
,
135 const wxString
&name
);
137 ~wxDataViewHeaderWindowMSW();
139 // called when any column setting is changed and/or changed
141 virtual void UpdateDisplay();
143 virtual void OnInternalIdle();
145 // called Refresh afterwards
146 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
148 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
151 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
153 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
155 wxSize
DoGetBestSize() const;
157 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
159 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
160 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
164 bool m_delayedUpdate
;
167 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
170 #else // !defined(__WXMSW__)
172 #define HEADER_WINDOW_HEIGHT 25
173 #define HEADER_HORIZ_BORDER 5
174 #define HEADER_VERT_BORDER 3
175 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
177 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
180 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
182 const wxPoint
&pos
= wxDefaultPosition
,
183 const wxSize
&size
= wxDefaultSize
,
184 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
187 Create(parent
, id
, pos
, size
, name
);
190 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
191 const wxPoint
&pos
, const wxSize
&size
,
192 const wxString
&name
);
194 ~wxGenericDataViewHeaderWindow()
196 delete m_resizeCursor
;
199 virtual void UpdateDisplay() { Refresh(); }
203 void OnPaint( wxPaintEvent
&event
);
204 void OnMouse( wxMouseEvent
&event
);
205 void OnSetFocus( wxFocusEvent
&event
);
210 // vars used for column resizing:
212 wxCursor
*m_resizeCursor
;
213 const wxCursor
*m_currentCursor
;
216 bool m_dirty
; // needs refresh?
217 int m_hover
; // index of the column under the mouse
218 int m_column
; // index of the column being resized
219 int m_currentX
; // divider line position in logical (unscrolled) coords
220 int m_minX
; // minimal position beyond which the divider line
221 // can't be dragged in logical coords
223 // the pen used to draw the current column width drag line
224 // when resizing the columsn
228 // internal utilities:
232 m_currentCursor
= (wxCursor
*) NULL
;
233 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
235 m_isDragging
= false;
238 m_hover
= wxNOT_FOUND
;
239 m_column
= wxNOT_FOUND
;
243 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
244 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
247 void AdjustDC(wxDC
& dc
);
250 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
251 DECLARE_EVENT_TABLE()
254 #endif // defined(__WXMSW__)
256 //-----------------------------------------------------------------------------
257 // wxDataViewRenameTimer
258 //-----------------------------------------------------------------------------
260 class wxDataViewRenameTimer
: public wxTimer
263 wxDataViewMainWindow
*m_owner
;
266 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
270 //-----------------------------------------------------------------------------
271 // wxDataViewTreeNode
272 //-----------------------------------------------------------------------------
273 class wxDataViewTreeNode
;
274 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
275 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
277 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
278 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
280 class wxDataViewTreeNode
283 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
290 m_hasChildren
= false;
294 ~wxDataViewTreeNode()
298 wxDataViewTreeNode
* GetParent() { return m_parent
; }
299 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
300 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
301 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
303 void AddNode( wxDataViewTreeNode
* node
)
305 m_leaves
.Add( node
->GetItem().GetID() );
307 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
310 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
312 void AddLeaf( void * leaf
)
314 m_leaves
.Add( leaf
);
316 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
319 wxDataViewItem
& GetItem() { return m_item
; }
320 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
322 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
323 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
327 wxDataViewTreeNode
* node
= this;
328 while( node
->GetParent()->GetParent() != NULL
)
330 node
= node
->GetParent();
343 int len
= m_nodes
.GetCount();
345 for ( int i
= 0 ;i
< len
; i
++)
346 sum
+= m_nodes
[i
]->GetSubTreeCount();
348 sum
+= m_leaves
.GetCount();
351 ChangeSubTreeCount(-sum
);
357 ChangeSubTreeCount(sum
);
360 bool HasChildren() { return m_hasChildren
; }
361 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
363 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
364 int GetSubTreeCount() { return m_subTreeCount
; }
365 void ChangeSubTreeCount( int num
)
369 m_subTreeCount
+= num
;
371 m_parent
->ChangeSubTreeCount(num
);
378 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
379 int len
= m_nodes
.GetCount();
380 for (int i
= 0; i
< len
; i
++)
381 m_nodes
[i
]->Resort();
382 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
387 wxDataViewTreeNode
*m_parent
;
388 wxDataViewTreeNodes m_nodes
;
389 wxDataViewTreeLeaves m_leaves
;
390 wxDataViewItem m_item
;
396 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
398 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
401 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
403 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
408 //-----------------------------------------------------------------------------
409 // wxDataViewMainWindow
410 //-----------------------------------------------------------------------------
412 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
414 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
415 WX_DEFINE_LIST(ItemList
)
417 class wxDataViewMainWindow
: public wxWindow
420 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
422 const wxPoint
&pos
= wxDefaultPosition
,
423 const wxSize
&size
= wxDefaultSize
,
424 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
425 virtual ~wxDataViewMainWindow();
427 // notifications from wxDataViewModel
428 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
429 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
430 bool ItemChanged( const wxDataViewItem
&item
);
431 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
445 g_model
= GetOwner()->GetModel();
446 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
449 if (g_model
->HasDefaultCompare())
457 g_column
= col
->GetModelColumn();
458 g_asending
= col
->IsSortOrderAscending();
461 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
462 wxDataViewCtrl
*GetOwner() { return m_owner
; }
463 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
465 void OnPaint( wxPaintEvent
&event
);
466 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
467 void OnChar( wxKeyEvent
&event
);
468 void OnMouse( wxMouseEvent
&event
);
469 void OnSetFocus( wxFocusEvent
&event
);
470 void OnKillFocus( wxFocusEvent
&event
);
472 void UpdateDisplay();
473 void RecalculateDisplay();
474 void OnInternalIdle();
476 void OnRenameTimer();
478 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
479 void ScrollTo( int rows
, int column
);
481 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
482 void ChangeCurrentRow( unsigned int row
);
484 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
485 bool IsEmpty() { return GetRowCount() == 0; }
487 int GetCountPerPage() const;
488 int GetEndOfLastCol() const;
489 unsigned int GetFirstVisibleRow() const;
490 //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
491 unsigned int GetLastVisibleRow();
492 unsigned int GetRowCount() ;
494 wxDataViewItem
GetSelection() const;
495 wxDataViewSelection
GetSelections(){ return m_selection
; }
496 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
497 void Select( const wxArrayInt
& aSelections
);
498 void SelectAllRows( bool on
);
499 void SelectRow( unsigned int row
, bool on
);
500 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
501 void ReverseRowSelection( unsigned int row
);
502 bool IsRowSelected( unsigned int row
);
503 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
505 void RefreshRow( unsigned int row
);
506 void RefreshRows( unsigned int from
, unsigned int to
);
507 void RefreshRowsAfter( unsigned int firstRow
);
509 // returns the colour to be used for drawing the rules
510 wxColour
GetRuleColour() const
512 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
515 wxRect
GetLineRect( unsigned int row
) const;
517 //Some useful functions for row and item mapping
518 wxDataViewItem
GetItemByRow( unsigned int row
) const;
519 int GetRowByItem( const wxDataViewItem
& item
);
521 //Methods for building the mapping tree
522 void BuildTree( wxDataViewModel
* model
);
524 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
525 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
527 void Expand( unsigned int row
) { OnExpanding( row
); }
528 void Collapse( unsigned int row
) { OnCollapsing( row
); }
530 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
531 //We did not need this temporarily
532 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
534 int RecalculateCount() ;
536 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
537 void OnExpanding( unsigned int row
);
538 void OnCollapsing( unsigned int row
);
540 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
543 wxDataViewCtrl
*m_owner
;
547 wxDataViewColumn
*m_currentCol
;
548 unsigned int m_currentRow
;
549 wxDataViewSelection m_selection
;
551 wxDataViewRenameTimer
*m_renameTimer
;
559 // for double click logic
560 unsigned int m_lineLastClicked
,
561 m_lineBeforeLastClicked
,
562 m_lineSelectSingleOnUp
;
564 // the pen used to draw horiz/vertical rules
567 // the pen used to draw the expander and the lines
570 //This is the tree structure of the model
571 wxDataViewTreeNode
* m_root
;
573 //This is the tree node under the cursor
574 wxDataViewTreeNode
* m_underMouse
;
576 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
577 DECLARE_EVENT_TABLE()
580 // ---------------------------------------------------------
581 // wxGenericDataViewModelNotifier
582 // ---------------------------------------------------------
584 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
587 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
588 { m_mainWindow
= mainWindow
; }
590 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
591 { return m_mainWindow
->ItemAdded( parent
, item
); }
592 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
593 { return m_mainWindow
->ItemDeleted( parent
, item
); }
594 virtual bool ItemChanged( const wxDataViewItem
& item
)
595 { return m_mainWindow
->ItemChanged(item
); }
596 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
597 { return m_mainWindow
->ValueChanged( item
, col
); }
598 virtual bool Cleared()
599 { return m_mainWindow
->Cleared(); }
600 virtual void Resort()
601 { m_mainWindow
->Resort(); }
603 wxDataViewMainWindow
*m_mainWindow
;
606 // ---------------------------------------------------------
607 // wxDataViewRenderer
608 // ---------------------------------------------------------
610 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
612 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
613 wxDataViewCellMode mode
,
615 wxDataViewRendererBase( varianttype
, mode
, align
)
624 wxDataViewRenderer::~wxDataViewRenderer()
630 wxDC
*wxDataViewRenderer::GetDC()
634 if (GetOwner() == NULL
)
636 if (GetOwner()->GetOwner() == NULL
)
638 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
644 // ---------------------------------------------------------
645 // wxDataViewCustomRenderer
646 // ---------------------------------------------------------
648 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
650 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
651 wxDataViewCellMode mode
, int align
) :
652 wxDataViewRenderer( varianttype
, mode
, align
)
656 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
658 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
659 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
660 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
661 view
->GetForegroundColour();
662 dc
->SetTextForeground(col
);
663 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
666 // ---------------------------------------------------------
667 // wxDataViewTextRenderer
668 // ---------------------------------------------------------
670 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
672 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
673 wxDataViewCellMode mode
, int align
) :
674 wxDataViewCustomRenderer( varianttype
, mode
, align
)
678 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
680 m_text
= value
.GetString();
685 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
690 bool wxDataViewTextRenderer::HasEditorCtrl()
695 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
696 wxRect labelRect
, const wxVariant
&value
)
698 return new wxTextCtrl( parent
, wxID_ANY
, value
,
699 wxPoint(labelRect
.x
,labelRect
.y
),
700 wxSize(labelRect
.width
,labelRect
.height
) );
703 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
705 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
706 value
= text
->GetValue();
710 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
712 RenderText( m_text
, 0, cell
, dc
, state
);
716 wxSize
wxDataViewTextRenderer::GetSize() const
718 const wxDataViewCtrl
*view
= GetView();
722 view
->GetTextExtent( m_text
, &x
, &y
);
723 return wxSize( x
, y
);
725 return wxSize(80,20);
728 // ---------------------------------------------------------
729 // wxDataViewTextRendererAttr
730 // ---------------------------------------------------------
732 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
734 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
735 wxDataViewCellMode mode
, int align
) :
736 wxDataViewTextRenderer( varianttype
, mode
, align
)
741 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
748 if (m_attr
.HasColour())
750 colour
= dc
->GetTextForeground();
751 dc
->SetTextForeground( m_attr
.GetColour() );
754 if (m_attr
.GetBold() || m_attr
.GetItalic())
756 font
= dc
->GetFont();
757 wxFont myfont
= font
;
758 if (m_attr
.GetBold())
759 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
760 if (m_attr
.GetItalic())
761 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
762 dc
->SetFont( myfont
);
766 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
771 if (m_attr
.HasColour())
772 dc
->SetTextForeground( colour
);
774 if (m_attr
.GetBold() || m_attr
.GetItalic())
782 // ---------------------------------------------------------
783 // wxDataViewBitmapRenderer
784 // ---------------------------------------------------------
786 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
788 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
789 wxDataViewCellMode mode
, int align
) :
790 wxDataViewCustomRenderer( varianttype
, mode
, align
)
794 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
796 if (value
.GetType() == wxT("wxBitmap"))
798 if (value
.GetType() == wxT("wxIcon"))
804 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
809 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
812 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
813 else if (m_icon
.Ok())
814 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
819 wxSize
wxDataViewBitmapRenderer::GetSize() const
822 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
823 else if (m_icon
.Ok())
824 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
826 return wxSize(16,16);
829 // ---------------------------------------------------------
830 // wxDataViewToggleRenderer
831 // ---------------------------------------------------------
833 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
835 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
836 wxDataViewCellMode mode
, int align
) :
837 wxDataViewCustomRenderer( varianttype
, mode
, align
)
842 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
844 m_toggle
= value
.GetBool();
849 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
854 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
856 // User wxRenderer here
859 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
861 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
866 flags
|= wxCONTROL_CHECKED
;
867 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
868 flags
|= wxCONTROL_DISABLED
;
870 wxRendererNative::Get().DrawCheckBox(
871 GetOwner()->GetOwner(),
879 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
880 wxDataViewModel
*model
,
881 const wxDataViewItem
& item
, unsigned int col
)
883 bool value
= !m_toggle
;
884 wxVariant variant
= value
;
885 model
->SetValue( variant
, item
, col
);
886 model
->ValueChanged( item
, col
);
890 wxSize
wxDataViewToggleRenderer::GetSize() const
892 return wxSize(20,20);
895 // ---------------------------------------------------------
896 // wxDataViewProgressRenderer
897 // ---------------------------------------------------------
899 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
901 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
902 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
903 wxDataViewCustomRenderer( varianttype
, mode
, align
)
909 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
913 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
915 m_value
= (long) value
;
917 if (m_value
< 0) m_value
= 0;
918 if (m_value
> 100) m_value
= 100;
923 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
925 value
= (long) m_value
;
929 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
931 double pct
= (double)m_value
/ 100.0;
933 bar
.width
= (int)(cell
.width
* pct
);
934 dc
->SetPen( *wxTRANSPARENT_PEN
);
935 dc
->SetBrush( *wxBLUE_BRUSH
);
936 dc
->DrawRectangle( bar
);
938 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
939 dc
->SetPen( *wxBLACK_PEN
);
940 dc
->DrawRectangle( cell
);
945 wxSize
wxDataViewProgressRenderer::GetSize() const
947 return wxSize(40,12);
950 // ---------------------------------------------------------
951 // wxDataViewDateRenderer
952 // ---------------------------------------------------------
954 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
956 #if wxUSE_DATE_RENDERER_POPUP
958 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
961 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
962 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
963 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
968 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
969 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
970 sizer
->Add( m_cal
, 1, wxGROW
);
975 void OnCalendar( wxCalendarEvent
&event
);
977 wxCalendarCtrl
*m_cal
;
978 wxDataViewModel
*m_model
;
980 const wxDataViewItem
& m_item
;
983 virtual void OnDismiss()
988 DECLARE_EVENT_TABLE()
991 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
992 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
995 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
997 wxDateTime date
= event
.GetDate();
998 wxVariant value
= date
;
999 m_model
->SetValue( value
, m_item
, m_col
);
1000 m_model
->ValueChanged( m_item
, m_col
);
1004 #endif // wxUSE_DATE_RENDERER_POPUP
1006 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1008 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1009 wxDataViewCellMode mode
, int align
) :
1010 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1014 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1016 m_date
= value
.GetDateTime();
1021 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1027 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1029 wxString tmp
= m_date
.FormatDate();
1030 RenderText( tmp
, 0, cell
, dc
, state
);
1034 wxSize
wxDataViewDateRenderer::GetSize() const
1036 const wxDataViewCtrl
* view
= GetView();
1037 wxString tmp
= m_date
.FormatDate();
1039 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1040 return wxSize(x
,y
+d
);
1043 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1044 const wxDataViewItem
& item
, unsigned int col
)
1047 model
->GetValue( variant
, item
, col
);
1048 wxDateTime value
= variant
.GetDateTime();
1050 #if wxUSE_DATE_RENDERER_POPUP
1051 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1052 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1053 wxPoint pos
= wxGetMousePosition();
1056 popup
->Popup( popup
->m_cal
);
1057 #else // !wxUSE_DATE_RENDERER_POPUP
1058 wxMessageBox(value
.Format());
1059 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1063 // ---------------------------------------------------------
1064 // wxDataViewIconTextRenderer
1065 // ---------------------------------------------------------
1067 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1069 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1070 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1071 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1074 SetAlignment(align
);
1077 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1081 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1087 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1092 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1095 const wxIcon
&icon
= m_value
.GetIcon();
1098 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1099 xoffset
= icon
.GetWidth()+4;
1102 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1107 wxSize
wxDataViewIconTextRenderer::GetSize() const
1109 const wxDataViewCtrl
*view
= GetView();
1110 if (!m_value
.GetText().empty())
1113 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1115 if (m_value
.GetIcon().IsOk())
1116 x
+= m_value
.GetIcon().GetWidth() + 4;
1117 return wxSize( x
, y
);
1119 return wxSize(80,20);
1123 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1124 wxRect
WXUNUSED(labelRect
),
1125 const wxVariant
& WXUNUSED(value
))
1131 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1132 wxVariant
& WXUNUSED(value
))
1137 // ---------------------------------------------------------
1139 // ---------------------------------------------------------
1141 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1143 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1144 unsigned int model_column
,
1145 int width
, wxAlignment align
, int flags
) :
1146 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1148 SetAlignment(align
);
1152 m_autosize
= width
< 0; // TODO
1154 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1157 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1158 unsigned int model_column
,
1159 int width
, wxAlignment align
, int flags
) :
1160 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1162 SetAlignment(align
);
1165 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1168 wxDataViewColumn::~wxDataViewColumn()
1172 void wxDataViewColumn::Init( int width
)
1175 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1179 void wxDataViewColumn::SetResizeable( bool resizeable
)
1182 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1184 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1187 void wxDataViewColumn::SetHidden( bool hidden
)
1190 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1192 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1194 // tell our owner to e.g. update its scrollbars:
1196 GetOwner()->OnColumnChange();
1199 void wxDataViewColumn::SetSortable( bool sortable
)
1202 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1204 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1206 // Update header button
1208 GetOwner()->OnColumnChange();
1211 void wxDataViewColumn::SetReorderable( bool reorderable
)
1214 m_flags
|= wxDATAVIEW_COL_REORDERABLE
;
1216 m_flags
&= ~wxDATAVIEW_COL_REORDERABLE
;
1219 void wxDataViewColumn::SetSortOrder( bool ascending
)
1221 m_ascending
= ascending
;
1223 // Update header button
1225 GetOwner()->OnColumnChange();
1228 bool wxDataViewColumn::IsSortOrderAscending() const
1233 void wxDataViewColumn::SetInternalWidth( int width
)
1237 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1238 if (m_owner
&& m_owner
->m_clientArea
)
1239 m_owner
->m_clientArea
->RecalculateDisplay();
1242 void wxDataViewColumn::SetWidth( int width
)
1244 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1246 SetInternalWidth(width
);
1250 //-----------------------------------------------------------------------------
1251 // wxDataViewHeaderWindowBase
1252 //-----------------------------------------------------------------------------
1254 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1256 wxWindow
*parent
= GetParent();
1257 wxDataViewEvent
le(type
, parent
->GetId());
1259 le
.SetEventObject(parent
);
1261 le
.SetDataViewColumn(GetColumn(n
));
1262 le
.SetModel(GetOwner()->GetModel());
1264 // for events created by wxDataViewHeaderWindow the
1265 // row / value fields are not valid
1267 parent
->GetEventHandler()->ProcessEvent(le
);
1270 #ifdef USE_NATIVE_HEADER_WINDOW
1272 #ifndef HDS_DRAGDROP
1273 #define HDS_DRAGDROP 0x0040
1275 #ifndef HDS_FULLDRAG
1276 #define HDS_FULLDRAG 0x0080
1279 // implemented in msw/listctrl.cpp:
1280 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1282 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1284 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1285 const wxPoint
&pos
, const wxSize
&size
,
1286 const wxString
&name
)
1290 m_scrollOffsetX
= 0;
1291 m_delayedUpdate
= false;
1292 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this );
1294 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1295 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1296 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1299 wxSize
new_size(w
,h
);
1301 if ( !CreateControl(parent
, id
, pos
, new_size
, 0, wxDefaultValidator
, name
) )
1304 // create the native WC_HEADER window:
1305 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1306 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1309 msStyle
|= WS_VISIBLE
;
1311 m_hWnd
= CreateWindowEx(0,
1322 wxLogLastError(_T("CreateWindowEx"));
1326 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1327 // to call wxDataViewHeaderWindow::MSWOnNotify
1328 SubclassWin(m_hWnd
);
1330 // the following is required to get the default win's font for
1331 // header windows and must be done befor sending the HDM_LAYOUT msg
1337 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1342 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1344 return wxSize( 80, m_buttonHeight
+2 );
1347 void wxDataViewHeaderWindowMSW::OnInternalIdle()
1349 if (m_delayedUpdate
)
1351 m_delayedUpdate
= false;
1356 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1358 // remove old columns
1359 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1360 Header_DeleteItem((HWND
)m_hWnd
, 0);
1362 // add the updated array of columns to the header control
1363 unsigned int cols
= GetOwner()->GetColumnCount();
1364 unsigned int added
= 0;
1365 for (unsigned int i
= 0; i
< cols
; i
++)
1367 wxDataViewColumn
*col
= GetColumn( i
);
1368 if (col
->IsHidden())
1369 continue; // don't add it!
1372 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1373 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1374 hdi
.cxy
= col
->GetWidth();
1375 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1376 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1377 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1379 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1381 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1382 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1383 // VZ: works with 5.81
1384 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1387 // lParam is reserved for application's use:
1388 // we store there the column index to use it later in MSWOnNotify
1389 // (since columns may have been hidden)
1390 hdi
.lParam
= (LPARAM
)i
;
1392 // the native wxMSW implementation of the header window
1393 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1394 // on the right: to correct this effect we make the column
1395 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1397 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1399 switch (col
->GetAlignment())
1402 hdi
.fmt
|= HDF_LEFT
;
1404 case wxALIGN_CENTER
:
1405 case wxALIGN_CENTER_HORIZONTAL
:
1406 hdi
.fmt
|= HDF_CENTER
;
1409 hdi
.fmt
|= HDF_RIGHT
;
1413 // such alignment is not allowed for the column header!
1417 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1418 (WPARAM
)added
, (LPARAM
)&hdi
);
1423 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1427 // NOTE: we don't just return nmHDR->iItem because when there are
1428 // hidden columns, nmHDR->iItem may be different from
1429 // nmHDR->pitem->lParam
1431 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1433 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1438 item
.mask
= HDI_LPARAM
;
1439 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1441 return (unsigned int)item
.lParam
;
1444 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1446 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1448 // is it a message from the header?
1449 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1450 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1452 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1453 switch ( nmhdr
->code
)
1455 case HDN_BEGINTRACK
:
1456 // user has started to resize a column:
1457 // do we need to veto it?
1458 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1466 // user has started to reorder a column
1467 if (!GetColumn(nmHDR
->iItem
)->IsReorderable())
1474 case HDN_ENDDRAG
: // user has finished reordering a column
1476 wxDataViewColumn
*col
= GetColumn(nmHDR
->iItem
);
1477 unsigned int new_pos
= nmHDR
->pitem
->iOrder
;
1478 m_owner
->ColumnMoved( col
, new_pos
);
1479 m_delayedUpdate
= true;
1483 case HDN_ITEMCHANGING
:
1484 if (nmHDR
->pitem
!= NULL
&&
1485 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1487 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1488 if (nmHDR
->pitem
->cxy
< minWidth
)
1490 // do not allow the user to resize this column under
1491 // its minimal width:
1497 case HDN_ITEMCHANGED
: // user is resizing a column
1498 case HDN_ENDTRACK
: // user has finished resizing a column
1500 // update the width of the modified column:
1501 if (nmHDR
->pitem
!= NULL
&&
1502 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1504 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1505 unsigned int w
= nmHDR
->pitem
->cxy
;
1506 wxDataViewColumn
*col
= GetColumn(idx
);
1508 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1509 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1510 w
-= COLUMN_WIDTH_OFFSET
;
1512 if (w
>= (unsigned)col
->GetMinWidth())
1513 col
->SetInternalWidth(w
);
1519 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1520 wxDataViewModel
* model
= GetOwner()->GetModel();
1522 if(nmHDR
->iButton
== 0)
1524 wxDataViewColumn
*col
= GetColumn(idx
);
1525 if(col
->IsSortable())
1527 if(model
&& m_owner
->GetSortingColumn() == col
)
1529 bool order
= col
->IsSortOrderAscending();
1530 col
->SetSortOrder(!order
);
1534 m_owner
->SetSortingColumn(col
);
1542 wxEventType evt
= nmHDR
->iButton
== 0 ?
1543 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1544 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1545 SendEvent(evt
, idx
);
1551 // NOTE: for some reason (i.e. for a bug in Windows)
1552 // the HDN_ITEMCLICK notification is not sent on
1553 // right clicks, so we need to handle NM_RCLICK
1556 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1557 if (column
!= wxNOT_FOUND
)
1560 item
.mask
= HDI_LPARAM
;
1561 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1563 // 'idx' may be different from 'column' if there are
1564 // hidden columns...
1565 unsigned int idx
= (unsigned int)item
.lParam
;
1566 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1572 case HDN_GETDISPINFOW
:
1573 // see wxListCtrl::MSWOnNotify for more info!
1576 case HDN_ITEMDBLCLICK
:
1578 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1579 int w
= GetOwner()->GetBestColumnWidth(idx
);
1581 // update the native control:
1583 ZeroMemory(&hd
, sizeof(hd
));
1584 hd
.mask
= HDI_WIDTH
;
1586 Header_SetItem(GetHwnd(),
1587 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1590 // update the wxDataViewColumn class:
1591 GetColumn(idx
)->SetInternalWidth(w
);
1596 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1602 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1603 const wxRect
* WXUNUSED(rect
))
1605 m_scrollOffsetX
+= dx
;
1607 GetParent()->Layout();
1610 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1614 // TODO: why is there a border + 2px around it?
1615 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1618 #else // !defined(__WXMSW__)
1620 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1621 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1622 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1623 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1624 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1627 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1628 const wxPoint
&pos
, const wxSize
&size
,
1629 const wxString
&name
)
1633 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1636 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1637 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1638 SetOwnForegroundColour( attr
.colFg
);
1639 SetOwnBackgroundColour( attr
.colBg
);
1641 SetOwnFont( attr
.font
);
1643 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1644 // a wxBoxSizer and in order to avoid super-big header windows,
1645 // we need to set our height as fixed
1646 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1647 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1652 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1655 GetClientSize( &w
, &h
);
1657 wxAutoBufferedPaintDC
dc( this );
1659 dc
.SetBackground(GetBackgroundColour());
1663 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1666 m_owner
->GetViewStart( &x
, NULL
);
1668 // account for the horz scrollbar offset
1669 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1671 dc
.SetFont( GetFont() );
1673 unsigned int cols
= GetOwner()->GetColumnCount();
1676 for (i
= 0; i
< cols
; i
++)
1678 wxDataViewColumn
*col
= GetColumn( i
);
1679 if (col
->IsHidden())
1680 continue; // skip it!
1682 int cw
= col
->GetWidth();
1685 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1686 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1688 if (col
->IsSortOrderAscending())
1689 sortArrow
= wxHDR_SORT_ICON_UP
;
1691 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1695 if (m_parent
->IsEnabled())
1697 if ((int) i
== m_hover
)
1698 state
= wxCONTROL_CURRENT
;
1702 state
= (int) wxCONTROL_DISABLED
;
1705 wxRendererNative::Get().DrawHeaderButton
1709 wxRect(xpos
, 0, cw
, ch
-1),
1714 // align as required the column title:
1716 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1717 switch (col
->GetAlignment())
1720 x
+= HEADER_HORIZ_BORDER
;
1723 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1726 case wxALIGN_CENTER
:
1727 case wxALIGN_CENTER_HORIZONTAL
:
1728 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1732 // always center the title vertically:
1733 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1735 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1737 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1738 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1739 dc
.DrawText( col
->GetTitle(), x
, y
);
1740 dc
.DestroyClippingRegion();
1746 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1748 GetParent()->SetFocus();
1752 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1754 // we want to work with logical coords
1756 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1757 int y
= event
.GetY();
1761 // we don't draw the line beyond our window,
1762 // but we allow dragging it there
1764 GetClientSize( &w
, NULL
);
1765 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1768 if (event
.ButtonUp())
1770 m_isDragging
= false;
1776 m_currentX
= wxMax(m_minX
+ 7, x
);
1780 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1782 GetOwner()->Refresh();
1786 else // not dragging
1789 m_column
= wxNOT_FOUND
;
1791 bool hit_border
= false;
1793 // end of the current column
1796 // find the column where this event occured
1797 int countCol
= m_owner
->GetColumnCount();
1798 for (int column
= 0; column
< countCol
; column
++)
1800 wxDataViewColumn
*p
= GetColumn(column
);
1803 continue; // skip if not shown
1805 xpos
+= p
->GetWidth();
1807 if ((abs(x
-xpos
) < 3) && (y
< 22))
1815 // inside the column
1822 int old_hover
= m_hover
;
1824 if (event
.Leaving())
1825 m_hover
= wxNOT_FOUND
;
1826 if (old_hover
!= m_hover
)
1829 if (m_column
== wxNOT_FOUND
)
1832 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1833 if (event
.LeftDClick() && resizeable
)
1835 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1838 else if (event
.LeftDown() || event
.RightUp())
1840 if (hit_border
&& event
.LeftDown() && resizeable
)
1842 m_isDragging
= true;
1846 else // click on a column
1848 wxDataViewModel
* model
= GetOwner()->GetModel();
1849 wxEventType evt
= event
.LeftDown() ?
1850 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1851 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1852 SendEvent(evt
, m_column
);
1854 //Left click the header
1855 if(event
.LeftDown())
1857 wxDataViewColumn
*col
= GetColumn(m_column
);
1858 if(col
->IsSortable())
1860 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1861 if(model
&& sortCol
== col
)
1863 bool order
= col
->IsSortOrderAscending();
1864 col
->SetSortOrder(!order
);
1868 m_owner
->SetSortingColumn(col
);
1874 //Send the column sorted event
1875 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1879 else if (event
.Moving())
1881 if (hit_border
&& resizeable
)
1882 m_currentCursor
= m_resizeCursor
;
1884 m_currentCursor
= wxSTANDARD_CURSOR
;
1886 SetCursor(*m_currentCursor
);
1891 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1895 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1896 m_owner
->GetViewStart( &x
, NULL
);
1898 // shift the DC origin to match the position of the main window horizontal
1899 // scrollbar: this allows us to always use logical coords
1900 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1903 #endif // defined(__WXMSW__)
1905 //-----------------------------------------------------------------------------
1906 // wxDataViewRenameTimer
1907 //-----------------------------------------------------------------------------
1909 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1914 void wxDataViewRenameTimer::Notify()
1916 m_owner
->OnRenameTimer();
1919 //-----------------------------------------------------------------------------
1920 // wxDataViewMainWindow
1921 //-----------------------------------------------------------------------------
1923 //The tree building helper, declared firstly
1924 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1926 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1928 if (row1
> row2
) return 1;
1929 if (row1
== row2
) return 0;
1934 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1936 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1937 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1938 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1939 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1940 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1941 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1944 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1945 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1946 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1947 m_selection( wxDataViewSelectionCmp
)
1952 m_lastOnSame
= false;
1953 m_renameTimer
= new wxDataViewRenameTimer( this );
1955 // TODO: user better initial values/nothing selected
1956 m_currentCol
= NULL
;
1959 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1962 m_dragStart
= wxPoint(0,0);
1963 m_lineLastClicked
= (unsigned int) -1;
1964 m_lineBeforeLastClicked
= (unsigned int) -1;
1965 m_lineSelectSingleOnUp
= (unsigned int) -1;
1969 SetBackgroundColour( *wxWHITE
);
1971 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1973 m_penRule
= wxPen(GetRuleColour());
1975 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1976 m_penExpander
= wxPen(wxColour(0,0,0));
1977 //Some new added code to deal with the tree structure
1978 m_root
= new wxDataViewTreeNode( NULL
);
1979 m_root
->SetHasChildren(true);
1981 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1983 m_underMouse
= NULL
;
1987 wxDataViewMainWindow::~wxDataViewMainWindow()
1990 delete m_renameTimer
;
1993 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1995 wxDataViewModel
*model
= GetOwner()->GetModel();
1996 wxAutoBufferedPaintDC
dc( this );
1999 dc
.SetPen( *wxTRANSPARENT_PEN
);
2000 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
2001 dc
.SetBrush( *wxWHITE_BRUSH
);
2002 wxSize
size( GetClientSize() );
2003 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
2007 GetOwner()->PrepareDC( dc
);
2008 dc
.SetFont( GetFont() );
2010 wxRect update
= GetUpdateRegion().GetBox();
2011 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2013 // compute which items needs to be redrawn
2014 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2015 unsigned int item_count
=
2016 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2017 (int)(GetRowCount( ) - item_start
));
2018 unsigned int item_last
= item_start
+ item_count
;
2020 // compute which columns needs to be redrawn
2021 unsigned int cols
= GetOwner()->GetColumnCount();
2022 unsigned int col_start
= 0;
2023 unsigned int x_start
= 0;
2024 for (x_start
= 0; col_start
< cols
; col_start
++)
2026 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2027 if (col
->IsHidden())
2028 continue; // skip it!
2030 unsigned int w
= col
->GetWidth();
2031 if (x_start
+w
>= (unsigned int)update
.x
)
2037 unsigned int col_last
= col_start
;
2038 unsigned int x_last
= x_start
;
2039 for (; col_last
< cols
; col_last
++)
2041 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2042 if (col
->IsHidden())
2043 continue; // skip it!
2045 if (x_last
> (unsigned int)update
.GetRight())
2048 x_last
+= col
->GetWidth();
2051 // Draw horizontal rules if required
2052 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2054 dc
.SetPen(m_penRule
);
2055 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2057 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2059 int y
= i
* m_lineHeight
;
2060 dc
.DrawLine(x_start
, y
, x_last
, y
);
2064 // Draw vertical rules if required
2065 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2067 dc
.SetPen(m_penRule
);
2068 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2071 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2073 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2074 if (col
->IsHidden())
2075 continue; // skip it
2077 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2078 x
, item_last
* m_lineHeight
);
2080 x
+= col
->GetWidth();
2083 // Draw last vertical rule
2084 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2085 x
, item_last
* m_lineHeight
);
2088 // redraw the background for the items which are selected/current
2089 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2091 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2092 if (selected
|| item
== m_currentRow
)
2094 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2095 if (item
== m_currentRow
)
2096 flags
|= wxCONTROL_CURRENT
;
2098 flags
|= wxCONTROL_FOCUSED
;
2100 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2101 wxRendererNative::Get().DrawItemSelectionRect
2111 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2114 // TODO: last column for RTL support
2115 expander
= GetOwner()->GetColumn( 0 );
2116 GetOwner()->SetExpanderColumn(expander
);
2119 // redraw all cells for all rows which must be repainted and for all columns
2121 cell_rect
.x
= x_start
;
2122 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2123 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2125 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2126 wxDataViewRenderer
*cell
= col
->GetRenderer();
2127 cell_rect
.width
= col
->GetWidth();
2129 if (col
->IsHidden())
2130 continue; // skipt it!
2133 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2135 // get the cell value and set it into the renderer
2137 wxDataViewTreeNode
*node
= NULL
;
2138 wxDataViewItem dataitem
;
2142 node
= GetTreeNodeByRow(item
);
2146 dataitem
= node
->GetItem();
2148 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2153 dataitem
= wxDataViewItem( (void*) item
);
2156 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2157 cell
->SetValue( value
);
2159 if (cell
->GetWantsAttr())
2161 wxDataViewItemAttr attr
;
2162 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2164 cell
->SetAttr( attr
);
2165 cell
->SetHasAttr( ret
);
2168 // update the y offset
2169 cell_rect
.y
= item
* m_lineHeight
;
2171 //Draw the expander here.
2173 if ((m_root
) && (col
== expander
))
2175 indent
= node
->GetIndentLevel();
2177 //Calculate the indent first
2178 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2180 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2181 // change the cell_rect.x to the appropriate pos
2182 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2183 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2184 dc
.SetPen( m_penExpander
);
2185 dc
.SetBrush( wxNullBrush
);
2186 if( node
->HasChildren() )
2188 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2190 if (m_underMouse
== node
)
2192 flag
|= wxCONTROL_CURRENT
;
2194 if( node
->IsOpen() )
2195 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2197 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2201 // I am wondering whether we should draw dot lines between tree nodes
2204 // Yes, if the node does not have any child, it must be a leaf which
2205 // mean that it is a temporarily created by GetTreeNodeByRow
2208 //force the expander column to left-center align
2209 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2213 // cannot be bigger than allocated space
2214 wxSize size
= cell
->GetSize();
2215 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2216 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2217 // size.y = wxMin( size.y, cell_rect.height );
2218 size
.y
= cell_rect
.height
;
2220 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2221 int align
= cell
->GetAlignment();
2223 // horizontal alignment:
2224 item_rect
.x
= cell_rect
.x
;
2225 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2226 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2227 else if (align
& wxALIGN_RIGHT
)
2228 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2229 //else: wxALIGN_LEFT is the default
2231 // vertical alignment:
2232 item_rect
.y
= cell_rect
.y
;
2233 if (align
& wxALIGN_CENTER_VERTICAL
)
2234 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2235 else if (align
& wxALIGN_BOTTOM
)
2236 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2237 //else: wxALIGN_TOP is the default
2240 item_rect
.x
+= PADDING_RIGHTLEFT
;
2241 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2243 //Here we add the tree indent
2244 item_rect
.x
+= indent
;
2247 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2248 state
|= wxDATAVIEW_CELL_SELECTED
;
2250 // TODO: it would be much more efficient to create a clipping
2251 // region for the entire column being rendered (in the OnPaint
2252 // of wxDataViewMainWindow) instead of a single clip region for
2253 // each cell. However it would mean that each renderer should
2254 // respect the given wxRect's top & bottom coords, eventually
2255 // violating only the left & right coords - however the user can
2256 // make its own renderer and thus we cannot be sure of that.
2257 dc
.SetClippingRegion( item_rect
);
2258 cell
->Render( item_rect
, &dc
, state
);
2259 dc
.DestroyClippingRegion();
2262 cell_rect
.x
+= cell_rect
.width
;
2266 void wxDataViewMainWindow::OnRenameTimer()
2268 // We have to call this here because changes may just have
2269 // been made and no screen update taken place.
2274 unsigned int cols
= GetOwner()->GetColumnCount();
2276 for (i
= 0; i
< cols
; i
++)
2278 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2280 continue; // skip it!
2282 if (c
== m_currentCol
)
2284 xpos
+= c
->GetWidth();
2286 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2287 m_currentCol
->GetWidth(), m_lineHeight
);
2289 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2290 &labelRect
.x
, &labelRect
.y
);
2292 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2293 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2297 //------------------------------------------------------------------
2298 // Helper class for do operation on the tree node
2299 //------------------------------------------------------------------
2306 //The return value control how the tree-walker tranverse the tree
2307 // 0: Job done, stop tranverse and return
2308 // 1: Ignore the current node's subtree and continue
2309 // 2: Job not done, continue
2310 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2311 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2312 virtual int operator() ( void * n
) = 0;
2315 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2320 switch( func( node
) )
2331 wxDataViewTreeNodes nodes
= node
->GetNodes();
2332 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2334 int len_nodes
= nodes
.GetCount();
2335 int len
= leaves
.GetCount();
2336 int i
= 0, nodes_i
= 0;
2338 for( ; i
< len
; i
++ )
2340 void * n
= leaves
[i
];
2341 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2343 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2346 if( Walker( nd
, func
) )
2365 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2376 wxDataViewTreeNode
* node
;
2377 node
= FindNode(parent
);
2382 node
->SetHasChildren( true );
2384 if( g_model
->IsContainer( item
) )
2386 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2387 newnode
->SetItem(item
);
2388 newnode
->SetHasChildren( true );
2389 node
->AddNode( newnode
);
2392 node
->AddLeaf( item
.GetID() );
2394 node
->ChangeSubTreeCount(1);
2402 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2404 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2405 const wxDataViewItem
& item
)
2410 if( m_currentRow
> GetRowCount() )
2411 m_currentRow
= m_count
- 1;
2413 m_selection
.Empty();
2420 wxDataViewTreeNode
* node
= FindNode(parent
);
2422 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2423 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2426 node
->GetChildren().Remove( item
.GetID() );
2427 //Manuplate selection
2428 if( m_selection
.GetCount() > 1 )
2430 m_selection
.Empty();
2432 bool isContainer
= false;
2433 wxDataViewTreeNodes nds
= node
->GetNodes();
2434 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2436 if (nds
[i
]->GetItem() == item
)
2444 wxDataViewTreeNode
* n
= NULL
;
2445 wxDataViewTreeNodes nodes
= node
->GetNodes();
2446 int len
= nodes
.GetCount();
2447 for( int i
= 0 ; i
< len
; i
++)
2449 if( nodes
[i
]->GetItem() == item
)
2456 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2458 node
->GetNodes().Remove( n
);
2459 sub
-= n
->GetSubTreeCount();
2460 DestroyTreeHelper(n
);
2462 //Make the row number invalid and get a new valid one when user call GetRowCount
2464 node
->ChangeSubTreeCount(sub
);
2465 if( node
->GetChildrenNumber() == 0)
2467 node
->GetParent()->GetNodes().Remove( node
);
2471 //Change the current row to the last row if the current exceed the max row number
2472 if( m_currentRow
> GetRowCount() )
2473 m_currentRow
= m_count
- 1;
2480 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2486 wxWindow
*parent
= GetParent();
2487 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2488 le
.SetEventObject(parent
);
2489 le
.SetModel(GetOwner()->GetModel());
2491 parent
->GetEventHandler()->ProcessEvent(le
);
2496 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2498 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2499 /*#define MAX_VIRTUAL_WIDTH 100000
2501 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2502 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2503 Refresh( true, &rect );
2511 wxWindow
*parent
= GetParent();
2512 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2513 le
.SetEventObject(parent
);
2514 le
.SetModel(GetOwner()->GetModel());
2517 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2518 parent
->GetEventHandler()->ProcessEvent(le
);
2523 bool wxDataViewMainWindow::Cleared()
2528 BuildTree( GetOwner()->GetModel() );
2535 void wxDataViewMainWindow::UpdateDisplay()
2540 void wxDataViewMainWindow::OnInternalIdle()
2542 wxWindow::OnInternalIdle();
2546 RecalculateDisplay();
2551 void wxDataViewMainWindow::RecalculateDisplay()
2553 wxDataViewModel
*model
= GetOwner()->GetModel();
2560 int width
= GetEndOfLastCol();
2561 int height
= GetRowCount() * m_lineHeight
;
2563 SetVirtualSize( width
, height
);
2564 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2569 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2571 wxWindow::ScrollWindow( dx
, dy
, rect
);
2573 if (GetOwner()->m_headerArea
)
2574 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2577 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2580 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2581 int sy
= rows
*m_lineHeight
/y
;
2585 wxRect rect
= GetClientRect();
2587 int x_start
= 0, x_end
= 0, w
= 0;
2589 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2590 for (x_start
= 0; colnum
< column
; colnum
++)
2592 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2593 if (col
->IsHidden())
2594 continue; // skip it!
2596 w
= col
->GetWidth();
2600 x_end
= x_start
+ w
;
2601 xe
= xx
+ rect
.width
;
2604 sx
= ( xx
+ x_end
- xe
)/x
;
2611 m_owner
->Scroll( sx
, sy
);
2614 int wxDataViewMainWindow::GetCountPerPage() const
2616 wxSize size
= GetClientSize();
2617 return size
.y
/ m_lineHeight
;
2620 int wxDataViewMainWindow::GetEndOfLastCol() const
2624 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2626 const wxDataViewColumn
*c
=
2627 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2630 width
+= c
->GetWidth();
2635 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2639 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2641 return y
/ m_lineHeight
;
2644 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2646 wxSize client_size
= GetClientSize();
2647 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2648 &client_size
.x
, &client_size
.y
);
2650 //we should deal with the pixel here
2651 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2653 return wxMin( GetRowCount()-1, row
);
2656 unsigned int wxDataViewMainWindow::GetRowCount()
2658 if ( m_count
== -1 )
2660 m_count
= RecalculateCount();
2662 GetVirtualSize( &width
, &height
);
2663 height
= m_count
* m_lineHeight
;
2665 SetVirtualSize( width
, height
);
2670 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2677 void wxDataViewMainWindow::SelectAllRows( bool on
)
2684 m_selection
.Clear();
2685 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2686 m_selection
.Add( i
);
2691 unsigned int first_visible
= GetFirstVisibleRow();
2692 unsigned int last_visible
= GetLastVisibleRow();
2694 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2696 unsigned int row
= m_selection
[i
];
2697 if ((row
>= first_visible
) && (row
<= last_visible
))
2700 m_selection
.Clear();
2704 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2706 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2710 m_selection
.Add( row
);
2718 m_selection
.Remove( row
);
2724 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2728 unsigned int tmp
= from
;
2734 for (i
= from
; i
<= to
; i
++)
2736 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2739 m_selection
.Add( i
);
2744 m_selection
.Remove( i
);
2747 RefreshRows( from
, to
);
2750 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2752 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2754 int n
= aSelections
[i
];
2756 m_selection
.Add( n
);
2761 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2763 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2764 m_selection
.Add( row
);
2766 m_selection
.Remove( row
);
2770 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2772 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2775 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2777 wxWindow
*parent
= GetParent();
2778 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2780 le
.SetEventObject(parent
);
2781 le
.SetModel(GetOwner()->GetModel());
2784 parent
->GetEventHandler()->ProcessEvent(le
);
2787 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2789 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2790 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2792 wxSize client_size
= GetClientSize();
2793 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2794 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2795 if (intersect_rect
.width
> 0)
2796 Refresh( true, &intersect_rect
);
2799 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2803 unsigned int tmp
= to
;
2808 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2809 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2811 wxSize client_size
= GetClientSize();
2812 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2813 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2814 if (intersect_rect
.width
> 0)
2815 Refresh( true, &intersect_rect
);
2818 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2820 unsigned int count
= GetRowCount();
2821 if (firstRow
> count
)
2824 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2825 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2827 wxSize client_size
= GetClientSize();
2828 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2829 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2830 if (intersect_rect
.width
> 0)
2831 Refresh( true, &intersect_rect
);
2834 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2836 wxCHECK_RET( newCurrent
< GetRowCount(),
2837 _T("invalid item index in OnArrowChar()") );
2839 // if there is no selection, we cannot move it anywhere
2840 if (!HasCurrentRow())
2843 unsigned int oldCurrent
= m_currentRow
;
2845 // in single selection we just ignore Shift as we can't select several
2847 if ( event
.ShiftDown() && !IsSingleSel() )
2849 RefreshRow( oldCurrent
);
2851 ChangeCurrentRow( newCurrent
);
2853 // select all the items between the old and the new one
2854 if ( oldCurrent
> newCurrent
)
2856 newCurrent
= oldCurrent
;
2857 oldCurrent
= m_currentRow
;
2860 SelectRows( oldCurrent
, newCurrent
, true );
2861 if (oldCurrent
!=newCurrent
)
2862 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2866 RefreshRow( oldCurrent
);
2868 // all previously selected items are unselected unless ctrl is held
2869 if ( !event
.ControlDown() )
2870 SelectAllRows(false);
2872 ChangeCurrentRow( newCurrent
);
2874 if ( !event
.ControlDown() )
2876 SelectRow( m_currentRow
, true );
2877 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2880 RefreshRow( m_currentRow
);
2883 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2886 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2890 rect
.y
= m_lineHeight
* row
;
2891 rect
.width
= GetEndOfLastCol();
2892 rect
.height
= m_lineHeight
;
2897 class RowToItemJob
: public DoJob
2900 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2901 virtual ~RowToItemJob(){};
2903 virtual int operator() ( wxDataViewTreeNode
* node
)
2906 if( current
== static_cast<int>(row
))
2908 ret
= node
->GetItem() ;
2912 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2914 current
+= node
->GetSubTreeCount();
2919 //If the current has no child node, we can find the desired item of the row number directly.
2920 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2921 if( node
->GetNodes().GetCount() == 0)
2923 int index
= static_cast<int>(row
) - current
- 1;
2924 ret
= node
->GetChildren().Item( index
);
2931 virtual int operator() ( void * n
)
2934 if( current
== static_cast<int>(row
))
2936 ret
= wxDataViewItem( n
) ;
2941 wxDataViewItem
GetResult(){ return ret
; }
2948 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2952 return wxDataViewItem( (void*) row
);
2956 RowToItemJob
job( row
, -2 );
2957 Walker( m_root
, job
);
2958 return job
.GetResult();
2962 class RowToTreeNodeJob
: public DoJob
2965 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2968 this->current
= current
;
2972 virtual ~RowToTreeNodeJob(){};
2974 virtual int operator() ( wxDataViewTreeNode
* node
)
2977 if( current
== static_cast<int>(row
))
2983 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2985 current
+= node
->GetSubTreeCount();
2991 //If the current has no child node, we can find the desired item of the row number directly.
2992 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2993 if( node
->GetNodes().GetCount() == 0)
2995 int index
= static_cast<int>(row
) - current
- 1;
2996 void * n
= node
->GetChildren().Item( index
);
2997 ret
= new wxDataViewTreeNode( parent
) ;
2998 ret
->SetItem( wxDataViewItem( n
));
2999 ret
->SetHasChildren(false);
3008 virtual int operator() ( void * n
)
3011 if( current
== static_cast<int>(row
))
3013 ret
= new wxDataViewTreeNode( parent
) ;
3014 ret
->SetItem( wxDataViewItem( n
));
3015 ret
->SetHasChildren(false);
3021 wxDataViewTreeNode
* GetResult(){ return ret
; }
3025 wxDataViewTreeNode
* ret
;
3026 wxDataViewTreeNode
* parent
;
3030 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
3038 RowToTreeNodeJob
job( row
, -2, m_root
);
3039 Walker( m_root
, job
);
3040 return job
.GetResult();
3044 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3046 wxWindow
*parent
= GetParent();
3047 wxDataViewEvent
le(type
, parent
->GetId());
3049 le
.SetEventObject(parent
);
3050 le
.SetModel(GetOwner()->GetModel());
3053 parent
->GetEventHandler()->ProcessEvent(le
);
3057 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3059 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3062 if( node
->HasChildren())
3064 if( !node
->IsOpen())
3066 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3067 //Check if the user prevent expanding
3068 if( e
.GetSkipped() )
3072 //Here I build the children of current node
3073 if( node
->GetChildrenNumber() == 0 )
3076 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3080 //Send the expanded event
3081 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3085 SelectRow( row
, false );
3086 SelectRow( row
+ 1, true );
3087 ChangeCurrentRow( row
+ 1 );
3088 SendSelectionChangedEvent( GetItemByRow(row
+1));
3096 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3098 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3101 wxDataViewTreeNode
* nd
= node
;
3103 if( node
->HasChildren() && node
->IsOpen() )
3105 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3106 if( e
.GetSkipped() )
3111 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3115 node
= node
->GetParent();
3118 int parent
= GetRowByItem( node
->GetItem() ) ;
3121 SelectRow( row
, false);
3122 SelectRow(parent
, true );
3123 ChangeCurrentRow( parent
);
3124 SendSelectionChangedEvent( node
->GetItem() );
3128 if( !nd
->HasChildren())
3133 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3135 wxDataViewModel
* model
= GetOwner()->GetModel();
3139 //Compose the a parent-chain of the finding item
3141 list
.DeleteContents( true );
3142 wxDataViewItem
it( item
);
3145 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3146 list
.Insert( pItem
);
3147 it
= model
->GetParent( it
);
3150 //Find the item along the parent-chain.
3151 //This algorithm is designed to speed up the node-finding method
3152 wxDataViewTreeNode
* node
= m_root
;
3153 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3155 if( node
->HasChildren() )
3157 if( node
->GetChildrenNumber() == 0 )
3160 BuildTreeHelper(model
, node
->GetItem(), node
);
3163 wxDataViewTreeNodes nodes
= node
->GetNodes();
3165 for (; i
< nodes
.GetCount(); i
++)
3167 if (nodes
[i
]->GetItem() == (**iter
))
3173 if (i
== nodes
.GetCount())
3182 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3184 wxDataViewColumn
*col
= NULL
;
3185 unsigned int cols
= GetOwner()->GetColumnCount();
3186 unsigned int colnum
= 0;
3187 unsigned int x_start
= 0;
3189 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3190 for (x_start
= 0; colnum
< cols
; colnum
++)
3192 col
= GetOwner()->GetColumn(colnum
);
3193 if (col
->IsHidden())
3194 continue; // skip it!
3196 unsigned int w
= col
->GetWidth();
3197 if (x_start
+w
>= (unsigned int)x
)
3204 item
= GetItemByRow( y
/m_lineHeight
);
3207 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3209 int row
= GetRowByItem(item
);
3210 int y
= row
*m_lineHeight
;
3211 int h
= m_lineHeight
;
3213 wxDataViewColumn
*col
= NULL
;
3214 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3216 col
= GetOwner()->GetColumn( i
);
3217 x
+= col
->GetWidth();
3218 if( GetOwner()->GetColumn(i
+1) == column
)
3221 int w
= col
->GetWidth();
3222 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3223 return wxRect(x
, y
, w
, h
);
3226 int wxDataViewMainWindow::RecalculateCount()
3230 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3232 return list_model
->GetLastIndex() + 1;
3234 return list_model
->GetLastIndex() - 1;
3239 return m_root
->GetSubTreeCount();
3243 class ItemToRowJob
: public DoJob
3246 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3247 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3248 virtual ~ItemToRowJob(){};
3250 //Maybe binary search will help to speed up this process
3251 virtual int operator() ( wxDataViewTreeNode
* node
)
3254 if( node
->GetItem() == item
)
3259 if( node
->GetItem() == **m_iter
)
3266 ret
+= node
->GetSubTreeCount();
3272 virtual int operator() ( void * n
)
3275 if( n
== item
.GetID() )
3279 //the row number is begin from zero
3280 int GetResult(){ return ret
-1 ; }
3282 ItemList::const_iterator m_iter
;
3283 wxDataViewItem item
;
3288 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3290 wxDataViewModel
* model
= GetOwner()->GetModel();
3296 return wxPtrToUInt( item
.GetID() );
3303 //Compose the a parent-chain of the finding item
3305 wxDataViewItem
* pItem
= NULL
;
3306 list
.DeleteContents( true );
3307 wxDataViewItem
it( item
);
3310 pItem
= new wxDataViewItem( it
);
3311 list
.Insert( pItem
);
3312 it
= model
->GetParent( it
);
3314 pItem
= new wxDataViewItem( );
3315 list
.Insert( pItem
);
3317 ItemToRowJob
job( item
, list
.begin() );
3318 Walker(m_root
, job
);
3319 return job
.GetResult();
3323 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3325 if( !model
->IsContainer( item
) )
3328 wxDataViewItemArray children
;
3329 unsigned int num
= model
->GetChildren( item
, children
);
3330 unsigned int index
= 0;
3331 while( index
< num
)
3333 if( model
->IsContainer( children
[index
] ) )
3335 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3336 n
->SetItem(children
[index
]);
3337 n
->SetHasChildren( true ) ;
3342 node
->AddLeaf( children
[index
].GetID() );
3346 node
->SetSubTreeCount( num
);
3347 wxDataViewTreeNode
* n
= node
->GetParent();
3349 n
->ChangeSubTreeCount(num
);
3353 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3357 if (GetOwner()->GetModel()->IsIndexListModel())
3363 m_root
= new wxDataViewTreeNode( NULL
);
3364 m_root
->SetHasChildren(true);
3366 //First we define a invalid item to fetch the top-level elements
3367 wxDataViewItem item
;
3369 BuildTreeHelper( model
, item
, m_root
);
3373 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3375 if( node
->GetNodeNumber() != 0 )
3377 int len
= node
->GetNodeNumber();
3379 wxDataViewTreeNodes nodes
= node
->GetNodes();
3380 for( ; i
< len
; i
++ )
3382 DestroyTreeHelper(nodes
[i
]);
3388 void wxDataViewMainWindow::DestroyTree()
3392 DestroyTreeHelper(m_root
);
3398 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3400 if ( HandleAsNavigationKey(event
) )
3403 // no item -> nothing to do
3404 if (!HasCurrentRow())
3410 // don't use m_linesPerPage directly as it might not be computed yet
3411 const int pageSize
= GetCountPerPage();
3412 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3414 switch ( event
.GetKeyCode() )
3418 if (m_currentRow
> 0)
3420 wxWindow
*parent
= GetParent();
3421 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3422 le
.SetItem( GetItemByRow(m_currentRow
) );
3423 le
.SetEventObject(parent
);
3424 le
.SetModel(GetOwner()->GetModel());
3426 parent
->GetEventHandler()->ProcessEvent(le
);
3431 if ( m_currentRow
> 0 )
3432 OnArrowChar( m_currentRow
- 1, event
);
3436 if ( m_currentRow
< GetRowCount() - 1 )
3437 OnArrowChar( m_currentRow
+ 1, event
);
3439 //Add the process for tree expanding/collapsing
3441 OnCollapsing(m_currentRow
);
3444 OnExpanding( m_currentRow
);
3448 OnArrowChar( GetRowCount() - 1, event
);
3453 OnArrowChar( 0, event
);
3458 int steps
= pageSize
- 1;
3459 int index
= m_currentRow
- steps
;
3463 OnArrowChar( index
, event
);
3469 int steps
= pageSize
- 1;
3470 unsigned int index
= m_currentRow
+ steps
;
3471 unsigned int count
= GetRowCount();
3472 if ( index
>= count
)
3475 OnArrowChar( index
, event
);
3484 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3486 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3488 // let the base handle mouse wheel events.
3493 int x
= event
.GetX();
3494 int y
= event
.GetY();
3495 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3496 wxDataViewColumn
*col
= NULL
;
3499 unsigned int cols
= GetOwner()->GetColumnCount();
3501 for (i
= 0; i
< cols
; i
++)
3503 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3505 continue; // skip it!
3507 if (x
< xpos
+ c
->GetWidth())
3512 xpos
+= c
->GetWidth();
3517 wxDataViewRenderer
*cell
= col
->GetRenderer();
3518 unsigned int current
= y
/ m_lineHeight
;
3519 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3521 // Unselect all if below the last row ?
3525 //Test whether the mouse is hovered on the tree item button
3527 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3529 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3530 if( node
!=NULL
&& node
->HasChildren() )
3532 int indent
= node
->GetIndentLevel();
3533 indent
= GetOwner()->GetIndent()*indent
;
3534 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3535 if( rect
.Contains( x
, y
) )
3537 //So the mouse is over the expander
3539 if (m_underMouse
&& m_underMouse
!= node
)
3541 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3542 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3544 if (m_underMouse
!= node
)
3546 //wxLogMessage("Do the row: %d", current);
3547 RefreshRow(current
);
3549 m_underMouse
= node
;
3552 if (node
!=NULL
&& !node
->HasChildren())
3557 if (m_underMouse
!= NULL
)
3559 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3560 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3561 m_underMouse
= NULL
;
3565 wxDataViewModel
*model
= GetOwner()->GetModel();
3567 if (event
.Dragging())
3569 if (m_dragCount
== 0)
3571 // we have to report the raw, physical coords as we want to be
3572 // able to call HitTest(event.m_pointDrag) from the user code to
3573 // get the item being dragged
3574 m_dragStart
= event
.GetPosition();
3579 if (m_dragCount
!= 3)
3582 if (event
.LeftIsDown())
3584 // Notify cell about drag
3593 bool forceClick
= false;
3595 if (event
.ButtonDClick())
3597 m_renameTimer
->Stop();
3598 m_lastOnSame
= false;
3601 wxDataViewItem item
= GetItemByRow(current
);
3602 bool ignore_other_columns
=
3603 ((GetOwner()->GetExpanderColumn() != col
) &&
3604 (model
->IsContainer(item
)) &&
3605 (!model
->HasContainerColumns(item
)));
3607 if (event
.LeftDClick())
3609 if ( current
== m_lineLastClicked
)
3611 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3614 model
->GetValue( value
, item
, col
->GetModelColumn() );
3615 cell
->SetValue( value
);
3616 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3617 col
->GetWidth(), m_lineHeight
);
3618 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3623 wxWindow
*parent
= GetParent();
3624 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3626 le
.SetEventObject(parent
);
3627 le
.SetModel(GetOwner()->GetModel());
3629 parent
->GetEventHandler()->ProcessEvent(le
);
3635 // The first click was on another item, so don't interpret this as
3636 // a double click, but as a simple click instead
3643 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3645 // select single line
3646 SelectAllRows( false );
3647 SelectRow( m_lineSelectSingleOnUp
, true );
3650 //Process the event of user clicking the expander
3651 bool expander
= false;
3652 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3654 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3655 if( node
!=NULL
&& node
->HasChildren() )
3657 int indent
= node
->GetIndentLevel();
3658 indent
= GetOwner()->GetIndent()*indent
;
3659 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3660 if( rect
.Contains( x
, y
) )
3663 if( node
->IsOpen() )
3664 OnCollapsing(current
);
3666 OnExpanding( current
);
3670 //If the user click the expander, we do not do editing even if the column with expander are editable
3671 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3673 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3674 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3676 m_renameTimer
->Start( 100, true );
3680 m_lastOnSame
= false;
3681 m_lineSelectSingleOnUp
= (unsigned int)-1;
3685 // This is necessary, because after a DnD operation in
3686 // from and to ourself, the up event is swallowed by the
3687 // DnD code. So on next non-up event (which means here and
3688 // now) m_lineSelectSingleOnUp should be reset.
3689 m_lineSelectSingleOnUp
= (unsigned int)-1;
3692 if (event
.RightDown())
3694 m_lineBeforeLastClicked
= m_lineLastClicked
;
3695 m_lineLastClicked
= current
;
3697 // If the item is already selected, do not update the selection.
3698 // Multi-selections should not be cleared if a selected item is clicked.
3699 if (!IsRowSelected(current
))
3701 SelectAllRows(false);
3702 ChangeCurrentRow(current
);
3703 SelectRow(m_currentRow
,true);
3704 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3708 model
->GetValue( value
, item
, col
->GetModelColumn() );
3709 wxWindow
*parent
= GetParent();
3710 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3712 le
.SetEventObject(parent
);
3713 le
.SetModel(GetOwner()->GetModel());
3715 parent
->GetEventHandler()->ProcessEvent(le
);
3717 else if (event
.MiddleDown())
3720 if (event
.LeftDown() || forceClick
)
3724 m_lineBeforeLastClicked
= m_lineLastClicked
;
3725 m_lineLastClicked
= current
;
3727 unsigned int oldCurrentRow
= m_currentRow
;
3728 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3730 bool cmdModifierDown
= event
.CmdDown();
3731 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3733 if ( IsSingleSel() || !IsRowSelected(current
) )
3735 SelectAllRows( false );
3736 ChangeCurrentRow(current
);
3737 SelectRow(m_currentRow
,true);
3738 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3740 else // multi sel & current is highlighted & no mod keys
3742 m_lineSelectSingleOnUp
= current
;
3743 ChangeCurrentRow(current
); // change focus
3746 else // multi sel & either ctrl or shift is down
3748 if (cmdModifierDown
)
3750 ChangeCurrentRow(current
);
3751 ReverseRowSelection(m_currentRow
);
3752 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3754 else if (event
.ShiftDown())
3756 ChangeCurrentRow(current
);
3758 unsigned int lineFrom
= oldCurrentRow
,
3761 if ( lineTo
< lineFrom
)
3764 lineFrom
= m_currentRow
;
3767 SelectRows(lineFrom
, lineTo
, true);
3768 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3770 else // !ctrl, !shift
3772 // test in the enclosing if should make it impossible
3773 wxFAIL_MSG( _T("how did we get here?") );
3777 if (m_currentRow
!= oldCurrentRow
)
3778 RefreshRow( oldCurrentRow
);
3780 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3782 // Update selection here...
3785 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3786 (current
== oldCurrentRow
)) && oldWasSelected
;
3788 // Call LeftClick after everything else as under GTK+
3789 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3791 // notify cell about right click
3793 model
->GetValue( value
, item
, col
->GetModelColumn() );
3794 cell
->SetValue( value
);
3795 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3796 col
->GetWidth(), m_lineHeight
);
3797 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3802 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3806 if (HasCurrentRow())
3812 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3816 if (HasCurrentRow())
3822 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3824 if( m_selection
.GetCount() != 1 )
3825 return wxDataViewItem();
3827 return GetItemByRow( m_selection
.Item(0));
3830 //-----------------------------------------------------------------------------
3832 //-----------------------------------------------------------------------------
3833 WX_DEFINE_LIST(wxDataViewColumnList
)
3835 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3837 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3838 EVT_SIZE(wxDataViewCtrl::OnSize
)
3841 wxDataViewCtrl::~wxDataViewCtrl()
3844 GetModel()->RemoveNotifier( m_notifier
);
3847 void wxDataViewCtrl::Init()
3852 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3853 const wxPoint
& pos
, const wxSize
& size
,
3854 long style
, const wxValidator
& validator
)
3856 if ( (style
& wxBORDER_MASK
) == 0)
3857 style
|= wxBORDER_SUNKEN
;
3859 if (!wxControl::Create( parent
, id
, pos
, size
,
3860 style
| wxScrolledWindowStyle
, validator
))
3863 SetInitialSize(size
);
3868 MacSetClipChildren( true ) ;
3871 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3873 if (HasFlag(wxDV_NO_HEADER
))
3874 m_headerArea
= NULL
;
3876 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3878 SetTargetWindow( m_clientArea
);
3880 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3882 sizer
->Add( m_headerArea
, 0, wxGROW
);
3883 sizer
->Add( m_clientArea
, 1, wxGROW
);
3890 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3894 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3897 // we need to process arrows ourselves for scrolling
3898 if ( nMsg
== WM_GETDLGCODE
)
3900 rc
|= DLGC_WANTARROWS
;
3908 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3910 // We need to override OnSize so that our scrolled
3911 // window a) does call Layout() to use sizers for
3912 // positioning the controls but b) does not query
3913 // the sizer for their size and use that for setting
3914 // the scrollable area as set that ourselves by
3915 // calling SetScrollbar() further down.
3922 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3924 if (!wxDataViewCtrlBase::AssociateModel( model
))
3927 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3929 model
->AddNotifier( m_notifier
);
3931 m_clientArea
->DestroyTree();
3933 m_clientArea
->BuildTree(model
);
3935 m_clientArea
->UpdateDisplay();
3940 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3942 if (!wxDataViewCtrlBase::AppendColumn(col
))
3945 m_cols
.Append( col
);
3950 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3952 if (!wxDataViewCtrlBase::PrependColumn(col
))
3955 m_cols
.Insert( col
);
3960 void wxDataViewCtrl::OnColumnChange()
3963 m_headerArea
->UpdateDisplay();
3965 m_clientArea
->UpdateDisplay();
3968 void wxDataViewCtrl::DoSetExpanderColumn()
3970 m_clientArea
->UpdateDisplay();
3973 void wxDataViewCtrl::DoSetIndent()
3975 m_clientArea
->UpdateDisplay();
3978 unsigned int wxDataViewCtrl::GetColumnCount() const
3980 return m_cols
.GetCount();
3983 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3985 wxDataViewColumnList::const_iterator iter
;
3987 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3992 if ((*iter
)->IsHidden())
3999 void wxDataViewCtrl::ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
)
4001 if (new_pos
> m_cols
.GetCount()) return;
4002 m_cols
.DeleteObject( col
);
4003 m_cols
.Insert( new_pos
, col
);
4005 m_clientArea
->UpdateDisplay();
4008 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4010 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4021 bool wxDataViewCtrl::ClearColumns()
4028 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4030 int ret
= 0, dead
= 0;
4031 int len
= GetColumnCount();
4032 for (int i
=0; i
<len
; i
++)
4034 wxDataViewColumn
* col
= GetColumn(i
);
4035 if (col
->IsHidden())
4037 ret
+= col
->GetWidth();
4040 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
4047 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4052 //Selection code with wxDataViewItem as parameters
4053 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4055 return m_clientArea
->GetSelection();
4058 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4061 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4062 int len
= selection
.GetCount();
4063 for( int i
= 0; i
< len
; i
++)
4065 unsigned int row
= selection
[i
];
4066 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4071 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4073 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4074 int len
= sel
.GetCount();
4075 for( int i
= 0; i
< len
; i
++ )
4077 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4079 selection
.Add( static_cast<unsigned int>(row
) );
4081 m_clientArea
->SetSelections( selection
);
4084 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4086 int row
= m_clientArea
->GetRowByItem( item
);
4089 //Unselect all rows before select another in the single select mode
4090 if (m_clientArea
->IsSingleSel())
4091 m_clientArea
->SelectAllRows(false);
4092 m_clientArea
->SelectRow(row
, true);
4096 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4098 int row
= m_clientArea
->GetRowByItem( item
);
4100 m_clientArea
->SelectRow(row
, false);
4103 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4105 int row
= m_clientArea
->GetRowByItem( item
);
4108 return m_clientArea
->IsRowSelected(row
);
4113 //Selection code with row number as parameter
4114 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4117 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4118 int len
= selection
.GetCount();
4119 for( int i
= 0; i
< len
; i
++)
4121 unsigned int row
= selection
[i
];
4127 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4129 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4130 int len
= sel
.GetCount();
4131 for( int i
= 0; i
< len
; i
++ )
4135 selection
.Add( static_cast<unsigned int>(row
) );
4137 m_clientArea
->SetSelections( selection
);
4140 void wxDataViewCtrl::Select( int row
)
4144 if (m_clientArea
->IsSingleSel())
4145 m_clientArea
->SelectAllRows(false);
4146 m_clientArea
->SelectRow( row
, true );
4150 void wxDataViewCtrl::Unselect( int row
)
4153 m_clientArea
->SelectRow(row
, false);
4156 bool wxDataViewCtrl::IsSelected( int row
) const
4159 return m_clientArea
->IsRowSelected(row
);
4163 void wxDataViewCtrl::SelectRange( int from
, int to
)
4166 for( int i
= from
; i
< to
; i
++ )
4168 m_clientArea
->Select(sel
);
4171 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4173 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4174 for( int i
= from
; i
< to
; i
++ )
4175 if( sel
.Index( i
) != wxNOT_FOUND
)
4177 m_clientArea
->SetSelections(sel
);
4180 void wxDataViewCtrl::SelectAll()
4182 m_clientArea
->SelectAllRows(true);
4185 void wxDataViewCtrl::UnselectAll()
4187 m_clientArea
->SelectAllRows(false);
4190 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4194 if( row
> (int) m_clientArea
->GetRowCount() )
4195 row
= m_clientArea
->GetRowCount();
4197 int first
= m_clientArea
->GetFirstVisibleRow();
4198 int last
= m_clientArea
->GetLastVisibleRow();
4200 m_clientArea
->ScrollTo( row
, column
);
4201 else if( row
> last
)
4202 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4204 m_clientArea
->ScrollTo( first
, column
);
4207 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4209 int row
= m_clientArea
->GetRowByItem(item
);
4212 if( column
== NULL
)
4213 EnsureVisible(row
, -1);
4217 int len
= GetColumnCount();
4218 for( int i
= 0; i
< len
; i
++ )
4219 if( GetColumn(i
) == column
)
4224 EnsureVisible( row
, col
);
4230 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4232 m_clientArea
->HitTest(point
, item
, column
);
4235 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4237 return m_clientArea
->GetItemRect(item
, column
);
4240 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4242 return m_clientArea
->GetItemByRow( row
);
4245 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4247 return m_clientArea
->GetRowByItem( item
);
4250 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4252 int row
= m_clientArea
->GetRowByItem( item
);
4254 m_clientArea
->Expand(row
);
4257 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4259 int row
= m_clientArea
->GetRowByItem( item
);
4261 m_clientArea
->Collapse(row
);
4265 // !wxUSE_GENERICDATAVIEWCTRL
4268 // wxUSE_DATAVIEWCTRL