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 // called Refresh afterwards
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
147 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
149 wxSize
DoGetBestSize() const;
151 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
153 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
154 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
160 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
163 #else // !defined(__WXMSW__)
165 #define HEADER_WINDOW_HEIGHT 25
166 #define HEADER_HORIZ_BORDER 5
167 #define HEADER_VERT_BORDER 3
168 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
170 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
173 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
175 const wxPoint
&pos
= wxDefaultPosition
,
176 const wxSize
&size
= wxDefaultSize
,
177 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
180 Create(parent
, id
, pos
, size
, name
);
183 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
184 const wxPoint
&pos
, const wxSize
&size
,
185 const wxString
&name
);
187 ~wxGenericDataViewHeaderWindow()
189 delete m_resizeCursor
;
192 virtual void UpdateDisplay() { Refresh(); }
196 void OnPaint( wxPaintEvent
&event
);
197 void OnMouse( wxMouseEvent
&event
);
198 void OnSetFocus( wxFocusEvent
&event
);
203 // vars used for column resizing:
205 wxCursor
*m_resizeCursor
;
206 const wxCursor
*m_currentCursor
;
209 bool m_dirty
; // needs refresh?
210 int m_hover
; // index of the column under the mouse
211 int m_column
; // index of the column being resized
212 int m_currentX
; // divider line position in logical (unscrolled) coords
213 int m_minX
; // minimal position beyond which the divider line
214 // can't be dragged in logical coords
216 // the pen used to draw the current column width drag line
217 // when resizing the columsn
221 // internal utilities:
225 m_currentCursor
= (wxCursor
*) NULL
;
226 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
228 m_isDragging
= false;
231 m_hover
= wxNOT_FOUND
;
232 m_column
= wxNOT_FOUND
;
236 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
237 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
240 void AdjustDC(wxDC
& dc
);
243 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
244 DECLARE_EVENT_TABLE()
247 #endif // defined(__WXMSW__)
249 //-----------------------------------------------------------------------------
250 // wxDataViewRenameTimer
251 //-----------------------------------------------------------------------------
253 class wxDataViewRenameTimer
: public wxTimer
256 wxDataViewMainWindow
*m_owner
;
259 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
263 //-----------------------------------------------------------------------------
264 // wxDataViewTreeNode
265 //-----------------------------------------------------------------------------
266 class wxDataViewTreeNode
;
267 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
268 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
270 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
271 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
273 class wxDataViewTreeNode
276 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
283 m_hasChildren
= false;
287 ~wxDataViewTreeNode()
291 wxDataViewTreeNode
* GetParent() { return m_parent
; }
292 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
293 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
294 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
296 void AddNode( wxDataViewTreeNode
* node
)
298 m_leaves
.Add( node
->GetItem().GetID() );
300 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
303 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
305 void AddLeaf( void * leaf
)
307 m_leaves
.Add( leaf
);
309 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
312 wxDataViewItem
& GetItem() { return m_item
; }
313 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
315 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
316 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
320 wxDataViewTreeNode
* node
= this;
321 while( node
->GetParent()->GetParent() != NULL
)
323 node
= node
->GetParent();
336 int len
= m_nodes
.GetCount();
338 for ( int i
= 0 ;i
< len
; i
++)
339 sum
+= m_nodes
[i
]->GetSubTreeCount();
341 sum
+= m_leaves
.GetCount();
344 ChangeSubTreeCount(-sum
);
350 ChangeSubTreeCount(sum
);
353 bool HasChildren() { return m_hasChildren
; }
354 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
356 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
357 int GetSubTreeCount() { return m_subTreeCount
; }
358 void ChangeSubTreeCount( int num
)
362 m_subTreeCount
+= num
;
364 m_parent
->ChangeSubTreeCount(num
);
371 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
372 int len
= m_nodes
.GetCount();
373 for (int i
= 0; i
< len
; i
++)
374 m_nodes
[i
]->Resort();
375 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
380 wxDataViewTreeNode
*m_parent
;
381 wxDataViewTreeNodes m_nodes
;
382 wxDataViewTreeLeaves m_leaves
;
383 wxDataViewItem m_item
;
389 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
391 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
394 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
396 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
401 //-----------------------------------------------------------------------------
402 // wxDataViewMainWindow
403 //-----------------------------------------------------------------------------
405 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
407 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
408 WX_DEFINE_LIST(ItemList
)
410 class wxDataViewMainWindow
: public wxWindow
413 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
415 const wxPoint
&pos
= wxDefaultPosition
,
416 const wxSize
&size
= wxDefaultSize
,
417 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
418 virtual ~wxDataViewMainWindow();
420 // notifications from wxDataViewModel
421 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
422 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
423 bool ItemChanged( const wxDataViewItem
&item
);
424 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
438 g_model
= GetOwner()->GetModel();
439 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
442 if (g_model
->HasDefaultCompare())
450 g_column
= col
->GetModelColumn();
451 g_asending
= col
->IsSortOrderAscending();
454 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
455 wxDataViewCtrl
*GetOwner() { return m_owner
; }
456 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
458 void OnPaint( wxPaintEvent
&event
);
459 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
460 void OnChar( wxKeyEvent
&event
);
461 void OnMouse( wxMouseEvent
&event
);
462 void OnSetFocus( wxFocusEvent
&event
);
463 void OnKillFocus( wxFocusEvent
&event
);
465 void UpdateDisplay();
466 void RecalculateDisplay();
467 void OnInternalIdle();
469 void OnRenameTimer();
471 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
472 void ScrollTo( int rows
, int column
);
474 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
475 void ChangeCurrentRow( unsigned int row
);
477 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
478 bool IsEmpty() { return GetRowCount() == 0; }
480 int GetCountPerPage() const;
481 int GetEndOfLastCol() const;
482 unsigned int GetFirstVisibleRow() const;
483 //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
484 unsigned int GetLastVisibleRow();
485 unsigned int GetRowCount() ;
487 wxDataViewItem
GetSelection() const;
488 wxDataViewSelection
GetSelections(){ return m_selection
; }
489 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
490 void Select( const wxArrayInt
& aSelections
);
491 void SelectAllRows( bool on
);
492 void SelectRow( unsigned int row
, bool on
);
493 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
494 void ReverseRowSelection( unsigned int row
);
495 bool IsRowSelected( unsigned int row
);
496 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
498 void RefreshRow( unsigned int row
);
499 void RefreshRows( unsigned int from
, unsigned int to
);
500 void RefreshRowsAfter( unsigned int firstRow
);
502 // returns the colour to be used for drawing the rules
503 wxColour
GetRuleColour() const
505 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
508 wxRect
GetLineRect( unsigned int row
) const;
510 //Some useful functions for row and item mapping
511 wxDataViewItem
GetItemByRow( unsigned int row
) const;
512 int GetRowByItem( const wxDataViewItem
& item
);
514 //Methods for building the mapping tree
515 void BuildTree( wxDataViewModel
* model
);
517 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
518 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
520 void Expand( unsigned int row
) { OnExpanding( row
); }
521 void Collapse( unsigned int row
) { OnCollapsing( row
); }
523 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
524 //We did not need this temporarily
525 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
527 int RecalculateCount() ;
529 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
530 void OnExpanding( unsigned int row
);
531 void OnCollapsing( unsigned int row
);
533 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
536 wxDataViewCtrl
*m_owner
;
540 wxDataViewColumn
*m_currentCol
;
541 unsigned int m_currentRow
;
542 wxDataViewSelection m_selection
;
544 wxDataViewRenameTimer
*m_renameTimer
;
552 // for double click logic
553 unsigned int m_lineLastClicked
,
554 m_lineBeforeLastClicked
,
555 m_lineSelectSingleOnUp
;
557 // the pen used to draw horiz/vertical rules
560 // the pen used to draw the expander and the lines
563 //This is the tree structure of the model
564 wxDataViewTreeNode
* m_root
;
566 //This is the tree node under the cursor
567 wxDataViewTreeNode
* m_underMouse
;
569 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
570 DECLARE_EVENT_TABLE()
573 // ---------------------------------------------------------
574 // wxGenericDataViewModelNotifier
575 // ---------------------------------------------------------
577 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
580 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
581 { m_mainWindow
= mainWindow
; }
583 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
584 { return m_mainWindow
->ItemAdded( parent
, item
); }
585 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
586 { return m_mainWindow
->ItemDeleted( parent
, item
); }
587 virtual bool ItemChanged( const wxDataViewItem
& item
)
588 { return m_mainWindow
->ItemChanged(item
); }
589 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
590 { return m_mainWindow
->ValueChanged( item
, col
); }
591 virtual bool Cleared()
592 { return m_mainWindow
->Cleared(); }
593 virtual void Resort()
594 { m_mainWindow
->Resort(); }
596 wxDataViewMainWindow
*m_mainWindow
;
599 // ---------------------------------------------------------
600 // wxDataViewRenderer
601 // ---------------------------------------------------------
603 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
605 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
606 wxDataViewCellMode mode
,
608 wxDataViewRendererBase( varianttype
, mode
, align
)
617 wxDataViewRenderer::~wxDataViewRenderer()
623 wxDC
*wxDataViewRenderer::GetDC()
627 if (GetOwner() == NULL
)
629 if (GetOwner()->GetOwner() == NULL
)
631 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
637 // ---------------------------------------------------------
638 // wxDataViewCustomRenderer
639 // ---------------------------------------------------------
641 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
643 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
644 wxDataViewCellMode mode
, int align
) :
645 wxDataViewRenderer( varianttype
, mode
, align
)
649 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
651 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
652 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
653 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
654 view
->GetForegroundColour();
655 dc
->SetTextForeground(col
);
656 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
659 // ---------------------------------------------------------
660 // wxDataViewTextRenderer
661 // ---------------------------------------------------------
663 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
665 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
666 wxDataViewCellMode mode
, int align
) :
667 wxDataViewCustomRenderer( varianttype
, mode
, align
)
671 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
673 m_text
= value
.GetString();
678 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
683 bool wxDataViewTextRenderer::HasEditorCtrl()
688 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
689 wxRect labelRect
, const wxVariant
&value
)
691 return new wxTextCtrl( parent
, wxID_ANY
, value
,
692 wxPoint(labelRect
.x
,labelRect
.y
),
693 wxSize(labelRect
.width
,labelRect
.height
) );
696 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
698 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
699 value
= text
->GetValue();
703 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
705 RenderText( m_text
, 0, cell
, dc
, state
);
709 wxSize
wxDataViewTextRenderer::GetSize() const
711 const wxDataViewCtrl
*view
= GetView();
715 view
->GetTextExtent( m_text
, &x
, &y
);
716 return wxSize( x
, y
);
718 return wxSize(80,20);
721 // ---------------------------------------------------------
722 // wxDataViewTextRendererAttr
723 // ---------------------------------------------------------
725 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
727 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
728 wxDataViewCellMode mode
, int align
) :
729 wxDataViewTextRenderer( varianttype
, mode
, align
)
734 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
741 if (m_attr
.HasColour())
743 colour
= dc
->GetTextForeground();
744 dc
->SetTextForeground( m_attr
.GetColour() );
747 if (m_attr
.GetBold() || m_attr
.GetItalic())
749 font
= dc
->GetFont();
750 wxFont myfont
= font
;
751 if (m_attr
.GetBold())
752 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
753 if (m_attr
.GetItalic())
754 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
755 dc
->SetFont( myfont
);
759 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
764 if (m_attr
.HasColour())
765 dc
->SetTextForeground( colour
);
767 if (m_attr
.GetBold() || m_attr
.GetItalic())
775 // ---------------------------------------------------------
776 // wxDataViewBitmapRenderer
777 // ---------------------------------------------------------
779 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
781 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
782 wxDataViewCellMode mode
, int align
) :
783 wxDataViewCustomRenderer( varianttype
, mode
, align
)
787 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
789 if (value
.GetType() == wxT("wxBitmap"))
791 if (value
.GetType() == wxT("wxIcon"))
797 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
802 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
805 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
806 else if (m_icon
.Ok())
807 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
812 wxSize
wxDataViewBitmapRenderer::GetSize() const
815 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
816 else if (m_icon
.Ok())
817 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
819 return wxSize(16,16);
822 // ---------------------------------------------------------
823 // wxDataViewToggleRenderer
824 // ---------------------------------------------------------
826 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
828 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
829 wxDataViewCellMode mode
, int align
) :
830 wxDataViewCustomRenderer( varianttype
, mode
, align
)
835 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
837 m_toggle
= value
.GetBool();
842 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
847 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
849 // User wxRenderer here
852 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
854 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
859 flags
|= wxCONTROL_CHECKED
;
860 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
861 flags
|= wxCONTROL_DISABLED
;
863 wxRendererNative::Get().DrawCheckBox(
864 GetOwner()->GetOwner(),
872 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
873 wxDataViewModel
*model
,
874 const wxDataViewItem
& item
, unsigned int col
)
876 bool value
= !m_toggle
;
877 wxVariant variant
= value
;
878 model
->SetValue( variant
, item
, col
);
879 model
->ValueChanged( item
, col
);
883 wxSize
wxDataViewToggleRenderer::GetSize() const
885 return wxSize(20,20);
888 // ---------------------------------------------------------
889 // wxDataViewProgressRenderer
890 // ---------------------------------------------------------
892 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
894 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
895 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
896 wxDataViewCustomRenderer( varianttype
, mode
, align
)
902 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
906 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
908 m_value
= (long) value
;
910 if (m_value
< 0) m_value
= 0;
911 if (m_value
> 100) m_value
= 100;
916 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
918 value
= (long) m_value
;
922 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
924 double pct
= (double)m_value
/ 100.0;
926 bar
.width
= (int)(cell
.width
* pct
);
927 dc
->SetPen( *wxTRANSPARENT_PEN
);
928 dc
->SetBrush( *wxBLUE_BRUSH
);
929 dc
->DrawRectangle( bar
);
931 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
932 dc
->SetPen( *wxBLACK_PEN
);
933 dc
->DrawRectangle( cell
);
938 wxSize
wxDataViewProgressRenderer::GetSize() const
940 return wxSize(40,12);
943 // ---------------------------------------------------------
944 // wxDataViewDateRenderer
945 // ---------------------------------------------------------
947 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
949 #if wxUSE_DATE_RENDERER_POPUP
951 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
954 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
955 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
956 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
961 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
962 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
963 sizer
->Add( m_cal
, 1, wxGROW
);
968 void OnCalendar( wxCalendarEvent
&event
);
970 wxCalendarCtrl
*m_cal
;
971 wxDataViewModel
*m_model
;
973 const wxDataViewItem
& m_item
;
976 virtual void OnDismiss()
981 DECLARE_EVENT_TABLE()
984 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
985 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
988 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
990 wxDateTime date
= event
.GetDate();
991 wxVariant value
= date
;
992 m_model
->SetValue( value
, m_item
, m_col
);
993 m_model
->ValueChanged( m_item
, m_col
);
997 #endif // wxUSE_DATE_RENDERER_POPUP
999 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1001 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1002 wxDataViewCellMode mode
, int align
) :
1003 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1007 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1009 m_date
= value
.GetDateTime();
1014 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1020 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1022 wxString tmp
= m_date
.FormatDate();
1023 RenderText( tmp
, 0, cell
, dc
, state
);
1027 wxSize
wxDataViewDateRenderer::GetSize() const
1029 const wxDataViewCtrl
* view
= GetView();
1030 wxString tmp
= m_date
.FormatDate();
1032 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1033 return wxSize(x
,y
+d
);
1036 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1037 const wxDataViewItem
& item
, unsigned int col
)
1040 model
->GetValue( variant
, item
, col
);
1041 wxDateTime value
= variant
.GetDateTime();
1043 #if wxUSE_DATE_RENDERER_POPUP
1044 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1045 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1046 wxPoint pos
= wxGetMousePosition();
1049 popup
->Popup( popup
->m_cal
);
1050 #else // !wxUSE_DATE_RENDERER_POPUP
1051 wxMessageBox(value
.Format());
1052 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1056 // ---------------------------------------------------------
1057 // wxDataViewIconTextRenderer
1058 // ---------------------------------------------------------
1060 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1062 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1063 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1064 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1067 SetAlignment(align
);
1070 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1074 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1080 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1085 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1088 const wxIcon
&icon
= m_value
.GetIcon();
1091 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1092 xoffset
= icon
.GetWidth()+4;
1095 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1100 wxSize
wxDataViewIconTextRenderer::GetSize() const
1102 const wxDataViewCtrl
*view
= GetView();
1103 if (!m_value
.GetText().empty())
1106 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1108 if (m_value
.GetIcon().IsOk())
1109 x
+= m_value
.GetIcon().GetWidth() + 4;
1110 return wxSize( x
, y
);
1112 return wxSize(80,20);
1116 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1117 wxRect
WXUNUSED(labelRect
),
1118 const wxVariant
& WXUNUSED(value
))
1124 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1125 wxVariant
& WXUNUSED(value
))
1130 // ---------------------------------------------------------
1132 // ---------------------------------------------------------
1134 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1136 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1137 unsigned int model_column
,
1138 int width
, wxAlignment align
, int flags
) :
1139 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1141 SetAlignment(align
);
1145 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1148 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1149 unsigned int model_column
,
1150 int width
, wxAlignment align
, int flags
) :
1151 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1153 SetAlignment(align
);
1156 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1159 wxDataViewColumn::~wxDataViewColumn()
1163 void wxDataViewColumn::Init( int width
)
1166 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1170 void wxDataViewColumn::SetResizeable( bool resizeable
)
1173 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1175 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1178 void wxDataViewColumn::SetHidden( bool hidden
)
1181 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1183 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1185 // tell our owner to e.g. update its scrollbars:
1187 GetOwner()->OnColumnChange();
1190 void wxDataViewColumn::SetSortable( bool sortable
)
1193 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1195 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1197 // Update header button
1199 GetOwner()->OnColumnChange();
1202 void wxDataViewColumn::SetReorderable( bool reorderable
)
1207 void wxDataViewColumn::SetSortOrder( bool ascending
)
1209 m_ascending
= ascending
;
1211 // Update header button
1213 GetOwner()->OnColumnChange();
1216 bool wxDataViewColumn::IsSortOrderAscending() const
1221 void wxDataViewColumn::SetInternalWidth( int width
)
1225 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1226 if (m_owner
&& m_owner
->m_clientArea
)
1227 m_owner
->m_clientArea
->RecalculateDisplay();
1230 void wxDataViewColumn::SetWidth( int width
)
1232 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1234 SetInternalWidth(width
);
1238 //-----------------------------------------------------------------------------
1239 // wxDataViewHeaderWindowBase
1240 //-----------------------------------------------------------------------------
1242 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1244 wxWindow
*parent
= GetParent();
1245 wxDataViewEvent
le(type
, parent
->GetId());
1247 le
.SetEventObject(parent
);
1249 le
.SetDataViewColumn(GetColumn(n
));
1250 le
.SetModel(GetOwner()->GetModel());
1252 // for events created by wxDataViewHeaderWindow the
1253 // row / value fields are not valid
1255 parent
->GetEventHandler()->ProcessEvent(le
);
1258 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1260 #ifndef HDS_DRAGDROP
1261 #define HDS_DRAGDROP 0x0040
1263 #ifndef HDS_FULLDRAG
1264 #define HDS_FULLDRAG 0x0080
1267 // implemented in msw/listctrl.cpp:
1268 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1270 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1272 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1273 const wxPoint
&pos
, const wxSize
&size
,
1274 const wxString
&name
)
1278 m_scrollOffsetX
= 0;
1279 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this ) + 10;
1281 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1282 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1283 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1284 h
= size
.y
== wxDefaultCoord
? m_buttonHeight
: size
.y
;
1286 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1289 // create the native WC_HEADER window:
1290 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1291 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1294 msStyle
|= WS_VISIBLE
;
1296 m_hWnd
= CreateWindowEx(0,
1307 wxLogLastError(_T("CreateWindowEx"));
1311 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1312 // to call wxDataViewHeaderWindow::MSWOnNotify
1313 SubclassWin(m_hWnd
);
1315 // the following is required to get the default win's font for
1316 // header windows and must be done befor sending the HDM_LAYOUT msg
1322 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1327 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1329 return wxSize(80, m_buttonHeight
);
1332 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1334 // remove old columns
1335 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1336 Header_DeleteItem((HWND
)m_hWnd
, 0);
1338 // add the updated array of columns to the header control
1339 unsigned int cols
= GetOwner()->GetColumnCount();
1340 unsigned int added
= 0;
1341 for (unsigned int i
= 0; i
< cols
; i
++)
1343 wxDataViewColumn
*col
= GetColumn( i
);
1344 if (col
->IsHidden())
1345 continue; // don't add it!
1348 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1349 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1350 hdi
.cxy
= col
->GetWidth();
1351 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1352 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1353 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1355 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1357 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1358 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1359 // VZ: works with 5.81
1360 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1363 // lParam is reserved for application's use:
1364 // we store there the column index to use it later in MSWOnNotify
1365 // (since columns may have been hidden)
1366 hdi
.lParam
= (LPARAM
)i
;
1368 // the native wxMSW implementation of the header window
1369 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1370 // on the right: to correct this effect we make the column
1371 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1373 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1375 switch (col
->GetAlignment())
1378 hdi
.fmt
|= HDF_LEFT
;
1380 case wxALIGN_CENTER
:
1381 case wxALIGN_CENTER_HORIZONTAL
:
1382 hdi
.fmt
|= HDF_CENTER
;
1385 hdi
.fmt
|= HDF_RIGHT
;
1389 // such alignment is not allowed for the column header!
1393 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1394 (WPARAM
)added
, (LPARAM
)&hdi
);
1399 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1403 // NOTE: we don't just return nmHDR->iItem because when there are
1404 // hidden columns, nmHDR->iItem may be different from
1405 // nmHDR->pitem->lParam
1407 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1409 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1414 item
.mask
= HDI_LPARAM
;
1415 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1417 return (unsigned int)item
.lParam
;
1420 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1422 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1424 // is it a message from the header?
1425 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1426 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1428 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1429 switch ( nmhdr
->code
)
1431 case HDN_BEGINTRACK
:
1432 // user has started to resize a column:
1433 // do we need to veto it?
1434 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1442 // user has started to reorder a column
1445 case HDN_ITEMCHANGING
:
1446 if (nmHDR
->pitem
!= NULL
&&
1447 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1449 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1450 if (nmHDR
->pitem
->cxy
< minWidth
)
1452 // do not allow the user to resize this column under
1453 // its minimal width:
1459 case HDN_ITEMCHANGED
: // user is resizing a column
1460 case HDN_ENDTRACK
: // user has finished resizing a column
1461 case HDN_ENDDRAG
: // user has finished reordering a column
1463 // update the width of the modified column:
1464 if (nmHDR
->pitem
!= NULL
&&
1465 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1467 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1468 unsigned int w
= nmHDR
->pitem
->cxy
;
1469 wxDataViewColumn
*col
= GetColumn(idx
);
1471 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1472 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1473 w
-= COLUMN_WIDTH_OFFSET
;
1475 if (w
>= (unsigned)col
->GetMinWidth())
1476 col
->SetInternalWidth(w
);
1482 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1483 wxDataViewModel
* model
= GetOwner()->GetModel();
1485 if(nmHDR
->iButton
== 0)
1487 wxDataViewColumn
*col
= GetColumn(idx
);
1488 if(col
->IsSortable())
1490 if(model
&& m_owner
->GetSortingColumn() == col
)
1492 bool order
= col
->IsSortOrderAscending();
1493 col
->SetSortOrder(!order
);
1497 m_owner
->SetSortingColumn(col
);
1505 wxEventType evt
= nmHDR
->iButton
== 0 ?
1506 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1507 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1508 SendEvent(evt
, idx
);
1514 // NOTE: for some reason (i.e. for a bug in Windows)
1515 // the HDN_ITEMCLICK notification is not sent on
1516 // right clicks, so we need to handle NM_RCLICK
1519 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1520 if (column
!= wxNOT_FOUND
)
1523 item
.mask
= HDI_LPARAM
;
1524 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1526 // 'idx' may be different from 'column' if there are
1527 // hidden columns...
1528 unsigned int idx
= (unsigned int)item
.lParam
;
1529 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1535 case HDN_GETDISPINFOW
:
1536 // see wxListCtrl::MSWOnNotify for more info!
1539 case HDN_ITEMDBLCLICK
:
1541 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1542 int w
= GetOwner()->GetBestColumnWidth(idx
);
1544 // update the native control:
1546 ZeroMemory(&hd
, sizeof(hd
));
1547 hd
.mask
= HDI_WIDTH
;
1549 Header_SetItem(GetHwnd(),
1550 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1553 // update the wxDataViewColumn class:
1554 GetColumn(idx
)->SetInternalWidth(w
);
1559 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1565 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1566 const wxRect
* WXUNUSED(rect
))
1568 m_scrollOffsetX
+= dx
;
1570 GetParent()->Layout();
1573 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1577 // TODO: why is there a border + 2px around it?
1578 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1581 #else // !defined(__WXMSW__)
1583 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1584 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1585 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1586 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1587 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1590 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1591 const wxPoint
&pos
, const wxSize
&size
,
1592 const wxString
&name
)
1596 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1599 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1600 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1601 SetOwnForegroundColour( attr
.colFg
);
1602 SetOwnBackgroundColour( attr
.colBg
);
1604 SetOwnFont( attr
.font
);
1606 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1607 // a wxBoxSizer and in order to avoid super-big header windows,
1608 // we need to set our height as fixed
1609 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1610 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1615 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1618 GetClientSize( &w
, &h
);
1620 wxAutoBufferedPaintDC
dc( this );
1622 dc
.SetBackground(GetBackgroundColour());
1626 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1629 m_owner
->GetViewStart( &x
, NULL
);
1631 // account for the horz scrollbar offset
1632 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1634 dc
.SetFont( GetFont() );
1636 unsigned int cols
= GetOwner()->GetColumnCount();
1639 for (i
= 0; i
< cols
; i
++)
1641 wxDataViewColumn
*col
= GetColumn( i
);
1642 if (col
->IsHidden())
1643 continue; // skip it!
1645 int cw
= col
->GetWidth();
1648 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1649 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1651 if (col
->IsSortOrderAscending())
1652 sortArrow
= wxHDR_SORT_ICON_UP
;
1654 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1658 if (m_parent
->IsEnabled())
1660 if ((int) i
== m_hover
)
1661 state
= wxCONTROL_CURRENT
;
1665 state
= (int) wxCONTROL_DISABLED
;
1668 wxRendererNative::Get().DrawHeaderButton
1672 wxRect(xpos
, 0, cw
, ch
-1),
1677 // align as required the column title:
1679 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1680 switch (col
->GetAlignment())
1683 x
+= HEADER_HORIZ_BORDER
;
1686 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1689 case wxALIGN_CENTER
:
1690 case wxALIGN_CENTER_HORIZONTAL
:
1691 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1695 // always center the title vertically:
1696 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1698 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1700 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1701 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1702 dc
.DrawText( col
->GetTitle(), x
, y
);
1703 dc
.DestroyClippingRegion();
1709 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1711 GetParent()->SetFocus();
1715 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1717 // we want to work with logical coords
1719 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1720 int y
= event
.GetY();
1724 // we don't draw the line beyond our window,
1725 // but we allow dragging it there
1727 GetClientSize( &w
, NULL
);
1728 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1731 if (event
.ButtonUp())
1733 m_isDragging
= false;
1739 m_currentX
= wxMax(m_minX
+ 7, x
);
1743 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1745 GetOwner()->Refresh();
1749 else // not dragging
1752 m_column
= wxNOT_FOUND
;
1754 bool hit_border
= false;
1756 // end of the current column
1759 // find the column where this event occured
1760 int countCol
= m_owner
->GetColumnCount();
1761 for (int column
= 0; column
< countCol
; column
++)
1763 wxDataViewColumn
*p
= GetColumn(column
);
1766 continue; // skip if not shown
1768 xpos
+= p
->GetWidth();
1770 if ((abs(x
-xpos
) < 3) && (y
< 22))
1778 // inside the column
1785 int old_hover
= m_hover
;
1787 if (event
.Leaving())
1788 m_hover
= wxNOT_FOUND
;
1789 if (old_hover
!= m_hover
)
1792 if (m_column
== wxNOT_FOUND
)
1795 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1796 if (event
.LeftDClick() && resizeable
)
1798 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1801 else if (event
.LeftDown() || event
.RightUp())
1803 if (hit_border
&& event
.LeftDown() && resizeable
)
1805 m_isDragging
= true;
1809 else // click on a column
1811 wxDataViewModel
* model
= GetOwner()->GetModel();
1812 wxEventType evt
= event
.LeftDown() ?
1813 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1814 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1815 SendEvent(evt
, m_column
);
1817 //Left click the header
1818 if(event
.LeftDown())
1820 wxDataViewColumn
*col
= GetColumn(m_column
);
1821 if(col
->IsSortable())
1823 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1824 if(model
&& sortCol
== col
)
1826 bool order
= col
->IsSortOrderAscending();
1827 col
->SetSortOrder(!order
);
1831 m_owner
->SetSortingColumn(col
);
1837 //Send the column sorted event
1838 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1842 else if (event
.Moving())
1844 if (hit_border
&& resizeable
)
1845 m_currentCursor
= m_resizeCursor
;
1847 m_currentCursor
= wxSTANDARD_CURSOR
;
1849 SetCursor(*m_currentCursor
);
1854 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1858 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1859 m_owner
->GetViewStart( &x
, NULL
);
1861 // shift the DC origin to match the position of the main window horizontal
1862 // scrollbar: this allows us to always use logical coords
1863 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1866 #endif // defined(__WXMSW__)
1868 //-----------------------------------------------------------------------------
1869 // wxDataViewRenameTimer
1870 //-----------------------------------------------------------------------------
1872 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1877 void wxDataViewRenameTimer::Notify()
1879 m_owner
->OnRenameTimer();
1882 //-----------------------------------------------------------------------------
1883 // wxDataViewMainWindow
1884 //-----------------------------------------------------------------------------
1886 //The tree building helper, declared firstly
1887 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1889 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1891 if (row1
> row2
) return 1;
1892 if (row1
== row2
) return 0;
1897 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1899 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1900 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1901 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1902 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1903 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1904 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1907 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1908 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1909 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1910 m_selection( wxDataViewSelectionCmp
)
1915 m_lastOnSame
= false;
1916 m_renameTimer
= new wxDataViewRenameTimer( this );
1918 // TODO: user better initial values/nothing selected
1919 m_currentCol
= NULL
;
1922 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1925 m_dragStart
= wxPoint(0,0);
1926 m_lineLastClicked
= (unsigned int) -1;
1927 m_lineBeforeLastClicked
= (unsigned int) -1;
1928 m_lineSelectSingleOnUp
= (unsigned int) -1;
1932 SetBackgroundColour( *wxWHITE
);
1934 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1936 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1937 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1938 //Some new added code to deal with the tree structure
1939 m_root
= new wxDataViewTreeNode( NULL
);
1940 m_root
->SetHasChildren(true);
1942 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1944 m_underMouse
= NULL
;
1948 wxDataViewMainWindow::~wxDataViewMainWindow()
1951 delete m_renameTimer
;
1954 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1956 wxDataViewModel
*model
= GetOwner()->GetModel();
1957 wxAutoBufferedPaintDC
dc( this );
1960 dc
.SetPen( *wxTRANSPARENT_PEN
);
1961 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1962 dc
.SetBrush( *wxWHITE_BRUSH
);
1963 wxSize
size( GetClientSize() );
1964 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1968 GetOwner()->PrepareDC( dc
);
1969 dc
.SetFont( GetFont() );
1971 wxRect update
= GetUpdateRegion().GetBox();
1972 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1974 // compute which items needs to be redrawn
1975 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1976 unsigned int item_count
=
1977 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1978 (int)(GetRowCount( ) - item_start
));
1979 unsigned int item_last
= item_start
+ item_count
;
1981 // compute which columns needs to be redrawn
1982 unsigned int cols
= GetOwner()->GetColumnCount();
1983 unsigned int col_start
= 0;
1984 unsigned int x_start
= 0;
1985 for (x_start
= 0; col_start
< cols
; col_start
++)
1987 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
1988 if (col
->IsHidden())
1989 continue; // skip it!
1991 unsigned int w
= col
->GetWidth();
1992 if (x_start
+w
>= (unsigned int)update
.x
)
1998 unsigned int col_last
= col_start
;
1999 unsigned int x_last
= x_start
;
2000 for (; col_last
< cols
; col_last
++)
2002 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2003 if (col
->IsHidden())
2004 continue; // skip it!
2006 if (x_last
> (unsigned int)update
.GetRight())
2009 x_last
+= col
->GetWidth();
2012 // Draw horizontal rules if required
2013 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2015 dc
.SetPen(m_penRule
);
2016 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2018 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2020 int y
= i
* m_lineHeight
;
2021 dc
.DrawLine(x_start
, y
, x_last
, y
);
2025 // Draw vertical rules if required
2026 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2028 dc
.SetPen(m_penRule
);
2029 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2032 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2034 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2035 if (col
->IsHidden())
2036 continue; // skip it
2038 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2039 x
, item_last
* m_lineHeight
);
2041 x
+= col
->GetWidth();
2044 // Draw last vertical rule
2045 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2046 x
, item_last
* m_lineHeight
);
2049 // redraw the background for the items which are selected/current
2050 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2052 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2053 if (selected
|| item
== m_currentRow
)
2055 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2056 if (item
== m_currentRow
)
2057 flags
|= wxCONTROL_CURRENT
;
2059 flags
|= wxCONTROL_FOCUSED
;
2061 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2062 wxRendererNative::Get().DrawItemSelectionRect
2072 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2075 // TODO: last column for RTL support
2076 expander
= GetOwner()->GetColumn( 0 );
2077 GetOwner()->SetExpanderColumn(expander
);
2080 // redraw all cells for all rows which must be repainted and for all columns
2082 cell_rect
.x
= x_start
;
2083 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2084 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2086 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2087 wxDataViewRenderer
*cell
= col
->GetRenderer();
2088 cell_rect
.width
= col
->GetWidth();
2090 if (col
->IsHidden())
2091 continue; // skipt it!
2094 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2096 // get the cell value and set it into the renderer
2098 wxDataViewTreeNode
*node
= NULL
;
2099 wxDataViewItem dataitem
;
2103 node
= GetTreeNodeByRow(item
);
2107 dataitem
= node
->GetItem();
2109 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2114 dataitem
= wxDataViewItem( (void*) item
);
2117 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2118 cell
->SetValue( value
);
2120 if (cell
->GetWantsAttr())
2122 wxDataViewItemAttr attr
;
2123 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2125 cell
->SetAttr( attr
);
2126 cell
->SetHasAttr( ret
);
2129 // update the y offset
2130 cell_rect
.y
= item
* m_lineHeight
;
2132 //Draw the expander here.
2134 if ((m_root
) && (col
== expander
))
2136 indent
= node
->GetIndentLevel();
2138 //Calculate the indent first
2139 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2141 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2142 // change the cell_rect.x to the appropriate pos
2143 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2144 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2145 dc
.SetPen( m_penExpander
);
2146 dc
.SetBrush( wxNullBrush
);
2147 if( node
->HasChildren() )
2149 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2151 if (m_underMouse
== node
)
2153 flag
|= wxCONTROL_CURRENT
;
2155 if( node
->IsOpen() )
2156 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2158 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2162 // I am wondering whether we should draw dot lines between tree nodes
2165 // Yes, if the node does not have any child, it must be a leaf which
2166 // mean that it is a temporarily created by GetTreeNodeByRow
2169 //force the expander column to left-center align
2170 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2174 // cannot be bigger than allocated space
2175 wxSize size
= cell
->GetSize();
2176 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2177 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2178 // size.y = wxMin( size.y, cell_rect.height );
2179 size
.y
= cell_rect
.height
;
2181 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2182 int align
= cell
->GetAlignment();
2184 // horizontal alignment:
2185 item_rect
.x
= cell_rect
.x
;
2186 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2187 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2188 else if (align
& wxALIGN_RIGHT
)
2189 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2190 //else: wxALIGN_LEFT is the default
2192 // vertical alignment:
2193 item_rect
.y
= cell_rect
.y
;
2194 if (align
& wxALIGN_CENTER_VERTICAL
)
2195 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2196 else if (align
& wxALIGN_BOTTOM
)
2197 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2198 //else: wxALIGN_TOP is the default
2201 item_rect
.x
+= PADDING_RIGHTLEFT
;
2202 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2204 //Here we add the tree indent
2205 item_rect
.x
+= indent
;
2208 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2209 state
|= wxDATAVIEW_CELL_SELECTED
;
2211 // TODO: it would be much more efficient to create a clipping
2212 // region for the entire column being rendered (in the OnPaint
2213 // of wxDataViewMainWindow) instead of a single clip region for
2214 // each cell. However it would mean that each renderer should
2215 // respect the given wxRect's top & bottom coords, eventually
2216 // violating only the left & right coords - however the user can
2217 // make its own renderer and thus we cannot be sure of that.
2218 dc
.SetClippingRegion( item_rect
);
2219 cell
->Render( item_rect
, &dc
, state
);
2220 dc
.DestroyClippingRegion();
2223 cell_rect
.x
+= cell_rect
.width
;
2227 void wxDataViewMainWindow::OnRenameTimer()
2229 // We have to call this here because changes may just have
2230 // been made and no screen update taken place.
2235 unsigned int cols
= GetOwner()->GetColumnCount();
2237 for (i
= 0; i
< cols
; i
++)
2239 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2241 continue; // skip it!
2243 if (c
== m_currentCol
)
2245 xpos
+= c
->GetWidth();
2247 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
2248 m_currentCol
->GetWidth(), m_lineHeight
);
2250 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
2251 &labelRect
.x
, &labelRect
.y
);
2253 wxDataViewItem item
= GetItemByRow( m_currentRow
);
2254 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
2258 //------------------------------------------------------------------
2259 // Helper class for do operation on the tree node
2260 //------------------------------------------------------------------
2267 //The return value control how the tree-walker tranverse the tree
2268 // 0: Job done, stop tranverse and return
2269 // 1: Ignore the current node's subtree and continue
2270 // 2: Job not done, continue
2271 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
2272 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
2273 virtual int operator() ( void * n
) = 0;
2276 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2281 switch( func( node
) )
2292 wxDataViewTreeNodes nodes
= node
->GetNodes();
2293 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2295 int len_nodes
= nodes
.GetCount();
2296 int len
= leaves
.GetCount();
2297 int i
= 0, nodes_i
= 0;
2299 for( ; i
< len
; i
++ )
2301 void * n
= leaves
[i
];
2302 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2304 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2307 if( Walker( nd
, func
) )
2326 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2337 wxDataViewTreeNode
* node
;
2338 node
= FindNode(parent
);
2343 node
->SetHasChildren( true );
2345 if( g_model
->IsContainer( item
) )
2347 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2348 newnode
->SetItem(item
);
2349 newnode
->SetHasChildren( true );
2350 node
->AddNode( newnode
);
2353 node
->AddLeaf( item
.GetID() );
2355 node
->ChangeSubTreeCount(1);
2363 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2365 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2366 const wxDataViewItem
& item
)
2371 if( m_currentRow
> GetRowCount() )
2372 m_currentRow
= m_count
- 1;
2374 m_selection
.Empty();
2381 wxDataViewTreeNode
* node
= FindNode(parent
);
2383 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2384 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2387 node
->GetChildren().Remove( item
.GetID() );
2388 //Manuplate selection
2389 if( m_selection
.GetCount() > 1 )
2391 m_selection
.Empty();
2393 bool isContainer
= false;
2394 wxDataViewTreeNodes nds
= node
->GetNodes();
2395 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2397 if (nds
[i
]->GetItem() == item
)
2405 wxDataViewTreeNode
* n
= NULL
;
2406 wxDataViewTreeNodes nodes
= node
->GetNodes();
2407 int len
= nodes
.GetCount();
2408 for( int i
= 0 ; i
< len
; i
++)
2410 if( nodes
[i
]->GetItem() == item
)
2417 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2419 node
->GetNodes().Remove( n
);
2420 sub
-= n
->GetSubTreeCount();
2421 DestroyTreeHelper(n
);
2423 //Make the row number invalid and get a new valid one when user call GetRowCount
2425 node
->ChangeSubTreeCount(sub
);
2426 if( node
->GetChildrenNumber() == 0)
2428 node
->GetParent()->GetNodes().Remove( node
);
2432 //Change the current row to the last row if the current exceed the max row number
2433 if( m_currentRow
> GetRowCount() )
2434 m_currentRow
= m_count
- 1;
2441 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2447 wxWindow
*parent
= GetParent();
2448 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2449 le
.SetEventObject(parent
);
2450 le
.SetModel(GetOwner()->GetModel());
2452 parent
->GetEventHandler()->ProcessEvent(le
);
2457 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2459 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2460 /*#define MAX_VIRTUAL_WIDTH 100000
2462 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2463 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2464 Refresh( true, &rect );
2472 wxWindow
*parent
= GetParent();
2473 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2474 le
.SetEventObject(parent
);
2475 le
.SetModel(GetOwner()->GetModel());
2478 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2479 parent
->GetEventHandler()->ProcessEvent(le
);
2484 bool wxDataViewMainWindow::Cleared()
2489 BuildTree( GetOwner()->GetModel() );
2496 void wxDataViewMainWindow::UpdateDisplay()
2501 void wxDataViewMainWindow::OnInternalIdle()
2503 wxWindow::OnInternalIdle();
2507 RecalculateDisplay();
2512 void wxDataViewMainWindow::RecalculateDisplay()
2514 wxDataViewModel
*model
= GetOwner()->GetModel();
2521 int width
= GetEndOfLastCol();
2522 int height
= GetRowCount() * m_lineHeight
;
2524 SetVirtualSize( width
, height
);
2525 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2530 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2532 wxWindow::ScrollWindow( dx
, dy
, rect
);
2534 if (GetOwner()->m_headerArea
)
2535 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2538 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2541 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2542 int sy
= rows
*m_lineHeight
/y
;
2546 wxRect rect
= GetClientRect();
2548 int x_start
= 0, x_end
= 0, w
= 0;
2550 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2551 for (x_start
= 0; colnum
< column
; colnum
++)
2553 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2554 if (col
->IsHidden())
2555 continue; // skip it!
2557 w
= col
->GetWidth();
2561 x_end
= x_start
+ w
;
2562 xe
= xx
+ rect
.width
;
2565 sx
= ( xx
+ x_end
- xe
)/x
;
2572 m_owner
->Scroll( sx
, sy
);
2575 int wxDataViewMainWindow::GetCountPerPage() const
2577 wxSize size
= GetClientSize();
2578 return size
.y
/ m_lineHeight
;
2581 int wxDataViewMainWindow::GetEndOfLastCol() const
2585 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2587 const wxDataViewColumn
*c
=
2588 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2591 width
+= c
->GetWidth();
2596 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2600 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2602 return y
/ m_lineHeight
;
2605 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2607 wxSize client_size
= GetClientSize();
2608 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2609 &client_size
.x
, &client_size
.y
);
2611 //we should deal with the pixel here
2612 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2614 return wxMin( GetRowCount()-1, row
);
2617 unsigned int wxDataViewMainWindow::GetRowCount()
2619 if ( m_count
== -1 )
2621 m_count
= RecalculateCount();
2623 GetVirtualSize( &width
, &height
);
2624 height
= m_count
* m_lineHeight
;
2626 SetVirtualSize( width
, height
);
2631 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2638 void wxDataViewMainWindow::SelectAllRows( bool on
)
2645 m_selection
.Clear();
2646 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2647 m_selection
.Add( i
);
2652 unsigned int first_visible
= GetFirstVisibleRow();
2653 unsigned int last_visible
= GetLastVisibleRow();
2655 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2657 unsigned int row
= m_selection
[i
];
2658 if ((row
>= first_visible
) && (row
<= last_visible
))
2661 m_selection
.Clear();
2665 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2667 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2671 m_selection
.Add( row
);
2679 m_selection
.Remove( row
);
2685 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2689 unsigned int tmp
= from
;
2695 for (i
= from
; i
<= to
; i
++)
2697 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2700 m_selection
.Add( i
);
2705 m_selection
.Remove( i
);
2708 RefreshRows( from
, to
);
2711 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2713 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2715 int n
= aSelections
[i
];
2717 m_selection
.Add( n
);
2722 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2724 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2725 m_selection
.Add( row
);
2727 m_selection
.Remove( row
);
2731 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2733 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2736 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2738 wxWindow
*parent
= GetParent();
2739 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2741 le
.SetEventObject(parent
);
2742 le
.SetModel(GetOwner()->GetModel());
2745 parent
->GetEventHandler()->ProcessEvent(le
);
2748 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2750 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2751 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2753 wxSize client_size
= GetClientSize();
2754 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2755 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2756 if (intersect_rect
.width
> 0)
2757 Refresh( true, &intersect_rect
);
2760 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2764 unsigned int tmp
= to
;
2769 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2770 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2772 wxSize client_size
= GetClientSize();
2773 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2774 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2775 if (intersect_rect
.width
> 0)
2776 Refresh( true, &intersect_rect
);
2779 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2781 unsigned int count
= GetRowCount();
2782 if (firstRow
> count
)
2785 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2786 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2788 wxSize client_size
= GetClientSize();
2789 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2790 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2791 if (intersect_rect
.width
> 0)
2792 Refresh( true, &intersect_rect
);
2795 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2797 wxCHECK_RET( newCurrent
< GetRowCount(),
2798 _T("invalid item index in OnArrowChar()") );
2800 // if there is no selection, we cannot move it anywhere
2801 if (!HasCurrentRow())
2804 unsigned int oldCurrent
= m_currentRow
;
2806 // in single selection we just ignore Shift as we can't select several
2808 if ( event
.ShiftDown() && !IsSingleSel() )
2810 RefreshRow( oldCurrent
);
2812 ChangeCurrentRow( newCurrent
);
2814 // select all the items between the old and the new one
2815 if ( oldCurrent
> newCurrent
)
2817 newCurrent
= oldCurrent
;
2818 oldCurrent
= m_currentRow
;
2821 SelectRows( oldCurrent
, newCurrent
, true );
2822 if (oldCurrent
!=newCurrent
)
2823 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2827 RefreshRow( oldCurrent
);
2829 // all previously selected items are unselected unless ctrl is held
2830 if ( !event
.ControlDown() )
2831 SelectAllRows(false);
2833 ChangeCurrentRow( newCurrent
);
2835 if ( !event
.ControlDown() )
2837 SelectRow( m_currentRow
, true );
2838 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2841 RefreshRow( m_currentRow
);
2844 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2847 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2851 rect
.y
= m_lineHeight
* row
;
2852 rect
.width
= GetEndOfLastCol();
2853 rect
.height
= m_lineHeight
;
2858 class RowToItemJob
: public DoJob
2861 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2862 virtual ~RowToItemJob(){};
2864 virtual int operator() ( wxDataViewTreeNode
* node
)
2867 if( current
== static_cast<int>(row
))
2869 ret
= node
->GetItem() ;
2873 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2875 current
+= node
->GetSubTreeCount();
2880 //If the current has no child node, we can find the desired item of the row number directly.
2881 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2882 if( node
->GetNodes().GetCount() == 0)
2884 int index
= static_cast<int>(row
) - current
- 1;
2885 ret
= node
->GetChildren().Item( index
);
2892 virtual int operator() ( void * n
)
2895 if( current
== static_cast<int>(row
))
2897 ret
= wxDataViewItem( n
) ;
2902 wxDataViewItem
GetResult(){ return ret
; }
2909 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2913 return wxDataViewItem( (void*) row
);
2917 RowToItemJob
job( row
, -2 );
2918 Walker( m_root
, job
);
2919 return job
.GetResult();
2923 class RowToTreeNodeJob
: public DoJob
2926 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2929 this->current
= current
;
2933 virtual ~RowToTreeNodeJob(){};
2935 virtual int operator() ( wxDataViewTreeNode
* node
)
2938 if( current
== static_cast<int>(row
))
2944 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2946 current
+= node
->GetSubTreeCount();
2952 //If the current has no child node, we can find the desired item of the row number directly.
2953 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2954 if( node
->GetNodes().GetCount() == 0)
2956 int index
= static_cast<int>(row
) - current
- 1;
2957 void * n
= node
->GetChildren().Item( index
);
2958 ret
= new wxDataViewTreeNode( parent
) ;
2959 ret
->SetItem( wxDataViewItem( n
));
2960 ret
->SetHasChildren(false);
2969 virtual int operator() ( void * n
)
2972 if( current
== static_cast<int>(row
))
2974 ret
= new wxDataViewTreeNode( parent
) ;
2975 ret
->SetItem( wxDataViewItem( n
));
2976 ret
->SetHasChildren(false);
2982 wxDataViewTreeNode
* GetResult(){ return ret
; }
2986 wxDataViewTreeNode
* ret
;
2987 wxDataViewTreeNode
* parent
;
2991 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2999 RowToTreeNodeJob
job( row
, -2, m_root
);
3000 Walker( m_root
, job
);
3001 return job
.GetResult();
3005 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
3007 wxWindow
*parent
= GetParent();
3008 wxDataViewEvent
le(type
, parent
->GetId());
3010 le
.SetEventObject(parent
);
3011 le
.SetModel(GetOwner()->GetModel());
3014 parent
->GetEventHandler()->ProcessEvent(le
);
3018 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3020 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3023 if( node
->HasChildren())
3025 if( !node
->IsOpen())
3027 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3028 //Check if the user prevent expanding
3029 if( e
.GetSkipped() )
3033 //Here I build the children of current node
3034 if( node
->GetChildrenNumber() == 0 )
3037 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3041 //Send the expanded event
3042 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3046 SelectRow( row
, false );
3047 SelectRow( row
+ 1, true );
3048 ChangeCurrentRow( row
+ 1 );
3049 SendSelectionChangedEvent( GetItemByRow(row
+1));
3057 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3059 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3062 wxDataViewTreeNode
* nd
= node
;
3064 if( node
->HasChildren() && node
->IsOpen() )
3066 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3067 if( e
.GetSkipped() )
3072 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3076 node
= node
->GetParent();
3079 int parent
= GetRowByItem( node
->GetItem() ) ;
3082 SelectRow( row
, false);
3083 SelectRow(parent
, true );
3084 ChangeCurrentRow( parent
);
3085 SendSelectionChangedEvent( node
->GetItem() );
3089 if( !nd
->HasChildren())
3094 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3096 wxDataViewModel
* model
= GetOwner()->GetModel();
3100 //Compose the a parent-chain of the finding item
3102 list
.DeleteContents( true );
3103 wxDataViewItem
it( item
);
3106 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3107 list
.Insert( pItem
);
3108 it
= model
->GetParent( it
);
3111 //Find the item along the parent-chain.
3112 //This algorithm is designed to speed up the node-finding method
3113 wxDataViewTreeNode
* node
= m_root
;
3114 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3116 if( node
->HasChildren() )
3118 if( node
->GetChildrenNumber() == 0 )
3121 BuildTreeHelper(model
, node
->GetItem(), node
);
3124 wxDataViewTreeNodes nodes
= node
->GetNodes();
3126 for (; i
< nodes
.GetCount(); i
++)
3128 if (nodes
[i
]->GetItem() == (**iter
))
3134 if (i
== nodes
.GetCount())
3143 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3145 wxDataViewColumn
*col
= NULL
;
3146 unsigned int cols
= GetOwner()->GetColumnCount();
3147 unsigned int colnum
= 0;
3148 unsigned int x_start
= 0;
3150 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3151 for (x_start
= 0; colnum
< cols
; colnum
++)
3153 col
= GetOwner()->GetColumn(colnum
);
3154 if (col
->IsHidden())
3155 continue; // skip it!
3157 unsigned int w
= col
->GetWidth();
3158 if (x_start
+w
>= (unsigned int)x
)
3165 item
= GetItemByRow( y
/m_lineHeight
);
3168 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3170 int row
= GetRowByItem(item
);
3171 int y
= row
*m_lineHeight
;
3172 int h
= m_lineHeight
;
3174 wxDataViewColumn
*col
= NULL
;
3175 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3177 col
= GetOwner()->GetColumn( i
);
3178 x
+= col
->GetWidth();
3179 if( GetOwner()->GetColumn(i
+1) == column
)
3182 int w
= col
->GetWidth();
3183 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3184 return wxRect(x
, y
, w
, h
);
3187 int wxDataViewMainWindow::RecalculateCount()
3191 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3193 return list_model
->GetLastIndex() + 1;
3195 return list_model
->GetLastIndex() - 1;
3200 return m_root
->GetSubTreeCount();
3204 class ItemToRowJob
: public DoJob
3207 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3208 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3209 virtual ~ItemToRowJob(){};
3211 //Maybe binary search will help to speed up this process
3212 virtual int operator() ( wxDataViewTreeNode
* node
)
3215 if( node
->GetItem() == item
)
3220 if( node
->GetItem() == **m_iter
)
3227 ret
+= node
->GetSubTreeCount();
3233 virtual int operator() ( void * n
)
3236 if( n
== item
.GetID() )
3240 //the row number is begin from zero
3241 int GetResult(){ return ret
-1 ; }
3243 ItemList::const_iterator m_iter
;
3244 wxDataViewItem item
;
3249 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3251 wxDataViewModel
* model
= GetOwner()->GetModel();
3257 return wxPtrToUInt( item
.GetID() );
3264 //Compose the a parent-chain of the finding item
3266 wxDataViewItem
* pItem
= NULL
;
3267 list
.DeleteContents( true );
3268 wxDataViewItem
it( item
);
3271 pItem
= new wxDataViewItem( it
);
3272 list
.Insert( pItem
);
3273 it
= model
->GetParent( it
);
3275 pItem
= new wxDataViewItem( );
3276 list
.Insert( pItem
);
3278 ItemToRowJob
job( item
, list
.begin() );
3279 Walker(m_root
, job
);
3280 return job
.GetResult();
3284 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3286 if( !model
->IsContainer( item
) )
3289 wxDataViewItemArray children
;
3290 unsigned int num
= model
->GetChildren( item
, children
);
3291 unsigned int index
= 0;
3292 while( index
< num
)
3294 if( model
->IsContainer( children
[index
] ) )
3296 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3297 n
->SetItem(children
[index
]);
3298 n
->SetHasChildren( true ) ;
3303 node
->AddLeaf( children
[index
].GetID() );
3307 node
->SetSubTreeCount( num
);
3308 wxDataViewTreeNode
* n
= node
->GetParent();
3310 n
->ChangeSubTreeCount(num
);
3314 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3318 if (GetOwner()->GetModel()->IsIndexListModel())
3324 m_root
= new wxDataViewTreeNode( NULL
);
3325 m_root
->SetHasChildren(true);
3327 //First we define a invalid item to fetch the top-level elements
3328 wxDataViewItem item
;
3330 BuildTreeHelper( model
, item
, m_root
);
3334 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3336 if( node
->GetNodeNumber() != 0 )
3338 int len
= node
->GetNodeNumber();
3340 wxDataViewTreeNodes nodes
= node
->GetNodes();
3341 for( ; i
< len
; i
++ )
3343 DestroyTreeHelper(nodes
[i
]);
3349 void wxDataViewMainWindow::DestroyTree()
3353 DestroyTreeHelper(m_root
);
3359 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3361 if (event
.GetKeyCode() == WXK_TAB
)
3363 wxNavigationKeyEvent nevent
;
3364 nevent
.SetWindowChange( event
.ControlDown() );
3365 nevent
.SetDirection( !event
.ShiftDown() );
3366 nevent
.SetEventObject( GetParent()->GetParent() );
3367 nevent
.SetCurrentFocus( m_parent
);
3368 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3372 // no item -> nothing to do
3373 if (!HasCurrentRow())
3379 // don't use m_linesPerPage directly as it might not be computed yet
3380 const int pageSize
= GetCountPerPage();
3381 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3383 switch ( event
.GetKeyCode() )
3387 if (m_currentRow
> 0)
3389 wxWindow
*parent
= GetParent();
3390 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3391 le
.SetItem( GetItemByRow(m_currentRow
) );
3392 le
.SetEventObject(parent
);
3393 le
.SetModel(GetOwner()->GetModel());
3395 parent
->GetEventHandler()->ProcessEvent(le
);
3400 if ( m_currentRow
> 0 )
3401 OnArrowChar( m_currentRow
- 1, event
);
3405 if ( m_currentRow
< GetRowCount() - 1 )
3406 OnArrowChar( m_currentRow
+ 1, event
);
3408 //Add the process for tree expanding/collapsing
3410 OnCollapsing(m_currentRow
);
3413 OnExpanding( m_currentRow
);
3417 OnArrowChar( GetRowCount() - 1, event
);
3422 OnArrowChar( 0, event
);
3427 int steps
= pageSize
- 1;
3428 int index
= m_currentRow
- steps
;
3432 OnArrowChar( index
, event
);
3438 int steps
= pageSize
- 1;
3439 unsigned int index
= m_currentRow
+ steps
;
3440 unsigned int count
= GetRowCount();
3441 if ( index
>= count
)
3444 OnArrowChar( index
, event
);
3453 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3455 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3457 // let the base handle mouse wheel events.
3462 int x
= event
.GetX();
3463 int y
= event
.GetY();
3464 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3465 wxDataViewColumn
*col
= NULL
;
3468 unsigned int cols
= GetOwner()->GetColumnCount();
3470 for (i
= 0; i
< cols
; i
++)
3472 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3474 continue; // skip it!
3476 if (x
< xpos
+ c
->GetWidth())
3481 xpos
+= c
->GetWidth();
3486 wxDataViewRenderer
*cell
= col
->GetRenderer();
3487 unsigned int current
= y
/ m_lineHeight
;
3488 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3490 // Unselect all if below the last row ?
3494 //Test whether the mouse is hovered on the tree item button
3496 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3498 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3499 if( node
!=NULL
&& node
->HasChildren() )
3501 int indent
= node
->GetIndentLevel();
3502 indent
= GetOwner()->GetIndent()*indent
;
3503 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3504 if( rect
.Contains( x
, y
) )
3506 //So the mouse is over the expander
3508 if (m_underMouse
&& m_underMouse
!= node
)
3510 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3511 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3513 if (m_underMouse
!= node
)
3515 //wxLogMessage("Do the row: %d", current);
3516 RefreshRow(current
);
3518 m_underMouse
= node
;
3521 if (node
!=NULL
&& !node
->HasChildren())
3526 if (m_underMouse
!= NULL
)
3528 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3529 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3530 m_underMouse
= NULL
;
3534 wxDataViewModel
*model
= GetOwner()->GetModel();
3536 if (event
.Dragging())
3538 if (m_dragCount
== 0)
3540 // we have to report the raw, physical coords as we want to be
3541 // able to call HitTest(event.m_pointDrag) from the user code to
3542 // get the item being dragged
3543 m_dragStart
= event
.GetPosition();
3548 if (m_dragCount
!= 3)
3551 if (event
.LeftIsDown())
3553 // Notify cell about drag
3562 bool forceClick
= false;
3564 if (event
.ButtonDClick())
3566 m_renameTimer
->Stop();
3567 m_lastOnSame
= false;
3570 wxDataViewItem item
= GetItemByRow(current
);
3571 bool ignore_other_columns
=
3572 ((GetOwner()->GetExpanderColumn() != col
) &&
3573 (model
->IsContainer(item
)) &&
3574 (!model
->HasContainerColumns(item
)));
3576 if (event
.LeftDClick())
3578 if ( current
== m_lineLastClicked
)
3580 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3583 model
->GetValue( value
, item
, col
->GetModelColumn() );
3584 cell
->SetValue( value
);
3585 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3586 col
->GetWidth(), m_lineHeight
);
3587 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3592 wxWindow
*parent
= GetParent();
3593 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3595 le
.SetEventObject(parent
);
3596 le
.SetModel(GetOwner()->GetModel());
3598 parent
->GetEventHandler()->ProcessEvent(le
);
3604 // The first click was on another item, so don't interpret this as
3605 // a double click, but as a simple click instead
3612 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3614 // select single line
3615 SelectAllRows( false );
3616 SelectRow( m_lineSelectSingleOnUp
, true );
3619 //Process the event of user clicking the expander
3620 bool expander
= false;
3621 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3623 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3624 if( node
!=NULL
&& node
->HasChildren() )
3626 int indent
= node
->GetIndentLevel();
3627 indent
= GetOwner()->GetIndent()*indent
;
3628 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3629 if( rect
.Contains( x
, y
) )
3632 if( node
->IsOpen() )
3633 OnCollapsing(current
);
3635 OnExpanding( current
);
3639 //If the user click the expander, we do not do editing even if the column with expander are editable
3640 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3642 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3643 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3645 m_renameTimer
->Start( 100, true );
3649 m_lastOnSame
= false;
3650 m_lineSelectSingleOnUp
= (unsigned int)-1;
3654 // This is necessary, because after a DnD operation in
3655 // from and to ourself, the up event is swallowed by the
3656 // DnD code. So on next non-up event (which means here and
3657 // now) m_lineSelectSingleOnUp should be reset.
3658 m_lineSelectSingleOnUp
= (unsigned int)-1;
3661 if (event
.RightDown())
3663 m_lineBeforeLastClicked
= m_lineLastClicked
;
3664 m_lineLastClicked
= current
;
3666 // If the item is already selected, do not update the selection.
3667 // Multi-selections should not be cleared if a selected item is clicked.
3668 if (!IsRowSelected(current
))
3670 SelectAllRows(false);
3671 ChangeCurrentRow(current
);
3672 SelectRow(m_currentRow
,true);
3673 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3676 // notify cell about right click
3679 // Allow generation of context menu event
3682 else if (event
.MiddleDown())
3684 // notify cell about middle click
3687 if (event
.LeftDown() || forceClick
)
3691 m_lineBeforeLastClicked
= m_lineLastClicked
;
3692 m_lineLastClicked
= current
;
3694 unsigned int oldCurrentRow
= m_currentRow
;
3695 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3697 bool cmdModifierDown
= event
.CmdDown();
3698 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3700 if ( IsSingleSel() || !IsRowSelected(current
) )
3702 SelectAllRows( false );
3703 ChangeCurrentRow(current
);
3704 SelectRow(m_currentRow
,true);
3705 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3707 else // multi sel & current is highlighted & no mod keys
3709 m_lineSelectSingleOnUp
= current
;
3710 ChangeCurrentRow(current
); // change focus
3713 else // multi sel & either ctrl or shift is down
3715 if (cmdModifierDown
)
3717 ChangeCurrentRow(current
);
3718 ReverseRowSelection(m_currentRow
);
3719 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3721 else if (event
.ShiftDown())
3723 ChangeCurrentRow(current
);
3725 unsigned int lineFrom
= oldCurrentRow
,
3728 if ( lineTo
< lineFrom
)
3731 lineFrom
= m_currentRow
;
3734 SelectRows(lineFrom
, lineTo
, true);
3735 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3737 else // !ctrl, !shift
3739 // test in the enclosing if should make it impossible
3740 wxFAIL_MSG( _T("how did we get here?") );
3744 if (m_currentRow
!= oldCurrentRow
)
3745 RefreshRow( oldCurrentRow
);
3747 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3749 // Update selection here...
3752 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3753 (current
== oldCurrentRow
)) && oldWasSelected
;
3757 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3761 if (HasCurrentRow())
3767 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3771 if (HasCurrentRow())
3777 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3779 if( m_selection
.GetCount() != 1 )
3780 return wxDataViewItem();
3782 return GetItemByRow( m_selection
.Item(0));
3785 //-----------------------------------------------------------------------------
3787 //-----------------------------------------------------------------------------
3788 WX_DEFINE_LIST(wxDataViewColumnList
)
3790 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3792 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3793 EVT_SIZE(wxDataViewCtrl::OnSize
)
3796 wxDataViewCtrl::~wxDataViewCtrl()
3799 GetModel()->RemoveNotifier( m_notifier
);
3802 void wxDataViewCtrl::Init()
3807 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3808 const wxPoint
& pos
, const wxSize
& size
,
3809 long style
, const wxValidator
& validator
)
3811 if (!wxControl::Create( parent
, id
, pos
, size
,
3812 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3815 SetInitialSize(size
);
3820 MacSetClipChildren( true ) ;
3823 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3825 if (HasFlag(wxDV_NO_HEADER
))
3826 m_headerArea
= NULL
;
3828 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3830 SetTargetWindow( m_clientArea
);
3832 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3834 sizer
->Add( m_headerArea
, 0, wxGROW
);
3835 sizer
->Add( m_clientArea
, 1, wxGROW
);
3842 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3846 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3849 // we need to process arrows ourselves for scrolling
3850 if ( nMsg
== WM_GETDLGCODE
)
3852 rc
|= DLGC_WANTARROWS
;
3860 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3862 // We need to override OnSize so that our scrolled
3863 // window a) does call Layout() to use sizers for
3864 // positioning the controls but b) does not query
3865 // the sizer for their size and use that for setting
3866 // the scrollable area as set that ourselves by
3867 // calling SetScrollbar() further down.
3874 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3876 if (!wxDataViewCtrlBase::AssociateModel( model
))
3879 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3881 model
->AddNotifier( m_notifier
);
3883 m_clientArea
->DestroyTree();
3885 m_clientArea
->BuildTree(model
);
3887 m_clientArea
->UpdateDisplay();
3892 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3894 if (!wxDataViewCtrlBase::AppendColumn(col
))
3897 m_cols
.Append( col
);
3902 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3904 if (!wxDataViewCtrlBase::PrependColumn(col
))
3907 m_cols
.Insert( col
);
3912 void wxDataViewCtrl::OnColumnChange()
3915 m_headerArea
->UpdateDisplay();
3917 m_clientArea
->UpdateDisplay();
3920 void wxDataViewCtrl::DoSetExpanderColumn()
3922 m_clientArea
->UpdateDisplay();
3925 void wxDataViewCtrl::DoSetIndent()
3927 m_clientArea
->UpdateDisplay();
3930 unsigned int wxDataViewCtrl::GetColumnCount() const
3932 return m_cols
.GetCount();
3935 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3937 wxDataViewColumnList::const_iterator iter
;
3939 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3944 if ((*iter
)->IsHidden())
3951 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3953 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3964 bool wxDataViewCtrl::ClearColumns()
3971 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3973 int ret
= 0, dead
= 0;
3974 int len
= GetColumnCount();
3975 for (int i
=0; i
<len
; i
++)
3977 wxDataViewColumn
* col
= GetColumn(i
);
3978 if (col
->IsHidden())
3980 ret
+= col
->GetWidth();
3983 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3990 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3995 //Selection code with wxDataViewItem as parameters
3996 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3998 return m_clientArea
->GetSelection();
4001 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4004 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4005 int len
= selection
.GetCount();
4006 for( int i
= 0; i
< len
; i
++)
4008 unsigned int row
= selection
[i
];
4009 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4014 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4016 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4017 int len
= sel
.GetCount();
4018 for( int i
= 0; i
< len
; i
++ )
4020 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4022 selection
.Add( static_cast<unsigned int>(row
) );
4024 m_clientArea
->SetSelections( selection
);
4027 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4029 int row
= m_clientArea
->GetRowByItem( item
);
4032 //Unselect all rows before select another in the single select mode
4033 if (m_clientArea
->IsSingleSel())
4034 m_clientArea
->SelectAllRows(false);
4035 m_clientArea
->SelectRow(row
, true);
4039 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4041 int row
= m_clientArea
->GetRowByItem( item
);
4043 m_clientArea
->SelectRow(row
, false);
4046 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4048 int row
= m_clientArea
->GetRowByItem( item
);
4051 return m_clientArea
->IsRowSelected(row
);
4056 //Selection code with row number as parameter
4057 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4060 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4061 int len
= selection
.GetCount();
4062 for( int i
= 0; i
< len
; i
++)
4064 unsigned int row
= selection
[i
];
4070 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4072 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4073 int len
= sel
.GetCount();
4074 for( int i
= 0; i
< len
; i
++ )
4078 selection
.Add( static_cast<unsigned int>(row
) );
4080 m_clientArea
->SetSelections( selection
);
4083 void wxDataViewCtrl::Select( int row
)
4087 if (m_clientArea
->IsSingleSel())
4088 m_clientArea
->SelectAllRows(false);
4089 m_clientArea
->SelectRow( row
, true );
4093 void wxDataViewCtrl::Unselect( int row
)
4096 m_clientArea
->SelectRow(row
, false);
4099 bool wxDataViewCtrl::IsSelected( int row
) const
4102 return m_clientArea
->IsRowSelected(row
);
4106 void wxDataViewCtrl::SelectRange( int from
, int to
)
4109 for( int i
= from
; i
< to
; i
++ )
4111 m_clientArea
->Select(sel
);
4114 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4116 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4117 for( int i
= from
; i
< to
; i
++ )
4118 if( sel
.Index( i
) != wxNOT_FOUND
)
4120 m_clientArea
->SetSelections(sel
);
4123 void wxDataViewCtrl::SelectAll()
4125 m_clientArea
->SelectAllRows(true);
4128 void wxDataViewCtrl::UnselectAll()
4130 m_clientArea
->SelectAllRows(false);
4133 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4137 if( row
> (int) m_clientArea
->GetRowCount() )
4138 row
= m_clientArea
->GetRowCount();
4140 int first
= m_clientArea
->GetFirstVisibleRow();
4141 int last
= m_clientArea
->GetLastVisibleRow();
4143 m_clientArea
->ScrollTo( row
, column
);
4144 else if( row
> last
)
4145 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4147 m_clientArea
->ScrollTo( first
, column
);
4150 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4152 int row
= m_clientArea
->GetRowByItem(item
);
4155 if( column
== NULL
)
4156 EnsureVisible(row
, -1);
4160 int len
= GetColumnCount();
4161 for( int i
= 0; i
< len
; i
++ )
4162 if( GetColumn(i
) == column
)
4167 EnsureVisible( row
, col
);
4173 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4175 m_clientArea
->HitTest(point
, item
, column
);
4178 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4180 return m_clientArea
->GetItemRect(item
, column
);
4183 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4185 return m_clientArea
->GetItemByRow( row
);
4188 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4190 return m_clientArea
->GetRowByItem( item
);
4193 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4195 int row
= m_clientArea
->GetRowByItem( item
);
4197 m_clientArea
->Expand(row
);
4200 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4202 int row
= m_clientArea
->GetRowByItem( item
);
4204 m_clientArea
->Collapse(row
);
4208 // !wxUSE_GENERICDATAVIEWCTRL
4211 // wxUSE_DATAVIEWCTRL