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
);
435 g_model
= GetOwner()->GetModel();
436 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
439 if (g_model
->HasDefaultCompare())
447 g_column
= col
->GetModelColumn();
448 g_asending
= col
->IsSortOrderAscending();
451 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
452 wxDataViewCtrl
*GetOwner() { return m_owner
; }
453 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
455 void OnPaint( wxPaintEvent
&event
);
456 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
457 void OnChar( wxKeyEvent
&event
);
458 void OnMouse( wxMouseEvent
&event
);
459 void OnSetFocus( wxFocusEvent
&event
);
460 void OnKillFocus( wxFocusEvent
&event
);
462 void UpdateDisplay();
463 void RecalculateDisplay();
464 void OnInternalIdle();
466 void OnRenameTimer();
468 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
469 void ScrollTo( int rows
, int column
);
471 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
472 void ChangeCurrentRow( unsigned int row
);
474 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
475 bool IsEmpty() { return GetRowCount() == 0; }
477 int GetCountPerPage() const;
478 int GetEndOfLastCol() const;
479 unsigned int GetFirstVisibleRow() const;
480 //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
481 unsigned int GetLastVisibleRow();
482 unsigned int GetRowCount() ;
484 wxDataViewItem
GetSelection() const;
485 wxDataViewSelection
GetSelections(){ return m_selection
; }
486 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
487 void Select( const wxArrayInt
& aSelections
);
488 void SelectAllRows( bool on
);
489 void SelectRow( unsigned int row
, bool on
);
490 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
491 void ReverseRowSelection( unsigned int row
);
492 bool IsRowSelected( unsigned int row
);
493 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
495 void RefreshRow( unsigned int row
);
496 void RefreshRows( unsigned int from
, unsigned int to
);
497 void RefreshRowsAfter( unsigned int firstRow
);
499 // returns the colour to be used for drawing the rules
500 wxColour
GetRuleColour() const
502 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
505 wxRect
GetLineRect( unsigned int row
) const;
507 //Some useful functions for row and item mapping
508 wxDataViewItem
GetItemByRow( unsigned int row
) const;
509 int GetRowByItem( const wxDataViewItem
& item
);
511 //Methods for building the mapping tree
512 void BuildTree( wxDataViewModel
* model
);
514 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
515 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
517 void Expand( unsigned int row
) { OnExpanding( row
); }
518 void Collapse( unsigned int row
) { OnCollapsing( row
); }
520 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
521 //We did not need this temporarily
522 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
524 int RecalculateCount() ;
526 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
527 void OnExpanding( unsigned int row
);
528 void OnCollapsing( unsigned int row
);
530 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
533 wxDataViewCtrl
*m_owner
;
537 wxDataViewColumn
*m_currentCol
;
538 unsigned int m_currentRow
;
539 wxDataViewSelection m_selection
;
541 wxDataViewRenameTimer
*m_renameTimer
;
549 // for double click logic
550 unsigned int m_lineLastClicked
,
551 m_lineBeforeLastClicked
,
552 m_lineSelectSingleOnUp
;
554 // the pen used to draw horiz/vertical rules
557 // the pen used to draw the expander and the lines
560 //This is the tree structure of the model
561 wxDataViewTreeNode
* m_root
;
563 //This is the tree node under the cursor
564 wxDataViewTreeNode
* m_underMouse
;
566 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
567 DECLARE_EVENT_TABLE()
570 // ---------------------------------------------------------
571 // wxGenericDataViewModelNotifier
572 // ---------------------------------------------------------
574 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
577 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
578 { m_mainWindow
= mainWindow
; }
580 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
581 { return m_mainWindow
->ItemAdded( parent
, item
); }
582 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
583 { return m_mainWindow
->ItemDeleted( parent
, item
); }
584 virtual bool ItemChanged( const wxDataViewItem
& item
)
585 { return m_mainWindow
->ItemChanged(item
); }
586 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
587 { return m_mainWindow
->ValueChanged( item
, col
); }
588 virtual bool Cleared()
589 { return m_mainWindow
->Cleared(); }
590 virtual void Resort()
591 { m_mainWindow
->Resort(); }
593 wxDataViewMainWindow
*m_mainWindow
;
596 // ---------------------------------------------------------
597 // wxDataViewRenderer
598 // ---------------------------------------------------------
600 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
602 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
603 wxDataViewCellMode mode
,
605 wxDataViewRendererBase( varianttype
, mode
, align
)
614 wxDataViewRenderer::~wxDataViewRenderer()
620 wxDC
*wxDataViewRenderer::GetDC()
624 if (GetOwner() == NULL
)
626 if (GetOwner()->GetOwner() == NULL
)
628 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
634 // ---------------------------------------------------------
635 // wxDataViewCustomRenderer
636 // ---------------------------------------------------------
638 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
640 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
641 wxDataViewCellMode mode
, int align
) :
642 wxDataViewRenderer( varianttype
, mode
, align
)
646 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
648 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
649 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
650 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
651 view
->GetForegroundColour();
652 dc
->SetTextForeground(col
);
653 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
656 // ---------------------------------------------------------
657 // wxDataViewTextRenderer
658 // ---------------------------------------------------------
660 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
662 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
663 wxDataViewCellMode mode
, int align
) :
664 wxDataViewCustomRenderer( varianttype
, mode
, align
)
668 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
670 m_text
= value
.GetString();
675 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
680 bool wxDataViewTextRenderer::HasEditorCtrl()
685 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
686 wxRect labelRect
, const wxVariant
&value
)
688 return new wxTextCtrl( parent
, wxID_ANY
, value
,
689 wxPoint(labelRect
.x
,labelRect
.y
),
690 wxSize(labelRect
.width
,labelRect
.height
) );
693 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
695 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
696 value
= text
->GetValue();
700 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
702 RenderText( m_text
, 0, cell
, dc
, state
);
706 wxSize
wxDataViewTextRenderer::GetSize() const
708 const wxDataViewCtrl
*view
= GetView();
712 view
->GetTextExtent( m_text
, &x
, &y
);
713 return wxSize( x
, y
);
715 return wxSize(80,20);
718 // ---------------------------------------------------------
719 // wxDataViewTextRendererAttr
720 // ---------------------------------------------------------
722 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
724 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
725 wxDataViewCellMode mode
, int align
) :
726 wxDataViewTextRenderer( varianttype
, mode
, align
)
731 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
738 if (m_attr
.HasColour())
740 colour
= dc
->GetTextForeground();
741 dc
->SetTextForeground( m_attr
.GetColour() );
744 if (m_attr
.GetBold() || m_attr
.GetItalic())
746 font
= dc
->GetFont();
747 wxFont myfont
= font
;
748 if (m_attr
.GetBold())
749 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
750 if (m_attr
.GetItalic())
751 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
752 dc
->SetFont( myfont
);
756 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
761 if (m_attr
.HasColour())
762 dc
->SetTextForeground( colour
);
764 if (m_attr
.GetBold() || m_attr
.GetItalic())
772 // ---------------------------------------------------------
773 // wxDataViewBitmapRenderer
774 // ---------------------------------------------------------
776 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
778 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
779 wxDataViewCellMode mode
, int align
) :
780 wxDataViewCustomRenderer( varianttype
, mode
, align
)
784 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
786 if (value
.GetType() == wxT("wxBitmap"))
788 if (value
.GetType() == wxT("wxIcon"))
794 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
799 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
802 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
803 else if (m_icon
.Ok())
804 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
809 wxSize
wxDataViewBitmapRenderer::GetSize() const
812 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
813 else if (m_icon
.Ok())
814 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
816 return wxSize(16,16);
819 // ---------------------------------------------------------
820 // wxDataViewToggleRenderer
821 // ---------------------------------------------------------
823 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
825 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
826 wxDataViewCellMode mode
, int align
) :
827 wxDataViewCustomRenderer( varianttype
, mode
, align
)
832 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
834 m_toggle
= value
.GetBool();
839 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
844 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
846 // User wxRenderer here
849 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
851 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
856 flags
|= wxCONTROL_CHECKED
;
857 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
858 flags
|= wxCONTROL_DISABLED
;
860 wxRendererNative::Get().DrawCheckBox(
861 GetOwner()->GetOwner(),
869 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
870 wxDataViewModel
*model
,
871 const wxDataViewItem
& item
, unsigned int col
)
873 bool value
= !m_toggle
;
874 wxVariant variant
= value
;
875 model
->SetValue( variant
, item
, col
);
876 model
->ValueChanged( item
, col
);
880 wxSize
wxDataViewToggleRenderer::GetSize() const
882 return wxSize(20,20);
885 // ---------------------------------------------------------
886 // wxDataViewProgressRenderer
887 // ---------------------------------------------------------
889 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
891 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
892 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
893 wxDataViewCustomRenderer( varianttype
, mode
, align
)
899 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
903 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
905 m_value
= (long) value
;
907 if (m_value
< 0) m_value
= 0;
908 if (m_value
> 100) m_value
= 100;
913 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
915 value
= (long) m_value
;
919 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
921 double pct
= (double)m_value
/ 100.0;
923 bar
.width
= (int)(cell
.width
* pct
);
924 dc
->SetPen( *wxTRANSPARENT_PEN
);
925 dc
->SetBrush( *wxBLUE_BRUSH
);
926 dc
->DrawRectangle( bar
);
928 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
929 dc
->SetPen( *wxBLACK_PEN
);
930 dc
->DrawRectangle( cell
);
935 wxSize
wxDataViewProgressRenderer::GetSize() const
937 return wxSize(40,12);
940 // ---------------------------------------------------------
941 // wxDataViewDateRenderer
942 // ---------------------------------------------------------
944 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
946 #if wxUSE_DATE_RENDERER_POPUP
948 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
951 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
952 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
953 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
958 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
959 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
960 sizer
->Add( m_cal
, 1, wxGROW
);
965 void OnCalendar( wxCalendarEvent
&event
);
967 wxCalendarCtrl
*m_cal
;
968 wxDataViewModel
*m_model
;
970 const wxDataViewItem
& m_item
;
973 virtual void OnDismiss()
978 DECLARE_EVENT_TABLE()
981 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
982 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
985 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
987 wxDateTime date
= event
.GetDate();
988 wxVariant value
= date
;
989 m_model
->SetValue( value
, m_item
, m_col
);
990 m_model
->ValueChanged( m_item
, m_col
);
994 #endif // wxUSE_DATE_RENDERER_POPUP
996 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
998 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
999 wxDataViewCellMode mode
, int align
) :
1000 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1004 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1006 m_date
= value
.GetDateTime();
1011 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1017 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1019 wxString tmp
= m_date
.FormatDate();
1020 RenderText( tmp
, 0, cell
, dc
, state
);
1024 wxSize
wxDataViewDateRenderer::GetSize() const
1026 const wxDataViewCtrl
* view
= GetView();
1027 wxString tmp
= m_date
.FormatDate();
1029 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1030 return wxSize(x
,y
+d
);
1033 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1034 const wxDataViewItem
& item
, unsigned int col
)
1037 model
->GetValue( variant
, item
, col
);
1038 wxDateTime value
= variant
.GetDateTime();
1040 #if wxUSE_DATE_RENDERER_POPUP
1041 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1042 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1043 wxPoint pos
= wxGetMousePosition();
1046 popup
->Popup( popup
->m_cal
);
1047 #else // !wxUSE_DATE_RENDERER_POPUP
1048 wxMessageBox(value
.Format());
1049 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1053 // ---------------------------------------------------------
1054 // wxDataViewIconTextRenderer
1055 // ---------------------------------------------------------
1057 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1059 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1060 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1061 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1064 SetAlignment(align
);
1067 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1071 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1077 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1082 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1085 const wxIcon
&icon
= m_value
.GetIcon();
1088 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1089 xoffset
= icon
.GetWidth()+4;
1092 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1097 wxSize
wxDataViewIconTextRenderer::GetSize() const
1099 const wxDataViewCtrl
*view
= GetView();
1100 if (!m_value
.GetText().empty())
1103 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1105 if (m_value
.GetIcon().IsOk())
1106 x
+= m_value
.GetIcon().GetWidth() + 4;
1107 return wxSize( x
, y
);
1109 return wxSize(80,20);
1113 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1114 wxRect
WXUNUSED(labelRect
),
1115 const wxVariant
& WXUNUSED(value
))
1121 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1122 wxVariant
& WXUNUSED(value
))
1127 // ---------------------------------------------------------
1129 // ---------------------------------------------------------
1131 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1133 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1134 unsigned int model_column
,
1135 int width
, wxAlignment align
, int flags
) :
1136 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1138 SetAlignment(align
);
1142 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1145 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1146 unsigned int model_column
,
1147 int width
, wxAlignment align
, int flags
) :
1148 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1150 SetAlignment(align
);
1153 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1156 wxDataViewColumn::~wxDataViewColumn()
1160 void wxDataViewColumn::Init( int width
)
1163 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1167 void wxDataViewColumn::SetResizeable( bool resizeable
)
1170 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1172 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1175 void wxDataViewColumn::SetHidden( bool hidden
)
1178 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1180 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1182 // tell our owner to e.g. update its scrollbars:
1184 GetOwner()->OnColumnChange();
1187 void wxDataViewColumn::SetSortable( bool sortable
)
1190 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1192 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1194 // Update header button
1196 GetOwner()->OnColumnChange();
1199 void wxDataViewColumn::SetSortOrder( bool ascending
)
1201 m_ascending
= ascending
;
1203 // Update header button
1205 GetOwner()->OnColumnChange();
1208 bool wxDataViewColumn::IsSortOrderAscending() const
1213 void wxDataViewColumn::SetInternalWidth( int width
)
1217 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1218 if (m_owner
&& m_owner
->m_clientArea
)
1219 m_owner
->m_clientArea
->RecalculateDisplay();
1222 void wxDataViewColumn::SetWidth( int width
)
1224 m_owner
->m_headerArea
->UpdateDisplay();
1226 SetInternalWidth(width
);
1230 //-----------------------------------------------------------------------------
1231 // wxDataViewHeaderWindowBase
1232 //-----------------------------------------------------------------------------
1234 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1236 wxWindow
*parent
= GetParent();
1237 wxDataViewEvent
le(type
, parent
->GetId());
1239 le
.SetEventObject(parent
);
1241 le
.SetDataViewColumn(GetColumn(n
));
1242 le
.SetModel(GetOwner()->GetModel());
1244 // for events created by wxDataViewHeaderWindow the
1245 // row / value fields are not valid
1247 parent
->GetEventHandler()->ProcessEvent(le
);
1250 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1252 #ifndef HDS_DRAGDROP
1253 #define HDS_DRAGDROP 0x0040
1255 #ifndef HDS_FULLDRAG
1256 #define HDS_FULLDRAG 0x0080
1259 // implemented in msw/listctrl.cpp:
1260 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1262 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1264 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1265 const wxPoint
&pos
, const wxSize
&size
,
1266 const wxString
&name
)
1270 m_scrollOffsetX
= 0;
1271 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this ) + 10;
1273 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1274 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1275 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1276 h
= size
.y
== wxDefaultCoord
? m_buttonHeight
: size
.y
;
1278 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1281 // create the native WC_HEADER window:
1282 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1283 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1286 msStyle
|= WS_VISIBLE
;
1288 m_hWnd
= CreateWindowEx(0,
1299 wxLogLastError(_T("CreateWindowEx"));
1303 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1304 // to call wxDataViewHeaderWindow::MSWOnNotify
1305 SubclassWin(m_hWnd
);
1307 // the following is required to get the default win's font for
1308 // header windows and must be done befor sending the HDM_LAYOUT msg
1314 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1319 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1321 return wxSize(80, m_buttonHeight
);
1324 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1326 // remove old columns
1327 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1328 Header_DeleteItem((HWND
)m_hWnd
, 0);
1330 // add the updated array of columns to the header control
1331 unsigned int cols
= GetOwner()->GetColumnCount();
1332 unsigned int added
= 0;
1333 for (unsigned int i
= 0; i
< cols
; i
++)
1335 wxDataViewColumn
*col
= GetColumn( i
);
1336 if (col
->IsHidden())
1337 continue; // don't add it!
1340 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1341 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1342 hdi
.cxy
= col
->GetWidth();
1343 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1344 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1345 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1347 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1349 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1350 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1351 // VZ: works with 5.81
1352 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1355 // lParam is reserved for application's use:
1356 // we store there the column index to use it later in MSWOnNotify
1357 // (since columns may have been hidden)
1358 hdi
.lParam
= (LPARAM
)i
;
1360 // the native wxMSW implementation of the header window
1361 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1362 // on the right: to correct this effect we make the column
1363 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1365 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1367 switch (col
->GetAlignment())
1370 hdi
.fmt
|= HDF_LEFT
;
1372 case wxALIGN_CENTER
:
1373 case wxALIGN_CENTER_HORIZONTAL
:
1374 hdi
.fmt
|= HDF_CENTER
;
1377 hdi
.fmt
|= HDF_RIGHT
;
1381 // such alignment is not allowed for the column header!
1385 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1386 (WPARAM
)added
, (LPARAM
)&hdi
);
1391 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1395 // NOTE: we don't just return nmHDR->iItem because when there are
1396 // hidden columns, nmHDR->iItem may be different from
1397 // nmHDR->pitem->lParam
1399 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1401 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1406 item
.mask
= HDI_LPARAM
;
1407 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1409 return (unsigned int)item
.lParam
;
1412 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1414 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1416 // is it a message from the header?
1417 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1418 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1420 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1421 switch ( nmhdr
->code
)
1423 case HDN_BEGINTRACK
:
1424 // user has started to resize a column:
1425 // do we need to veto it?
1426 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1434 // user has started to reorder a column
1437 case HDN_ITEMCHANGING
:
1438 if (nmHDR
->pitem
!= NULL
&&
1439 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1441 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1442 if (nmHDR
->pitem
->cxy
< minWidth
)
1444 // do not allow the user to resize this column under
1445 // its minimal width:
1451 case HDN_ITEMCHANGED
: // user is resizing a column
1452 case HDN_ENDTRACK
: // user has finished resizing a column
1453 case HDN_ENDDRAG
: // user has finished reordering a column
1455 // update the width of the modified column:
1456 if (nmHDR
->pitem
!= NULL
&&
1457 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1459 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1460 unsigned int w
= nmHDR
->pitem
->cxy
;
1461 wxDataViewColumn
*col
= GetColumn(idx
);
1463 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1464 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1465 w
-= COLUMN_WIDTH_OFFSET
;
1467 if (w
>= (unsigned)col
->GetMinWidth())
1468 col
->SetInternalWidth(w
);
1474 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1475 wxDataViewModel
* model
= GetOwner()->GetModel();
1477 if(nmHDR
->iButton
== 0)
1479 wxDataViewColumn
*col
= GetColumn(idx
);
1480 if(col
->IsSortable())
1482 if(model
&& m_owner
->GetSortingColumn() == col
)
1484 bool order
= col
->IsSortOrderAscending();
1485 col
->SetSortOrder(!order
);
1489 m_owner
->SetSortingColumn(col
);
1497 wxEventType evt
= nmHDR
->iButton
== 0 ?
1498 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1499 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1500 SendEvent(evt
, idx
);
1506 // NOTE: for some reason (i.e. for a bug in Windows)
1507 // the HDN_ITEMCLICK notification is not sent on
1508 // right clicks, so we need to handle NM_RCLICK
1511 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1512 if (column
!= wxNOT_FOUND
)
1515 item
.mask
= HDI_LPARAM
;
1516 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1518 // 'idx' may be different from 'column' if there are
1519 // hidden columns...
1520 unsigned int idx
= (unsigned int)item
.lParam
;
1521 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1527 case HDN_GETDISPINFOW
:
1528 // see wxListCtrl::MSWOnNotify for more info!
1531 case HDN_ITEMDBLCLICK
:
1533 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1534 int w
= GetOwner()->GetBestColumnWidth(idx
);
1536 // update the native control:
1538 ZeroMemory(&hd
, sizeof(hd
));
1539 hd
.mask
= HDI_WIDTH
;
1541 Header_SetItem(GetHwnd(),
1542 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1545 // update the wxDataViewColumn class:
1546 GetColumn(idx
)->SetInternalWidth(w
);
1551 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1557 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1558 const wxRect
* WXUNUSED(rect
))
1560 m_scrollOffsetX
+= dx
;
1562 GetParent()->Layout();
1565 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1569 // TODO: why is there a border + 2px around it?
1570 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1573 #else // !defined(__WXMSW__)
1575 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1576 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1577 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1578 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1579 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1582 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1583 const wxPoint
&pos
, const wxSize
&size
,
1584 const wxString
&name
)
1588 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1591 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1592 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1593 SetOwnForegroundColour( attr
.colFg
);
1594 SetOwnBackgroundColour( attr
.colBg
);
1596 SetOwnFont( attr
.font
);
1598 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1599 // a wxBoxSizer and in order to avoid super-big header windows,
1600 // we need to set our height as fixed
1601 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1602 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1607 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1610 GetClientSize( &w
, &h
);
1612 wxAutoBufferedPaintDC
dc( this );
1614 dc
.SetBackground(GetBackgroundColour());
1618 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1621 m_owner
->GetViewStart( &x
, NULL
);
1623 // account for the horz scrollbar offset
1624 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1626 dc
.SetFont( GetFont() );
1628 unsigned int cols
= GetOwner()->GetColumnCount();
1631 for (i
= 0; i
< cols
; i
++)
1633 wxDataViewColumn
*col
= GetColumn( i
);
1634 if (col
->IsHidden())
1635 continue; // skip it!
1637 int cw
= col
->GetWidth();
1640 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1641 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1643 if (col
->IsSortOrderAscending())
1644 sortArrow
= wxHDR_SORT_ICON_UP
;
1646 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1650 if (m_parent
->IsEnabled())
1652 if ((int) i
== m_hover
)
1653 state
= wxCONTROL_CURRENT
;
1657 state
= (int) wxCONTROL_DISABLED
;
1660 wxRendererNative::Get().DrawHeaderButton
1664 wxRect(xpos
, 0, cw
, ch
-1),
1669 // align as required the column title:
1671 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1672 switch (col
->GetAlignment())
1675 x
+= HEADER_HORIZ_BORDER
;
1678 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1681 case wxALIGN_CENTER
:
1682 case wxALIGN_CENTER_HORIZONTAL
:
1683 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1687 // always center the title vertically:
1688 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1690 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1692 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1693 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1694 dc
.DrawText( col
->GetTitle(), x
, y
);
1695 dc
.DestroyClippingRegion();
1701 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1703 GetParent()->SetFocus();
1707 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1709 // we want to work with logical coords
1711 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1712 int y
= event
.GetY();
1716 // we don't draw the line beyond our window,
1717 // but we allow dragging it there
1719 GetClientSize( &w
, NULL
);
1720 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1723 if (event
.ButtonUp())
1725 m_isDragging
= false;
1731 m_currentX
= wxMax(m_minX
+ 7, x
);
1735 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1737 GetOwner()->Refresh();
1741 else // not dragging
1744 m_column
= wxNOT_FOUND
;
1746 bool hit_border
= false;
1748 // end of the current column
1751 // find the column where this event occured
1752 int countCol
= m_owner
->GetColumnCount();
1753 for (int column
= 0; column
< countCol
; column
++)
1755 wxDataViewColumn
*p
= GetColumn(column
);
1758 continue; // skip if not shown
1760 xpos
+= p
->GetWidth();
1762 if ((abs(x
-xpos
) < 3) && (y
< 22))
1770 // inside the column
1777 int old_hover
= m_hover
;
1779 if (event
.Leaving())
1780 m_hover
= wxNOT_FOUND
;
1781 if (old_hover
!= m_hover
)
1784 if (m_column
== wxNOT_FOUND
)
1787 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1788 if (event
.LeftDClick() && resizeable
)
1790 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1793 else if (event
.LeftDown() || event
.RightUp())
1795 if (hit_border
&& event
.LeftDown() && resizeable
)
1797 m_isDragging
= true;
1801 else // click on a column
1803 wxDataViewModel
* model
= GetOwner()->GetModel();
1804 wxEventType evt
= event
.LeftDown() ?
1805 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1806 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1807 SendEvent(evt
, m_column
);
1809 //Left click the header
1810 if(event
.LeftDown())
1812 wxDataViewColumn
*col
= GetColumn(m_column
);
1813 if(col
->IsSortable())
1815 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1816 if(model
&& sortCol
== col
)
1818 bool order
= col
->IsSortOrderAscending();
1819 col
->SetSortOrder(!order
);
1823 m_owner
->SetSortingColumn(col
);
1829 //Send the column sorted event
1830 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1834 else if (event
.Moving())
1836 if (hit_border
&& resizeable
)
1837 m_currentCursor
= m_resizeCursor
;
1839 m_currentCursor
= wxSTANDARD_CURSOR
;
1841 SetCursor(*m_currentCursor
);
1846 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1850 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1851 m_owner
->GetViewStart( &x
, NULL
);
1853 // shift the DC origin to match the position of the main window horizontal
1854 // scrollbar: this allows us to always use logical coords
1855 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1858 #endif // defined(__WXMSW__)
1860 //-----------------------------------------------------------------------------
1861 // wxDataViewRenameTimer
1862 //-----------------------------------------------------------------------------
1864 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1869 void wxDataViewRenameTimer::Notify()
1871 m_owner
->OnRenameTimer();
1874 //-----------------------------------------------------------------------------
1875 // wxDataViewMainWindow
1876 //-----------------------------------------------------------------------------
1878 //The tree building helper, declared firstly
1879 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1881 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1883 if (row1
> row2
) return 1;
1884 if (row1
== row2
) return 0;
1889 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1891 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1892 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1893 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1894 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1895 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1896 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1899 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1900 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1901 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1902 m_selection( wxDataViewSelectionCmp
)
1907 m_lastOnSame
= false;
1908 m_renameTimer
= new wxDataViewRenameTimer( this );
1910 // TODO: user better initial values/nothing selected
1911 m_currentCol
= NULL
;
1914 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1917 m_dragStart
= wxPoint(0,0);
1918 m_lineLastClicked
= (unsigned int) -1;
1919 m_lineBeforeLastClicked
= (unsigned int) -1;
1920 m_lineSelectSingleOnUp
= (unsigned int) -1;
1924 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1925 SetBackgroundColour( *wxWHITE
);
1927 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1929 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1930 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1931 //Some new added code to deal with the tree structure
1932 m_root
= new wxDataViewTreeNode( NULL
);
1933 m_root
->SetHasChildren(true);
1935 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1937 m_underMouse
= NULL
;
1941 wxDataViewMainWindow::~wxDataViewMainWindow()
1944 delete m_renameTimer
;
1947 void wxDataViewMainWindow::OnRenameTimer()
1949 // We have to call this here because changes may just have
1950 // been made and no screen update taken place.
1955 unsigned int cols
= GetOwner()->GetColumnCount();
1957 for (i
= 0; i
< cols
; i
++)
1959 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1961 continue; // skip it!
1963 if (c
== m_currentCol
)
1965 xpos
+= c
->GetWidth();
1967 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1968 m_currentCol
->GetWidth(), m_lineHeight
);
1970 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1971 &labelRect
.x
, &labelRect
.y
);
1973 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1974 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1978 //------------------------------------------------------------------
1979 // Helper class for do operation on the tree node
1980 //------------------------------------------------------------------
1987 //The return value control how the tree-walker tranverse the tree
1988 // 0: Job done, stop tranverse and return
1989 // 1: Ignore the current node's subtree and continue
1990 // 2: Job not done, continue
1991 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1992 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1993 virtual int operator() ( void * n
) = 0;
1996 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
2001 switch( func( node
) )
2012 wxDataViewTreeNodes nodes
= node
->GetNodes();
2013 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2015 int len_nodes
= nodes
.GetCount();
2016 int len
= leaves
.GetCount();
2017 int i
= 0, nodes_i
= 0;
2019 for( ; i
< len
; i
++ )
2021 void * n
= leaves
[i
];
2022 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2024 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2027 if( Walker( nd
, func
) )
2046 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2050 wxDataViewTreeNode
* node
;
2051 node
= FindNode(parent
);
2056 node
->SetHasChildren( true );
2058 if( g_model
->IsContainer( item
) )
2060 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2061 newnode
->SetItem(item
);
2062 newnode
->SetHasChildren( true );
2063 node
->AddNode( newnode
);
2066 node
->AddLeaf( item
.GetID() );
2068 node
->ChangeSubTreeCount(1);
2076 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2078 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2079 const wxDataViewItem
& item
)
2081 wxDataViewTreeNode
* node
= FindNode(parent
);
2083 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2084 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2087 node
->GetChildren().Remove( item
.GetID() );
2088 //Manuplate selection
2089 if( m_selection
.GetCount() > 1 )
2091 m_selection
.Empty();
2093 bool isContainer
= false;
2094 wxDataViewTreeNodes nds
= node
->GetNodes();
2095 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2097 if (nds
[i
]->GetItem() == item
)
2105 wxDataViewTreeNode
* n
= NULL
;
2106 wxDataViewTreeNodes nodes
= node
->GetNodes();
2107 int len
= nodes
.GetCount();
2108 for( int i
= 0 ; i
< len
; i
++)
2110 if( nodes
[i
]->GetItem() == item
)
2117 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2119 node
->GetNodes().Remove( n
);
2120 sub
-= n
->GetSubTreeCount();
2121 DestroyTreeHelper(n
);
2123 //Make the row number invalid and get a new valid one when user call GetRowCount
2125 node
->ChangeSubTreeCount(sub
);
2126 if( node
->GetChildrenNumber() == 0)
2128 node
->GetParent()->GetNodes().Remove( node
);
2132 //Change the current row to the last row if the current exceed the max row number
2133 if( m_currentRow
> GetRowCount() )
2134 m_currentRow
= m_count
- 1;
2141 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2147 wxWindow
*parent
= GetParent();
2148 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2149 le
.SetEventObject(parent
);
2150 le
.SetModel(GetOwner()->GetModel());
2152 parent
->GetEventHandler()->ProcessEvent(le
);
2157 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2159 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2160 /*#define MAX_VIRTUAL_WIDTH 100000
2162 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2163 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2164 Refresh( true, &rect );
2172 wxWindow
*parent
= GetParent();
2173 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2174 le
.SetEventObject(parent
);
2175 le
.SetModel(GetOwner()->GetModel());
2178 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2179 parent
->GetEventHandler()->ProcessEvent(le
);
2184 bool wxDataViewMainWindow::Cleared()
2194 void wxDataViewMainWindow::UpdateDisplay()
2199 void wxDataViewMainWindow::OnInternalIdle()
2201 wxWindow::OnInternalIdle();
2205 RecalculateDisplay();
2210 void wxDataViewMainWindow::RecalculateDisplay()
2212 wxDataViewModel
*model
= GetOwner()->GetModel();
2219 int width
= GetEndOfLastCol();
2220 int height
= GetRowCount() * m_lineHeight
;
2222 SetVirtualSize( width
, height
);
2223 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2228 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2230 wxWindow::ScrollWindow( dx
, dy
, rect
);
2232 if (GetOwner()->m_headerArea
)
2233 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2236 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2239 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2240 int sy
= rows
*m_lineHeight
/y
;
2244 wxRect rect
= GetClientRect();
2246 int x_start
= 0, x_end
= 0, w
= 0;
2248 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2249 for (x_start
= 0; colnum
< column
; colnum
++)
2251 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2252 if (col
->IsHidden())
2253 continue; // skip it!
2255 w
= col
->GetWidth();
2259 x_end
= x_start
+ w
;
2260 xe
= xx
+ rect
.width
;
2263 sx
= ( xx
+ x_end
- xe
)/x
;
2270 m_owner
->Scroll( sx
, sy
);
2273 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2275 wxDataViewModel
*model
= GetOwner()->GetModel();
2276 wxAutoBufferedPaintDC
dc( this );
2279 dc
.SetBackground(GetBackgroundColour());
2281 GetOwner()->PrepareDC( dc
);
2282 dc
.SetFont( GetFont() );
2284 wxRect update
= GetUpdateRegion().GetBox();
2285 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2287 // compute which items needs to be redrawn
2288 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2289 unsigned int item_count
=
2290 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2291 (int)(GetRowCount( )- item_start
) );
2292 unsigned int item_last
= item_start
+ item_count
;
2294 // compute which columns needs to be redrawn
2295 unsigned int cols
= GetOwner()->GetColumnCount();
2296 unsigned int col_start
= 0;
2297 unsigned int x_start
= 0;
2298 for (x_start
= 0; col_start
< cols
; col_start
++)
2300 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2301 if (col
->IsHidden())
2302 continue; // skip it!
2304 unsigned int w
= col
->GetWidth();
2305 if (x_start
+w
>= (unsigned int)update
.x
)
2311 unsigned int col_last
= col_start
;
2312 unsigned int x_last
= x_start
;
2313 for (; col_last
< cols
; col_last
++)
2315 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2316 if (col
->IsHidden())
2317 continue; // skip it!
2319 if (x_last
> (unsigned int)update
.GetRight())
2322 x_last
+= col
->GetWidth();
2325 // Draw horizontal rules if required
2326 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2328 dc
.SetPen(m_penRule
);
2329 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2331 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2333 int y
= i
* m_lineHeight
;
2334 dc
.DrawLine(x_start
, y
, x_last
, y
);
2338 // Draw vertical rules if required
2339 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2341 dc
.SetPen(m_penRule
);
2342 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2345 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2347 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2348 if (col
->IsHidden())
2349 continue; // skip it
2351 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2352 x
, item_last
* m_lineHeight
);
2354 x
+= col
->GetWidth();
2357 // Draw last vertical rule
2358 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2359 x
, item_last
* m_lineHeight
);
2362 // redraw the background for the items which are selected/current
2363 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2365 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2366 if (selected
|| item
== m_currentRow
)
2368 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2369 if (item
== m_currentRow
)
2370 flags
|= wxCONTROL_CURRENT
;
2372 flags
|= wxCONTROL_FOCUSED
;
2374 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2375 wxRendererNative::Get().DrawItemSelectionRect
2385 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2388 // TODO: last column for RTL support
2389 expander
= GetOwner()->GetColumn( 0 );
2390 GetOwner()->SetExpanderColumn(expander
);
2393 // redraw all cells for all rows which must be repainted and for all columns
2395 cell_rect
.x
= x_start
;
2396 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2397 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2399 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2400 wxDataViewRenderer
*cell
= col
->GetRenderer();
2401 cell_rect
.width
= col
->GetWidth();
2403 if (col
->IsHidden())
2404 continue; // skipt it!
2407 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2409 // get the cell value and set it into the renderer
2411 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2415 wxDataViewItem dataitem
= node
->GetItem();
2417 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2420 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2421 cell
->SetValue( value
);
2423 if (cell
->GetWantsAttr())
2425 wxDataViewItemAttr attr
;
2426 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
2428 cell
->SetAttr( attr
);
2429 cell
->SetHasAttr( ret
);
2432 // update the y offset
2433 cell_rect
.y
= item
* m_lineHeight
;
2435 //Draw the expander here.
2436 int indent
= node
->GetIndentLevel();
2437 if( col
== expander
)
2439 //Calculate the indent first
2440 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2442 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2443 // change the cell_rect.x to the appropriate pos
2444 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2445 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2446 dc
.SetPen( m_penExpander
);
2447 dc
.SetBrush( wxNullBrush
);
2448 if( node
->HasChildren() )
2450 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2452 if (m_underMouse
== node
)
2454 flag
|= wxCONTROL_CURRENT
;
2456 if( node
->IsOpen() )
2457 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2459 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2463 // I am wandering whether we should draw dot lines between tree nodes
2465 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2468 //force the expander column to left-center align
2469 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2473 // cannot be bigger than allocated space
2474 wxSize size
= cell
->GetSize();
2475 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2476 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2477 // size.y = wxMin( size.y, cell_rect.height );
2478 size
.y
= cell_rect
.height
;
2480 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2481 int align
= cell
->GetAlignment();
2483 // horizontal alignment:
2484 item_rect
.x
= cell_rect
.x
;
2485 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2486 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2487 else if (align
& wxALIGN_RIGHT
)
2488 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2489 //else: wxALIGN_LEFT is the default
2491 // vertical alignment:
2492 item_rect
.y
= cell_rect
.y
;
2493 if (align
& wxALIGN_CENTER_VERTICAL
)
2494 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2495 else if (align
& wxALIGN_BOTTOM
)
2496 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2497 //else: wxALIGN_TOP is the default
2500 item_rect
.x
+= PADDING_RIGHTLEFT
;
2501 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2503 //Here we add the tree indent
2504 item_rect
.x
+= indent
;
2507 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2508 state
|= wxDATAVIEW_CELL_SELECTED
;
2510 // TODO: it would be much more efficient to create a clipping
2511 // region for the entire column being rendered (in the OnPaint
2512 // of wxDataViewMainWindow) instead of a single clip region for
2513 // each cell. However it would mean that each renderer should
2514 // respect the given wxRect's top & bottom coords, eventually
2515 // violating only the left & right coords - however the user can
2516 // make its own renderer and thus we cannot be sure of that.
2517 dc
.SetClippingRegion( item_rect
);
2518 cell
->Render( item_rect
, &dc
, state
);
2519 dc
.DestroyClippingRegion();
2522 cell_rect
.x
+= cell_rect
.width
;
2526 int wxDataViewMainWindow::GetCountPerPage() const
2528 wxSize size
= GetClientSize();
2529 return size
.y
/ m_lineHeight
;
2532 int wxDataViewMainWindow::GetEndOfLastCol() const
2536 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2538 const wxDataViewColumn
*c
=
2539 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2542 width
+= c
->GetWidth();
2547 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2551 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2553 return y
/ m_lineHeight
;
2556 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2558 wxSize client_size
= GetClientSize();
2559 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2560 &client_size
.x
, &client_size
.y
);
2562 //we should deal with the pixel here
2563 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2565 return wxMin( GetRowCount()-1, row
);
2568 unsigned int wxDataViewMainWindow::GetRowCount()
2570 if ( m_count
== -1 )
2572 m_count
= RecalculateCount();
2574 GetVirtualSize( &width
, &height
);
2575 height
= m_count
* m_lineHeight
;
2577 SetVirtualSize( width
, height
);
2582 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2589 void wxDataViewMainWindow::SelectAllRows( bool on
)
2596 m_selection
.Clear();
2597 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2598 m_selection
.Add( i
);
2603 unsigned int first_visible
= GetFirstVisibleRow();
2604 unsigned int last_visible
= GetLastVisibleRow();
2606 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2608 unsigned int row
= m_selection
[i
];
2609 if ((row
>= first_visible
) && (row
<= last_visible
))
2612 m_selection
.Clear();
2616 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2618 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2622 m_selection
.Add( row
);
2630 m_selection
.Remove( row
);
2636 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2640 unsigned int tmp
= from
;
2646 for (i
= from
; i
<= to
; i
++)
2648 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2651 m_selection
.Add( i
);
2656 m_selection
.Remove( i
);
2659 RefreshRows( from
, to
);
2662 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2664 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2666 int n
= aSelections
[i
];
2668 m_selection
.Add( n
);
2673 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2675 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2676 m_selection
.Add( row
);
2678 m_selection
.Remove( row
);
2682 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2684 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2687 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2689 wxWindow
*parent
= GetParent();
2690 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2692 le
.SetEventObject(parent
);
2693 le
.SetModel(GetOwner()->GetModel());
2696 parent
->GetEventHandler()->ProcessEvent(le
);
2699 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2701 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2702 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2704 wxSize client_size
= GetClientSize();
2705 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2706 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2707 if (intersect_rect
.width
> 0)
2708 Refresh( true, &intersect_rect
);
2711 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2715 unsigned int tmp
= to
;
2720 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2721 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2723 wxSize client_size
= GetClientSize();
2724 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2725 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2726 if (intersect_rect
.width
> 0)
2727 Refresh( true, &intersect_rect
);
2730 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2732 unsigned int count
= GetRowCount();
2733 if (firstRow
> count
)
2736 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2737 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2739 wxSize client_size
= GetClientSize();
2740 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2741 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2742 if (intersect_rect
.width
> 0)
2743 Refresh( true, &intersect_rect
);
2746 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2748 wxCHECK_RET( newCurrent
< GetRowCount(),
2749 _T("invalid item index in OnArrowChar()") );
2751 // if there is no selection, we cannot move it anywhere
2752 if (!HasCurrentRow())
2755 unsigned int oldCurrent
= m_currentRow
;
2757 // in single selection we just ignore Shift as we can't select several
2759 if ( event
.ShiftDown() && !IsSingleSel() )
2761 RefreshRow( oldCurrent
);
2763 ChangeCurrentRow( newCurrent
);
2765 // select all the items between the old and the new one
2766 if ( oldCurrent
> newCurrent
)
2768 newCurrent
= oldCurrent
;
2769 oldCurrent
= m_currentRow
;
2772 SelectRows( oldCurrent
, newCurrent
, true );
2773 if (oldCurrent
!=newCurrent
)
2774 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2778 RefreshRow( oldCurrent
);
2780 // all previously selected items are unselected unless ctrl is held
2781 if ( !event
.ControlDown() )
2782 SelectAllRows(false);
2784 ChangeCurrentRow( newCurrent
);
2786 if ( !event
.ControlDown() )
2788 SelectRow( m_currentRow
, true );
2789 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2792 RefreshRow( m_currentRow
);
2795 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2798 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2802 rect
.y
= m_lineHeight
* row
;
2803 rect
.width
= GetEndOfLastCol();
2804 rect
.height
= m_lineHeight
;
2809 class RowToItemJob
: public DoJob
2812 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2813 virtual ~RowToItemJob(){};
2815 virtual int operator() ( wxDataViewTreeNode
* node
)
2818 if( current
== static_cast<int>(row
))
2820 ret
= node
->GetItem() ;
2824 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2826 current
+= node
->GetSubTreeCount();
2831 //If the current has no child node, we can find the desired item of the row number directly.
2832 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2833 if( node
->GetNodes().GetCount() == 0)
2835 int index
= static_cast<int>(row
) - current
- 1;
2836 ret
= node
->GetChildren().Item( index
);
2843 virtual int operator() ( void * n
)
2846 if( current
== static_cast<int>(row
))
2848 ret
= wxDataViewItem( n
) ;
2853 wxDataViewItem
GetResult(){ return ret
; }
2860 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2862 RowToItemJob
job( row
, -2 );
2863 Walker( m_root
, job
);
2864 return job
.GetResult();
2867 class RowToTreeNodeJob
: public DoJob
2870 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2873 this->current
= current
;
2877 virtual ~RowToTreeNodeJob(){};
2879 virtual int operator() ( wxDataViewTreeNode
* node
)
2882 if( current
== static_cast<int>(row
))
2888 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2890 current
+= node
->GetSubTreeCount();
2896 //If the current has no child node, we can find the desired item of the row number directly.
2897 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2898 if( node
->GetNodes().GetCount() == 0)
2900 int index
= static_cast<int>(row
) - current
- 1;
2901 void * n
= node
->GetChildren().Item( index
);
2902 ret
= new wxDataViewTreeNode( parent
) ;
2903 ret
->SetItem( wxDataViewItem( n
));
2904 ret
->SetHasChildren(false);
2913 virtual int operator() ( void * n
)
2916 if( current
== static_cast<int>(row
))
2918 ret
= new wxDataViewTreeNode( parent
) ;
2919 ret
->SetItem( wxDataViewItem( n
));
2920 ret
->SetHasChildren(false);
2926 wxDataViewTreeNode
* GetResult(){ return ret
; }
2930 wxDataViewTreeNode
* ret
;
2931 wxDataViewTreeNode
* parent
;
2935 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2937 RowToTreeNodeJob
job( row
, -2, m_root
);
2938 Walker( m_root
, job
);
2939 return job
.GetResult();
2942 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2944 wxWindow
*parent
= GetParent();
2945 wxDataViewEvent
le(type
, parent
->GetId());
2947 le
.SetEventObject(parent
);
2948 le
.SetModel(GetOwner()->GetModel());
2951 parent
->GetEventHandler()->ProcessEvent(le
);
2955 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2957 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2960 if( node
->HasChildren())
2962 if( !node
->IsOpen())
2964 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2965 //Check if the user prevent expanding
2966 if( e
.GetSkipped() )
2970 //Here I build the children of current node
2971 if( node
->GetChildrenNumber() == 0 )
2974 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2978 //Send the expanded event
2979 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2983 SelectRow( row
, false );
2984 SelectRow( row
+ 1, true );
2985 ChangeCurrentRow( row
+ 1 );
2986 SendSelectionChangedEvent( GetItemByRow(row
+1));
2994 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2996 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2999 wxDataViewTreeNode
* nd
= node
;
3001 if( node
->HasChildren() && node
->IsOpen() )
3003 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
3004 if( e
.GetSkipped() )
3009 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3013 node
= node
->GetParent();
3016 int parent
= GetRowByItem( node
->GetItem() ) ;
3019 SelectRow( row
, false);
3020 SelectRow(parent
, true );
3021 ChangeCurrentRow( parent
);
3022 SendSelectionChangedEvent( node
->GetItem() );
3026 if( !nd
->HasChildren())
3031 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3033 wxDataViewModel
* model
= GetOwner()->GetModel();
3037 //Compose the a parent-chain of the finding item
3039 list
.DeleteContents( true );
3040 wxDataViewItem
it( item
);
3043 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3044 list
.Insert( pItem
);
3045 it
= model
->GetParent( it
);
3048 //Find the item along the parent-chain.
3049 //This algorithm is designed to speed up the node-finding method
3050 wxDataViewTreeNode
* node
= m_root
;
3051 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3053 if( node
->HasChildren() )
3055 if( node
->GetChildrenNumber() == 0 )
3058 BuildTreeHelper(model
, node
->GetItem(), node
);
3061 wxDataViewTreeNodes nodes
= node
->GetNodes();
3063 for (; i
< nodes
.GetCount(); i
++)
3065 if (nodes
[i
]->GetItem() == (**iter
))
3071 if (i
== nodes
.GetCount())
3080 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3082 wxDataViewColumn
*col
= NULL
;
3083 unsigned int cols
= GetOwner()->GetColumnCount();
3084 unsigned int colnum
= 0;
3085 unsigned int x_start
= 0;
3087 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3088 for (x_start
= 0; colnum
< cols
; colnum
++)
3090 col
= GetOwner()->GetColumn(colnum
);
3091 if (col
->IsHidden())
3092 continue; // skip it!
3094 unsigned int w
= col
->GetWidth();
3095 if (x_start
+w
>= (unsigned int)x
)
3102 item
= GetItemByRow( y
/m_lineHeight
);
3105 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3107 int row
= GetRowByItem(item
);
3108 int y
= row
*m_lineHeight
;
3109 int h
= m_lineHeight
;
3111 wxDataViewColumn
*col
= NULL
;
3112 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3114 col
= GetOwner()->GetColumn( i
);
3115 x
+= col
->GetWidth();
3116 if( GetOwner()->GetColumn(i
+1) == column
)
3119 int w
= col
->GetWidth();
3120 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3121 return wxRect(x
, y
, w
, h
);
3124 int wxDataViewMainWindow::RecalculateCount()
3126 return m_root
->GetSubTreeCount();
3129 class ItemToRowJob
: public DoJob
3132 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3133 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3134 virtual ~ItemToRowJob(){};
3136 //Maybe binary search will help to speed up this process
3137 virtual int operator() ( wxDataViewTreeNode
* node
)
3140 if( node
->GetItem() == item
)
3145 if( node
->GetItem() == **m_iter
)
3152 ret
+= node
->GetSubTreeCount();
3158 virtual int operator() ( void * n
)
3161 if( n
== item
.GetID() )
3165 //the row number is begin from zero
3166 int GetResult(){ return ret
-1 ; }
3168 ItemList::const_iterator m_iter
;
3169 wxDataViewItem item
;
3174 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3176 wxDataViewModel
* model
= GetOwner()->GetModel();
3183 //Compose the a parent-chain of the finding item
3185 wxDataViewItem
* pItem
= NULL
;
3186 list
.DeleteContents( true );
3187 wxDataViewItem
it( item
);
3190 pItem
= new wxDataViewItem( it
);
3191 list
.Insert( pItem
);
3192 it
= model
->GetParent( it
);
3194 pItem
= new wxDataViewItem( );
3195 list
.Insert( pItem
);
3197 ItemToRowJob
job( item
, list
.begin() );
3198 Walker(m_root
, job
);
3199 return job
.GetResult();
3202 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3204 if( !model
->IsContainer( item
) )
3207 wxDataViewItemArray children
;
3208 unsigned int num
= model
->GetChildren( item
, children
);
3209 unsigned int index
= 0;
3210 while( index
< num
)
3212 if( model
->IsContainer( children
[index
] ) )
3214 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3215 n
->SetItem(children
[index
]);
3216 n
->SetHasChildren( true ) ;
3221 node
->AddLeaf( children
[index
].GetID() );
3225 node
->SetSubTreeCount( num
);
3226 wxDataViewTreeNode
* n
= node
->GetParent();
3228 n
->ChangeSubTreeCount(num
);
3232 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3236 m_root
= new wxDataViewTreeNode( NULL
);
3237 m_root
->SetHasChildren(true);
3239 //First we define a invalid item to fetch the top-level elements
3240 wxDataViewItem item
;
3242 BuildTreeHelper( model
, item
, m_root
);
3246 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3248 if( node
->GetNodeNumber() != 0 )
3250 int len
= node
->GetNodeNumber();
3252 wxDataViewTreeNodes nodes
= node
->GetNodes();
3253 for( ; i
< len
; i
++ )
3255 DestroyTreeHelper(nodes
[i
]);
3261 void wxDataViewMainWindow::DestroyTree()
3265 DestroyTreeHelper(m_root
);
3271 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3273 if (event
.GetKeyCode() == WXK_TAB
)
3275 wxNavigationKeyEvent nevent
;
3276 nevent
.SetWindowChange( event
.ControlDown() );
3277 nevent
.SetDirection( !event
.ShiftDown() );
3278 nevent
.SetEventObject( GetParent()->GetParent() );
3279 nevent
.SetCurrentFocus( m_parent
);
3280 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3284 // no item -> nothing to do
3285 if (!HasCurrentRow())
3291 // don't use m_linesPerPage directly as it might not be computed yet
3292 const int pageSize
= GetCountPerPage();
3293 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3295 switch ( event
.GetKeyCode() )
3299 if (m_currentRow
> 0)
3301 wxWindow
*parent
= GetParent();
3302 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3303 le
.SetItem( GetItemByRow(m_currentRow
) );
3304 le
.SetEventObject(parent
);
3305 le
.SetModel(GetOwner()->GetModel());
3307 parent
->GetEventHandler()->ProcessEvent(le
);
3312 if ( m_currentRow
> 0 )
3313 OnArrowChar( m_currentRow
- 1, event
);
3317 if ( m_currentRow
< GetRowCount() - 1 )
3318 OnArrowChar( m_currentRow
+ 1, event
);
3320 //Add the process for tree expanding/collapsing
3322 OnCollapsing(m_currentRow
);
3325 OnExpanding( m_currentRow
);
3329 OnArrowChar( GetRowCount() - 1, event
);
3334 OnArrowChar( 0, event
);
3339 int steps
= pageSize
- 1;
3340 int index
= m_currentRow
- steps
;
3344 OnArrowChar( index
, event
);
3350 int steps
= pageSize
- 1;
3351 unsigned int index
= m_currentRow
+ steps
;
3352 unsigned int count
= GetRowCount();
3353 if ( index
>= count
)
3356 OnArrowChar( index
, event
);
3365 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3367 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3369 // let the base handle mouse wheel events.
3374 int x
= event
.GetX();
3375 int y
= event
.GetY();
3376 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3377 wxDataViewColumn
*col
= NULL
;
3380 unsigned int cols
= GetOwner()->GetColumnCount();
3382 for (i
= 0; i
< cols
; i
++)
3384 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3386 continue; // skip it!
3388 if (x
< xpos
+ c
->GetWidth())
3393 xpos
+= c
->GetWidth();
3398 wxDataViewRenderer
*cell
= col
->GetRenderer();
3399 unsigned int current
= y
/ m_lineHeight
;
3400 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3402 // Unselect all if below the last row ?
3406 //Test whether the mouse is hovered on the tree item button
3408 if (GetOwner()->GetExpanderColumn() == col
)
3410 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3411 if( node
!=NULL
&& node
->HasChildren() )
3413 int indent
= node
->GetIndentLevel();
3414 indent
= GetOwner()->GetIndent()*indent
;
3415 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3416 if( rect
.Contains( x
, y
) )
3418 //So the mouse is over the expander
3420 if (m_underMouse
&& m_underMouse
!= node
)
3422 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3423 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3425 if (m_underMouse
!= node
)
3427 //wxLogMessage("Do the row: %d", current);
3428 RefreshRow(current
);
3430 m_underMouse
= node
;
3433 if (node
!=NULL
&& !node
->HasChildren())
3438 if (m_underMouse
!= NULL
)
3440 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3441 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3442 m_underMouse
= NULL
;
3446 wxDataViewModel
*model
= GetOwner()->GetModel();
3448 if (event
.Dragging())
3450 if (m_dragCount
== 0)
3452 // we have to report the raw, physical coords as we want to be
3453 // able to call HitTest(event.m_pointDrag) from the user code to
3454 // get the item being dragged
3455 m_dragStart
= event
.GetPosition();
3460 if (m_dragCount
!= 3)
3463 if (event
.LeftIsDown())
3465 // Notify cell about drag
3474 bool forceClick
= false;
3476 if (event
.ButtonDClick())
3478 m_renameTimer
->Stop();
3479 m_lastOnSame
= false;
3482 wxDataViewItem item
= GetItemByRow(current
);
3483 bool ignore_other_columns
=
3484 ((GetOwner()->GetExpanderColumn() != col
) &&
3485 (model
->IsContainer(item
)) &&
3486 (!model
->HasContainerColumns(item
)));
3488 if (event
.LeftDClick())
3490 if ( current
== m_lineLastClicked
)
3492 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3495 model
->GetValue( value
, item
, col
->GetModelColumn() );
3496 cell
->SetValue( value
);
3497 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3498 col
->GetWidth(), m_lineHeight
);
3499 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3504 wxWindow
*parent
= GetParent();
3505 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3507 le
.SetEventObject(parent
);
3508 le
.SetModel(GetOwner()->GetModel());
3510 parent
->GetEventHandler()->ProcessEvent(le
);
3516 // The first click was on another item, so don't interpret this as
3517 // a double click, but as a simple click instead
3524 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3526 // select single line
3527 SelectAllRows( false );
3528 SelectRow( m_lineSelectSingleOnUp
, true );
3531 //Process the event of user clicking the expander
3532 bool expander
= false;
3533 if (GetOwner()->GetExpanderColumn() == col
)
3535 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3536 if( node
!=NULL
&& node
->HasChildren() )
3538 int indent
= node
->GetIndentLevel();
3539 indent
= GetOwner()->GetIndent()*indent
;
3540 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3541 if( rect
.Contains( x
, y
) )
3544 if( node
->IsOpen() )
3545 OnCollapsing(current
);
3547 OnExpanding( current
);
3551 //If the user click the expander, we do not do editing even if the column with expander are editable
3552 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3554 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3555 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3557 m_renameTimer
->Start( 100, true );
3561 m_lastOnSame
= false;
3562 m_lineSelectSingleOnUp
= (unsigned int)-1;
3566 // This is necessary, because after a DnD operation in
3567 // from and to ourself, the up event is swallowed by the
3568 // DnD code. So on next non-up event (which means here and
3569 // now) m_lineSelectSingleOnUp should be reset.
3570 m_lineSelectSingleOnUp
= (unsigned int)-1;
3573 if (event
.RightDown())
3575 m_lineBeforeLastClicked
= m_lineLastClicked
;
3576 m_lineLastClicked
= current
;
3578 // If the item is already selected, do not update the selection.
3579 // Multi-selections should not be cleared if a selected item is clicked.
3580 if (!IsRowSelected(current
))
3582 SelectAllRows(false);
3583 ChangeCurrentRow(current
);
3584 SelectRow(m_currentRow
,true);
3585 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3588 // notify cell about right click
3591 // Allow generation of context menu event
3594 else if (event
.MiddleDown())
3596 // notify cell about middle click
3599 if (event
.LeftDown() || forceClick
)
3603 m_lineBeforeLastClicked
= m_lineLastClicked
;
3604 m_lineLastClicked
= current
;
3606 unsigned int oldCurrentRow
= m_currentRow
;
3607 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3609 bool cmdModifierDown
= event
.CmdDown();
3610 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3612 if ( IsSingleSel() || !IsRowSelected(current
) )
3614 SelectAllRows( false );
3615 ChangeCurrentRow(current
);
3616 SelectRow(m_currentRow
,true);
3617 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3619 else // multi sel & current is highlighted & no mod keys
3621 m_lineSelectSingleOnUp
= current
;
3622 ChangeCurrentRow(current
); // change focus
3625 else // multi sel & either ctrl or shift is down
3627 if (cmdModifierDown
)
3629 ChangeCurrentRow(current
);
3630 ReverseRowSelection(m_currentRow
);
3631 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3633 else if (event
.ShiftDown())
3635 ChangeCurrentRow(current
);
3637 unsigned int lineFrom
= oldCurrentRow
,
3640 if ( lineTo
< lineFrom
)
3643 lineFrom
= m_currentRow
;
3646 SelectRows(lineFrom
, lineTo
, true);
3647 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3649 else // !ctrl, !shift
3651 // test in the enclosing if should make it impossible
3652 wxFAIL_MSG( _T("how did we get here?") );
3656 if (m_currentRow
!= oldCurrentRow
)
3657 RefreshRow( oldCurrentRow
);
3659 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3661 // Update selection here...
3664 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3665 (current
== oldCurrentRow
)) && oldWasSelected
;
3669 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3673 if (HasCurrentRow())
3679 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3683 if (HasCurrentRow())
3689 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3691 if( m_selection
.GetCount() != 1 )
3692 return wxDataViewItem();
3694 return GetItemByRow( m_selection
.Item(0));
3697 //-----------------------------------------------------------------------------
3699 //-----------------------------------------------------------------------------
3700 WX_DEFINE_LIST(wxDataViewColumnList
)
3702 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3704 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3705 EVT_SIZE(wxDataViewCtrl::OnSize
)
3708 wxDataViewCtrl::~wxDataViewCtrl()
3711 GetModel()->RemoveNotifier( m_notifier
);
3714 void wxDataViewCtrl::Init()
3719 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3720 const wxPoint
& pos
, const wxSize
& size
,
3721 long style
, const wxValidator
& validator
)
3723 if (!wxControl::Create( parent
, id
, pos
, size
,
3724 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3727 SetInitialSize(size
);
3732 MacSetClipChildren( true ) ;
3735 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3737 if (HasFlag(wxDV_NO_HEADER
))
3738 m_headerArea
= NULL
;
3740 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3742 SetTargetWindow( m_clientArea
);
3744 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3746 sizer
->Add( m_headerArea
, 0, wxGROW
);
3747 sizer
->Add( m_clientArea
, 1, wxGROW
);
3754 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3758 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3761 // we need to process arrows ourselves for scrolling
3762 if ( nMsg
== WM_GETDLGCODE
)
3764 rc
|= DLGC_WANTARROWS
;
3772 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3774 // We need to override OnSize so that our scrolled
3775 // window a) does call Layout() to use sizers for
3776 // positioning the controls but b) does not query
3777 // the sizer for their size and use that for setting
3778 // the scrollable area as set that ourselves by
3779 // calling SetScrollbar() further down.
3786 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3788 if (!wxDataViewCtrlBase::AssociateModel( model
))
3791 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3793 model
->AddNotifier( m_notifier
);
3795 m_clientArea
->DestroyTree();
3797 m_clientArea
->BuildTree(model
);
3799 m_clientArea
->UpdateDisplay();
3804 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3806 if (!wxDataViewCtrlBase::AppendColumn(col
))
3809 m_cols
.Append( col
);
3814 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3816 if (!wxDataViewCtrlBase::PrependColumn(col
))
3819 m_cols
.Insert( col
);
3824 void wxDataViewCtrl::OnColumnChange()
3827 m_headerArea
->UpdateDisplay();
3829 m_clientArea
->UpdateDisplay();
3832 void wxDataViewCtrl::DoSetExpanderColumn()
3834 m_clientArea
->UpdateDisplay();
3837 void wxDataViewCtrl::DoSetIndent()
3839 m_clientArea
->UpdateDisplay();
3842 unsigned int wxDataViewCtrl::GetColumnCount() const
3844 return m_cols
.GetCount();
3847 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3849 wxDataViewColumnList::const_iterator iter
;
3851 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3856 if ((*iter
)->IsHidden())
3863 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3865 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3876 bool wxDataViewCtrl::ClearColumns()
3883 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3885 int ret
= 0, dead
= 0;
3886 int len
= GetColumnCount();
3887 for (int i
=0; i
<len
; i
++)
3889 wxDataViewColumn
* col
= GetColumn(i
);
3890 if (col
->IsHidden())
3892 ret
+= col
->GetWidth();
3895 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3902 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3907 //Selection code with wxDataViewItem as parameters
3908 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3910 return m_clientArea
->GetSelection();
3913 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3916 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3917 int len
= selection
.GetCount();
3918 for( int i
= 0; i
< len
; i
++)
3920 unsigned int row
= selection
[i
];
3921 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3926 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3928 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3929 int len
= sel
.GetCount();
3930 for( int i
= 0; i
< len
; i
++ )
3932 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3934 selection
.Add( static_cast<unsigned int>(row
) );
3936 m_clientArea
->SetSelections( selection
);
3939 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3941 int row
= m_clientArea
->GetRowByItem( item
);
3944 //Unselect all rows before select another in the single select mode
3945 if (m_clientArea
->IsSingleSel())
3946 m_clientArea
->SelectAllRows(false);
3947 m_clientArea
->SelectRow(row
, true);
3951 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3953 int row
= m_clientArea
->GetRowByItem( item
);
3955 m_clientArea
->SelectRow(row
, false);
3958 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3960 int row
= m_clientArea
->GetRowByItem( item
);
3963 return m_clientArea
->IsRowSelected(row
);
3968 //Selection code with row number as parameter
3969 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3972 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3973 int len
= selection
.GetCount();
3974 for( int i
= 0; i
< len
; i
++)
3976 unsigned int row
= selection
[i
];
3982 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3984 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3985 int len
= sel
.GetCount();
3986 for( int i
= 0; i
< len
; i
++ )
3990 selection
.Add( static_cast<unsigned int>(row
) );
3992 m_clientArea
->SetSelections( selection
);
3995 void wxDataViewCtrl::Select( int row
)
3999 if (m_clientArea
->IsSingleSel())
4000 m_clientArea
->SelectAllRows(false);
4001 m_clientArea
->SelectRow( row
, true );
4005 void wxDataViewCtrl::Unselect( int row
)
4008 m_clientArea
->SelectRow(row
, false);
4011 bool wxDataViewCtrl::IsSelected( int row
) const
4014 return m_clientArea
->IsRowSelected(row
);
4018 void wxDataViewCtrl::SelectRange( int from
, int to
)
4021 for( int i
= from
; i
< to
; i
++ )
4023 m_clientArea
->Select(sel
);
4026 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4028 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4029 for( int i
= from
; i
< to
; i
++ )
4030 if( sel
.Index( i
) != wxNOT_FOUND
)
4032 m_clientArea
->SetSelections(sel
);
4035 void wxDataViewCtrl::SelectAll()
4037 m_clientArea
->SelectAllRows(true);
4040 void wxDataViewCtrl::UnselectAll()
4042 m_clientArea
->SelectAllRows(false);
4045 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4049 if( row
> (int) m_clientArea
->GetRowCount() )
4050 row
= m_clientArea
->GetRowCount();
4052 int first
= m_clientArea
->GetFirstVisibleRow();
4053 int last
= m_clientArea
->GetLastVisibleRow();
4055 m_clientArea
->ScrollTo( row
, column
);
4056 else if( row
> last
)
4057 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4059 m_clientArea
->ScrollTo( first
, column
);
4062 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4064 int row
= m_clientArea
->GetRowByItem(item
);
4067 if( column
== NULL
)
4068 EnsureVisible(row
, -1);
4072 int len
= GetColumnCount();
4073 for( int i
= 0; i
< len
; i
++ )
4074 if( GetColumn(i
) == column
)
4079 EnsureVisible( row
, col
);
4085 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4087 m_clientArea
->HitTest(point
, item
, column
);
4090 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4092 return m_clientArea
->GetItemRect(item
, column
);
4095 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4097 return m_clientArea
->GetItemByRow( row
);
4100 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4102 return m_clientArea
->GetRowByItem( item
);
4105 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4107 int row
= m_clientArea
->GetRowByItem( item
);
4109 m_clientArea
->Expand(row
);
4112 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4114 int row
= m_clientArea
->GetRowByItem( item
);
4116 m_clientArea
->Collapse(row
);
4120 // !wxUSE_GENERICDATAVIEWCTRL
4123 // wxUSE_DATAVIEWCTRL