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::SetSortOrder( bool ascending
)
1204 m_ascending
= ascending
;
1206 // Update header button
1208 GetOwner()->OnColumnChange();
1211 bool wxDataViewColumn::IsSortOrderAscending() const
1216 void wxDataViewColumn::SetInternalWidth( int width
)
1220 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1221 if (m_owner
&& m_owner
->m_clientArea
)
1222 m_owner
->m_clientArea
->RecalculateDisplay();
1225 void wxDataViewColumn::SetWidth( int width
)
1227 if (m_owner
->m_headerArea
) m_owner
->m_headerArea
->UpdateDisplay();
1229 SetInternalWidth(width
);
1233 //-----------------------------------------------------------------------------
1234 // wxDataViewHeaderWindowBase
1235 //-----------------------------------------------------------------------------
1237 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1239 wxWindow
*parent
= GetParent();
1240 wxDataViewEvent
le(type
, parent
->GetId());
1242 le
.SetEventObject(parent
);
1244 le
.SetDataViewColumn(GetColumn(n
));
1245 le
.SetModel(GetOwner()->GetModel());
1247 // for events created by wxDataViewHeaderWindow the
1248 // row / value fields are not valid
1250 parent
->GetEventHandler()->ProcessEvent(le
);
1253 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1255 #ifndef HDS_DRAGDROP
1256 #define HDS_DRAGDROP 0x0040
1258 #ifndef HDS_FULLDRAG
1259 #define HDS_FULLDRAG 0x0080
1262 // implemented in msw/listctrl.cpp:
1263 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1265 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1267 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1268 const wxPoint
&pos
, const wxSize
&size
,
1269 const wxString
&name
)
1273 m_scrollOffsetX
= 0;
1274 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this ) + 10;
1276 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1277 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1278 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1279 h
= size
.y
== wxDefaultCoord
? m_buttonHeight
: size
.y
;
1281 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1284 // create the native WC_HEADER window:
1285 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1286 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1289 msStyle
|= WS_VISIBLE
;
1291 m_hWnd
= CreateWindowEx(0,
1302 wxLogLastError(_T("CreateWindowEx"));
1306 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1307 // to call wxDataViewHeaderWindow::MSWOnNotify
1308 SubclassWin(m_hWnd
);
1310 // the following is required to get the default win's font for
1311 // header windows and must be done befor sending the HDM_LAYOUT msg
1317 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1322 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1324 return wxSize(80, m_buttonHeight
);
1327 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1329 // remove old columns
1330 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1331 Header_DeleteItem((HWND
)m_hWnd
, 0);
1333 // add the updated array of columns to the header control
1334 unsigned int cols
= GetOwner()->GetColumnCount();
1335 unsigned int added
= 0;
1336 for (unsigned int i
= 0; i
< cols
; i
++)
1338 wxDataViewColumn
*col
= GetColumn( i
);
1339 if (col
->IsHidden())
1340 continue; // don't add it!
1343 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1344 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1345 hdi
.cxy
= col
->GetWidth();
1346 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1347 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1348 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1350 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1352 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1353 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1354 // VZ: works with 5.81
1355 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1358 // lParam is reserved for application's use:
1359 // we store there the column index to use it later in MSWOnNotify
1360 // (since columns may have been hidden)
1361 hdi
.lParam
= (LPARAM
)i
;
1363 // the native wxMSW implementation of the header window
1364 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1365 // on the right: to correct this effect we make the column
1366 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1368 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1370 switch (col
->GetAlignment())
1373 hdi
.fmt
|= HDF_LEFT
;
1375 case wxALIGN_CENTER
:
1376 case wxALIGN_CENTER_HORIZONTAL
:
1377 hdi
.fmt
|= HDF_CENTER
;
1380 hdi
.fmt
|= HDF_RIGHT
;
1384 // such alignment is not allowed for the column header!
1388 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1389 (WPARAM
)added
, (LPARAM
)&hdi
);
1394 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1398 // NOTE: we don't just return nmHDR->iItem because when there are
1399 // hidden columns, nmHDR->iItem may be different from
1400 // nmHDR->pitem->lParam
1402 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1404 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1409 item
.mask
= HDI_LPARAM
;
1410 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1412 return (unsigned int)item
.lParam
;
1415 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1417 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1419 // is it a message from the header?
1420 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1421 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1423 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1424 switch ( nmhdr
->code
)
1426 case HDN_BEGINTRACK
:
1427 // user has started to resize a column:
1428 // do we need to veto it?
1429 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1437 // user has started to reorder a column
1440 case HDN_ITEMCHANGING
:
1441 if (nmHDR
->pitem
!= NULL
&&
1442 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1444 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1445 if (nmHDR
->pitem
->cxy
< minWidth
)
1447 // do not allow the user to resize this column under
1448 // its minimal width:
1454 case HDN_ITEMCHANGED
: // user is resizing a column
1455 case HDN_ENDTRACK
: // user has finished resizing a column
1456 case HDN_ENDDRAG
: // user has finished reordering a column
1458 // update the width of the modified column:
1459 if (nmHDR
->pitem
!= NULL
&&
1460 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1462 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1463 unsigned int w
= nmHDR
->pitem
->cxy
;
1464 wxDataViewColumn
*col
= GetColumn(idx
);
1466 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1467 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1468 w
-= COLUMN_WIDTH_OFFSET
;
1470 if (w
>= (unsigned)col
->GetMinWidth())
1471 col
->SetInternalWidth(w
);
1477 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1478 wxDataViewModel
* model
= GetOwner()->GetModel();
1480 if(nmHDR
->iButton
== 0)
1482 wxDataViewColumn
*col
= GetColumn(idx
);
1483 if(col
->IsSortable())
1485 if(model
&& m_owner
->GetSortingColumn() == col
)
1487 bool order
= col
->IsSortOrderAscending();
1488 col
->SetSortOrder(!order
);
1492 m_owner
->SetSortingColumn(col
);
1500 wxEventType evt
= nmHDR
->iButton
== 0 ?
1501 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1502 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1503 SendEvent(evt
, idx
);
1509 // NOTE: for some reason (i.e. for a bug in Windows)
1510 // the HDN_ITEMCLICK notification is not sent on
1511 // right clicks, so we need to handle NM_RCLICK
1514 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1515 if (column
!= wxNOT_FOUND
)
1518 item
.mask
= HDI_LPARAM
;
1519 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1521 // 'idx' may be different from 'column' if there are
1522 // hidden columns...
1523 unsigned int idx
= (unsigned int)item
.lParam
;
1524 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1530 case HDN_GETDISPINFOW
:
1531 // see wxListCtrl::MSWOnNotify for more info!
1534 case HDN_ITEMDBLCLICK
:
1536 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1537 int w
= GetOwner()->GetBestColumnWidth(idx
);
1539 // update the native control:
1541 ZeroMemory(&hd
, sizeof(hd
));
1542 hd
.mask
= HDI_WIDTH
;
1544 Header_SetItem(GetHwnd(),
1545 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1548 // update the wxDataViewColumn class:
1549 GetColumn(idx
)->SetInternalWidth(w
);
1554 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1560 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1561 const wxRect
* WXUNUSED(rect
))
1563 m_scrollOffsetX
+= dx
;
1565 GetParent()->Layout();
1568 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1572 // TODO: why is there a border + 2px around it?
1573 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1576 #else // !defined(__WXMSW__)
1578 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1579 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1580 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1581 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1582 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1585 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1586 const wxPoint
&pos
, const wxSize
&size
,
1587 const wxString
&name
)
1591 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1594 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1595 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1596 SetOwnForegroundColour( attr
.colFg
);
1597 SetOwnBackgroundColour( attr
.colBg
);
1599 SetOwnFont( attr
.font
);
1601 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1602 // a wxBoxSizer and in order to avoid super-big header windows,
1603 // we need to set our height as fixed
1604 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1605 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1610 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1613 GetClientSize( &w
, &h
);
1615 wxAutoBufferedPaintDC
dc( this );
1617 dc
.SetBackground(GetBackgroundColour());
1621 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1624 m_owner
->GetViewStart( &x
, NULL
);
1626 // account for the horz scrollbar offset
1627 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1629 dc
.SetFont( GetFont() );
1631 unsigned int cols
= GetOwner()->GetColumnCount();
1634 for (i
= 0; i
< cols
; i
++)
1636 wxDataViewColumn
*col
= GetColumn( i
);
1637 if (col
->IsHidden())
1638 continue; // skip it!
1640 int cw
= col
->GetWidth();
1643 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1644 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1646 if (col
->IsSortOrderAscending())
1647 sortArrow
= wxHDR_SORT_ICON_UP
;
1649 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1653 if (m_parent
->IsEnabled())
1655 if ((int) i
== m_hover
)
1656 state
= wxCONTROL_CURRENT
;
1660 state
= (int) wxCONTROL_DISABLED
;
1663 wxRendererNative::Get().DrawHeaderButton
1667 wxRect(xpos
, 0, cw
, ch
-1),
1672 // align as required the column title:
1674 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1675 switch (col
->GetAlignment())
1678 x
+= HEADER_HORIZ_BORDER
;
1681 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1684 case wxALIGN_CENTER
:
1685 case wxALIGN_CENTER_HORIZONTAL
:
1686 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1690 // always center the title vertically:
1691 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1693 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1695 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1696 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1697 dc
.DrawText( col
->GetTitle(), x
, y
);
1698 dc
.DestroyClippingRegion();
1704 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1706 GetParent()->SetFocus();
1710 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1712 // we want to work with logical coords
1714 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1715 int y
= event
.GetY();
1719 // we don't draw the line beyond our window,
1720 // but we allow dragging it there
1722 GetClientSize( &w
, NULL
);
1723 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1726 if (event
.ButtonUp())
1728 m_isDragging
= false;
1734 m_currentX
= wxMax(m_minX
+ 7, x
);
1738 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1740 GetOwner()->Refresh();
1744 else // not dragging
1747 m_column
= wxNOT_FOUND
;
1749 bool hit_border
= false;
1751 // end of the current column
1754 // find the column where this event occured
1755 int countCol
= m_owner
->GetColumnCount();
1756 for (int column
= 0; column
< countCol
; column
++)
1758 wxDataViewColumn
*p
= GetColumn(column
);
1761 continue; // skip if not shown
1763 xpos
+= p
->GetWidth();
1765 if ((abs(x
-xpos
) < 3) && (y
< 22))
1773 // inside the column
1780 int old_hover
= m_hover
;
1782 if (event
.Leaving())
1783 m_hover
= wxNOT_FOUND
;
1784 if (old_hover
!= m_hover
)
1787 if (m_column
== wxNOT_FOUND
)
1790 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1791 if (event
.LeftDClick() && resizeable
)
1793 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1796 else if (event
.LeftDown() || event
.RightUp())
1798 if (hit_border
&& event
.LeftDown() && resizeable
)
1800 m_isDragging
= true;
1804 else // click on a column
1806 wxDataViewModel
* model
= GetOwner()->GetModel();
1807 wxEventType evt
= event
.LeftDown() ?
1808 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1809 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1810 SendEvent(evt
, m_column
);
1812 //Left click the header
1813 if(event
.LeftDown())
1815 wxDataViewColumn
*col
= GetColumn(m_column
);
1816 if(col
->IsSortable())
1818 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1819 if(model
&& sortCol
== col
)
1821 bool order
= col
->IsSortOrderAscending();
1822 col
->SetSortOrder(!order
);
1826 m_owner
->SetSortingColumn(col
);
1832 //Send the column sorted event
1833 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1837 else if (event
.Moving())
1839 if (hit_border
&& resizeable
)
1840 m_currentCursor
= m_resizeCursor
;
1842 m_currentCursor
= wxSTANDARD_CURSOR
;
1844 SetCursor(*m_currentCursor
);
1849 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1853 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1854 m_owner
->GetViewStart( &x
, NULL
);
1856 // shift the DC origin to match the position of the main window horizontal
1857 // scrollbar: this allows us to always use logical coords
1858 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1861 #endif // defined(__WXMSW__)
1863 //-----------------------------------------------------------------------------
1864 // wxDataViewRenameTimer
1865 //-----------------------------------------------------------------------------
1867 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1872 void wxDataViewRenameTimer::Notify()
1874 m_owner
->OnRenameTimer();
1877 //-----------------------------------------------------------------------------
1878 // wxDataViewMainWindow
1879 //-----------------------------------------------------------------------------
1881 //The tree building helper, declared firstly
1882 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1884 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1886 if (row1
> row2
) return 1;
1887 if (row1
== row2
) return 0;
1892 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1894 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1895 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1896 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1897 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1898 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1899 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1902 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1903 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1904 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1905 m_selection( wxDataViewSelectionCmp
)
1910 m_lastOnSame
= false;
1911 m_renameTimer
= new wxDataViewRenameTimer( this );
1913 // TODO: user better initial values/nothing selected
1914 m_currentCol
= NULL
;
1917 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1920 m_dragStart
= wxPoint(0,0);
1921 m_lineLastClicked
= (unsigned int) -1;
1922 m_lineBeforeLastClicked
= (unsigned int) -1;
1923 m_lineSelectSingleOnUp
= (unsigned int) -1;
1927 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1928 SetBackgroundColour( *wxWHITE
);
1930 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1932 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1933 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1934 //Some new added code to deal with the tree structure
1935 m_root
= new wxDataViewTreeNode( NULL
);
1936 m_root
->SetHasChildren(true);
1938 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1940 m_underMouse
= NULL
;
1944 wxDataViewMainWindow::~wxDataViewMainWindow()
1947 delete m_renameTimer
;
1950 void wxDataViewMainWindow::OnRenameTimer()
1952 // We have to call this here because changes may just have
1953 // been made and no screen update taken place.
1958 unsigned int cols
= GetOwner()->GetColumnCount();
1960 for (i
= 0; i
< cols
; i
++)
1962 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1964 continue; // skip it!
1966 if (c
== m_currentCol
)
1968 xpos
+= c
->GetWidth();
1970 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1971 m_currentCol
->GetWidth(), m_lineHeight
);
1973 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1974 &labelRect
.x
, &labelRect
.y
);
1976 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1977 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1981 //------------------------------------------------------------------
1982 // Helper class for do operation on the tree node
1983 //------------------------------------------------------------------
1990 //The return value control how the tree-walker tranverse the tree
1991 // 0: Job done, stop tranverse and return
1992 // 1: Ignore the current node's subtree and continue
1993 // 2: Job not done, continue
1994 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1995 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1996 virtual int operator() ( void * n
) = 0;
1999 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2004 switch( func( node
) )
2015 wxDataViewTreeNodes nodes
= node
->GetNodes();
2016 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2018 int len_nodes
= nodes
.GetCount();
2019 int len
= leaves
.GetCount();
2020 int i
= 0, nodes_i
= 0;
2022 for( ; i
< len
; i
++ )
2024 void * n
= leaves
[i
];
2025 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2027 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2030 if( Walker( nd
, func
) )
2049 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2060 wxDataViewTreeNode
* node
;
2061 node
= FindNode(parent
);
2066 node
->SetHasChildren( true );
2068 if( g_model
->IsContainer( item
) )
2070 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2071 newnode
->SetItem(item
);
2072 newnode
->SetHasChildren( true );
2073 node
->AddNode( newnode
);
2076 node
->AddLeaf( item
.GetID() );
2078 node
->ChangeSubTreeCount(1);
2086 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2088 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2089 const wxDataViewItem
& item
)
2094 if( m_currentRow
> GetRowCount() )
2095 m_currentRow
= m_count
- 1;
2097 m_selection
.Empty();
2104 wxDataViewTreeNode
* node
= FindNode(parent
);
2106 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2107 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2110 node
->GetChildren().Remove( item
.GetID() );
2111 //Manuplate selection
2112 if( m_selection
.GetCount() > 1 )
2114 m_selection
.Empty();
2116 bool isContainer
= false;
2117 wxDataViewTreeNodes nds
= node
->GetNodes();
2118 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2120 if (nds
[i
]->GetItem() == item
)
2128 wxDataViewTreeNode
* n
= NULL
;
2129 wxDataViewTreeNodes nodes
= node
->GetNodes();
2130 int len
= nodes
.GetCount();
2131 for( int i
= 0 ; i
< len
; i
++)
2133 if( nodes
[i
]->GetItem() == item
)
2140 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2142 node
->GetNodes().Remove( n
);
2143 sub
-= n
->GetSubTreeCount();
2144 DestroyTreeHelper(n
);
2146 //Make the row number invalid and get a new valid one when user call GetRowCount
2148 node
->ChangeSubTreeCount(sub
);
2149 if( node
->GetChildrenNumber() == 0)
2151 node
->GetParent()->GetNodes().Remove( node
);
2155 //Change the current row to the last row if the current exceed the max row number
2156 if( m_currentRow
> GetRowCount() )
2157 m_currentRow
= m_count
- 1;
2164 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2170 wxWindow
*parent
= GetParent();
2171 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2172 le
.SetEventObject(parent
);
2173 le
.SetModel(GetOwner()->GetModel());
2175 parent
->GetEventHandler()->ProcessEvent(le
);
2180 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2182 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2183 /*#define MAX_VIRTUAL_WIDTH 100000
2185 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2186 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2187 Refresh( true, &rect );
2195 wxWindow
*parent
= GetParent();
2196 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2197 le
.SetEventObject(parent
);
2198 le
.SetModel(GetOwner()->GetModel());
2201 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2202 parent
->GetEventHandler()->ProcessEvent(le
);
2207 bool wxDataViewMainWindow::Cleared()
2217 void wxDataViewMainWindow::UpdateDisplay()
2222 void wxDataViewMainWindow::OnInternalIdle()
2224 wxWindow::OnInternalIdle();
2228 RecalculateDisplay();
2233 void wxDataViewMainWindow::RecalculateDisplay()
2235 wxDataViewModel
*model
= GetOwner()->GetModel();
2242 int width
= GetEndOfLastCol();
2243 int height
= GetRowCount() * m_lineHeight
;
2245 SetVirtualSize( width
, height
);
2246 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2251 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2253 wxWindow::ScrollWindow( dx
, dy
, rect
);
2255 if (GetOwner()->m_headerArea
)
2256 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2259 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2262 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2263 int sy
= rows
*m_lineHeight
/y
;
2267 wxRect rect
= GetClientRect();
2269 int x_start
= 0, x_end
= 0, w
= 0;
2271 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2272 for (x_start
= 0; colnum
< column
; colnum
++)
2274 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2275 if (col
->IsHidden())
2276 continue; // skip it!
2278 w
= col
->GetWidth();
2282 x_end
= x_start
+ w
;
2283 xe
= xx
+ rect
.width
;
2286 sx
= ( xx
+ x_end
- xe
)/x
;
2293 m_owner
->Scroll( sx
, sy
);
2296 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2298 wxDataViewModel
*model
= GetOwner()->GetModel();
2299 wxAutoBufferedPaintDC
dc( this );
2302 dc
.SetBackground(GetBackgroundColour());
2304 GetOwner()->PrepareDC( dc
);
2305 dc
.SetFont( GetFont() );
2307 wxRect update
= GetUpdateRegion().GetBox();
2308 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2310 // compute which items needs to be redrawn
2311 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2312 unsigned int item_count
=
2313 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2314 (int)(GetRowCount( ) - item_start
));
2315 unsigned int item_last
= item_start
+ item_count
;
2317 // compute which columns needs to be redrawn
2318 unsigned int cols
= GetOwner()->GetColumnCount();
2319 unsigned int col_start
= 0;
2320 unsigned int x_start
= 0;
2321 for (x_start
= 0; col_start
< cols
; col_start
++)
2323 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2324 if (col
->IsHidden())
2325 continue; // skip it!
2327 unsigned int w
= col
->GetWidth();
2328 if (x_start
+w
>= (unsigned int)update
.x
)
2334 unsigned int col_last
= col_start
;
2335 unsigned int x_last
= x_start
;
2336 for (; col_last
< cols
; col_last
++)
2338 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2339 if (col
->IsHidden())
2340 continue; // skip it!
2342 if (x_last
> (unsigned int)update
.GetRight())
2345 x_last
+= col
->GetWidth();
2348 // Draw horizontal rules if required
2349 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2351 dc
.SetPen(m_penRule
);
2352 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2354 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2356 int y
= i
* m_lineHeight
;
2357 dc
.DrawLine(x_start
, y
, x_last
, y
);
2361 // Draw vertical rules if required
2362 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2364 dc
.SetPen(m_penRule
);
2365 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2368 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2370 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2371 if (col
->IsHidden())
2372 continue; // skip it
2374 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2375 x
, item_last
* m_lineHeight
);
2377 x
+= col
->GetWidth();
2380 // Draw last vertical rule
2381 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2382 x
, item_last
* m_lineHeight
);
2385 // redraw the background for the items which are selected/current
2386 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2388 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2389 if (selected
|| item
== m_currentRow
)
2391 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2392 if (item
== m_currentRow
)
2393 flags
|= wxCONTROL_CURRENT
;
2395 flags
|= wxCONTROL_FOCUSED
;
2397 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2398 wxRendererNative::Get().DrawItemSelectionRect
2408 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2411 // TODO: last column for RTL support
2412 expander
= GetOwner()->GetColumn( 0 );
2413 GetOwner()->SetExpanderColumn(expander
);
2416 // redraw all cells for all rows which must be repainted and for all columns
2418 cell_rect
.x
= x_start
;
2419 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2420 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2422 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2423 wxDataViewRenderer
*cell
= col
->GetRenderer();
2424 cell_rect
.width
= col
->GetWidth();
2426 if (col
->IsHidden())
2427 continue; // skipt it!
2430 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2432 // get the cell value and set it into the renderer
2434 wxDataViewTreeNode
*node
= NULL
;
2435 wxDataViewItem dataitem
;
2439 node
= GetTreeNodeByRow(item
);
2443 dataitem
= node
->GetItem();
2445 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2450 dataitem
= wxDataViewItem( (void*) item
);
2453 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2454 cell
->SetValue( value
);
2456 if (cell
->GetWantsAttr())
2458 wxDataViewItemAttr attr
;
2459 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2461 cell
->SetAttr( attr
);
2462 cell
->SetHasAttr( ret
);
2465 // update the y offset
2466 cell_rect
.y
= item
* m_lineHeight
;
2468 //Draw the expander here.
2470 if ((m_root
) && (col
== expander
))
2472 indent
= node
->GetIndentLevel();
2474 //Calculate the indent first
2475 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2477 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2478 // change the cell_rect.x to the appropriate pos
2479 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2480 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2481 dc
.SetPen( m_penExpander
);
2482 dc
.SetBrush( wxNullBrush
);
2483 if( node
->HasChildren() )
2485 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2487 if (m_underMouse
== node
)
2489 flag
|= wxCONTROL_CURRENT
;
2491 if( node
->IsOpen() )
2492 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2494 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2498 // I am wondering whether we should draw dot lines between tree nodes
2501 // Yes, if the node does not have any child, it must be a leaf which
2502 // mean that it is a temporarily created by GetTreeNodeByRow
2505 //force the expander column to left-center align
2506 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2510 // cannot be bigger than allocated space
2511 wxSize size
= cell
->GetSize();
2512 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2513 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2514 // size.y = wxMin( size.y, cell_rect.height );
2515 size
.y
= cell_rect
.height
;
2517 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2518 int align
= cell
->GetAlignment();
2520 // horizontal alignment:
2521 item_rect
.x
= cell_rect
.x
;
2522 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2523 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2524 else if (align
& wxALIGN_RIGHT
)
2525 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2526 //else: wxALIGN_LEFT is the default
2528 // vertical alignment:
2529 item_rect
.y
= cell_rect
.y
;
2530 if (align
& wxALIGN_CENTER_VERTICAL
)
2531 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2532 else if (align
& wxALIGN_BOTTOM
)
2533 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2534 //else: wxALIGN_TOP is the default
2537 item_rect
.x
+= PADDING_RIGHTLEFT
;
2538 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2540 //Here we add the tree indent
2541 item_rect
.x
+= indent
;
2544 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2545 state
|= wxDATAVIEW_CELL_SELECTED
;
2547 // TODO: it would be much more efficient to create a clipping
2548 // region for the entire column being rendered (in the OnPaint
2549 // of wxDataViewMainWindow) instead of a single clip region for
2550 // each cell. However it would mean that each renderer should
2551 // respect the given wxRect's top & bottom coords, eventually
2552 // violating only the left & right coords - however the user can
2553 // make its own renderer and thus we cannot be sure of that.
2554 dc
.SetClippingRegion( item_rect
);
2555 cell
->Render( item_rect
, &dc
, state
);
2556 dc
.DestroyClippingRegion();
2559 cell_rect
.x
+= cell_rect
.width
;
2563 int wxDataViewMainWindow::GetCountPerPage() const
2565 wxSize size
= GetClientSize();
2566 return size
.y
/ m_lineHeight
;
2569 int wxDataViewMainWindow::GetEndOfLastCol() const
2573 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2575 const wxDataViewColumn
*c
=
2576 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2579 width
+= c
->GetWidth();
2584 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2588 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2590 return y
/ m_lineHeight
;
2593 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2595 wxSize client_size
= GetClientSize();
2596 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2597 &client_size
.x
, &client_size
.y
);
2599 //we should deal with the pixel here
2600 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2602 return wxMin( GetRowCount()-1, row
);
2605 unsigned int wxDataViewMainWindow::GetRowCount()
2607 if ( m_count
== -1 )
2609 m_count
= RecalculateCount();
2611 GetVirtualSize( &width
, &height
);
2612 height
= m_count
* m_lineHeight
;
2614 SetVirtualSize( width
, height
);
2619 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2626 void wxDataViewMainWindow::SelectAllRows( bool on
)
2633 m_selection
.Clear();
2634 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2635 m_selection
.Add( i
);
2640 unsigned int first_visible
= GetFirstVisibleRow();
2641 unsigned int last_visible
= GetLastVisibleRow();
2643 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2645 unsigned int row
= m_selection
[i
];
2646 if ((row
>= first_visible
) && (row
<= last_visible
))
2649 m_selection
.Clear();
2653 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2655 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2659 m_selection
.Add( row
);
2667 m_selection
.Remove( row
);
2673 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2677 unsigned int tmp
= from
;
2683 for (i
= from
; i
<= to
; i
++)
2685 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2688 m_selection
.Add( i
);
2693 m_selection
.Remove( i
);
2696 RefreshRows( from
, to
);
2699 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2701 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2703 int n
= aSelections
[i
];
2705 m_selection
.Add( n
);
2710 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2712 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2713 m_selection
.Add( row
);
2715 m_selection
.Remove( row
);
2719 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2721 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2724 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2726 wxWindow
*parent
= GetParent();
2727 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2729 le
.SetEventObject(parent
);
2730 le
.SetModel(GetOwner()->GetModel());
2733 parent
->GetEventHandler()->ProcessEvent(le
);
2736 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2738 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2739 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2741 wxSize client_size
= GetClientSize();
2742 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2743 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2744 if (intersect_rect
.width
> 0)
2745 Refresh( true, &intersect_rect
);
2748 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2752 unsigned int tmp
= to
;
2757 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2758 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2760 wxSize client_size
= GetClientSize();
2761 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2762 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2763 if (intersect_rect
.width
> 0)
2764 Refresh( true, &intersect_rect
);
2767 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2769 unsigned int count
= GetRowCount();
2770 if (firstRow
> count
)
2773 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2774 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2776 wxSize client_size
= GetClientSize();
2777 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2778 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2779 if (intersect_rect
.width
> 0)
2780 Refresh( true, &intersect_rect
);
2783 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2785 wxCHECK_RET( newCurrent
< GetRowCount(),
2786 _T("invalid item index in OnArrowChar()") );
2788 // if there is no selection, we cannot move it anywhere
2789 if (!HasCurrentRow())
2792 unsigned int oldCurrent
= m_currentRow
;
2794 // in single selection we just ignore Shift as we can't select several
2796 if ( event
.ShiftDown() && !IsSingleSel() )
2798 RefreshRow( oldCurrent
);
2800 ChangeCurrentRow( newCurrent
);
2802 // select all the items between the old and the new one
2803 if ( oldCurrent
> newCurrent
)
2805 newCurrent
= oldCurrent
;
2806 oldCurrent
= m_currentRow
;
2809 SelectRows( oldCurrent
, newCurrent
, true );
2810 if (oldCurrent
!=newCurrent
)
2811 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2815 RefreshRow( oldCurrent
);
2817 // all previously selected items are unselected unless ctrl is held
2818 if ( !event
.ControlDown() )
2819 SelectAllRows(false);
2821 ChangeCurrentRow( newCurrent
);
2823 if ( !event
.ControlDown() )
2825 SelectRow( m_currentRow
, true );
2826 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2829 RefreshRow( m_currentRow
);
2832 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2835 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2839 rect
.y
= m_lineHeight
* row
;
2840 rect
.width
= GetEndOfLastCol();
2841 rect
.height
= m_lineHeight
;
2846 class RowToItemJob
: public DoJob
2849 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2850 virtual ~RowToItemJob(){};
2852 virtual int operator() ( wxDataViewTreeNode
* node
)
2855 if( current
== static_cast<int>(row
))
2857 ret
= node
->GetItem() ;
2861 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2863 current
+= node
->GetSubTreeCount();
2868 //If the current has no child node, we can find the desired item of the row number directly.
2869 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2870 if( node
->GetNodes().GetCount() == 0)
2872 int index
= static_cast<int>(row
) - current
- 1;
2873 ret
= node
->GetChildren().Item( index
);
2880 virtual int operator() ( void * n
)
2883 if( current
== static_cast<int>(row
))
2885 ret
= wxDataViewItem( n
) ;
2890 wxDataViewItem
GetResult(){ return ret
; }
2897 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2901 return wxDataViewItem( (void*) row
);
2905 RowToItemJob
job( row
, -2 );
2906 Walker( m_root
, job
);
2907 return job
.GetResult();
2911 class RowToTreeNodeJob
: public DoJob
2914 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2917 this->current
= current
;
2921 virtual ~RowToTreeNodeJob(){};
2923 virtual int operator() ( wxDataViewTreeNode
* node
)
2926 if( current
== static_cast<int>(row
))
2932 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2934 current
+= node
->GetSubTreeCount();
2940 //If the current has no child node, we can find the desired item of the row number directly.
2941 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2942 if( node
->GetNodes().GetCount() == 0)
2944 int index
= static_cast<int>(row
) - current
- 1;
2945 void * n
= node
->GetChildren().Item( index
);
2946 ret
= new wxDataViewTreeNode( parent
) ;
2947 ret
->SetItem( wxDataViewItem( n
));
2948 ret
->SetHasChildren(false);
2957 virtual int operator() ( void * n
)
2960 if( current
== static_cast<int>(row
))
2962 ret
= new wxDataViewTreeNode( parent
) ;
2963 ret
->SetItem( wxDataViewItem( n
));
2964 ret
->SetHasChildren(false);
2970 wxDataViewTreeNode
* GetResult(){ return ret
; }
2974 wxDataViewTreeNode
* ret
;
2975 wxDataViewTreeNode
* parent
;
2979 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2987 RowToTreeNodeJob
job( row
, -2, m_root
);
2988 Walker( m_root
, job
);
2989 return job
.GetResult();
2993 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2995 wxWindow
*parent
= GetParent();
2996 wxDataViewEvent
le(type
, parent
->GetId());
2998 le
.SetEventObject(parent
);
2999 le
.SetModel(GetOwner()->GetModel());
3002 parent
->GetEventHandler()->ProcessEvent(le
);
3006 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
3008 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3011 if( node
->HasChildren())
3013 if( !node
->IsOpen())
3015 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
3016 //Check if the user prevent expanding
3017 if( e
.GetSkipped() )
3021 //Here I build the children of current node
3022 if( node
->GetChildrenNumber() == 0 )
3025 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
3029 //Send the expanded event
3030 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
3034 SelectRow( row
, false );
3035 SelectRow( row
+ 1, true );
3036 ChangeCurrentRow( row
+ 1 );
3037 SendSelectionChangedEvent( GetItemByRow(row
+1));
3045 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
3047 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
3050 wxDataViewTreeNode
* nd
= node
;
3052 if( node
->HasChildren() && node
->IsOpen() )
3054 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3055 if( e
.GetSkipped() )
3060 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3064 node
= node
->GetParent();
3067 int parent
= GetRowByItem( node
->GetItem() ) ;
3070 SelectRow( row
, false);
3071 SelectRow(parent
, true );
3072 ChangeCurrentRow( parent
);
3073 SendSelectionChangedEvent( node
->GetItem() );
3077 if( !nd
->HasChildren())
3082 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3084 wxDataViewModel
* model
= GetOwner()->GetModel();
3088 //Compose the a parent-chain of the finding item
3090 list
.DeleteContents( true );
3091 wxDataViewItem
it( item
);
3094 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3095 list
.Insert( pItem
);
3096 it
= model
->GetParent( it
);
3099 //Find the item along the parent-chain.
3100 //This algorithm is designed to speed up the node-finding method
3101 wxDataViewTreeNode
* node
= m_root
;
3102 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3104 if( node
->HasChildren() )
3106 if( node
->GetChildrenNumber() == 0 )
3109 BuildTreeHelper(model
, node
->GetItem(), node
);
3112 wxDataViewTreeNodes nodes
= node
->GetNodes();
3114 for (; i
< nodes
.GetCount(); i
++)
3116 if (nodes
[i
]->GetItem() == (**iter
))
3122 if (i
== nodes
.GetCount())
3131 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3133 wxDataViewColumn
*col
= NULL
;
3134 unsigned int cols
= GetOwner()->GetColumnCount();
3135 unsigned int colnum
= 0;
3136 unsigned int x_start
= 0;
3138 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3139 for (x_start
= 0; colnum
< cols
; colnum
++)
3141 col
= GetOwner()->GetColumn(colnum
);
3142 if (col
->IsHidden())
3143 continue; // skip it!
3145 unsigned int w
= col
->GetWidth();
3146 if (x_start
+w
>= (unsigned int)x
)
3153 item
= GetItemByRow( y
/m_lineHeight
);
3156 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3158 int row
= GetRowByItem(item
);
3159 int y
= row
*m_lineHeight
;
3160 int h
= m_lineHeight
;
3162 wxDataViewColumn
*col
= NULL
;
3163 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3165 col
= GetOwner()->GetColumn( i
);
3166 x
+= col
->GetWidth();
3167 if( GetOwner()->GetColumn(i
+1) == column
)
3170 int w
= col
->GetWidth();
3171 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3172 return wxRect(x
, y
, w
, h
);
3175 int wxDataViewMainWindow::RecalculateCount()
3179 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3181 return list_model
->GetLastIndex() + 1;
3183 return list_model
->GetLastIndex() - 1;
3188 return m_root
->GetSubTreeCount();
3192 class ItemToRowJob
: public DoJob
3195 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3196 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3197 virtual ~ItemToRowJob(){};
3199 //Maybe binary search will help to speed up this process
3200 virtual int operator() ( wxDataViewTreeNode
* node
)
3203 if( node
->GetItem() == item
)
3208 if( node
->GetItem() == **m_iter
)
3215 ret
+= node
->GetSubTreeCount();
3221 virtual int operator() ( void * n
)
3224 if( n
== item
.GetID() )
3228 //the row number is begin from zero
3229 int GetResult(){ return ret
-1 ; }
3231 ItemList::const_iterator m_iter
;
3232 wxDataViewItem item
;
3237 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3239 wxDataViewModel
* model
= GetOwner()->GetModel();
3245 return wxPtrToUInt( item
.GetID() );
3252 //Compose the a parent-chain of the finding item
3254 wxDataViewItem
* pItem
= NULL
;
3255 list
.DeleteContents( true );
3256 wxDataViewItem
it( item
);
3259 pItem
= new wxDataViewItem( it
);
3260 list
.Insert( pItem
);
3261 it
= model
->GetParent( it
);
3263 pItem
= new wxDataViewItem( );
3264 list
.Insert( pItem
);
3266 ItemToRowJob
job( item
, list
.begin() );
3267 Walker(m_root
, job
);
3268 return job
.GetResult();
3272 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3274 if( !model
->IsContainer( item
) )
3277 wxDataViewItemArray children
;
3278 unsigned int num
= model
->GetChildren( item
, children
);
3279 unsigned int index
= 0;
3280 while( index
< num
)
3282 if( model
->IsContainer( children
[index
] ) )
3284 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3285 n
->SetItem(children
[index
]);
3286 n
->SetHasChildren( true ) ;
3291 node
->AddLeaf( children
[index
].GetID() );
3295 node
->SetSubTreeCount( num
);
3296 wxDataViewTreeNode
* n
= node
->GetParent();
3298 n
->ChangeSubTreeCount(num
);
3302 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3306 if (GetOwner()->GetModel()->IsIndexListModel())
3312 m_root
= new wxDataViewTreeNode( NULL
);
3313 m_root
->SetHasChildren(true);
3315 //First we define a invalid item to fetch the top-level elements
3316 wxDataViewItem item
;
3318 BuildTreeHelper( model
, item
, m_root
);
3322 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3324 if( node
->GetNodeNumber() != 0 )
3326 int len
= node
->GetNodeNumber();
3328 wxDataViewTreeNodes nodes
= node
->GetNodes();
3329 for( ; i
< len
; i
++ )
3331 DestroyTreeHelper(nodes
[i
]);
3337 void wxDataViewMainWindow::DestroyTree()
3341 DestroyTreeHelper(m_root
);
3347 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3349 if (event
.GetKeyCode() == WXK_TAB
)
3351 wxNavigationKeyEvent nevent
;
3352 nevent
.SetWindowChange( event
.ControlDown() );
3353 nevent
.SetDirection( !event
.ShiftDown() );
3354 nevent
.SetEventObject( GetParent()->GetParent() );
3355 nevent
.SetCurrentFocus( m_parent
);
3356 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3360 // no item -> nothing to do
3361 if (!HasCurrentRow())
3367 // don't use m_linesPerPage directly as it might not be computed yet
3368 const int pageSize
= GetCountPerPage();
3369 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3371 switch ( event
.GetKeyCode() )
3375 if (m_currentRow
> 0)
3377 wxWindow
*parent
= GetParent();
3378 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3379 le
.SetItem( GetItemByRow(m_currentRow
) );
3380 le
.SetEventObject(parent
);
3381 le
.SetModel(GetOwner()->GetModel());
3383 parent
->GetEventHandler()->ProcessEvent(le
);
3388 if ( m_currentRow
> 0 )
3389 OnArrowChar( m_currentRow
- 1, event
);
3393 if ( m_currentRow
< GetRowCount() - 1 )
3394 OnArrowChar( m_currentRow
+ 1, event
);
3396 //Add the process for tree expanding/collapsing
3398 OnCollapsing(m_currentRow
);
3401 OnExpanding( m_currentRow
);
3405 OnArrowChar( GetRowCount() - 1, event
);
3410 OnArrowChar( 0, event
);
3415 int steps
= pageSize
- 1;
3416 int index
= m_currentRow
- steps
;
3420 OnArrowChar( index
, event
);
3426 int steps
= pageSize
- 1;
3427 unsigned int index
= m_currentRow
+ steps
;
3428 unsigned int count
= GetRowCount();
3429 if ( index
>= count
)
3432 OnArrowChar( index
, event
);
3441 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3443 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3445 // let the base handle mouse wheel events.
3450 int x
= event
.GetX();
3451 int y
= event
.GetY();
3452 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3453 wxDataViewColumn
*col
= NULL
;
3456 unsigned int cols
= GetOwner()->GetColumnCount();
3458 for (i
= 0; i
< cols
; i
++)
3460 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3462 continue; // skip it!
3464 if (x
< xpos
+ c
->GetWidth())
3469 xpos
+= c
->GetWidth();
3474 wxDataViewRenderer
*cell
= col
->GetRenderer();
3475 unsigned int current
= y
/ m_lineHeight
;
3476 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3478 // Unselect all if below the last row ?
3482 //Test whether the mouse is hovered on the tree item button
3484 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3486 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3487 if( node
!=NULL
&& node
->HasChildren() )
3489 int indent
= node
->GetIndentLevel();
3490 indent
= GetOwner()->GetIndent()*indent
;
3491 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3492 if( rect
.Contains( x
, y
) )
3494 //So the mouse is over the expander
3496 if (m_underMouse
&& m_underMouse
!= node
)
3498 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3499 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3501 if (m_underMouse
!= node
)
3503 //wxLogMessage("Do the row: %d", current);
3504 RefreshRow(current
);
3506 m_underMouse
= node
;
3509 if (node
!=NULL
&& !node
->HasChildren())
3514 if (m_underMouse
!= NULL
)
3516 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3517 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3518 m_underMouse
= NULL
;
3522 wxDataViewModel
*model
= GetOwner()->GetModel();
3524 if (event
.Dragging())
3526 if (m_dragCount
== 0)
3528 // we have to report the raw, physical coords as we want to be
3529 // able to call HitTest(event.m_pointDrag) from the user code to
3530 // get the item being dragged
3531 m_dragStart
= event
.GetPosition();
3536 if (m_dragCount
!= 3)
3539 if (event
.LeftIsDown())
3541 // Notify cell about drag
3550 bool forceClick
= false;
3552 if (event
.ButtonDClick())
3554 m_renameTimer
->Stop();
3555 m_lastOnSame
= false;
3558 wxDataViewItem item
= GetItemByRow(current
);
3559 bool ignore_other_columns
=
3560 ((GetOwner()->GetExpanderColumn() != col
) &&
3561 (model
->IsContainer(item
)) &&
3562 (!model
->HasContainerColumns(item
)));
3564 if (event
.LeftDClick())
3566 if ( current
== m_lineLastClicked
)
3568 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3571 model
->GetValue( value
, item
, col
->GetModelColumn() );
3572 cell
->SetValue( value
);
3573 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3574 col
->GetWidth(), m_lineHeight
);
3575 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3580 wxWindow
*parent
= GetParent();
3581 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3583 le
.SetEventObject(parent
);
3584 le
.SetModel(GetOwner()->GetModel());
3586 parent
->GetEventHandler()->ProcessEvent(le
);
3592 // The first click was on another item, so don't interpret this as
3593 // a double click, but as a simple click instead
3600 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3602 // select single line
3603 SelectAllRows( false );
3604 SelectRow( m_lineSelectSingleOnUp
, true );
3607 //Process the event of user clicking the expander
3608 bool expander
= false;
3609 if ((m_root
) && (GetOwner()->GetExpanderColumn() == col
))
3611 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3612 if( node
!=NULL
&& node
->HasChildren() )
3614 int indent
= node
->GetIndentLevel();
3615 indent
= GetOwner()->GetIndent()*indent
;
3616 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3617 if( rect
.Contains( x
, y
) )
3620 if( node
->IsOpen() )
3621 OnCollapsing(current
);
3623 OnExpanding( current
);
3627 //If the user click the expander, we do not do editing even if the column with expander are editable
3628 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3630 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3631 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3633 m_renameTimer
->Start( 100, true );
3637 m_lastOnSame
= false;
3638 m_lineSelectSingleOnUp
= (unsigned int)-1;
3642 // This is necessary, because after a DnD operation in
3643 // from and to ourself, the up event is swallowed by the
3644 // DnD code. So on next non-up event (which means here and
3645 // now) m_lineSelectSingleOnUp should be reset.
3646 m_lineSelectSingleOnUp
= (unsigned int)-1;
3649 if (event
.RightDown())
3651 m_lineBeforeLastClicked
= m_lineLastClicked
;
3652 m_lineLastClicked
= current
;
3654 // If the item is already selected, do not update the selection.
3655 // Multi-selections should not be cleared if a selected item is clicked.
3656 if (!IsRowSelected(current
))
3658 SelectAllRows(false);
3659 ChangeCurrentRow(current
);
3660 SelectRow(m_currentRow
,true);
3661 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3664 // notify cell about right click
3667 // Allow generation of context menu event
3670 else if (event
.MiddleDown())
3672 // notify cell about middle click
3675 if (event
.LeftDown() || forceClick
)
3679 m_lineBeforeLastClicked
= m_lineLastClicked
;
3680 m_lineLastClicked
= current
;
3682 unsigned int oldCurrentRow
= m_currentRow
;
3683 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3685 bool cmdModifierDown
= event
.CmdDown();
3686 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3688 if ( IsSingleSel() || !IsRowSelected(current
) )
3690 SelectAllRows( false );
3691 ChangeCurrentRow(current
);
3692 SelectRow(m_currentRow
,true);
3693 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3695 else // multi sel & current is highlighted & no mod keys
3697 m_lineSelectSingleOnUp
= current
;
3698 ChangeCurrentRow(current
); // change focus
3701 else // multi sel & either ctrl or shift is down
3703 if (cmdModifierDown
)
3705 ChangeCurrentRow(current
);
3706 ReverseRowSelection(m_currentRow
);
3707 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3709 else if (event
.ShiftDown())
3711 ChangeCurrentRow(current
);
3713 unsigned int lineFrom
= oldCurrentRow
,
3716 if ( lineTo
< lineFrom
)
3719 lineFrom
= m_currentRow
;
3722 SelectRows(lineFrom
, lineTo
, true);
3723 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3725 else // !ctrl, !shift
3727 // test in the enclosing if should make it impossible
3728 wxFAIL_MSG( _T("how did we get here?") );
3732 if (m_currentRow
!= oldCurrentRow
)
3733 RefreshRow( oldCurrentRow
);
3735 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3737 // Update selection here...
3740 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3741 (current
== oldCurrentRow
)) && oldWasSelected
;
3745 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3749 if (HasCurrentRow())
3755 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3759 if (HasCurrentRow())
3765 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3767 if( m_selection
.GetCount() != 1 )
3768 return wxDataViewItem();
3770 return GetItemByRow( m_selection
.Item(0));
3773 //-----------------------------------------------------------------------------
3775 //-----------------------------------------------------------------------------
3776 WX_DEFINE_LIST(wxDataViewColumnList
)
3778 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3780 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3781 EVT_SIZE(wxDataViewCtrl::OnSize
)
3784 wxDataViewCtrl::~wxDataViewCtrl()
3787 GetModel()->RemoveNotifier( m_notifier
);
3790 void wxDataViewCtrl::Init()
3795 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3796 const wxPoint
& pos
, const wxSize
& size
,
3797 long style
, const wxValidator
& validator
)
3799 if (!wxControl::Create( parent
, id
, pos
, size
,
3800 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3803 SetInitialSize(size
);
3808 MacSetClipChildren( true ) ;
3811 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3813 if (HasFlag(wxDV_NO_HEADER
))
3814 m_headerArea
= NULL
;
3816 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3818 SetTargetWindow( m_clientArea
);
3820 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3822 sizer
->Add( m_headerArea
, 0, wxGROW
);
3823 sizer
->Add( m_clientArea
, 1, wxGROW
);
3830 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3834 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3837 // we need to process arrows ourselves for scrolling
3838 if ( nMsg
== WM_GETDLGCODE
)
3840 rc
|= DLGC_WANTARROWS
;
3848 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3850 // We need to override OnSize so that our scrolled
3851 // window a) does call Layout() to use sizers for
3852 // positioning the controls but b) does not query
3853 // the sizer for their size and use that for setting
3854 // the scrollable area as set that ourselves by
3855 // calling SetScrollbar() further down.
3862 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3864 if (!wxDataViewCtrlBase::AssociateModel( model
))
3867 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3869 model
->AddNotifier( m_notifier
);
3871 m_clientArea
->DestroyTree();
3873 m_clientArea
->BuildTree(model
);
3875 m_clientArea
->UpdateDisplay();
3880 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3882 if (!wxDataViewCtrlBase::AppendColumn(col
))
3885 m_cols
.Append( col
);
3890 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3892 if (!wxDataViewCtrlBase::PrependColumn(col
))
3895 m_cols
.Insert( col
);
3900 void wxDataViewCtrl::OnColumnChange()
3903 m_headerArea
->UpdateDisplay();
3905 m_clientArea
->UpdateDisplay();
3908 void wxDataViewCtrl::DoSetExpanderColumn()
3910 m_clientArea
->UpdateDisplay();
3913 void wxDataViewCtrl::DoSetIndent()
3915 m_clientArea
->UpdateDisplay();
3918 unsigned int wxDataViewCtrl::GetColumnCount() const
3920 return m_cols
.GetCount();
3923 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3925 wxDataViewColumnList::const_iterator iter
;
3927 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3932 if ((*iter
)->IsHidden())
3939 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3941 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3952 bool wxDataViewCtrl::ClearColumns()
3959 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3961 int ret
= 0, dead
= 0;
3962 int len
= GetColumnCount();
3963 for (int i
=0; i
<len
; i
++)
3965 wxDataViewColumn
* col
= GetColumn(i
);
3966 if (col
->IsHidden())
3968 ret
+= col
->GetWidth();
3971 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3978 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3983 //Selection code with wxDataViewItem as parameters
3984 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3986 return m_clientArea
->GetSelection();
3989 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3992 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3993 int len
= selection
.GetCount();
3994 for( int i
= 0; i
< len
; i
++)
3996 unsigned int row
= selection
[i
];
3997 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4002 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4004 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4005 int len
= sel
.GetCount();
4006 for( int i
= 0; i
< len
; i
++ )
4008 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
4010 selection
.Add( static_cast<unsigned int>(row
) );
4012 m_clientArea
->SetSelections( selection
);
4015 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4017 int row
= m_clientArea
->GetRowByItem( item
);
4020 //Unselect all rows before select another in the single select mode
4021 if (m_clientArea
->IsSingleSel())
4022 m_clientArea
->SelectAllRows(false);
4023 m_clientArea
->SelectRow(row
, true);
4027 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4029 int row
= m_clientArea
->GetRowByItem( item
);
4031 m_clientArea
->SelectRow(row
, false);
4034 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4036 int row
= m_clientArea
->GetRowByItem( item
);
4039 return m_clientArea
->IsRowSelected(row
);
4044 //Selection code with row number as parameter
4045 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4048 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4049 int len
= selection
.GetCount();
4050 for( int i
= 0; i
< len
; i
++)
4052 unsigned int row
= selection
[i
];
4058 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4060 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
4061 int len
= sel
.GetCount();
4062 for( int i
= 0; i
< len
; i
++ )
4066 selection
.Add( static_cast<unsigned int>(row
) );
4068 m_clientArea
->SetSelections( selection
);
4071 void wxDataViewCtrl::Select( int row
)
4075 if (m_clientArea
->IsSingleSel())
4076 m_clientArea
->SelectAllRows(false);
4077 m_clientArea
->SelectRow( row
, true );
4081 void wxDataViewCtrl::Unselect( int row
)
4084 m_clientArea
->SelectRow(row
, false);
4087 bool wxDataViewCtrl::IsSelected( int row
) const
4090 return m_clientArea
->IsRowSelected(row
);
4094 void wxDataViewCtrl::SelectRange( int from
, int to
)
4097 for( int i
= from
; i
< to
; i
++ )
4099 m_clientArea
->Select(sel
);
4102 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4104 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4105 for( int i
= from
; i
< to
; i
++ )
4106 if( sel
.Index( i
) != wxNOT_FOUND
)
4108 m_clientArea
->SetSelections(sel
);
4111 void wxDataViewCtrl::SelectAll()
4113 m_clientArea
->SelectAllRows(true);
4116 void wxDataViewCtrl::UnselectAll()
4118 m_clientArea
->SelectAllRows(false);
4121 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4125 if( row
> (int) m_clientArea
->GetRowCount() )
4126 row
= m_clientArea
->GetRowCount();
4128 int first
= m_clientArea
->GetFirstVisibleRow();
4129 int last
= m_clientArea
->GetLastVisibleRow();
4131 m_clientArea
->ScrollTo( row
, column
);
4132 else if( row
> last
)
4133 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4135 m_clientArea
->ScrollTo( first
, column
);
4138 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4140 int row
= m_clientArea
->GetRowByItem(item
);
4143 if( column
== NULL
)
4144 EnsureVisible(row
, -1);
4148 int len
= GetColumnCount();
4149 for( int i
= 0; i
< len
; i
++ )
4150 if( GetColumn(i
) == column
)
4155 EnsureVisible( row
, col
);
4161 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4163 m_clientArea
->HitTest(point
, item
, column
);
4166 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4168 return m_clientArea
->GetItemRect(item
, column
);
4171 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4173 return m_clientArea
->GetItemByRow( row
);
4176 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4178 return m_clientArea
->GetRowByItem( item
);
4181 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4183 int row
= m_clientArea
->GetRowByItem( item
);
4185 m_clientArea
->Expand(row
);
4188 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4190 int row
= m_clientArea
->GetRowByItem( item
);
4192 m_clientArea
->Collapse(row
);
4196 // !wxUSE_GENERICDATAVIEWCTRL
4199 // wxUSE_DATAVIEWCTRL