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 ((nmHDR
->iItem
!= -1) && (!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 static 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
);
2199 //force the expander column to left-center align
2200 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2202 if (node
&& !node
->HasChildren())
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
2209 // cannot be bigger than allocated space
2210 wxSize size
= cell
->GetSize();
2211 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2212 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2213 // size.y = wxMin( size.y, cell_rect.height );
2214 size
.y
= cell_rect
.height
;
2216 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2217 int align
= cell
->GetAlignment();
2219 // horizontal alignment:
2220 item_rect
.x
= cell_rect
.x
;
2221 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2222 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2223 else if (align
& wxALIGN_RIGHT
)
2224 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2225 //else: wxALIGN_LEFT is the default
2227 // vertical alignment:
2228 item_rect
.y
= cell_rect
.y
;
2229 if (align
& wxALIGN_CENTER_VERTICAL
)
2230 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2231 else if (align
& wxALIGN_BOTTOM
)
2232 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2233 //else: wxALIGN_TOP is the default
2236 item_rect
.x
+= PADDING_RIGHTLEFT
;
2237 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2239 //Here we add the tree indent
2240 item_rect
.x
+= indent
;
2243 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2244 state
|= wxDATAVIEW_CELL_SELECTED
;
2246 // TODO: it would be much more efficient to create a clipping
2247 // region for the entire column being rendered (in the OnPaint
2248 // of wxDataViewMainWindow) instead of a single clip region for
2249 // each cell. However it would mean that each renderer should
2250 // respect the given wxRect's top & bottom coords, eventually
2251 // violating only the left & right coords - however the user can
2252 // make its own renderer and thus we cannot be sure of that.
2253 dc
.SetClippingRegion( item_rect
);
2254 cell
->Render( item_rect
, &dc
, state
);
2255 dc
.DestroyClippingRegion();
2258 cell_rect
.x
+= cell_rect
.width
;
2262 void wxDataViewMainWindow::OnRenameTimer()
2264 // We have to call this here because changes may just have
2265 // been made and no screen update taken place.
2270 unsigned int cols
= GetOwner()->GetColumnCount();
2272 for (i
= 0; i
< cols
; i
++)
2274 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2276 continue; // skip it!
2278 if (c
== m_currentCol
)
2280 xpos
+= c
->GetWidth();
2282 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2283 m_currentCol
->GetWidth(), m_lineHeight
);
2285 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2286 &labelRect
.x
, &labelRect
.y
);
2288 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2289 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2293 //------------------------------------------------------------------
2294 // Helper class for do operation on the tree node
2295 //------------------------------------------------------------------
2302 //The return value control how the tree-walker tranverse the tree
2303 // 0: Job done, stop tranverse and return
2304 // 1: Ignore the current node's subtree and continue
2305 // 2: Job not done, continue
2306 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2307 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2308 virtual int operator() ( void * n
) = 0;
2311 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2316 switch( func( node
) )
2327 wxDataViewTreeNodes nodes
= node
->GetNodes();
2328 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2330 int len_nodes
= nodes
.GetCount();
2331 int len
= leaves
.GetCount();
2332 int i
= 0, nodes_i
= 0;
2334 for( ; i
< len
; i
++ )
2336 void * n
= leaves
[i
];
2337 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2339 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2342 if( Walker( nd
, func
) )
2361 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2372 wxDataViewTreeNode
* node
;
2373 node
= FindNode(parent
);
2378 node
->SetHasChildren( true );
2380 if( g_model
->IsContainer( item
) )
2382 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2383 newnode
->SetItem(item
);
2384 newnode
->SetHasChildren( true );
2385 node
->AddNode( newnode
);
2388 node
->AddLeaf( item
.GetID() );
2390 node
->ChangeSubTreeCount(1);
2398 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2400 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2401 const wxDataViewItem
& item
)
2406 if( m_currentRow
> GetRowCount() )
2407 m_currentRow
= m_count
- 1;
2409 m_selection
.Empty();
2416 wxDataViewTreeNode
* node
= FindNode(parent
);
2418 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2419 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2422 node
->GetChildren().Remove( item
.GetID() );
2423 //Manuplate selection
2424 if( m_selection
.GetCount() > 1 )
2426 m_selection
.Empty();
2428 bool isContainer
= false;
2429 wxDataViewTreeNodes nds
= node
->GetNodes();
2430 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2432 if (nds
[i
]->GetItem() == item
)
2440 wxDataViewTreeNode
* n
= NULL
;
2441 wxDataViewTreeNodes nodes
= node
->GetNodes();
2442 int len
= nodes
.GetCount();
2443 for( int i
= 0 ; i
< len
; i
++)
2445 if( nodes
[i
]->GetItem() == item
)
2452 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2454 node
->GetNodes().Remove( n
);
2455 sub
-= n
->GetSubTreeCount();
2456 ::DestroyTreeHelper(n
);
2458 //Make the row number invalid and get a new valid one when user call GetRowCount
2460 node
->ChangeSubTreeCount(sub
);
2461 if( node
->GetChildrenNumber() == 0)
2463 node
->GetParent()->GetNodes().Remove( node
);
2467 //Change the current row to the last row if the current exceed the max row number
2468 if( m_currentRow
> GetRowCount() )
2469 m_currentRow
= m_count
- 1;
2476 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2482 wxWindow
*parent
= GetParent();
2483 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2484 le
.SetEventObject(parent
);
2485 le
.SetModel(GetOwner()->GetModel());
2487 parent
->GetEventHandler()->ProcessEvent(le
);
2492 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2494 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2495 /*#define MAX_VIRTUAL_WIDTH 100000
2497 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2498 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2499 Refresh( true, &rect );
2507 wxWindow
*parent
= GetParent();
2508 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2509 le
.SetEventObject(parent
);
2510 le
.SetModel(GetOwner()->GetModel());
2513 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2514 parent
->GetEventHandler()->ProcessEvent(le
);
2519 bool wxDataViewMainWindow::Cleared()
2524 BuildTree( GetOwner()->GetModel() );
2531 void wxDataViewMainWindow::UpdateDisplay()
2536 void wxDataViewMainWindow::OnInternalIdle()
2538 wxWindow::OnInternalIdle();
2542 RecalculateDisplay();
2547 void wxDataViewMainWindow::RecalculateDisplay()
2549 wxDataViewModel
*model
= GetOwner()->GetModel();
2556 int width
= GetEndOfLastCol();
2557 int height
= GetRowCount() * m_lineHeight
;
2559 SetVirtualSize( width
, height
);
2560 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2565 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2567 wxWindow::ScrollWindow( dx
, dy
, rect
);
2569 if (GetOwner()->m_headerArea
)
2570 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2573 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2576 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2577 int sy
= rows
*m_lineHeight
/y
;
2581 wxRect rect
= GetClientRect();
2583 int x_start
= 0, x_end
= 0, w
= 0;
2585 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2586 for (x_start
= 0; colnum
< column
; colnum
++)
2588 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2589 if (col
->IsHidden())
2590 continue; // skip it!
2592 w
= col
->GetWidth();
2596 x_end
= x_start
+ w
;
2597 xe
= xx
+ rect
.width
;
2600 sx
= ( xx
+ x_end
- xe
)/x
;
2607 m_owner
->Scroll( sx
, sy
);
2610 int wxDataViewMainWindow::GetCountPerPage() const
2612 wxSize size
= GetClientSize();
2613 return size
.y
/ m_lineHeight
;
2616 int wxDataViewMainWindow::GetEndOfLastCol() const
2620 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2622 const wxDataViewColumn
*c
=
2623 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2626 width
+= c
->GetWidth();
2631 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2635 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2637 return y
/ m_lineHeight
;
2640 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2642 wxSize client_size
= GetClientSize();
2643 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2644 &client_size
.x
, &client_size
.y
);
2646 //we should deal with the pixel here
2647 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2649 return wxMin( GetRowCount()-1, row
);
2652 unsigned int wxDataViewMainWindow::GetRowCount()
2654 if ( m_count
== -1 )
2656 m_count
= RecalculateCount();
2658 GetVirtualSize( &width
, &height
);
2659 height
= m_count
* m_lineHeight
;
2661 SetVirtualSize( width
, height
);
2666 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2673 void wxDataViewMainWindow::SelectAllRows( bool on
)
2680 m_selection
.Clear();
2681 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2682 m_selection
.Add( i
);
2687 unsigned int first_visible
= GetFirstVisibleRow();
2688 unsigned int last_visible
= GetLastVisibleRow();
2690 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2692 unsigned int row
= m_selection
[i
];
2693 if ((row
>= first_visible
) && (row
<= last_visible
))
2696 m_selection
.Clear();
2700 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2702 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2706 m_selection
.Add( row
);
2714 m_selection
.Remove( row
);
2720 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2724 unsigned int tmp
= from
;
2730 for (i
= from
; i
<= to
; i
++)
2732 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2735 m_selection
.Add( i
);
2740 m_selection
.Remove( i
);
2743 RefreshRows( from
, to
);
2746 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2748 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2750 int n
= aSelections
[i
];
2752 m_selection
.Add( n
);
2757 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2759 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2760 m_selection
.Add( row
);
2762 m_selection
.Remove( row
);
2766 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2768 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2771 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2773 wxWindow
*parent
= GetParent();
2774 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2776 le
.SetEventObject(parent
);
2777 le
.SetModel(GetOwner()->GetModel());
2780 parent
->GetEventHandler()->ProcessEvent(le
);
2783 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2785 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2786 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2788 wxSize client_size
= GetClientSize();
2789 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2790 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2791 if (intersect_rect
.width
> 0)
2792 Refresh( true, &intersect_rect
);
2795 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2799 unsigned int tmp
= to
;
2804 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2805 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2807 wxSize client_size
= GetClientSize();
2808 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2809 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2810 if (intersect_rect
.width
> 0)
2811 Refresh( true, &intersect_rect
);
2814 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2816 unsigned int count
= GetRowCount();
2817 if (firstRow
> count
)
2820 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2821 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2823 wxSize client_size
= GetClientSize();
2824 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2825 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2826 if (intersect_rect
.width
> 0)
2827 Refresh( true, &intersect_rect
);
2830 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2832 wxCHECK_RET( newCurrent
< GetRowCount(),
2833 _T("invalid item index in OnArrowChar()") );
2835 // if there is no selection, we cannot move it anywhere
2836 if (!HasCurrentRow())
2839 unsigned int oldCurrent
= m_currentRow
;
2841 // in single selection we just ignore Shift as we can't select several
2843 if ( event
.ShiftDown() && !IsSingleSel() )
2845 RefreshRow( oldCurrent
);
2847 ChangeCurrentRow( newCurrent
);
2849 // select all the items between the old and the new one
2850 if ( oldCurrent
> newCurrent
)
2852 newCurrent
= oldCurrent
;
2853 oldCurrent
= m_currentRow
;
2856 SelectRows( oldCurrent
, newCurrent
, true );
2857 if (oldCurrent
!=newCurrent
)
2858 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2862 RefreshRow( oldCurrent
);
2864 // all previously selected items are unselected unless ctrl is held
2865 if ( !event
.ControlDown() )
2866 SelectAllRows(false);
2868 ChangeCurrentRow( newCurrent
);
2870 if ( !event
.ControlDown() )
2872 SelectRow( m_currentRow
, true );
2873 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2876 RefreshRow( m_currentRow
);
2879 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2882 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2886 rect
.y
= m_lineHeight
* row
;
2887 rect
.width
= GetEndOfLastCol();
2888 rect
.height
= m_lineHeight
;
2893 class RowToItemJob
: public DoJob
2896 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2897 virtual ~RowToItemJob(){};
2899 virtual int operator() ( wxDataViewTreeNode
* node
)
2902 if( current
== static_cast<int>(row
))
2904 ret
= node
->GetItem() ;
2908 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2910 current
+= node
->GetSubTreeCount();
2915 //If the current has no child node, we can find the desired item of the row number directly.
2916 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2917 if( node
->GetNodes().GetCount() == 0)
2919 int index
= static_cast<int>(row
) - current
- 1;
2920 ret
= node
->GetChildren().Item( index
);
2927 virtual int operator() ( void * n
)
2930 if( current
== static_cast<int>(row
))
2932 ret
= wxDataViewItem( n
) ;
2937 wxDataViewItem
GetResult(){ return ret
; }
2944 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2948 return wxDataViewItem( (void*) row
);
2952 RowToItemJob
job( row
, -2 );
2953 Walker( m_root
, job
);
2954 return job
.GetResult();
2958 class RowToTreeNodeJob
: public DoJob
2961 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2964 this->current
= current
;
2968 virtual ~RowToTreeNodeJob(){};
2970 virtual int operator() ( wxDataViewTreeNode
* node
)
2973 if( current
== static_cast<int>(row
))
2979 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2981 current
+= node
->GetSubTreeCount();
2987 //If the current has no child node, we can find the desired item of the row number directly.
2988 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2989 if( node
->GetNodes().GetCount() == 0)
2991 int index
= static_cast<int>(row
) - current
- 1;
2992 void * n
= node
->GetChildren().Item( index
);
2993 ret
= new wxDataViewTreeNode( parent
) ;
2994 ret
->SetItem( wxDataViewItem( n
));
2995 ret
->SetHasChildren(false);
3004 virtual int operator() ( void * n
)
3007 if( current
== static_cast<int>(row
))
3009 ret
= new wxDataViewTreeNode( parent
) ;
3010 ret
->SetItem( wxDataViewItem( n
));
3011 ret
->SetHasChildren(false);
3017 wxDataViewTreeNode
* GetResult(){ return ret
; }
3021 wxDataViewTreeNode
* ret
;
3022 wxDataViewTreeNode
* parent
;
3026 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
3034 RowToTreeNodeJob
job( row
, -2, m_root
);
3035 Walker( m_root
, job
);
3036 return job
.GetResult();
3040 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3042 wxWindow
*parent
= GetParent();
3043 wxDataViewEvent
le(type
, parent
->GetId());
3045 le
.SetEventObject(parent
);
3046 le
.SetModel(GetOwner()->GetModel());
3049 parent
->GetEventHandler()->ProcessEvent(le
);
3053 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3055 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3058 if( node
->HasChildren())
3060 if( !node
->IsOpen())
3062 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3063 //Check if the user prevent expanding
3064 if( e
.GetSkipped() )
3068 //Here I build the children of current node
3069 if( node
->GetChildrenNumber() == 0 )
3072 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3076 //Send the expanded event
3077 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3081 SelectRow( row
, false );
3082 SelectRow( row
+ 1, true );
3083 ChangeCurrentRow( row
+ 1 );
3084 SendSelectionChangedEvent( GetItemByRow(row
+1));
3092 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3094 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3097 wxDataViewTreeNode
* nd
= node
;
3099 if( node
->HasChildren() && node
->IsOpen() )
3101 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3102 if( e
.GetSkipped() )
3107 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3111 node
= node
->GetParent();
3114 int parent
= GetRowByItem( node
->GetItem() ) ;
3117 SelectRow( row
, false);
3118 SelectRow(parent
, true );
3119 ChangeCurrentRow( parent
);
3120 SendSelectionChangedEvent( node
->GetItem() );
3124 if( !nd
->HasChildren())
3129 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3131 wxDataViewModel
* model
= GetOwner()->GetModel();
3135 //Compose the a parent-chain of the finding item
3137 list
.DeleteContents( true );
3138 wxDataViewItem
it( item
);
3141 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3142 list
.Insert( pItem
);
3143 it
= model
->GetParent( it
);
3146 //Find the item along the parent-chain.
3147 //This algorithm is designed to speed up the node-finding method
3148 wxDataViewTreeNode
* node
= m_root
;
3149 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3151 if( node
->HasChildren() )
3153 if( node
->GetChildrenNumber() == 0 )
3156 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3159 wxDataViewTreeNodes nodes
= node
->GetNodes();
3161 for (; i
< nodes
.GetCount(); i
++)
3163 if (nodes
[i
]->GetItem() == (**iter
))
3169 if (i
== nodes
.GetCount())
3178 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3180 wxDataViewColumn
*col
= NULL
;
3181 unsigned int cols
= GetOwner()->GetColumnCount();
3182 unsigned int colnum
= 0;
3183 unsigned int x_start
= 0;
3185 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3186 for (x_start
= 0; colnum
< cols
; colnum
++)
3188 col
= GetOwner()->GetColumn(colnum
);
3189 if (col
->IsHidden())
3190 continue; // skip it!
3192 unsigned int w
= col
->GetWidth();
3193 if (x_start
+w
>= (unsigned int)x
)
3200 item
= GetItemByRow( y
/m_lineHeight
);
3203 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3205 int row
= GetRowByItem(item
);
3206 int y
= row
*m_lineHeight
;
3207 int h
= m_lineHeight
;
3209 wxDataViewColumn
*col
= NULL
;
3210 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3212 col
= GetOwner()->GetColumn( i
);
3213 x
+= col
->GetWidth();
3214 if( GetOwner()->GetColumn(i
+1) == column
)
3217 int w
= col
->GetWidth();
3218 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3219 return wxRect(x
, y
, w
, h
);
3222 int wxDataViewMainWindow::RecalculateCount()
3226 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3228 return list_model
->GetLastIndex() + 1;
3230 return list_model
->GetLastIndex() - 1;
3235 return m_root
->GetSubTreeCount();
3239 class ItemToRowJob
: public DoJob
3242 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3243 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3244 virtual ~ItemToRowJob(){};
3246 //Maybe binary search will help to speed up this process
3247 virtual int operator() ( wxDataViewTreeNode
* node
)
3250 if( node
->GetItem() == item
)
3255 if( node
->GetItem() == **m_iter
)
3262 ret
+= node
->GetSubTreeCount();
3268 virtual int operator() ( void * n
)
3271 if( n
== item
.GetID() )
3275 //the row number is begin from zero
3276 int GetResult(){ return ret
-1 ; }
3278 ItemList::const_iterator m_iter
;
3279 wxDataViewItem item
;
3284 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3286 wxDataViewModel
* model
= GetOwner()->GetModel();
3292 return wxPtrToUInt( item
.GetID() );
3299 //Compose the a parent-chain of the finding item
3301 wxDataViewItem
* pItem
= NULL
;
3302 list
.DeleteContents( true );
3303 wxDataViewItem
it( item
);
3306 pItem
= new wxDataViewItem( it
);
3307 list
.Insert( pItem
);
3308 it
= model
->GetParent( it
);
3310 pItem
= new wxDataViewItem( );
3311 list
.Insert( pItem
);
3313 ItemToRowJob
job( item
, list
.begin() );
3314 Walker(m_root
, job
);
3315 return job
.GetResult();
3319 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3321 if( !model
->IsContainer( item
) )
3324 wxDataViewItemArray children
;
3325 unsigned int num
= model
->GetChildren( item
, children
);
3326 unsigned int index
= 0;
3327 while( index
< num
)
3329 if( model
->IsContainer( children
[index
] ) )
3331 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3332 n
->SetItem(children
[index
]);
3333 n
->SetHasChildren( true ) ;
3338 node
->AddLeaf( children
[index
].GetID() );
3342 node
->SetSubTreeCount( num
);
3343 wxDataViewTreeNode
* n
= node
->GetParent();
3345 n
->ChangeSubTreeCount(num
);
3349 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3353 if (GetOwner()->GetModel()->IsIndexListModel())
3359 m_root
= new wxDataViewTreeNode( NULL
);
3360 m_root
->SetHasChildren(true);
3362 //First we define a invalid item to fetch the top-level elements
3363 wxDataViewItem item
;
3365 BuildTreeHelper( model
, item
, m_root
);
3369 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3371 if( node
->GetNodeNumber() != 0 )
3373 int len
= node
->GetNodeNumber();
3375 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3376 for( ; i
< len
; i
++ )
3378 DestroyTreeHelper(nodes
[i
]);
3384 void wxDataViewMainWindow::DestroyTree()
3388 ::DestroyTreeHelper(m_root
);
3394 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3396 if ( HandleAsNavigationKey(event
) )
3399 // no item -> nothing to do
3400 if (!HasCurrentRow())
3406 // don't use m_linesPerPage directly as it might not be computed yet
3407 const int pageSize
= GetCountPerPage();
3408 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3410 switch ( event
.GetKeyCode() )
3414 if (m_currentRow
> 0)
3416 wxWindow
*parent
= GetParent();
3417 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3418 le
.SetItem( GetItemByRow(m_currentRow
) );
3419 le
.SetEventObject(parent
);
3420 le
.SetModel(GetOwner()->GetModel());
3422 parent
->GetEventHandler()->ProcessEvent(le
);
3427 if ( m_currentRow
> 0 )
3428 OnArrowChar( m_currentRow
- 1, event
);
3432 if ( m_currentRow
< GetRowCount() - 1 )
3433 OnArrowChar( m_currentRow
+ 1, event
);
3435 //Add the process for tree expanding/collapsing
3437 OnCollapsing(m_currentRow
);
3440 OnExpanding( m_currentRow
);
3444 OnArrowChar( GetRowCount() - 1, event
);
3449 OnArrowChar( 0, event
);
3454 int steps
= pageSize
- 1;
3455 int index
= m_currentRow
- steps
;
3459 OnArrowChar( index
, event
);
3465 int steps
= pageSize
- 1;
3466 unsigned int index
= m_currentRow
+ steps
;
3467 unsigned int count
= GetRowCount();
3468 if ( index
>= count
)
3471 OnArrowChar( index
, event
);
3480 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3482 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3484 // let the base handle mouse wheel events.
3489 int x
= event
.GetX();
3490 int y
= event
.GetY();
3491 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3492 wxDataViewColumn
*col
= NULL
;
3495 unsigned int cols
= GetOwner()->GetColumnCount();
3497 for (i
= 0; i
< cols
; i
++)
3499 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3501 continue; // skip it!
3503 if (x
< xpos
+ c
->GetWidth())
3508 xpos
+= c
->GetWidth();
3513 wxDataViewRenderer
*cell
= col
->GetRenderer();
3514 unsigned int current
= y
/ m_lineHeight
;
3515 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3517 // Unselect all if below the last row ?
3521 //Test whether the mouse is hovered on the tree item button
3523 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3525 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3526 if( node
!=NULL
&& node
->HasChildren() )
3528 int indent
= node
->GetIndentLevel();
3529 indent
= GetOwner()->GetIndent()*indent
;
3530 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3531 if( rect
.Contains( x
, y
) )
3533 //So the mouse is over the expander
3535 if (m_underMouse
&& m_underMouse
!= node
)
3537 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3538 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3540 if (m_underMouse
!= node
)
3542 //wxLogMessage("Do the row: %d", current);
3543 RefreshRow(current
);
3545 m_underMouse
= node
;
3548 if (node
!=NULL
&& !node
->HasChildren())
3553 if (m_underMouse
!= NULL
)
3555 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3556 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3557 m_underMouse
= NULL
;
3561 wxDataViewModel
*model
= GetOwner()->GetModel();
3563 if (event
.Dragging())
3565 if (m_dragCount
== 0)
3567 // we have to report the raw, physical coords as we want to be
3568 // able to call HitTest(event.m_pointDrag) from the user code to
3569 // get the item being dragged
3570 m_dragStart
= event
.GetPosition();
3575 if (m_dragCount
!= 3)
3578 if (event
.LeftIsDown())
3580 // Notify cell about drag
3589 bool forceClick
= false;
3591 if (event
.ButtonDClick())
3593 m_renameTimer
->Stop();
3594 m_lastOnSame
= false;
3597 wxDataViewItem item
= GetItemByRow(current
);
3598 bool ignore_other_columns
=
3599 ((GetOwner()->GetExpanderColumn() != col
) &&
3600 (model
->IsContainer(item
)) &&
3601 (!model
->HasContainerColumns(item
)));
3603 if (event
.LeftDClick())
3605 if ( current
== m_lineLastClicked
)
3607 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3610 model
->GetValue( value
, item
, col
->GetModelColumn() );
3611 cell
->SetValue( value
);
3612 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3613 col
->GetWidth(), m_lineHeight
);
3614 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3619 wxWindow
*parent
= GetParent();
3620 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3622 le
.SetEventObject(parent
);
3623 le
.SetModel(GetOwner()->GetModel());
3625 parent
->GetEventHandler()->ProcessEvent(le
);
3631 // The first click was on another item, so don't interpret this as
3632 // a double click, but as a simple click instead
3639 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3641 // select single line
3642 SelectAllRows( false );
3643 SelectRow( m_lineSelectSingleOnUp
, true );
3646 //Process the event of user clicking the expander
3647 bool expander
= false;
3648 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3650 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3651 if( node
!=NULL
&& node
->HasChildren() )
3653 int indent
= node
->GetIndentLevel();
3654 indent
= GetOwner()->GetIndent()*indent
;
3655 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3656 if( rect
.Contains( x
, y
) )
3659 if( node
->IsOpen() )
3660 OnCollapsing(current
);
3662 OnExpanding( current
);
3665 if (node
&& !node
->HasChildren())
3668 //If the user click the expander, we do not do editing even if the column with expander are editable
3669 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3671 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3672 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3674 m_renameTimer
->Start( 100, true );
3678 m_lastOnSame
= false;
3679 m_lineSelectSingleOnUp
= (unsigned int)-1;
3683 // This is necessary, because after a DnD operation in
3684 // from and to ourself, the up event is swallowed by the
3685 // DnD code. So on next non-up event (which means here and
3686 // now) m_lineSelectSingleOnUp should be reset.
3687 m_lineSelectSingleOnUp
= (unsigned int)-1;
3690 if (event
.RightDown())
3692 m_lineBeforeLastClicked
= m_lineLastClicked
;
3693 m_lineLastClicked
= current
;
3695 // If the item is already selected, do not update the selection.
3696 // Multi-selections should not be cleared if a selected item is clicked.
3697 if (!IsRowSelected(current
))
3699 SelectAllRows(false);
3700 ChangeCurrentRow(current
);
3701 SelectRow(m_currentRow
,true);
3702 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3706 model
->GetValue( value
, item
, col
->GetModelColumn() );
3707 wxWindow
*parent
= GetParent();
3708 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3710 le
.SetEventObject(parent
);
3711 le
.SetModel(GetOwner()->GetModel());
3713 parent
->GetEventHandler()->ProcessEvent(le
);
3715 else if (event
.MiddleDown())
3718 if (event
.LeftDown() || forceClick
)
3722 m_lineBeforeLastClicked
= m_lineLastClicked
;
3723 m_lineLastClicked
= current
;
3725 unsigned int oldCurrentRow
= m_currentRow
;
3726 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3728 bool cmdModifierDown
= event
.CmdDown();
3729 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3731 if ( IsSingleSel() || !IsRowSelected(current
) )
3733 SelectAllRows( false );
3734 ChangeCurrentRow(current
);
3735 SelectRow(m_currentRow
,true);
3736 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3738 else // multi sel & current is highlighted & no mod keys
3740 m_lineSelectSingleOnUp
= current
;
3741 ChangeCurrentRow(current
); // change focus
3744 else // multi sel & either ctrl or shift is down
3746 if (cmdModifierDown
)
3748 ChangeCurrentRow(current
);
3749 ReverseRowSelection(m_currentRow
);
3750 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3752 else if (event
.ShiftDown())
3754 ChangeCurrentRow(current
);
3756 unsigned int lineFrom
= oldCurrentRow
,
3759 if ( lineTo
< lineFrom
)
3762 lineFrom
= m_currentRow
;
3765 SelectRows(lineFrom
, lineTo
, true);
3766 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3768 else // !ctrl, !shift
3770 // test in the enclosing if should make it impossible
3771 wxFAIL_MSG( _T("how did we get here?") );
3775 if (m_currentRow
!= oldCurrentRow
)
3776 RefreshRow( oldCurrentRow
);
3778 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3780 // Update selection here...
3783 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3784 (current
== oldCurrentRow
)) && oldWasSelected
;
3786 // Call LeftClick after everything else as under GTK+
3787 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3789 // notify cell about right click
3791 model
->GetValue( value
, item
, col
->GetModelColumn() );
3792 cell
->SetValue( value
);
3793 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3794 col
->GetWidth(), m_lineHeight
);
3795 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3800 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3804 if (HasCurrentRow())
3810 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3814 if (HasCurrentRow())
3820 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3822 if( m_selection
.GetCount() != 1 )
3823 return wxDataViewItem();
3825 return GetItemByRow( m_selection
.Item(0));
3828 //-----------------------------------------------------------------------------
3830 //-----------------------------------------------------------------------------
3831 WX_DEFINE_LIST(wxDataViewColumnList
)
3833 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3835 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3836 EVT_SIZE(wxDataViewCtrl::OnSize
)
3839 wxDataViewCtrl::~wxDataViewCtrl()
3842 GetModel()->RemoveNotifier( m_notifier
);
3844 wxDataViewColumnList::const_iterator iter
;
3845 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3851 void wxDataViewCtrl::Init()
3856 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3857 const wxPoint
& pos
, const wxSize
& size
,
3858 long style
, const wxValidator
& validator
)
3860 if ( (style
& wxBORDER_MASK
) == 0)
3861 style
|= wxBORDER_SUNKEN
;
3863 if (!wxControl::Create( parent
, id
, pos
, size
,
3864 style
| wxScrolledWindowStyle
, validator
))
3867 SetInitialSize(size
);
3872 MacSetClipChildren( true ) ;
3875 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3877 if (HasFlag(wxDV_NO_HEADER
))
3878 m_headerArea
= NULL
;
3880 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3882 SetTargetWindow( m_clientArea
);
3884 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3886 sizer
->Add( m_headerArea
, 0, wxGROW
);
3887 sizer
->Add( m_clientArea
, 1, wxGROW
);
3894 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3898 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3901 // we need to process arrows ourselves for scrolling
3902 if ( nMsg
== WM_GETDLGCODE
)
3904 rc
|= DLGC_WANTARROWS
;
3912 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3914 // We need to override OnSize so that our scrolled
3915 // window a) does call Layout() to use sizers for
3916 // positioning the controls but b) does not query
3917 // the sizer for their size and use that for setting
3918 // the scrollable area as set that ourselves by
3919 // calling SetScrollbar() further down.
3926 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3928 if (!wxDataViewCtrlBase::AssociateModel( model
))
3931 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3933 model
->AddNotifier( m_notifier
);
3935 m_clientArea
->DestroyTree();
3937 m_clientArea
->BuildTree(model
);
3939 m_clientArea
->UpdateDisplay();
3944 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3946 if (!wxDataViewCtrlBase::AppendColumn(col
))
3949 m_cols
.Append( col
);
3954 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3956 if (!wxDataViewCtrlBase::PrependColumn(col
))
3959 m_cols
.Insert( col
);
3964 void wxDataViewCtrl::OnColumnChange()
3967 m_headerArea
->UpdateDisplay();
3969 m_clientArea
->UpdateDisplay();
3972 void wxDataViewCtrl::DoSetExpanderColumn()
3974 m_clientArea
->UpdateDisplay();
3977 void wxDataViewCtrl::DoSetIndent()
3979 m_clientArea
->UpdateDisplay();
3982 unsigned int wxDataViewCtrl::GetColumnCount() const
3984 return m_cols
.GetCount();
3987 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3989 wxDataViewColumnList::const_iterator iter
;
3991 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3996 if ((*iter
)->IsHidden())
4003 void wxDataViewCtrl::ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
)
4005 if (new_pos
> m_cols
.GetCount()) return;
4006 m_cols
.DeleteObject( col
);
4007 m_cols
.Insert( new_pos
, col
);
4009 m_clientArea
->UpdateDisplay();
4012 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4014 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4025 bool wxDataViewCtrl::ClearColumns()
4032 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4034 int ret
= 0, dead
= 0;
4035 int len
= GetColumnCount();
4036 for (int i
=0; i
<len
; i
++)
4038 wxDataViewColumn
* col
= GetColumn(i
);
4039 if (col
->IsHidden())
4041 ret
+= col
->GetWidth();
4044 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
4051 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4056 //Selection code with wxDataViewItem as parameters
4057 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4059 return m_clientArea
->GetSelection();
4062 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4065 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4066 int len
= selection
.GetCount();
4067 for( int i
= 0; i
< len
; i
++)
4069 unsigned int row
= selection
[i
];
4070 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4075 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4077 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4078 int len
= sel
.GetCount();
4079 for( int i
= 0; i
< len
; i
++ )
4081 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4083 selection
.Add( static_cast<unsigned int>(row
) );
4085 m_clientArea
->SetSelections( selection
);
4088 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4090 int row
= m_clientArea
->GetRowByItem( item
);
4093 //Unselect all rows before select another in the single select mode
4094 if (m_clientArea
->IsSingleSel())
4095 m_clientArea
->SelectAllRows(false);
4096 m_clientArea
->SelectRow(row
, true);
4100 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4102 int row
= m_clientArea
->GetRowByItem( item
);
4104 m_clientArea
->SelectRow(row
, false);
4107 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4109 int row
= m_clientArea
->GetRowByItem( item
);
4112 return m_clientArea
->IsRowSelected(row
);
4117 //Selection code with row number as parameter
4118 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4121 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4122 int len
= selection
.GetCount();
4123 for( int i
= 0; i
< len
; i
++)
4125 unsigned int row
= selection
[i
];
4131 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4133 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4134 int len
= sel
.GetCount();
4135 for( int i
= 0; i
< len
; i
++ )
4139 selection
.Add( static_cast<unsigned int>(row
) );
4141 m_clientArea
->SetSelections( selection
);
4144 void wxDataViewCtrl::Select( int row
)
4148 if (m_clientArea
->IsSingleSel())
4149 m_clientArea
->SelectAllRows(false);
4150 m_clientArea
->SelectRow( row
, true );
4154 void wxDataViewCtrl::Unselect( int row
)
4157 m_clientArea
->SelectRow(row
, false);
4160 bool wxDataViewCtrl::IsSelected( int row
) const
4163 return m_clientArea
->IsRowSelected(row
);
4167 void wxDataViewCtrl::SelectRange( int from
, int to
)
4170 for( int i
= from
; i
< to
; i
++ )
4172 m_clientArea
->Select(sel
);
4175 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4177 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4178 for( int i
= from
; i
< to
; i
++ )
4179 if( sel
.Index( i
) != wxNOT_FOUND
)
4181 m_clientArea
->SetSelections(sel
);
4184 void wxDataViewCtrl::SelectAll()
4186 m_clientArea
->SelectAllRows(true);
4189 void wxDataViewCtrl::UnselectAll()
4191 m_clientArea
->SelectAllRows(false);
4194 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4198 if( row
> (int) m_clientArea
->GetRowCount() )
4199 row
= m_clientArea
->GetRowCount();
4201 int first
= m_clientArea
->GetFirstVisibleRow();
4202 int last
= m_clientArea
->GetLastVisibleRow();
4204 m_clientArea
->ScrollTo( row
, column
);
4205 else if( row
> last
)
4206 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4208 m_clientArea
->ScrollTo( first
, column
);
4211 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4213 int row
= m_clientArea
->GetRowByItem(item
);
4216 if( column
== NULL
)
4217 EnsureVisible(row
, -1);
4221 int len
= GetColumnCount();
4222 for( int i
= 0; i
< len
; i
++ )
4223 if( GetColumn(i
) == column
)
4228 EnsureVisible( row
, col
);
4234 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4236 m_clientArea
->HitTest(point
, item
, column
);
4239 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4241 return m_clientArea
->GetItemRect(item
, column
);
4244 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4246 return m_clientArea
->GetItemByRow( row
);
4249 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4251 return m_clientArea
->GetRowByItem( item
);
4254 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4256 int row
= m_clientArea
->GetRowByItem( item
);
4258 m_clientArea
->Expand(row
);
4261 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4263 int row
= m_clientArea
->GetRowByItem( item
);
4265 m_clientArea
->Collapse(row
);
4269 // !wxUSE_GENERICDATAVIEWCTRL
4272 // wxUSE_DATAVIEWCTRL