1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 #define USE_NATIVE_HEADER_WINDOW 1
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 virtual void OnInternalIdle();
143 // called Refresh afterwards
144 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
146 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
149 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
151 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
153 wxSize
DoGetBestSize() const;
155 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
157 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
158 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
162 bool m_delayedUpdate
;
165 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
168 #else // !defined(__WXMSW__)
170 #define HEADER_WINDOW_HEIGHT 25
171 #define HEADER_HORIZ_BORDER 5
172 #define HEADER_VERT_BORDER 3
173 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
175 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
178 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
180 const wxPoint
&pos
= wxDefaultPosition
,
181 const wxSize
&size
= wxDefaultSize
,
182 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
185 Create(parent
, id
, pos
, size
, name
);
188 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
189 const wxPoint
&pos
, const wxSize
&size
,
190 const wxString
&name
);
192 ~wxGenericDataViewHeaderWindow()
194 delete m_resizeCursor
;
197 virtual void UpdateDisplay() { Refresh(); }
201 void OnPaint( wxPaintEvent
&event
);
202 void OnMouse( wxMouseEvent
&event
);
203 void OnSetFocus( wxFocusEvent
&event
);
208 // vars used for column resizing:
210 wxCursor
*m_resizeCursor
;
211 const wxCursor
*m_currentCursor
;
214 bool m_dirty
; // needs refresh?
215 int m_hover
; // index of the column under the mouse
216 int m_column
; // index of the column being resized
217 int m_currentX
; // divider line position in logical (unscrolled) coords
218 int m_minX
; // minimal position beyond which the divider line
219 // can't be dragged in logical coords
221 // the pen used to draw the current column width drag line
222 // when resizing the columsn
226 // internal utilities:
230 m_currentCursor
= (wxCursor
*) NULL
;
231 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
233 m_isDragging
= false;
236 m_hover
= wxNOT_FOUND
;
237 m_column
= wxNOT_FOUND
;
241 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
242 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
245 void AdjustDC(wxDC
& dc
);
248 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
249 DECLARE_EVENT_TABLE()
252 #endif // defined(__WXMSW__)
254 //-----------------------------------------------------------------------------
255 // wxDataViewRenameTimer
256 //-----------------------------------------------------------------------------
258 class wxDataViewRenameTimer
: public wxTimer
261 wxDataViewMainWindow
*m_owner
;
264 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
268 //-----------------------------------------------------------------------------
269 // wxDataViewTreeNode
270 //-----------------------------------------------------------------------------
271 class wxDataViewTreeNode
;
272 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
273 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
275 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
276 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
278 class wxDataViewTreeNode
281 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
288 m_hasChildren
= false;
292 ~wxDataViewTreeNode()
296 wxDataViewTreeNode
* GetParent() { return m_parent
; }
297 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
298 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
299 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
301 void AddNode( wxDataViewTreeNode
* node
)
303 m_leaves
.Add( node
->GetItem().GetID() );
305 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
308 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
310 void AddLeaf( void * leaf
)
312 m_leaves
.Add( leaf
);
314 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
317 wxDataViewItem
& GetItem() { return m_item
; }
318 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
320 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
321 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
325 wxDataViewTreeNode
* node
= this;
326 while( node
->GetParent()->GetParent() != NULL
)
328 node
= node
->GetParent();
341 int len
= m_nodes
.GetCount();
343 for ( int i
= 0 ;i
< len
; i
++)
344 sum
+= m_nodes
[i
]->GetSubTreeCount();
346 sum
+= m_leaves
.GetCount();
349 ChangeSubTreeCount(-sum
);
355 ChangeSubTreeCount(sum
);
358 bool HasChildren() { return m_hasChildren
; }
359 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
361 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
362 int GetSubTreeCount() { return m_subTreeCount
; }
363 void ChangeSubTreeCount( int num
)
367 m_subTreeCount
+= num
;
369 m_parent
->ChangeSubTreeCount(num
);
376 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
377 int len
= m_nodes
.GetCount();
378 for (int i
= 0; i
< len
; i
++)
379 m_nodes
[i
]->Resort();
380 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
385 wxDataViewTreeNode
*m_parent
;
386 wxDataViewTreeNodes m_nodes
;
387 wxDataViewTreeLeaves m_leaves
;
388 wxDataViewItem m_item
;
394 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
396 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
399 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
401 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
406 //-----------------------------------------------------------------------------
407 // wxDataViewMainWindow
408 //-----------------------------------------------------------------------------
410 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
412 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
413 WX_DEFINE_LIST(ItemList
)
415 class wxDataViewMainWindow
: public wxWindow
418 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
420 const wxPoint
&pos
= wxDefaultPosition
,
421 const wxSize
&size
= wxDefaultSize
,
422 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
423 virtual ~wxDataViewMainWindow();
425 // notifications from wxDataViewModel
426 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
427 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
428 bool ItemChanged( const wxDataViewItem
&item
);
429 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
443 g_model
= GetOwner()->GetModel();
444 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
447 if (g_model
->HasDefaultCompare())
455 g_column
= col
->GetModelColumn();
456 g_asending
= col
->IsSortOrderAscending();
459 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
460 wxDataViewCtrl
*GetOwner() { return m_owner
; }
461 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
463 void OnPaint( wxPaintEvent
&event
);
464 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
465 void OnChar( wxKeyEvent
&event
);
466 void OnMouse( wxMouseEvent
&event
);
467 void OnSetFocus( wxFocusEvent
&event
);
468 void OnKillFocus( wxFocusEvent
&event
);
470 void UpdateDisplay();
471 void RecalculateDisplay();
472 void OnInternalIdle();
474 void OnRenameTimer();
476 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
477 void ScrollTo( int rows
, int column
);
479 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
480 void ChangeCurrentRow( unsigned int row
);
482 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
483 bool IsEmpty() { return GetRowCount() == 0; }
485 int GetCountPerPage() const;
486 int GetEndOfLastCol() const;
487 unsigned int GetFirstVisibleRow() const;
488 //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
489 unsigned int GetLastVisibleRow();
490 unsigned int GetRowCount() ;
492 wxDataViewItem
GetSelection() const;
493 wxDataViewSelection
GetSelections(){ return m_selection
; }
494 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
495 void Select( const wxArrayInt
& aSelections
);
496 void SelectAllRows( bool on
);
497 void SelectRow( unsigned int row
, bool on
);
498 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
499 void ReverseRowSelection( unsigned int row
);
500 bool IsRowSelected( unsigned int row
);
501 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
503 void RefreshRow( unsigned int row
);
504 void RefreshRows( unsigned int from
, unsigned int to
);
505 void RefreshRowsAfter( unsigned int firstRow
);
507 // returns the colour to be used for drawing the rules
508 wxColour
GetRuleColour() const
510 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
513 wxRect
GetLineRect( unsigned int row
) const;
515 //Some useful functions for row and item mapping
516 wxDataViewItem
GetItemByRow( unsigned int row
) const;
517 int GetRowByItem( const wxDataViewItem
& item
);
519 //Methods for building the mapping tree
520 void BuildTree( wxDataViewModel
* model
);
522 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
523 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
525 void Expand( unsigned int row
) { OnExpanding( row
); }
526 void Collapse( unsigned int row
) { OnCollapsing( row
); }
528 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
529 //We did not need this temporarily
530 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
532 int RecalculateCount() ;
534 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
535 void OnExpanding( unsigned int row
);
536 void OnCollapsing( unsigned int row
);
538 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
541 wxDataViewCtrl
*m_owner
;
545 wxDataViewColumn
*m_currentCol
;
546 unsigned int m_currentRow
;
547 wxDataViewSelection m_selection
;
549 wxDataViewRenameTimer
*m_renameTimer
;
557 // for double click logic
558 unsigned int m_lineLastClicked
,
559 m_lineBeforeLastClicked
,
560 m_lineSelectSingleOnUp
;
562 // the pen used to draw horiz/vertical rules
565 // the pen used to draw the expander and the lines
568 //This is the tree structure of the model
569 wxDataViewTreeNode
* m_root
;
571 //This is the tree node under the cursor
572 wxDataViewTreeNode
* m_underMouse
;
574 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
575 DECLARE_EVENT_TABLE()
578 // ---------------------------------------------------------
579 // wxGenericDataViewModelNotifier
580 // ---------------------------------------------------------
582 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
585 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
586 { m_mainWindow
= mainWindow
; }
588 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
589 { return m_mainWindow
->ItemAdded( parent
, item
); }
590 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
591 { return m_mainWindow
->ItemDeleted( parent
, item
); }
592 virtual bool ItemChanged( const wxDataViewItem
& item
)
593 { return m_mainWindow
->ItemChanged(item
); }
594 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
595 { return m_mainWindow
->ValueChanged( item
, col
); }
596 virtual bool Cleared()
597 { return m_mainWindow
->Cleared(); }
598 virtual void Resort()
599 { m_mainWindow
->Resort(); }
601 wxDataViewMainWindow
*m_mainWindow
;
604 // ---------------------------------------------------------
605 // wxDataViewRenderer
606 // ---------------------------------------------------------
608 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
610 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
611 wxDataViewCellMode mode
,
613 wxDataViewRendererBase( varianttype
, mode
, align
)
622 wxDataViewRenderer::~wxDataViewRenderer()
628 wxDC
*wxDataViewRenderer::GetDC()
632 if (GetOwner() == NULL
)
634 if (GetOwner()->GetOwner() == NULL
)
636 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
642 // ---------------------------------------------------------
643 // wxDataViewCustomRenderer
644 // ---------------------------------------------------------
646 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
648 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
649 wxDataViewCellMode mode
, int align
) :
650 wxDataViewRenderer( varianttype
, mode
, align
)
654 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
656 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
657 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
658 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
659 view
->GetForegroundColour();
660 dc
->SetTextForeground(col
);
661 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
664 // ---------------------------------------------------------
665 // wxDataViewTextRenderer
666 // ---------------------------------------------------------
668 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
670 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
671 wxDataViewCellMode mode
, int align
) :
672 wxDataViewCustomRenderer( varianttype
, mode
, align
)
676 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
678 m_text
= value
.GetString();
683 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
688 bool wxDataViewTextRenderer::HasEditorCtrl()
693 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
694 wxRect labelRect
, const wxVariant
&value
)
696 return new wxTextCtrl( parent
, wxID_ANY
, value
,
697 wxPoint(labelRect
.x
,labelRect
.y
),
698 wxSize(labelRect
.width
,labelRect
.height
) );
701 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
703 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
704 value
= text
->GetValue();
708 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
710 RenderText( m_text
, 0, cell
, dc
, state
);
714 wxSize
wxDataViewTextRenderer::GetSize() const
716 const wxDataViewCtrl
*view
= GetView();
720 view
->GetTextExtent( m_text
, &x
, &y
);
721 return wxSize( x
, y
);
723 return wxSize(80,20);
726 // ---------------------------------------------------------
727 // wxDataViewTextRendererAttr
728 // ---------------------------------------------------------
730 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
732 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
733 wxDataViewCellMode mode
, int align
) :
734 wxDataViewTextRenderer( varianttype
, mode
, align
)
739 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
746 if (m_attr
.HasColour())
748 colour
= dc
->GetTextForeground();
749 dc
->SetTextForeground( m_attr
.GetColour() );
752 if (m_attr
.GetBold() || m_attr
.GetItalic())
754 font
= dc
->GetFont();
755 wxFont myfont
= font
;
756 if (m_attr
.GetBold())
757 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
758 if (m_attr
.GetItalic())
759 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
760 dc
->SetFont( myfont
);
764 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
769 if (m_attr
.HasColour())
770 dc
->SetTextForeground( colour
);
772 if (m_attr
.GetBold() || m_attr
.GetItalic())
780 // ---------------------------------------------------------
781 // wxDataViewBitmapRenderer
782 // ---------------------------------------------------------
784 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
786 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
787 wxDataViewCellMode mode
, int align
) :
788 wxDataViewCustomRenderer( varianttype
, mode
, align
)
792 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
794 if (value
.GetType() == wxT("wxBitmap"))
796 if (value
.GetType() == wxT("wxIcon"))
802 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
807 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
810 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
811 else if (m_icon
.Ok())
812 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
817 wxSize
wxDataViewBitmapRenderer::GetSize() const
820 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
821 else if (m_icon
.Ok())
822 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
824 return wxSize(16,16);
827 // ---------------------------------------------------------
828 // wxDataViewToggleRenderer
829 // ---------------------------------------------------------
831 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
833 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
834 wxDataViewCellMode mode
, int align
) :
835 wxDataViewCustomRenderer( varianttype
, mode
, align
)
840 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
842 m_toggle
= value
.GetBool();
847 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
852 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
854 // User wxRenderer here
857 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
859 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
864 flags
|= wxCONTROL_CHECKED
;
865 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
866 flags
|= wxCONTROL_DISABLED
;
868 wxRendererNative::Get().DrawCheckBox(
869 GetOwner()->GetOwner(),
877 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
878 wxDataViewModel
*model
,
879 const wxDataViewItem
& item
, unsigned int col
)
881 bool value
= !m_toggle
;
882 wxVariant variant
= value
;
883 model
->SetValue( variant
, item
, col
);
884 model
->ValueChanged( item
, col
);
888 wxSize
wxDataViewToggleRenderer::GetSize() const
890 return wxSize(20,20);
893 // ---------------------------------------------------------
894 // wxDataViewProgressRenderer
895 // ---------------------------------------------------------
897 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
899 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
900 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
901 wxDataViewCustomRenderer( varianttype
, mode
, align
)
907 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
911 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
913 m_value
= (long) value
;
915 if (m_value
< 0) m_value
= 0;
916 if (m_value
> 100) m_value
= 100;
921 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
923 value
= (long) m_value
;
927 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
929 double pct
= (double)m_value
/ 100.0;
931 bar
.width
= (int)(cell
.width
* pct
);
932 dc
->SetPen( *wxTRANSPARENT_PEN
);
933 dc
->SetBrush( *wxBLUE_BRUSH
);
934 dc
->DrawRectangle( bar
);
936 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
937 dc
->SetPen( *wxBLACK_PEN
);
938 dc
->DrawRectangle( cell
);
943 wxSize
wxDataViewProgressRenderer::GetSize() const
945 return wxSize(40,12);
948 // ---------------------------------------------------------
949 // wxDataViewDateRenderer
950 // ---------------------------------------------------------
952 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
954 #if wxUSE_DATE_RENDERER_POPUP
956 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
959 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
960 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
961 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
966 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
967 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
968 sizer
->Add( m_cal
, 1, wxGROW
);
973 void OnCalendar( wxCalendarEvent
&event
);
975 wxCalendarCtrl
*m_cal
;
976 wxDataViewModel
*m_model
;
978 const wxDataViewItem
& m_item
;
981 virtual void OnDismiss()
986 DECLARE_EVENT_TABLE()
989 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
990 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
993 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
995 wxDateTime date
= event
.GetDate();
996 wxVariant value
= date
;
997 m_model
->SetValue( value
, m_item
, m_col
);
998 m_model
->ValueChanged( m_item
, m_col
);
1002 #endif // wxUSE_DATE_RENDERER_POPUP
1004 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1006 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1007 wxDataViewCellMode mode
, int align
) :
1008 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1012 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1014 m_date
= value
.GetDateTime();
1019 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1025 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1027 wxString tmp
= m_date
.FormatDate();
1028 RenderText( tmp
, 0, cell
, dc
, state
);
1032 wxSize
wxDataViewDateRenderer::GetSize() const
1034 const wxDataViewCtrl
* view
= GetView();
1035 wxString tmp
= m_date
.FormatDate();
1037 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1038 return wxSize(x
,y
+d
);
1041 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1042 const wxDataViewItem
& item
, unsigned int col
)
1045 model
->GetValue( variant
, item
, col
);
1046 wxDateTime value
= variant
.GetDateTime();
1048 #if wxUSE_DATE_RENDERER_POPUP
1049 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1050 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1051 wxPoint pos
= wxGetMousePosition();
1054 popup
->Popup( popup
->m_cal
);
1055 #else // !wxUSE_DATE_RENDERER_POPUP
1056 wxMessageBox(value
.Format());
1057 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1061 // ---------------------------------------------------------
1062 // wxDataViewIconTextRenderer
1063 // ---------------------------------------------------------
1065 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1067 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1068 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1069 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1072 SetAlignment(align
);
1075 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1079 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1085 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1090 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1093 const wxIcon
&icon
= m_value
.GetIcon();
1096 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1097 xoffset
= icon
.GetWidth()+4;
1100 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1105 wxSize
wxDataViewIconTextRenderer::GetSize() const
1107 const wxDataViewCtrl
*view
= GetView();
1108 if (!m_value
.GetText().empty())
1111 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1113 if (m_value
.GetIcon().IsOk())
1114 x
+= m_value
.GetIcon().GetWidth() + 4;
1115 return wxSize( x
, y
);
1117 return wxSize(80,20);
1121 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1122 wxRect
WXUNUSED(labelRect
),
1123 const wxVariant
& WXUNUSED(value
))
1129 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1130 wxVariant
& WXUNUSED(value
))
1135 // ---------------------------------------------------------
1137 // ---------------------------------------------------------
1139 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1141 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1142 unsigned int model_column
,
1143 int width
, wxAlignment align
, int flags
) :
1144 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1146 SetAlignment(align
);
1150 m_autosize
= width
< 0; // TODO
1152 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1155 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1156 unsigned int model_column
,
1157 int width
, wxAlignment align
, int flags
) :
1158 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1160 SetAlignment(align
);
1163 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1166 wxDataViewColumn::~wxDataViewColumn()
1170 void wxDataViewColumn::Init( int width
)
1173 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1177 void wxDataViewColumn::SetResizeable( bool resizeable
)
1180 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1182 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1185 void wxDataViewColumn::SetHidden( bool hidden
)
1188 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1190 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1192 // tell our owner to e.g. update its scrollbars:
1194 GetOwner()->OnColumnChange();
1197 void wxDataViewColumn::SetSortable( bool sortable
)
1200 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1202 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1204 // Update header button
1206 GetOwner()->OnColumnChange();
1209 void wxDataViewColumn::SetReorderable( bool reorderable
)
1212 m_flags
|= wxDATAVIEW_COL_REORDERABLE
;
1214 m_flags
&= ~wxDATAVIEW_COL_REORDERABLE
;
1217 void wxDataViewColumn::SetSortOrder( bool ascending
)
1219 m_ascending
= ascending
;
1221 // Update header button
1223 GetOwner()->OnColumnChange();
1226 bool wxDataViewColumn::IsSortOrderAscending() const
1231 void wxDataViewColumn::SetInternalWidth( int width
)
1235 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1236 if (m_owner
&& m_owner
->m_clientArea
)
1237 m_owner
->m_clientArea
->RecalculateDisplay();
1240 void wxDataViewColumn::SetWidth( int width
)
1242 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1244 SetInternalWidth(width
);
1248 //-----------------------------------------------------------------------------
1249 // wxDataViewHeaderWindowBase
1250 //-----------------------------------------------------------------------------
1252 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1254 wxWindow
*parent
= GetParent();
1255 wxDataViewEvent
le(type
, parent
->GetId());
1257 le
.SetEventObject(parent
);
1259 le
.SetDataViewColumn(GetColumn(n
));
1260 le
.SetModel(GetOwner()->GetModel());
1262 // for events created by wxDataViewHeaderWindow the
1263 // row / value fields are not valid
1265 parent
->GetEventHandler()->ProcessEvent(le
);
1268 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1270 #ifndef HDS_DRAGDROP
1271 #define HDS_DRAGDROP 0x0040
1273 #ifndef HDS_FULLDRAG
1274 #define HDS_FULLDRAG 0x0080
1277 // implemented in msw/listctrl.cpp:
1278 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1280 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1282 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1283 const wxPoint
&pos
, const wxSize
&size
,
1284 const wxString
&name
)
1288 m_scrollOffsetX
= 0;
1289 m_delayedUpdate
= false;
1290 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this );
1292 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1293 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1294 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1297 wxSize
new_size(w
,h
);
1299 if ( !CreateControl(parent
, id
, pos
, new_size
, 0, wxDefaultValidator
, name
) )
1302 // create the native WC_HEADER window:
1303 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1304 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1307 msStyle
|= WS_VISIBLE
;
1309 m_hWnd
= CreateWindowEx(0,
1320 wxLogLastError(_T("CreateWindowEx"));
1324 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1325 // to call wxDataViewHeaderWindow::MSWOnNotify
1326 SubclassWin(m_hWnd
);
1328 // the following is required to get the default win's font for
1329 // header windows and must be done befor sending the HDM_LAYOUT msg
1335 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1340 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1342 return wxSize( 80, m_buttonHeight
+2 );
1345 void wxDataViewHeaderWindowMSW::OnInternalIdle()
1347 if (m_delayedUpdate
)
1349 m_delayedUpdate
= false;
1354 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1356 // remove old columns
1357 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1358 Header_DeleteItem((HWND
)m_hWnd
, 0);
1360 // add the updated array of columns to the header control
1361 unsigned int cols
= GetOwner()->GetColumnCount();
1362 unsigned int added
= 0;
1363 for (unsigned int i
= 0; i
< cols
; i
++)
1365 wxDataViewColumn
*col
= GetColumn( i
);
1366 if (col
->IsHidden())
1367 continue; // don't add it!
1370 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1371 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1372 hdi
.cxy
= col
->GetWidth();
1373 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1374 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1375 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1377 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1379 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1380 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1381 // VZ: works with 5.81
1382 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1385 // lParam is reserved for application's use:
1386 // we store there the column index to use it later in MSWOnNotify
1387 // (since columns may have been hidden)
1388 hdi
.lParam
= (LPARAM
)i
;
1390 // the native wxMSW implementation of the header window
1391 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1392 // on the right: to correct this effect we make the column
1393 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1395 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1397 switch (col
->GetAlignment())
1400 hdi
.fmt
|= HDF_LEFT
;
1402 case wxALIGN_CENTER
:
1403 case wxALIGN_CENTER_HORIZONTAL
:
1404 hdi
.fmt
|= HDF_CENTER
;
1407 hdi
.fmt
|= HDF_RIGHT
;
1411 // such alignment is not allowed for the column header!
1415 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1416 (WPARAM
)added
, (LPARAM
)&hdi
);
1421 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1425 // NOTE: we don't just return nmHDR->iItem because when there are
1426 // hidden columns, nmHDR->iItem may be different from
1427 // nmHDR->pitem->lParam
1429 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1431 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1436 item
.mask
= HDI_LPARAM
;
1437 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1439 return (unsigned int)item
.lParam
;
1442 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1444 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1446 // is it a message from the header?
1447 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1448 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1450 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1451 switch ( nmhdr
->code
)
1453 case HDN_BEGINTRACK
:
1454 // user has started to resize a column:
1455 // do we need to veto it?
1456 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1464 // user has started to reorder a column
1465 if (!GetColumn(nmHDR
->iItem
)->IsReorderable())
1472 case HDN_ENDDRAG
: // user has finished reordering a column
1474 wxDataViewColumn
*col
= GetColumn(nmHDR
->iItem
);
1475 unsigned int new_pos
= nmHDR
->pitem
->iOrder
;
1476 m_owner
->ColumnMoved( col
, new_pos
);
1477 m_delayedUpdate
= true;
1481 case HDN_ITEMCHANGING
:
1482 if (nmHDR
->pitem
!= NULL
&&
1483 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1485 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1486 if (nmHDR
->pitem
->cxy
< minWidth
)
1488 // do not allow the user to resize this column under
1489 // its minimal width:
1495 case HDN_ITEMCHANGED
: // user is resizing a column
1496 case HDN_ENDTRACK
: // user has finished resizing a column
1498 // update the width of the modified column:
1499 if (nmHDR
->pitem
!= NULL
&&
1500 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1502 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1503 unsigned int w
= nmHDR
->pitem
->cxy
;
1504 wxDataViewColumn
*col
= GetColumn(idx
);
1506 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1507 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1508 w
-= COLUMN_WIDTH_OFFSET
;
1510 if (w
>= (unsigned)col
->GetMinWidth())
1511 col
->SetInternalWidth(w
);
1517 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1518 wxDataViewModel
* model
= GetOwner()->GetModel();
1520 if(nmHDR
->iButton
== 0)
1522 wxDataViewColumn
*col
= GetColumn(idx
);
1523 if(col
->IsSortable())
1525 if(model
&& m_owner
->GetSortingColumn() == col
)
1527 bool order
= col
->IsSortOrderAscending();
1528 col
->SetSortOrder(!order
);
1532 m_owner
->SetSortingColumn(col
);
1540 wxEventType evt
= nmHDR
->iButton
== 0 ?
1541 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1542 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1543 SendEvent(evt
, idx
);
1549 // NOTE: for some reason (i.e. for a bug in Windows)
1550 // the HDN_ITEMCLICK notification is not sent on
1551 // right clicks, so we need to handle NM_RCLICK
1554 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1555 if (column
!= wxNOT_FOUND
)
1558 item
.mask
= HDI_LPARAM
;
1559 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1561 // 'idx' may be different from 'column' if there are
1562 // hidden columns...
1563 unsigned int idx
= (unsigned int)item
.lParam
;
1564 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1570 case HDN_GETDISPINFOW
:
1571 // see wxListCtrl::MSWOnNotify for more info!
1574 case HDN_ITEMDBLCLICK
:
1576 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1577 int w
= GetOwner()->GetBestColumnWidth(idx
);
1579 // update the native control:
1581 ZeroMemory(&hd
, sizeof(hd
));
1582 hd
.mask
= HDI_WIDTH
;
1584 Header_SetItem(GetHwnd(),
1585 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1588 // update the wxDataViewColumn class:
1589 GetColumn(idx
)->SetInternalWidth(w
);
1594 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1600 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1601 const wxRect
* WXUNUSED(rect
))
1603 m_scrollOffsetX
+= dx
;
1605 GetParent()->Layout();
1608 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1612 // TODO: why is there a border + 2px around it?
1613 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1616 #else // !defined(__WXMSW__)
1618 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1619 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1620 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1621 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1622 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1625 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1626 const wxPoint
&pos
, const wxSize
&size
,
1627 const wxString
&name
)
1631 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1634 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1635 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1636 SetOwnForegroundColour( attr
.colFg
);
1637 SetOwnBackgroundColour( attr
.colBg
);
1639 SetOwnFont( attr
.font
);
1641 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1642 // a wxBoxSizer and in order to avoid super-big header windows,
1643 // we need to set our height as fixed
1644 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1645 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1650 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1653 GetClientSize( &w
, &h
);
1655 wxAutoBufferedPaintDC
dc( this );
1657 dc
.SetBackground(GetBackgroundColour());
1661 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1664 m_owner
->GetViewStart( &x
, NULL
);
1666 // account for the horz scrollbar offset
1667 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1669 dc
.SetFont( GetFont() );
1671 unsigned int cols
= GetOwner()->GetColumnCount();
1674 for (i
= 0; i
< cols
; i
++)
1676 wxDataViewColumn
*col
= GetColumn( i
);
1677 if (col
->IsHidden())
1678 continue; // skip it!
1680 int cw
= col
->GetWidth();
1683 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1684 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1686 if (col
->IsSortOrderAscending())
1687 sortArrow
= wxHDR_SORT_ICON_UP
;
1689 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1693 if (m_parent
->IsEnabled())
1695 if ((int) i
== m_hover
)
1696 state
= wxCONTROL_CURRENT
;
1700 state
= (int) wxCONTROL_DISABLED
;
1703 wxRendererNative::Get().DrawHeaderButton
1707 wxRect(xpos
, 0, cw
, ch
-1),
1712 // align as required the column title:
1714 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1715 switch (col
->GetAlignment())
1718 x
+= HEADER_HORIZ_BORDER
;
1721 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1724 case wxALIGN_CENTER
:
1725 case wxALIGN_CENTER_HORIZONTAL
:
1726 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1730 // always center the title vertically:
1731 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1733 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1735 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1736 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1737 dc
.DrawText( col
->GetTitle(), x
, y
);
1738 dc
.DestroyClippingRegion();
1744 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1746 GetParent()->SetFocus();
1750 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1752 // we want to work with logical coords
1754 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1755 int y
= event
.GetY();
1759 // we don't draw the line beyond our window,
1760 // but we allow dragging it there
1762 GetClientSize( &w
, NULL
);
1763 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1766 if (event
.ButtonUp())
1768 m_isDragging
= false;
1774 m_currentX
= wxMax(m_minX
+ 7, x
);
1778 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1780 GetOwner()->Refresh();
1784 else // not dragging
1787 m_column
= wxNOT_FOUND
;
1789 bool hit_border
= false;
1791 // end of the current column
1794 // find the column where this event occured
1795 int countCol
= m_owner
->GetColumnCount();
1796 for (int column
= 0; column
< countCol
; column
++)
1798 wxDataViewColumn
*p
= GetColumn(column
);
1801 continue; // skip if not shown
1803 xpos
+= p
->GetWidth();
1805 if ((abs(x
-xpos
) < 3) && (y
< 22))
1813 // inside the column
1820 int old_hover
= m_hover
;
1822 if (event
.Leaving())
1823 m_hover
= wxNOT_FOUND
;
1824 if (old_hover
!= m_hover
)
1827 if (m_column
== wxNOT_FOUND
)
1830 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1831 if (event
.LeftDClick() && resizeable
)
1833 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1836 else if (event
.LeftDown() || event
.RightUp())
1838 if (hit_border
&& event
.LeftDown() && resizeable
)
1840 m_isDragging
= true;
1844 else // click on a column
1846 wxDataViewModel
* model
= GetOwner()->GetModel();
1847 wxEventType evt
= event
.LeftDown() ?
1848 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1849 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1850 SendEvent(evt
, m_column
);
1852 //Left click the header
1853 if(event
.LeftDown())
1855 wxDataViewColumn
*col
= GetColumn(m_column
);
1856 if(col
->IsSortable())
1858 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1859 if(model
&& sortCol
== col
)
1861 bool order
= col
->IsSortOrderAscending();
1862 col
->SetSortOrder(!order
);
1866 m_owner
->SetSortingColumn(col
);
1872 //Send the column sorted event
1873 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1877 else if (event
.Moving())
1879 if (hit_border
&& resizeable
)
1880 m_currentCursor
= m_resizeCursor
;
1882 m_currentCursor
= wxSTANDARD_CURSOR
;
1884 SetCursor(*m_currentCursor
);
1889 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1893 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1894 m_owner
->GetViewStart( &x
, NULL
);
1896 // shift the DC origin to match the position of the main window horizontal
1897 // scrollbar: this allows us to always use logical coords
1898 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1901 #endif // defined(__WXMSW__)
1903 //-----------------------------------------------------------------------------
1904 // wxDataViewRenameTimer
1905 //-----------------------------------------------------------------------------
1907 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1912 void wxDataViewRenameTimer::Notify()
1914 m_owner
->OnRenameTimer();
1917 //-----------------------------------------------------------------------------
1918 // wxDataViewMainWindow
1919 //-----------------------------------------------------------------------------
1921 //The tree building helper, declared firstly
1922 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1924 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1926 if (row1
> row2
) return 1;
1927 if (row1
== row2
) return 0;
1932 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1934 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1935 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1936 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1937 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1938 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1939 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1942 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1943 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1944 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1945 m_selection( wxDataViewSelectionCmp
)
1950 m_lastOnSame
= false;
1951 m_renameTimer
= new wxDataViewRenameTimer( this );
1953 // TODO: user better initial values/nothing selected
1954 m_currentCol
= NULL
;
1957 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1960 m_dragStart
= wxPoint(0,0);
1961 m_lineLastClicked
= (unsigned int) -1;
1962 m_lineBeforeLastClicked
= (unsigned int) -1;
1963 m_lineSelectSingleOnUp
= (unsigned int) -1;
1967 SetBackgroundColour( *wxWHITE
);
1969 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1971 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1973 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1974 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1975 //Some new added code to deal with the tree structure
1976 m_root
= new wxDataViewTreeNode( NULL
);
1977 m_root
->SetHasChildren(true);
1979 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1981 m_underMouse
= NULL
;
1985 wxDataViewMainWindow::~wxDataViewMainWindow()
1988 delete m_renameTimer
;
1991 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1993 wxDataViewModel
*model
= GetOwner()->GetModel();
1994 wxAutoBufferedPaintDC
dc( this );
1997 dc
.SetPen( *wxTRANSPARENT_PEN
);
1998 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1999 dc
.SetBrush( *wxWHITE_BRUSH
);
2000 wxSize
size( GetClientSize() );
2001 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
2005 GetOwner()->PrepareDC( dc
);
2006 dc
.SetFont( GetFont() );
2008 wxRect update
= GetUpdateRegion().GetBox();
2009 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2011 // compute which items needs to be redrawn
2012 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2013 unsigned int item_count
=
2014 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2015 (int)(GetRowCount( ) - item_start
));
2016 unsigned int item_last
= item_start
+ item_count
;
2018 // compute which columns needs to be redrawn
2019 unsigned int cols
= GetOwner()->GetColumnCount();
2020 unsigned int col_start
= 0;
2021 unsigned int x_start
= 0;
2022 for (x_start
= 0; col_start
< cols
; col_start
++)
2024 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2025 if (col
->IsHidden())
2026 continue; // skip it!
2028 unsigned int w
= col
->GetWidth();
2029 if (x_start
+w
>= (unsigned int)update
.x
)
2035 unsigned int col_last
= col_start
;
2036 unsigned int x_last
= x_start
;
2037 for (; col_last
< cols
; col_last
++)
2039 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2040 if (col
->IsHidden())
2041 continue; // skip it!
2043 if (x_last
> (unsigned int)update
.GetRight())
2046 x_last
+= col
->GetWidth();
2049 // Draw horizontal rules if required
2050 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2052 dc
.SetPen(m_penRule
);
2053 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2055 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2057 int y
= i
* m_lineHeight
;
2058 dc
.DrawLine(x_start
, y
, x_last
, y
);
2062 // Draw vertical rules if required
2063 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2065 dc
.SetPen(m_penRule
);
2066 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2069 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2071 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2072 if (col
->IsHidden())
2073 continue; // skip it
2075 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2076 x
, item_last
* m_lineHeight
);
2078 x
+= col
->GetWidth();
2081 // Draw last vertical rule
2082 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2083 x
, item_last
* m_lineHeight
);
2086 // redraw the background for the items which are selected/current
2087 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2089 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2090 if (selected
|| item
== m_currentRow
)
2092 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2093 if (item
== m_currentRow
)
2094 flags
|= wxCONTROL_CURRENT
;
2096 flags
|= wxCONTROL_FOCUSED
;
2098 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2099 wxRendererNative::Get().DrawItemSelectionRect
2109 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2112 // TODO: last column for RTL support
2113 expander
= GetOwner()->GetColumn( 0 );
2114 GetOwner()->SetExpanderColumn(expander
);
2117 // redraw all cells for all rows which must be repainted and for all columns
2119 cell_rect
.x
= x_start
;
2120 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2121 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2123 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2124 wxDataViewRenderer
*cell
= col
->GetRenderer();
2125 cell_rect
.width
= col
->GetWidth();
2127 if (col
->IsHidden())
2128 continue; // skipt it!
2131 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2133 // get the cell value and set it into the renderer
2135 wxDataViewTreeNode
*node
= NULL
;
2136 wxDataViewItem dataitem
;
2140 node
= GetTreeNodeByRow(item
);
2144 dataitem
= node
->GetItem();
2146 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2151 dataitem
= wxDataViewItem( (void*) item
);
2154 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2155 cell
->SetValue( value
);
2157 if (cell
->GetWantsAttr())
2159 wxDataViewItemAttr attr
;
2160 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2162 cell
->SetAttr( attr
);
2163 cell
->SetHasAttr( ret
);
2166 // update the y offset
2167 cell_rect
.y
= item
* m_lineHeight
;
2169 //Draw the expander here.
2171 if ((m_root
) && (col
== expander
))
2173 indent
= node
->GetIndentLevel();
2175 //Calculate the indent first
2176 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2178 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2179 // change the cell_rect.x to the appropriate pos
2180 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2181 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2182 dc
.SetPen( m_penExpander
);
2183 dc
.SetBrush( wxNullBrush
);
2184 if( node
->HasChildren() )
2186 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2188 if (m_underMouse
== node
)
2190 flag
|= wxCONTROL_CURRENT
;
2192 if( node
->IsOpen() )
2193 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2195 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2199 // I am wondering whether we should draw dot lines between tree nodes
2202 // Yes, if the node does not have any child, it must be a leaf which
2203 // mean that it is a temporarily created by GetTreeNodeByRow
2206 //force the expander column to left-center align
2207 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2211 // cannot be bigger than allocated space
2212 wxSize size
= cell
->GetSize();
2213 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2214 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2215 // size.y = wxMin( size.y, cell_rect.height );
2216 size
.y
= cell_rect
.height
;
2218 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2219 int align
= cell
->GetAlignment();
2221 // horizontal alignment:
2222 item_rect
.x
= cell_rect
.x
;
2223 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2224 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2225 else if (align
& wxALIGN_RIGHT
)
2226 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2227 //else: wxALIGN_LEFT is the default
2229 // vertical alignment:
2230 item_rect
.y
= cell_rect
.y
;
2231 if (align
& wxALIGN_CENTER_VERTICAL
)
2232 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2233 else if (align
& wxALIGN_BOTTOM
)
2234 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2235 //else: wxALIGN_TOP is the default
2238 item_rect
.x
+= PADDING_RIGHTLEFT
;
2239 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2241 //Here we add the tree indent
2242 item_rect
.x
+= indent
;
2245 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2246 state
|= wxDATAVIEW_CELL_SELECTED
;
2248 // TODO: it would be much more efficient to create a clipping
2249 // region for the entire column being rendered (in the OnPaint
2250 // of wxDataViewMainWindow) instead of a single clip region for
2251 // each cell. However it would mean that each renderer should
2252 // respect the given wxRect's top & bottom coords, eventually
2253 // violating only the left & right coords - however the user can
2254 // make its own renderer and thus we cannot be sure of that.
2255 dc
.SetClippingRegion( item_rect
);
2256 cell
->Render( item_rect
, &dc
, state
);
2257 dc
.DestroyClippingRegion();
2260 cell_rect
.x
+= cell_rect
.width
;
2264 void wxDataViewMainWindow::OnRenameTimer()
2266 // We have to call this here because changes may just have
2267 // been made and no screen update taken place.
2272 unsigned int cols
= GetOwner()->GetColumnCount();
2274 for (i
= 0; i
< cols
; i
++)
2276 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2278 continue; // skip it!
2280 if (c
== m_currentCol
)
2282 xpos
+= c
->GetWidth();
2284 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2285 m_currentCol
->GetWidth(), m_lineHeight
);
2287 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2288 &labelRect
.x
, &labelRect
.y
);
2290 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2291 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2295 //------------------------------------------------------------------
2296 // Helper class for do operation on the tree node
2297 //------------------------------------------------------------------
2304 //The return value control how the tree-walker tranverse the tree
2305 // 0: Job done, stop tranverse and return
2306 // 1: Ignore the current node's subtree and continue
2307 // 2: Job not done, continue
2308 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2309 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2310 virtual int operator() ( void * n
) = 0;
2313 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2318 switch( func( node
) )
2329 wxDataViewTreeNodes nodes
= node
->GetNodes();
2330 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2332 int len_nodes
= nodes
.GetCount();
2333 int len
= leaves
.GetCount();
2334 int i
= 0, nodes_i
= 0;
2336 for( ; i
< len
; i
++ )
2338 void * n
= leaves
[i
];
2339 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2341 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2344 if( Walker( nd
, func
) )
2363 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2374 wxDataViewTreeNode
* node
;
2375 node
= FindNode(parent
);
2380 node
->SetHasChildren( true );
2382 if( g_model
->IsContainer( item
) )
2384 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2385 newnode
->SetItem(item
);
2386 newnode
->SetHasChildren( true );
2387 node
->AddNode( newnode
);
2390 node
->AddLeaf( item
.GetID() );
2392 node
->ChangeSubTreeCount(1);
2400 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2402 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2403 const wxDataViewItem
& item
)
2408 if( m_currentRow
> GetRowCount() )
2409 m_currentRow
= m_count
- 1;
2411 m_selection
.Empty();
2418 wxDataViewTreeNode
* node
= FindNode(parent
);
2420 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2421 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2424 node
->GetChildren().Remove( item
.GetID() );
2425 //Manuplate selection
2426 if( m_selection
.GetCount() > 1 )
2428 m_selection
.Empty();
2430 bool isContainer
= false;
2431 wxDataViewTreeNodes nds
= node
->GetNodes();
2432 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2434 if (nds
[i
]->GetItem() == item
)
2442 wxDataViewTreeNode
* n
= NULL
;
2443 wxDataViewTreeNodes nodes
= node
->GetNodes();
2444 int len
= nodes
.GetCount();
2445 for( int i
= 0 ; i
< len
; i
++)
2447 if( nodes
[i
]->GetItem() == item
)
2454 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2456 node
->GetNodes().Remove( n
);
2457 sub
-= n
->GetSubTreeCount();
2458 DestroyTreeHelper(n
);
2460 //Make the row number invalid and get a new valid one when user call GetRowCount
2462 node
->ChangeSubTreeCount(sub
);
2463 if( node
->GetChildrenNumber() == 0)
2465 node
->GetParent()->GetNodes().Remove( node
);
2469 //Change the current row to the last row if the current exceed the max row number
2470 if( m_currentRow
> GetRowCount() )
2471 m_currentRow
= m_count
- 1;
2478 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2484 wxWindow
*parent
= GetParent();
2485 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2486 le
.SetEventObject(parent
);
2487 le
.SetModel(GetOwner()->GetModel());
2489 parent
->GetEventHandler()->ProcessEvent(le
);
2494 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2496 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2497 /*#define MAX_VIRTUAL_WIDTH 100000
2499 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2500 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2501 Refresh( true, &rect );
2509 wxWindow
*parent
= GetParent();
2510 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2511 le
.SetEventObject(parent
);
2512 le
.SetModel(GetOwner()->GetModel());
2515 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2516 parent
->GetEventHandler()->ProcessEvent(le
);
2521 bool wxDataViewMainWindow::Cleared()
2526 BuildTree( GetOwner()->GetModel() );
2533 void wxDataViewMainWindow::UpdateDisplay()
2538 void wxDataViewMainWindow::OnInternalIdle()
2540 wxWindow::OnInternalIdle();
2544 RecalculateDisplay();
2549 void wxDataViewMainWindow::RecalculateDisplay()
2551 wxDataViewModel
*model
= GetOwner()->GetModel();
2558 int width
= GetEndOfLastCol();
2559 int height
= GetRowCount() * m_lineHeight
;
2561 SetVirtualSize( width
, height
);
2562 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2567 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2569 wxWindow::ScrollWindow( dx
, dy
, rect
);
2571 if (GetOwner()->m_headerArea
)
2572 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2575 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2578 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2579 int sy
= rows
*m_lineHeight
/y
;
2583 wxRect rect
= GetClientRect();
2585 int x_start
= 0, x_end
= 0, w
= 0;
2587 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2588 for (x_start
= 0; colnum
< column
; colnum
++)
2590 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2591 if (col
->IsHidden())
2592 continue; // skip it!
2594 w
= col
->GetWidth();
2598 x_end
= x_start
+ w
;
2599 xe
= xx
+ rect
.width
;
2602 sx
= ( xx
+ x_end
- xe
)/x
;
2609 m_owner
->Scroll( sx
, sy
);
2612 int wxDataViewMainWindow::GetCountPerPage() const
2614 wxSize size
= GetClientSize();
2615 return size
.y
/ m_lineHeight
;
2618 int wxDataViewMainWindow::GetEndOfLastCol() const
2622 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2624 const wxDataViewColumn
*c
=
2625 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2628 width
+= c
->GetWidth();
2633 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2637 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2639 return y
/ m_lineHeight
;
2642 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2644 wxSize client_size
= GetClientSize();
2645 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2646 &client_size
.x
, &client_size
.y
);
2648 //we should deal with the pixel here
2649 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2651 return wxMin( GetRowCount()-1, row
);
2654 unsigned int wxDataViewMainWindow::GetRowCount()
2656 if ( m_count
== -1 )
2658 m_count
= RecalculateCount();
2660 GetVirtualSize( &width
, &height
);
2661 height
= m_count
* m_lineHeight
;
2663 SetVirtualSize( width
, height
);
2668 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2675 void wxDataViewMainWindow::SelectAllRows( bool on
)
2682 m_selection
.Clear();
2683 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2684 m_selection
.Add( i
);
2689 unsigned int first_visible
= GetFirstVisibleRow();
2690 unsigned int last_visible
= GetLastVisibleRow();
2692 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2694 unsigned int row
= m_selection
[i
];
2695 if ((row
>= first_visible
) && (row
<= last_visible
))
2698 m_selection
.Clear();
2702 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2704 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2708 m_selection
.Add( row
);
2716 m_selection
.Remove( row
);
2722 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2726 unsigned int tmp
= from
;
2732 for (i
= from
; i
<= to
; i
++)
2734 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2737 m_selection
.Add( i
);
2742 m_selection
.Remove( i
);
2745 RefreshRows( from
, to
);
2748 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2750 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2752 int n
= aSelections
[i
];
2754 m_selection
.Add( n
);
2759 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2761 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2762 m_selection
.Add( row
);
2764 m_selection
.Remove( row
);
2768 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2770 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2773 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2775 wxWindow
*parent
= GetParent();
2776 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2778 le
.SetEventObject(parent
);
2779 le
.SetModel(GetOwner()->GetModel());
2782 parent
->GetEventHandler()->ProcessEvent(le
);
2785 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2787 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2788 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2790 wxSize client_size
= GetClientSize();
2791 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2792 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2793 if (intersect_rect
.width
> 0)
2794 Refresh( true, &intersect_rect
);
2797 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2801 unsigned int tmp
= to
;
2806 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2807 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2809 wxSize client_size
= GetClientSize();
2810 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2811 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2812 if (intersect_rect
.width
> 0)
2813 Refresh( true, &intersect_rect
);
2816 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2818 unsigned int count
= GetRowCount();
2819 if (firstRow
> count
)
2822 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2823 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2825 wxSize client_size
= GetClientSize();
2826 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2827 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2828 if (intersect_rect
.width
> 0)
2829 Refresh( true, &intersect_rect
);
2832 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2834 wxCHECK_RET( newCurrent
< GetRowCount(),
2835 _T("invalid item index in OnArrowChar()") );
2837 // if there is no selection, we cannot move it anywhere
2838 if (!HasCurrentRow())
2841 unsigned int oldCurrent
= m_currentRow
;
2843 // in single selection we just ignore Shift as we can't select several
2845 if ( event
.ShiftDown() && !IsSingleSel() )
2847 RefreshRow( oldCurrent
);
2849 ChangeCurrentRow( newCurrent
);
2851 // select all the items between the old and the new one
2852 if ( oldCurrent
> newCurrent
)
2854 newCurrent
= oldCurrent
;
2855 oldCurrent
= m_currentRow
;
2858 SelectRows( oldCurrent
, newCurrent
, true );
2859 if (oldCurrent
!=newCurrent
)
2860 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2864 RefreshRow( oldCurrent
);
2866 // all previously selected items are unselected unless ctrl is held
2867 if ( !event
.ControlDown() )
2868 SelectAllRows(false);
2870 ChangeCurrentRow( newCurrent
);
2872 if ( !event
.ControlDown() )
2874 SelectRow( m_currentRow
, true );
2875 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2878 RefreshRow( m_currentRow
);
2881 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2884 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2888 rect
.y
= m_lineHeight
* row
;
2889 rect
.width
= GetEndOfLastCol();
2890 rect
.height
= m_lineHeight
;
2895 class RowToItemJob
: public DoJob
2898 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2899 virtual ~RowToItemJob(){};
2901 virtual int operator() ( wxDataViewTreeNode
* node
)
2904 if( current
== static_cast<int>(row
))
2906 ret
= node
->GetItem() ;
2910 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2912 current
+= node
->GetSubTreeCount();
2917 //If the current has no child node, we can find the desired item of the row number directly.
2918 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2919 if( node
->GetNodes().GetCount() == 0)
2921 int index
= static_cast<int>(row
) - current
- 1;
2922 ret
= node
->GetChildren().Item( index
);
2929 virtual int operator() ( void * n
)
2932 if( current
== static_cast<int>(row
))
2934 ret
= wxDataViewItem( n
) ;
2939 wxDataViewItem
GetResult(){ return ret
; }
2946 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2950 return wxDataViewItem( (void*) row
);
2954 RowToItemJob
job( row
, -2 );
2955 Walker( m_root
, job
);
2956 return job
.GetResult();
2960 class RowToTreeNodeJob
: public DoJob
2963 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2966 this->current
= current
;
2970 virtual ~RowToTreeNodeJob(){};
2972 virtual int operator() ( wxDataViewTreeNode
* node
)
2975 if( current
== static_cast<int>(row
))
2981 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2983 current
+= node
->GetSubTreeCount();
2989 //If the current has no child node, we can find the desired item of the row number directly.
2990 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2991 if( node
->GetNodes().GetCount() == 0)
2993 int index
= static_cast<int>(row
) - current
- 1;
2994 void * n
= node
->GetChildren().Item( index
);
2995 ret
= new wxDataViewTreeNode( parent
) ;
2996 ret
->SetItem( wxDataViewItem( n
));
2997 ret
->SetHasChildren(false);
3006 virtual int operator() ( void * n
)
3009 if( current
== static_cast<int>(row
))
3011 ret
= new wxDataViewTreeNode( parent
) ;
3012 ret
->SetItem( wxDataViewItem( n
));
3013 ret
->SetHasChildren(false);
3019 wxDataViewTreeNode
* GetResult(){ return ret
; }
3023 wxDataViewTreeNode
* ret
;
3024 wxDataViewTreeNode
* parent
;
3028 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
3036 RowToTreeNodeJob
job( row
, -2, m_root
);
3037 Walker( m_root
, job
);
3038 return job
.GetResult();
3042 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3044 wxWindow
*parent
= GetParent();
3045 wxDataViewEvent
le(type
, parent
->GetId());
3047 le
.SetEventObject(parent
);
3048 le
.SetModel(GetOwner()->GetModel());
3051 parent
->GetEventHandler()->ProcessEvent(le
);
3055 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3057 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3060 if( node
->HasChildren())
3062 if( !node
->IsOpen())
3064 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3065 //Check if the user prevent expanding
3066 if( e
.GetSkipped() )
3070 //Here I build the children of current node
3071 if( node
->GetChildrenNumber() == 0 )
3074 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3078 //Send the expanded event
3079 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3083 SelectRow( row
, false );
3084 SelectRow( row
+ 1, true );
3085 ChangeCurrentRow( row
+ 1 );
3086 SendSelectionChangedEvent( GetItemByRow(row
+1));
3094 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3096 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3099 wxDataViewTreeNode
* nd
= node
;
3101 if( node
->HasChildren() && node
->IsOpen() )
3103 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3104 if( e
.GetSkipped() )
3109 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3113 node
= node
->GetParent();
3116 int parent
= GetRowByItem( node
->GetItem() ) ;
3119 SelectRow( row
, false);
3120 SelectRow(parent
, true );
3121 ChangeCurrentRow( parent
);
3122 SendSelectionChangedEvent( node
->GetItem() );
3126 if( !nd
->HasChildren())
3131 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3133 wxDataViewModel
* model
= GetOwner()->GetModel();
3137 //Compose the a parent-chain of the finding item
3139 list
.DeleteContents( true );
3140 wxDataViewItem
it( item
);
3143 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3144 list
.Insert( pItem
);
3145 it
= model
->GetParent( it
);
3148 //Find the item along the parent-chain.
3149 //This algorithm is designed to speed up the node-finding method
3150 wxDataViewTreeNode
* node
= m_root
;
3151 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3153 if( node
->HasChildren() )
3155 if( node
->GetChildrenNumber() == 0 )
3158 BuildTreeHelper(model
, node
->GetItem(), node
);
3161 wxDataViewTreeNodes nodes
= node
->GetNodes();
3163 for (; i
< nodes
.GetCount(); i
++)
3165 if (nodes
[i
]->GetItem() == (**iter
))
3171 if (i
== nodes
.GetCount())
3180 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3182 wxDataViewColumn
*col
= NULL
;
3183 unsigned int cols
= GetOwner()->GetColumnCount();
3184 unsigned int colnum
= 0;
3185 unsigned int x_start
= 0;
3187 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3188 for (x_start
= 0; colnum
< cols
; colnum
++)
3190 col
= GetOwner()->GetColumn(colnum
);
3191 if (col
->IsHidden())
3192 continue; // skip it!
3194 unsigned int w
= col
->GetWidth();
3195 if (x_start
+w
>= (unsigned int)x
)
3202 item
= GetItemByRow( y
/m_lineHeight
);
3205 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3207 int row
= GetRowByItem(item
);
3208 int y
= row
*m_lineHeight
;
3209 int h
= m_lineHeight
;
3211 wxDataViewColumn
*col
= NULL
;
3212 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3214 col
= GetOwner()->GetColumn( i
);
3215 x
+= col
->GetWidth();
3216 if( GetOwner()->GetColumn(i
+1) == column
)
3219 int w
= col
->GetWidth();
3220 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3221 return wxRect(x
, y
, w
, h
);
3224 int wxDataViewMainWindow::RecalculateCount()
3228 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3230 return list_model
->GetLastIndex() + 1;
3232 return list_model
->GetLastIndex() - 1;
3237 return m_root
->GetSubTreeCount();
3241 class ItemToRowJob
: public DoJob
3244 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3245 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3246 virtual ~ItemToRowJob(){};
3248 //Maybe binary search will help to speed up this process
3249 virtual int operator() ( wxDataViewTreeNode
* node
)
3252 if( node
->GetItem() == item
)
3257 if( node
->GetItem() == **m_iter
)
3264 ret
+= node
->GetSubTreeCount();
3270 virtual int operator() ( void * n
)
3273 if( n
== item
.GetID() )
3277 //the row number is begin from zero
3278 int GetResult(){ return ret
-1 ; }
3280 ItemList::const_iterator m_iter
;
3281 wxDataViewItem item
;
3286 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3288 wxDataViewModel
* model
= GetOwner()->GetModel();
3294 return wxPtrToUInt( item
.GetID() );
3301 //Compose the a parent-chain of the finding item
3303 wxDataViewItem
* pItem
= NULL
;
3304 list
.DeleteContents( true );
3305 wxDataViewItem
it( item
);
3308 pItem
= new wxDataViewItem( it
);
3309 list
.Insert( pItem
);
3310 it
= model
->GetParent( it
);
3312 pItem
= new wxDataViewItem( );
3313 list
.Insert( pItem
);
3315 ItemToRowJob
job( item
, list
.begin() );
3316 Walker(m_root
, job
);
3317 return job
.GetResult();
3321 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3323 if( !model
->IsContainer( item
) )
3326 wxDataViewItemArray children
;
3327 unsigned int num
= model
->GetChildren( item
, children
);
3328 unsigned int index
= 0;
3329 while( index
< num
)
3331 if( model
->IsContainer( children
[index
] ) )
3333 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3334 n
->SetItem(children
[index
]);
3335 n
->SetHasChildren( true ) ;
3340 node
->AddLeaf( children
[index
].GetID() );
3344 node
->SetSubTreeCount( num
);
3345 wxDataViewTreeNode
* n
= node
->GetParent();
3347 n
->ChangeSubTreeCount(num
);
3351 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3355 if (GetOwner()->GetModel()->IsIndexListModel())
3361 m_root
= new wxDataViewTreeNode( NULL
);
3362 m_root
->SetHasChildren(true);
3364 //First we define a invalid item to fetch the top-level elements
3365 wxDataViewItem item
;
3367 BuildTreeHelper( model
, item
, m_root
);
3371 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3373 if( node
->GetNodeNumber() != 0 )
3375 int len
= node
->GetNodeNumber();
3377 wxDataViewTreeNodes nodes
= node
->GetNodes();
3378 for( ; i
< len
; i
++ )
3380 DestroyTreeHelper(nodes
[i
]);
3386 void wxDataViewMainWindow::DestroyTree()
3390 DestroyTreeHelper(m_root
);
3396 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3398 if (event
.GetKeyCode() == WXK_TAB
)
3400 wxNavigationKeyEvent nevent
;
3401 nevent
.SetWindowChange( event
.ControlDown() );
3402 nevent
.SetDirection( !event
.ShiftDown() );
3403 nevent
.SetEventObject( GetParent()->GetParent() );
3404 nevent
.SetCurrentFocus( m_parent
);
3405 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3409 // no item -> nothing to do
3410 if (!HasCurrentRow())
3416 // don't use m_linesPerPage directly as it might not be computed yet
3417 const int pageSize
= GetCountPerPage();
3418 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3420 switch ( event
.GetKeyCode() )
3424 if (m_currentRow
> 0)
3426 wxWindow
*parent
= GetParent();
3427 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3428 le
.SetItem( GetItemByRow(m_currentRow
) );
3429 le
.SetEventObject(parent
);
3430 le
.SetModel(GetOwner()->GetModel());
3432 parent
->GetEventHandler()->ProcessEvent(le
);
3437 if ( m_currentRow
> 0 )
3438 OnArrowChar( m_currentRow
- 1, event
);
3442 if ( m_currentRow
< GetRowCount() - 1 )
3443 OnArrowChar( m_currentRow
+ 1, event
);
3445 //Add the process for tree expanding/collapsing
3447 OnCollapsing(m_currentRow
);
3450 OnExpanding( m_currentRow
);
3454 OnArrowChar( GetRowCount() - 1, event
);
3459 OnArrowChar( 0, event
);
3464 int steps
= pageSize
- 1;
3465 int index
= m_currentRow
- steps
;
3469 OnArrowChar( index
, event
);
3475 int steps
= pageSize
- 1;
3476 unsigned int index
= m_currentRow
+ steps
;
3477 unsigned int count
= GetRowCount();
3478 if ( index
>= count
)
3481 OnArrowChar( index
, event
);
3490 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3492 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3494 // let the base handle mouse wheel events.
3499 int x
= event
.GetX();
3500 int y
= event
.GetY();
3501 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3502 wxDataViewColumn
*col
= NULL
;
3505 unsigned int cols
= GetOwner()->GetColumnCount();
3507 for (i
= 0; i
< cols
; i
++)
3509 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3511 continue; // skip it!
3513 if (x
< xpos
+ c
->GetWidth())
3518 xpos
+= c
->GetWidth();
3523 wxDataViewRenderer
*cell
= col
->GetRenderer();
3524 unsigned int current
= y
/ m_lineHeight
;
3525 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3527 // Unselect all if below the last row ?
3531 //Test whether the mouse is hovered on the tree item button
3533 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3535 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3536 if( node
!=NULL
&& node
->HasChildren() )
3538 int indent
= node
->GetIndentLevel();
3539 indent
= GetOwner()->GetIndent()*indent
;
3540 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3541 if( rect
.Contains( x
, y
) )
3543 //So the mouse is over the expander
3545 if (m_underMouse
&& m_underMouse
!= node
)
3547 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3548 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3550 if (m_underMouse
!= node
)
3552 //wxLogMessage("Do the row: %d", current);
3553 RefreshRow(current
);
3555 m_underMouse
= node
;
3558 if (node
!=NULL
&& !node
->HasChildren())
3563 if (m_underMouse
!= NULL
)
3565 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3566 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3567 m_underMouse
= NULL
;
3571 wxDataViewModel
*model
= GetOwner()->GetModel();
3573 if (event
.Dragging())
3575 if (m_dragCount
== 0)
3577 // we have to report the raw, physical coords as we want to be
3578 // able to call HitTest(event.m_pointDrag) from the user code to
3579 // get the item being dragged
3580 m_dragStart
= event
.GetPosition();
3585 if (m_dragCount
!= 3)
3588 if (event
.LeftIsDown())
3590 // Notify cell about drag
3599 bool forceClick
= false;
3601 if (event
.ButtonDClick())
3603 m_renameTimer
->Stop();
3604 m_lastOnSame
= false;
3607 wxDataViewItem item
= GetItemByRow(current
);
3608 bool ignore_other_columns
=
3609 ((GetOwner()->GetExpanderColumn() != col
) &&
3610 (model
->IsContainer(item
)) &&
3611 (!model
->HasContainerColumns(item
)));
3613 if (event
.LeftDClick())
3615 if ( current
== m_lineLastClicked
)
3617 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3620 model
->GetValue( value
, item
, col
->GetModelColumn() );
3621 cell
->SetValue( value
);
3622 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3623 col
->GetWidth(), m_lineHeight
);
3624 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3629 wxWindow
*parent
= GetParent();
3630 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3632 le
.SetEventObject(parent
);
3633 le
.SetModel(GetOwner()->GetModel());
3635 parent
->GetEventHandler()->ProcessEvent(le
);
3641 // The first click was on another item, so don't interpret this as
3642 // a double click, but as a simple click instead
3649 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3651 // select single line
3652 SelectAllRows( false );
3653 SelectRow( m_lineSelectSingleOnUp
, true );
3656 //Process the event of user clicking the expander
3657 bool expander
= false;
3658 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3660 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3661 if( node
!=NULL
&& node
->HasChildren() )
3663 int indent
= node
->GetIndentLevel();
3664 indent
= GetOwner()->GetIndent()*indent
;
3665 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3666 if( rect
.Contains( x
, y
) )
3669 if( node
->IsOpen() )
3670 OnCollapsing(current
);
3672 OnExpanding( current
);
3676 //If the user click the expander, we do not do editing even if the column with expander are editable
3677 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3679 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3680 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3682 m_renameTimer
->Start( 100, true );
3686 m_lastOnSame
= false;
3687 m_lineSelectSingleOnUp
= (unsigned int)-1;
3691 // This is necessary, because after a DnD operation in
3692 // from and to ourself, the up event is swallowed by the
3693 // DnD code. So on next non-up event (which means here and
3694 // now) m_lineSelectSingleOnUp should be reset.
3695 m_lineSelectSingleOnUp
= (unsigned int)-1;
3698 if (event
.RightDown())
3700 m_lineBeforeLastClicked
= m_lineLastClicked
;
3701 m_lineLastClicked
= current
;
3703 // If the item is already selected, do not update the selection.
3704 // Multi-selections should not be cleared if a selected item is clicked.
3705 if (!IsRowSelected(current
))
3707 SelectAllRows(false);
3708 ChangeCurrentRow(current
);
3709 SelectRow(m_currentRow
,true);
3710 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3713 // notify cell about right click
3716 // Allow generation of context menu event
3719 else if (event
.MiddleDown())
3721 // notify cell about middle click
3724 if (event
.LeftDown() || forceClick
)
3728 m_lineBeforeLastClicked
= m_lineLastClicked
;
3729 m_lineLastClicked
= current
;
3731 unsigned int oldCurrentRow
= m_currentRow
;
3732 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3734 bool cmdModifierDown
= event
.CmdDown();
3735 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3737 if ( IsSingleSel() || !IsRowSelected(current
) )
3739 SelectAllRows( false );
3740 ChangeCurrentRow(current
);
3741 SelectRow(m_currentRow
,true);
3742 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3744 else // multi sel & current is highlighted & no mod keys
3746 m_lineSelectSingleOnUp
= current
;
3747 ChangeCurrentRow(current
); // change focus
3750 else // multi sel & either ctrl or shift is down
3752 if (cmdModifierDown
)
3754 ChangeCurrentRow(current
);
3755 ReverseRowSelection(m_currentRow
);
3756 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3758 else if (event
.ShiftDown())
3760 ChangeCurrentRow(current
);
3762 unsigned int lineFrom
= oldCurrentRow
,
3765 if ( lineTo
< lineFrom
)
3768 lineFrom
= m_currentRow
;
3771 SelectRows(lineFrom
, lineTo
, true);
3772 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3774 else // !ctrl, !shift
3776 // test in the enclosing if should make it impossible
3777 wxFAIL_MSG( _T("how did we get here?") );
3781 if (m_currentRow
!= oldCurrentRow
)
3782 RefreshRow( oldCurrentRow
);
3784 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3786 // Update selection here...
3789 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3790 (current
== oldCurrentRow
)) && oldWasSelected
;
3794 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3798 if (HasCurrentRow())
3804 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3808 if (HasCurrentRow())
3814 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3816 if( m_selection
.GetCount() != 1 )
3817 return wxDataViewItem();
3819 return GetItemByRow( m_selection
.Item(0));
3822 //-----------------------------------------------------------------------------
3824 //-----------------------------------------------------------------------------
3825 WX_DEFINE_LIST(wxDataViewColumnList
)
3827 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3829 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3830 EVT_SIZE(wxDataViewCtrl::OnSize
)
3833 wxDataViewCtrl::~wxDataViewCtrl()
3836 GetModel()->RemoveNotifier( m_notifier
);
3839 void wxDataViewCtrl::Init()
3844 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3845 const wxPoint
& pos
, const wxSize
& size
,
3846 long style
, const wxValidator
& validator
)
3848 if (!wxControl::Create( parent
, id
, pos
, size
,
3849 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3852 SetInitialSize(size
);
3857 MacSetClipChildren( true ) ;
3860 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3862 if (HasFlag(wxDV_NO_HEADER
))
3863 m_headerArea
= NULL
;
3865 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3867 SetTargetWindow( m_clientArea
);
3869 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3871 sizer
->Add( m_headerArea
, 0, wxGROW
);
3872 sizer
->Add( m_clientArea
, 1, wxGROW
);
3879 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3883 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3886 // we need to process arrows ourselves for scrolling
3887 if ( nMsg
== WM_GETDLGCODE
)
3889 rc
|= DLGC_WANTARROWS
;
3897 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3899 // We need to override OnSize so that our scrolled
3900 // window a) does call Layout() to use sizers for
3901 // positioning the controls but b) does not query
3902 // the sizer for their size and use that for setting
3903 // the scrollable area as set that ourselves by
3904 // calling SetScrollbar() further down.
3911 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3913 if (!wxDataViewCtrlBase::AssociateModel( model
))
3916 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3918 model
->AddNotifier( m_notifier
);
3920 m_clientArea
->DestroyTree();
3922 m_clientArea
->BuildTree(model
);
3924 m_clientArea
->UpdateDisplay();
3929 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3931 if (!wxDataViewCtrlBase::AppendColumn(col
))
3934 m_cols
.Append( col
);
3939 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3941 if (!wxDataViewCtrlBase::PrependColumn(col
))
3944 m_cols
.Insert( col
);
3949 void wxDataViewCtrl::OnColumnChange()
3952 m_headerArea
->UpdateDisplay();
3954 m_clientArea
->UpdateDisplay();
3957 void wxDataViewCtrl::DoSetExpanderColumn()
3959 m_clientArea
->UpdateDisplay();
3962 void wxDataViewCtrl::DoSetIndent()
3964 m_clientArea
->UpdateDisplay();
3967 unsigned int wxDataViewCtrl::GetColumnCount() const
3969 return m_cols
.GetCount();
3972 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3974 wxDataViewColumnList::const_iterator iter
;
3976 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3981 if ((*iter
)->IsHidden())
3988 void wxDataViewCtrl::ColumnMoved( wxDataViewColumn
* col
, unsigned int new_pos
)
3990 if (new_pos
> m_cols
.GetCount()) return;
3991 m_cols
.DeleteObject( col
);
3992 m_cols
.Insert( new_pos
, col
);
3994 m_clientArea
->UpdateDisplay();
3997 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3999 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4010 bool wxDataViewCtrl::ClearColumns()
4017 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4019 int ret
= 0, dead
= 0;
4020 int len
= GetColumnCount();
4021 for (int i
=0; i
<len
; i
++)
4023 wxDataViewColumn
* col
= GetColumn(i
);
4024 if (col
->IsHidden())
4026 ret
+= col
->GetWidth();
4029 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
4036 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4041 //Selection code with wxDataViewItem as parameters
4042 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4044 return m_clientArea
->GetSelection();
4047 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4050 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4051 int len
= selection
.GetCount();
4052 for( int i
= 0; i
< len
; i
++)
4054 unsigned int row
= selection
[i
];
4055 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4060 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4062 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4063 int len
= sel
.GetCount();
4064 for( int i
= 0; i
< len
; i
++ )
4066 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4068 selection
.Add( static_cast<unsigned int>(row
) );
4070 m_clientArea
->SetSelections( selection
);
4073 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4075 int row
= m_clientArea
->GetRowByItem( item
);
4078 //Unselect all rows before select another in the single select mode
4079 if (m_clientArea
->IsSingleSel())
4080 m_clientArea
->SelectAllRows(false);
4081 m_clientArea
->SelectRow(row
, true);
4085 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4087 int row
= m_clientArea
->GetRowByItem( item
);
4089 m_clientArea
->SelectRow(row
, false);
4092 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4094 int row
= m_clientArea
->GetRowByItem( item
);
4097 return m_clientArea
->IsRowSelected(row
);
4102 //Selection code with row number as parameter
4103 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4106 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4107 int len
= selection
.GetCount();
4108 for( int i
= 0; i
< len
; i
++)
4110 unsigned int row
= selection
[i
];
4116 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4118 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4119 int len
= sel
.GetCount();
4120 for( int i
= 0; i
< len
; i
++ )
4124 selection
.Add( static_cast<unsigned int>(row
) );
4126 m_clientArea
->SetSelections( selection
);
4129 void wxDataViewCtrl::Select( int row
)
4133 if (m_clientArea
->IsSingleSel())
4134 m_clientArea
->SelectAllRows(false);
4135 m_clientArea
->SelectRow( row
, true );
4139 void wxDataViewCtrl::Unselect( int row
)
4142 m_clientArea
->SelectRow(row
, false);
4145 bool wxDataViewCtrl::IsSelected( int row
) const
4148 return m_clientArea
->IsRowSelected(row
);
4152 void wxDataViewCtrl::SelectRange( int from
, int to
)
4155 for( int i
= from
; i
< to
; i
++ )
4157 m_clientArea
->Select(sel
);
4160 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4162 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4163 for( int i
= from
; i
< to
; i
++ )
4164 if( sel
.Index( i
) != wxNOT_FOUND
)
4166 m_clientArea
->SetSelections(sel
);
4169 void wxDataViewCtrl::SelectAll()
4171 m_clientArea
->SelectAllRows(true);
4174 void wxDataViewCtrl::UnselectAll()
4176 m_clientArea
->SelectAllRows(false);
4179 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4183 if( row
> (int) m_clientArea
->GetRowCount() )
4184 row
= m_clientArea
->GetRowCount();
4186 int first
= m_clientArea
->GetFirstVisibleRow();
4187 int last
= m_clientArea
->GetLastVisibleRow();
4189 m_clientArea
->ScrollTo( row
, column
);
4190 else if( row
> last
)
4191 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4193 m_clientArea
->ScrollTo( first
, column
);
4196 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4198 int row
= m_clientArea
->GetRowByItem(item
);
4201 if( column
== NULL
)
4202 EnsureVisible(row
, -1);
4206 int len
= GetColumnCount();
4207 for( int i
= 0; i
< len
; i
++ )
4208 if( GetColumn(i
) == column
)
4213 EnsureVisible( row
, col
);
4219 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4221 m_clientArea
->HitTest(point
, item
, column
);
4224 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4226 return m_clientArea
->GetItemRect(item
, column
);
4229 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4231 return m_clientArea
->GetItemByRow( row
);
4234 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4236 return m_clientArea
->GetRowByItem( item
);
4239 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4241 int row
= m_clientArea
->GetRowByItem( item
);
4243 m_clientArea
->Expand(row
);
4246 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4248 int row
= m_clientArea
->GetRowByItem( item
);
4250 m_clientArea
->Collapse(row
);
4254 // !wxUSE_GENERICDATAVIEWCTRL
4257 // wxUSE_DATAVIEWCTRL