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 0
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 when the main window gets scrolled
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
146 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
148 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
150 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
151 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
154 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
157 #else // !defined(__WXMSW__)
159 #define HEADER_WINDOW_HEIGHT 25
160 #define HEADER_HORIZ_BORDER 5
161 #define HEADER_VERT_BORDER 3
162 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
164 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
167 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
169 const wxPoint
&pos
= wxDefaultPosition
,
170 const wxSize
&size
= wxDefaultSize
,
171 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
174 Create(parent
, id
, pos
, size
, name
);
177 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
,
179 const wxString
&name
);
181 ~wxGenericDataViewHeaderWindow()
183 delete m_resizeCursor
;
186 virtual void UpdateDisplay() { Refresh(); }
190 void OnPaint( wxPaintEvent
&event
);
191 void OnMouse( wxMouseEvent
&event
);
192 void OnSetFocus( wxFocusEvent
&event
);
197 // vars used for column resizing:
199 wxCursor
*m_resizeCursor
;
200 const wxCursor
*m_currentCursor
;
203 bool m_dirty
; // needs refresh?
204 int m_hover
; // index of the column under the mouse
205 int m_column
; // index of the column being resized
206 int m_currentX
; // divider line position in logical (unscrolled) coords
207 int m_minX
; // minimal position beyond which the divider line
208 // can't be dragged in logical coords
210 // the pen used to draw the current column width drag line
211 // when resizing the columsn
215 // internal utilities:
219 m_currentCursor
= (wxCursor
*) NULL
;
220 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
222 m_isDragging
= false;
225 m_hover
= wxNOT_FOUND
;
226 m_column
= wxNOT_FOUND
;
230 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
231 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
235 void AdjustDC(wxDC
& dc
);
238 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
239 DECLARE_EVENT_TABLE()
242 #endif // defined(__WXMSW__)
244 //-----------------------------------------------------------------------------
245 // wxDataViewRenameTimer
246 //-----------------------------------------------------------------------------
248 class wxDataViewRenameTimer
: public wxTimer
251 wxDataViewMainWindow
*m_owner
;
254 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
258 //-----------------------------------------------------------------------------
259 // wxDataViewTreeNode
260 //-----------------------------------------------------------------------------
261 class wxDataViewTreeNode
;
262 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
263 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
265 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
266 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
268 class wxDataViewTreeNode
271 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
272 { this->parent
= parent
;
280 //I don't know what I need to do in the destructure
281 ~wxDataViewTreeNode()
286 wxDataViewTreeNode
* GetParent() { return parent
; }
287 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
288 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
289 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
291 void AddNode( wxDataViewTreeNode
* node
)
293 leaves
.Add( node
->GetItem().GetID() );
295 leaves
.Sort( &wxGenericTreeModelItemCmp
);
298 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
300 void AddLeaf( void * leaf
)
304 leaves
.Sort( &wxGenericTreeModelItemCmp
);
307 wxDataViewItem
& GetItem() { return item
; }
308 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
310 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
311 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
315 wxDataViewTreeNode
* node
= this;
316 while( node
->GetParent()->GetParent() != NULL
)
318 node
= node
->GetParent();
331 int len
= nodes
.GetCount();
333 for ( int i
= 0 ;i
< len
; i
++)
334 sum
+= nodes
[i
]->GetSubTreeCount();
336 sum
+= leaves
.GetCount();
339 ChangeSubTreeCount(-sum
);
345 ChangeSubTreeCount(sum
);
348 bool HasChildren() { return hasChildren
; }
349 void SetHasChildren( bool has
){ hasChildren
= has
; }
351 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
352 int GetSubTreeCount() { return subTreeCount
; }
353 void ChangeSubTreeCount( int num
)
359 parent
->ChangeSubTreeCount(num
);
366 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
367 int len
= nodes
.GetCount();
368 for (int i
= 0; i
< len
; i
++)
372 leaves
.Sort( &wxGenericTreeModelItemCmp
);
377 wxDataViewTreeNode
* parent
;
378 wxDataViewTreeNodes nodes
;
379 wxDataViewTreeLeaves leaves
;
386 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
388 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
391 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
393 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
398 //-----------------------------------------------------------------------------
399 // wxDataViewMainWindow
400 //-----------------------------------------------------------------------------
402 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
404 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
405 WX_DEFINE_LIST(ItemList
);
407 class wxDataViewMainWindow
: public wxWindow
410 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
412 const wxPoint
&pos
= wxDefaultPosition
,
413 const wxSize
&size
= wxDefaultSize
,
414 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
415 virtual ~wxDataViewMainWindow();
417 // notifications from wxDataViewModel
418 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
419 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
420 bool ItemChanged( const wxDataViewItem
&item
);
421 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
432 g_model
= GetOwner()->GetModel();
433 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
436 if (g_model
->HasDefaultCompare())
444 g_column
= col
->GetModelColumn();
445 g_asending
= col
->IsSortOrderAscending();
448 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
449 wxDataViewCtrl
*GetOwner() { return m_owner
; }
450 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
452 void OnPaint( wxPaintEvent
&event
);
453 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
454 void OnChar( wxKeyEvent
&event
);
455 void OnMouse( wxMouseEvent
&event
);
456 void OnSetFocus( wxFocusEvent
&event
);
457 void OnKillFocus( wxFocusEvent
&event
);
459 void UpdateDisplay();
460 void RecalculateDisplay();
461 void OnInternalIdle();
463 void OnRenameTimer();
465 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
466 void ScrollTo( int rows
, int column
);
468 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
469 void ChangeCurrentRow( unsigned int row
);
471 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
472 bool IsEmpty() { return GetRowCount() == 0; }
474 int GetCountPerPage() const;
475 int GetEndOfLastCol() const;
476 unsigned int GetFirstVisibleRow() const;
477 //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
478 unsigned int GetLastVisibleRow();
479 unsigned int GetRowCount() ;
481 wxDataViewItem
GetSelection() const;
482 wxDataViewSelection
GetSelections(){ return m_selection
; }
483 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
484 void Select( const wxArrayInt
& aSelections
);
485 void SelectAllRows( bool on
);
486 void SelectRow( unsigned int row
, bool on
);
487 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
488 void ReverseRowSelection( unsigned int row
);
489 bool IsRowSelected( unsigned int row
);
490 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
492 void RefreshRow( unsigned int row
);
493 void RefreshRows( unsigned int from
, unsigned int to
);
494 void RefreshRowsAfter( unsigned int firstRow
);
496 // returns the colour to be used for drawing the rules
497 wxColour
GetRuleColour() const
499 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
502 //void EnsureVisible( unsigned int row );
503 wxRect
GetLineRect( unsigned int row
) const;
505 //Some useful functions for row and item mapping
506 wxDataViewItem
GetItemByRow( unsigned int row
) const;
507 int GetRowByItem( const wxDataViewItem
& item
);
509 //Methods for building the mapping tree
510 void BuildTree( wxDataViewModel
* model
);
512 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
513 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
515 void Expand( unsigned int row
) { OnExpanding( row
); }
516 void Collapse( unsigned int row
) { OnCollapsing( row
); }
518 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
519 //We did not need this temporarily
520 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
522 int RecalculateCount() ;
524 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
525 void OnExpanding( unsigned int row
);
526 void OnCollapsing( unsigned int row
);
528 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
531 wxDataViewCtrl
*m_owner
;
535 wxDataViewColumn
*m_currentCol
;
536 unsigned int m_currentRow
;
537 wxDataViewSelection m_selection
;
539 wxDataViewRenameTimer
*m_renameTimer
;
547 // for double click logic
548 unsigned int m_lineLastClicked
,
549 m_lineBeforeLastClicked
,
550 m_lineSelectSingleOnUp
;
552 // the pen used to draw horiz/vertical rules
555 // the pen used to draw the expander and the lines
558 //This is the tree structure of the model
559 wxDataViewTreeNode
* m_root
;
561 //This is the tree node under the cursor
562 wxDataViewTreeNode
* m_underMouse
;
564 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
565 DECLARE_EVENT_TABLE()
568 // ---------------------------------------------------------
569 // wxGenericDataViewModelNotifier
570 // ---------------------------------------------------------
572 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
575 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
576 { m_mainWindow
= mainWindow
; }
578 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
579 { return m_mainWindow
->ItemAdded( parent
, item
); }
580 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
581 { return m_mainWindow
->ItemDeleted( parent
, item
); }
582 virtual bool ItemChanged( const wxDataViewItem
& item
)
583 { return m_mainWindow
->ItemChanged(item
); }
584 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
585 { return m_mainWindow
->ValueChanged( item
, col
); }
586 virtual bool Cleared()
587 { return m_mainWindow
->Cleared(); }
588 virtual void Resort()
589 { m_mainWindow
->Resort(); }
591 wxDataViewMainWindow
*m_mainWindow
;
594 // ---------------------------------------------------------
595 // wxDataViewRenderer
596 // ---------------------------------------------------------
598 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
600 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
601 wxDataViewCellMode mode
,
603 wxDataViewRendererBase( varianttype
, mode
, align
)
610 wxDataViewRenderer::~wxDataViewRenderer()
616 wxDC
*wxDataViewRenderer::GetDC()
620 if (GetOwner() == NULL
)
622 if (GetOwner()->GetOwner() == NULL
)
624 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
630 // ---------------------------------------------------------
631 // wxDataViewCustomRenderer
632 // ---------------------------------------------------------
634 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
636 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
637 wxDataViewCellMode mode
, int align
) :
638 wxDataViewRenderer( varianttype
, mode
, align
)
642 // ---------------------------------------------------------
643 // wxDataViewTextRenderer
644 // ---------------------------------------------------------
646 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
648 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
649 wxDataViewCellMode mode
, int align
) :
650 wxDataViewCustomRenderer( varianttype
, mode
, align
)
654 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
656 m_text
= value
.GetString();
661 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
666 bool wxDataViewTextRenderer::HasEditorCtrl()
671 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
672 wxRect labelRect
, const wxVariant
&value
)
674 return new wxTextCtrl( parent
, wxID_ANY
, value
,
675 wxPoint(labelRect
.x
,labelRect
.y
),
676 wxSize(labelRect
.width
,labelRect
.height
) );
679 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
681 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
682 value
= text
->GetValue();
686 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
688 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
689 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
690 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
691 view
->GetForegroundColour();
693 dc
->SetTextForeground(col
);
694 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
699 wxSize
wxDataViewTextRenderer::GetSize() const
701 const wxDataViewCtrl
*view
= GetView();
705 view
->GetTextExtent( m_text
, &x
, &y
);
706 return wxSize( x
, y
);
708 return wxSize(80,20);
711 // ---------------------------------------------------------
712 // wxDataViewBitmapRenderer
713 // ---------------------------------------------------------
715 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
717 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
718 wxDataViewCellMode mode
, int align
) :
719 wxDataViewCustomRenderer( varianttype
, mode
, align
)
723 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
725 if (value
.GetType() == wxT("wxBitmap"))
727 if (value
.GetType() == wxT("wxIcon"))
733 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
738 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
741 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
742 else if (m_icon
.Ok())
743 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
748 wxSize
wxDataViewBitmapRenderer::GetSize() const
751 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
752 else if (m_icon
.Ok())
753 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
755 return wxSize(16,16);
758 // ---------------------------------------------------------
759 // wxDataViewToggleRenderer
760 // ---------------------------------------------------------
762 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
764 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
765 wxDataViewCellMode mode
, int align
) :
766 wxDataViewCustomRenderer( varianttype
, mode
, align
)
771 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
773 m_toggle
= value
.GetBool();
778 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
783 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
785 // User wxRenderer here
788 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
790 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
795 flags
|= wxCONTROL_CHECKED
;
796 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
797 flags
|= wxCONTROL_DISABLED
;
799 wxRendererNative::Get().DrawCheckBox(
800 GetOwner()->GetOwner(),
808 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
809 wxDataViewModel
*model
,
810 const wxDataViewItem
& item
, unsigned int col
)
812 bool value
= !m_toggle
;
813 wxVariant variant
= value
;
814 model
->SetValue( variant
, item
, col
);
815 model
->ValueChanged( item
, col
);
819 wxSize
wxDataViewToggleRenderer::GetSize() const
821 return wxSize(20,20);
824 // ---------------------------------------------------------
825 // wxDataViewProgressRenderer
826 // ---------------------------------------------------------
828 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
830 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
831 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
832 wxDataViewCustomRenderer( varianttype
, mode
, align
)
838 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
842 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
844 m_value
= (long) value
;
846 if (m_value
< 0) m_value
= 0;
847 if (m_value
> 100) m_value
= 100;
852 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
854 value
= (long) m_value
;
858 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
860 double pct
= (double)m_value
/ 100.0;
862 bar
.width
= (int)(cell
.width
* pct
);
863 dc
->SetPen( *wxTRANSPARENT_PEN
);
864 dc
->SetBrush( *wxBLUE_BRUSH
);
865 dc
->DrawRectangle( bar
);
867 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
868 dc
->SetPen( *wxBLACK_PEN
);
869 dc
->DrawRectangle( cell
);
874 wxSize
wxDataViewProgressRenderer::GetSize() const
876 return wxSize(40,12);
879 // ---------------------------------------------------------
880 // wxDataViewDateRenderer
881 // ---------------------------------------------------------
883 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
885 #if wxUSE_DATE_RENDERER_POPUP
887 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
890 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
891 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
892 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
897 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
898 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
899 sizer
->Add( m_cal
, 1, wxGROW
);
904 void OnCalendar( wxCalendarEvent
&event
);
906 wxCalendarCtrl
*m_cal
;
907 wxDataViewModel
*m_model
;
909 const wxDataViewItem
& m_item
;
912 virtual void OnDismiss()
917 DECLARE_EVENT_TABLE()
920 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
921 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
924 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
926 wxDateTime date
= event
.GetDate();
927 wxVariant value
= date
;
928 m_model
->SetValue( value
, m_item
, m_col
);
929 m_model
->ValueChanged( m_item
, m_col
);
933 #endif // wxUSE_DATE_RENDERER_POPUP
935 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
937 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
938 wxDataViewCellMode mode
, int align
) :
939 wxDataViewCustomRenderer( varianttype
, mode
, align
)
943 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
945 m_date
= value
.GetDateTime();
950 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
956 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
958 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
959 wxString tmp
= m_date
.FormatDate();
960 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
965 wxSize
wxDataViewDateRenderer::GetSize() const
967 const wxDataViewCtrl
* view
= GetView();
968 wxString tmp
= m_date
.FormatDate();
970 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
971 return wxSize(x
,y
+d
);
974 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
975 const wxDataViewItem
& item
, unsigned int col
)
978 model
->GetValue( variant
, item
, col
);
979 wxDateTime value
= variant
.GetDateTime();
981 #if wxUSE_DATE_RENDERER_POPUP
982 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
983 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
984 wxPoint pos
= wxGetMousePosition();
987 popup
->Popup( popup
->m_cal
);
988 #else // !wxUSE_DATE_RENDERER_POPUP
989 wxMessageBox(value
.Format());
990 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
994 // ---------------------------------------------------------
995 // wxDataViewIconTextRenderer
996 // ---------------------------------------------------------
998 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1000 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1001 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1002 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1005 SetAlignment(align
);
1008 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1012 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1018 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1023 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1025 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1027 dc
->SetFont( view
->GetFont() );
1029 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1030 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1031 view
->GetForegroundColour();
1033 dc
->SetTextForeground(col
);
1035 const wxIcon
&icon
= m_value
.GetIcon();
1038 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1039 cell
.x
+= icon
.GetWidth()+4;
1042 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1047 wxSize
wxDataViewIconTextRenderer::GetSize() const
1049 return wxSize(80,16); // TODO
1052 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1057 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1062 // ---------------------------------------------------------
1064 // ---------------------------------------------------------
1066 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1068 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1069 unsigned int model_column
,
1070 int width
, wxAlignment align
, int flags
) :
1071 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1073 SetAlignment(align
);
1077 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1080 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1081 unsigned int model_column
,
1082 int width
, wxAlignment align
, int flags
) :
1083 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1085 SetAlignment(align
);
1088 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1091 wxDataViewColumn::~wxDataViewColumn()
1095 void wxDataViewColumn::Init( int width
)
1098 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1102 void wxDataViewColumn::SetResizeable( bool resizeable
)
1105 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1107 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1110 void wxDataViewColumn::SetHidden( bool hidden
)
1113 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1115 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1117 // tell our owner to e.g. update its scrollbars:
1119 GetOwner()->OnColumnChange();
1122 void wxDataViewColumn::SetSortable( bool sortable
)
1125 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1127 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1129 // Update header button
1131 GetOwner()->OnColumnChange();
1134 void wxDataViewColumn::SetSortOrder( bool ascending
)
1136 m_ascending
= ascending
;
1138 // Update header button
1140 GetOwner()->OnColumnChange();
1143 bool wxDataViewColumn::IsSortOrderAscending() const
1148 void wxDataViewColumn::SetInternalWidth( int width
)
1152 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1153 if (m_owner
&& m_owner
->m_clientArea
)
1154 m_owner
->m_clientArea
->RecalculateDisplay();
1157 void wxDataViewColumn::SetWidth( int width
)
1159 m_owner
->m_headerArea
->UpdateDisplay();
1161 SetInternalWidth(width
);
1165 //-----------------------------------------------------------------------------
1166 // wxDataViewHeaderWindowBase
1167 //-----------------------------------------------------------------------------
1169 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1171 wxWindow
*parent
= GetParent();
1172 wxDataViewEvent
le(type
, parent
->GetId());
1174 le
.SetEventObject(parent
);
1176 le
.SetDataViewColumn(GetColumn(n
));
1177 le
.SetModel(GetOwner()->GetModel());
1179 // for events created by wxDataViewHeaderWindow the
1180 // row / value fields are not valid
1182 parent
->GetEventHandler()->ProcessEvent(le
);
1185 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1187 // implemented in msw/listctrl.cpp:
1188 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1190 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1192 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1193 const wxPoint
&pos
, const wxSize
&size
,
1194 const wxString
&name
)
1198 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1201 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1202 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1203 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1204 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1206 // create the native WC_HEADER window:
1207 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1208 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1209 m_hWnd
= CreateWindowEx(0,
1220 wxLogLastError(_T("CreateWindowEx"));
1224 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1225 // to call wxDataViewHeaderWindow::MSWOnNotify
1226 SubclassWin(m_hWnd
);
1228 // the following is required to get the default win's font for
1229 // header windows and must be done befor sending the HDM_LAYOUT msg
1236 // Retrieve the bounding rectangle of the parent window's
1237 // client area, and then request size and position values
1238 // from the header control.
1239 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1241 hdl
.prc
= &rcParent
;
1243 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1245 wxLogLastError(_T("SendMessage"));
1249 // Set the size, position, and visibility of the header control.
1250 SetWindowPos((HWND
)m_hWnd
,
1254 wp
.flags
| SWP_SHOWWINDOW
);
1256 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1257 // a wxBoxSizer and in order to avoid super-big header windows,
1258 // we need to set our height as fixed
1259 SetMinSize(wxSize(-1, wp
.cy
));
1260 SetMaxSize(wxSize(-1, wp
.cy
));
1265 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1270 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1272 // remove old columns
1273 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1274 Header_DeleteItem((HWND
)m_hWnd
, 0);
1276 // add the updated array of columns to the header control
1277 unsigned int cols
= GetOwner()->GetColumnCount();
1278 unsigned int added
= 0;
1279 wxDataViewModel
* model
= GetOwner()->GetModel();
1280 for (unsigned int i
= 0; i
< cols
; i
++)
1282 wxDataViewColumn
*col
= GetColumn( i
);
1283 if (col
->IsHidden())
1284 continue; // don't add it!
1287 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1288 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1289 hdi
.cxy
= col
->GetWidth();
1290 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1291 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1292 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1295 if(model
&& m_owner
->GetSortingColumn() == col
)
1297 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1298 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1299 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1303 // lParam is reserved for application's use:
1304 // we store there the column index to use it later in MSWOnNotify
1305 // (since columns may have been hidden)
1306 hdi
.lParam
= (LPARAM
)i
;
1308 // the native wxMSW implementation of the header window
1309 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1310 // on the right: to correct this effect we make the column
1311 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1313 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1315 switch (col
->GetAlignment())
1318 hdi
.fmt
|= HDF_LEFT
;
1320 case wxALIGN_CENTER
:
1321 case wxALIGN_CENTER_HORIZONTAL
:
1322 hdi
.fmt
|= HDF_CENTER
;
1325 hdi
.fmt
|= HDF_RIGHT
;
1329 // such alignment is not allowed for the column header!
1333 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1334 (WPARAM
)added
, (LPARAM
)&hdi
);
1339 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1343 // NOTE: we don't just return nmHDR->iItem because when there are
1344 // hidden columns, nmHDR->iItem may be different from
1345 // nmHDR->pitem->lParam
1347 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1349 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1354 item
.mask
= HDI_LPARAM
;
1355 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1357 return (unsigned int)item
.lParam
;
1360 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1362 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1364 // is it a message from the header?
1365 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1366 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1368 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1369 switch ( nmhdr
->code
)
1371 case HDN_BEGINTRACK
:
1372 // user has started to resize a column:
1373 // do we need to veto it?
1374 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1382 // user has started to reorder a column
1385 case HDN_ITEMCHANGING
:
1386 if (nmHDR
->pitem
!= NULL
&&
1387 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1389 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1390 if (nmHDR
->pitem
->cxy
< minWidth
)
1392 // do not allow the user to resize this column under
1393 // its minimal width:
1399 case HDN_ITEMCHANGED
: // user is resizing a column
1400 case HDN_ENDTRACK
: // user has finished resizing a column
1401 case HDN_ENDDRAG
: // user has finished reordering a column
1403 // update the width of the modified column:
1404 if (nmHDR
->pitem
!= NULL
&&
1405 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1407 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1408 unsigned int w
= nmHDR
->pitem
->cxy
;
1409 wxDataViewColumn
*col
= GetColumn(idx
);
1411 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1412 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1413 w
-= COLUMN_WIDTH_OFFSET
;
1415 if (w
>= (unsigned)col
->GetMinWidth())
1416 col
->SetInternalWidth(w
);
1422 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1423 wxDataViewModel
* model
= GetOwner()->GetModel();
1425 if(nmHDR
->iButton
== 0)
1427 wxDataViewColumn
*col
= GetColumn(idx
);
1428 if(col
->IsSortable())
1430 if(model
&& m_owner
->GetSortingColumn() == col
)
1432 bool order
= col
->IsSortOrderAscending();
1433 col
->SetSortOrder(!order
);
1437 m_owner
->SetSortingColumn(col
);
1445 wxEventType evt
= nmHDR
->iButton
== 0 ?
1446 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1447 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1448 SendEvent(evt
, idx
);
1454 // NOTE: for some reason (i.e. for a bug in Windows)
1455 // the HDN_ITEMCLICK notification is not sent on
1456 // right clicks, so we need to handle NM_RCLICK
1459 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1460 if (column
!= wxNOT_FOUND
)
1463 item
.mask
= HDI_LPARAM
;
1464 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1466 // 'idx' may be different from 'column' if there are
1467 // hidden columns...
1468 unsigned int idx
= (unsigned int)item
.lParam
;
1469 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1475 case HDN_GETDISPINFOW
:
1476 // see wxListCtrl::MSWOnNotify for more info!
1479 case HDN_ITEMDBLCLICK
:
1481 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1482 int w
= GetOwner()->GetBestColumnWidth(idx
);
1484 // update the native control:
1486 ZeroMemory(&hd
, sizeof(hd
));
1487 hd
.mask
= HDI_WIDTH
;
1489 Header_SetItem(GetHwnd(),
1490 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1493 // update the wxDataViewColumn class:
1494 GetColumn(idx
)->SetInternalWidth(w
);
1499 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1505 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1506 const wxRect
*WXUNUSED(rect
))
1508 wxSize ourSz
= GetClientSize();
1509 wxSize ownerSz
= m_owner
->GetClientSize();
1511 // where should the (logical) origin of this window be placed?
1513 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1515 // put this window on top of our parent and
1516 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1517 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1521 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1522 int WXUNUSED(w
), int WXUNUSED(h
),
1525 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1526 // the wxDataViewCtrl window gets resized: the following dummy call
1527 // to ScrollWindow() is required in order to get this header window
1528 // correctly repainted when it's (horizontally) scrolled:
1533 #else // !defined(__WXMSW__)
1535 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1536 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1537 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1538 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1539 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1542 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1543 const wxPoint
&pos
, const wxSize
&size
,
1544 const wxString
&name
)
1548 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1551 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1552 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1553 SetOwnForegroundColour( attr
.colFg
);
1554 SetOwnBackgroundColour( attr
.colBg
);
1556 SetOwnFont( attr
.font
);
1558 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1559 // a wxBoxSizer and in order to avoid super-big header windows,
1560 // we need to set our height as fixed
1561 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1562 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1567 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1570 GetClientSize( &w
, &h
);
1572 wxAutoBufferedPaintDC
dc( this );
1574 dc
.SetBackground(GetBackgroundColour());
1578 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1581 m_owner
->GetViewStart( &x
, NULL
);
1583 // account for the horz scrollbar offset
1584 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1586 dc
.SetFont( GetFont() );
1588 unsigned int cols
= GetOwner()->GetColumnCount();
1591 for (i
= 0; i
< cols
; i
++)
1593 wxDataViewColumn
*col
= GetColumn( i
);
1594 if (col
->IsHidden())
1595 continue; // skip it!
1597 int cw
= col
->GetWidth();
1600 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1601 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1603 if (col
->IsSortOrderAscending())
1604 sortArrow
= wxHDR_SORT_ICON_UP
;
1606 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1610 if (m_parent
->IsEnabled())
1612 if ((int) i
== m_hover
)
1613 state
= wxCONTROL_CURRENT
;
1617 state
= (int) wxCONTROL_DISABLED
;
1620 wxRendererNative::Get().DrawHeaderButton
1624 wxRect(xpos
, 0, cw
, ch
-1),
1629 // align as required the column title:
1631 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1632 switch (col
->GetAlignment())
1635 x
+= HEADER_HORIZ_BORDER
;
1638 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1641 case wxALIGN_CENTER
:
1642 case wxALIGN_CENTER_HORIZONTAL
:
1643 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1647 // always center the title vertically:
1648 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1650 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1652 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1653 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1654 dc
.DrawText( col
->GetTitle(), x
, y
);
1655 dc
.DestroyClippingRegion();
1661 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1663 GetParent()->SetFocus();
1667 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1669 // we want to work with logical coords
1671 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1672 int y
= event
.GetY();
1676 // we don't draw the line beyond our window,
1677 // but we allow dragging it there
1679 GetClientSize( &w
, NULL
);
1680 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1683 if (event
.ButtonUp())
1685 m_isDragging
= false;
1691 m_currentX
= wxMax(m_minX
+ 7, x
);
1695 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1697 GetOwner()->Refresh();
1701 else // not dragging
1704 m_column
= wxNOT_FOUND
;
1706 bool hit_border
= false;
1708 // end of the current column
1711 // find the column where this event occured
1712 int countCol
= m_owner
->GetColumnCount();
1713 for (int column
= 0; column
< countCol
; column
++)
1715 wxDataViewColumn
*p
= GetColumn(column
);
1718 continue; // skip if not shown
1720 xpos
+= p
->GetWidth();
1722 if ((abs(x
-xpos
) < 3) && (y
< 22))
1730 // inside the column
1737 int old_hover
= m_hover
;
1739 if (event
.Leaving())
1740 m_hover
= wxNOT_FOUND
;
1741 if (old_hover
!= m_hover
)
1744 if (m_column
== wxNOT_FOUND
)
1747 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1748 if (event
.LeftDClick() && resizeable
)
1750 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1753 else if (event
.LeftDown() || event
.RightUp())
1755 if (hit_border
&& event
.LeftDown() && resizeable
)
1757 m_isDragging
= true;
1761 else // click on a column
1763 wxDataViewModel
* model
= GetOwner()->GetModel();
1764 wxEventType evt
= event
.LeftDown() ?
1765 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1766 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1767 SendEvent(evt
, m_column
);
1769 //Left click the header
1770 if(event
.LeftDown())
1772 wxDataViewColumn
*col
= GetColumn(m_column
);
1773 if(col
->IsSortable())
1775 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1776 if(model
&& sortCol
== col
)
1778 bool order
= col
->IsSortOrderAscending();
1779 col
->SetSortOrder(!order
);
1783 m_owner
->SetSortingColumn(col
);
1789 //Send the column sorted event
1790 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1794 else if (event
.Moving())
1796 if (hit_border
&& resizeable
)
1797 m_currentCursor
= m_resizeCursor
;
1799 m_currentCursor
= wxSTANDARD_CURSOR
;
1801 SetCursor(*m_currentCursor
);
1806 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1807 void wxGenericDataViewHeaderWindow::DrawCurrent()
1810 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1812 int x1
= m_currentX
;
1814 ClientToScreen (&x1
, &y1
);
1816 int x2
= m_currentX
-1;
1818 ++x2
; // but why ????
1821 m_owner
->GetClientSize( NULL
, &y2
);
1822 m_owner
->ClientToScreen( &x2
, &y2
);
1825 dc
.SetLogicalFunction(wxINVERT
);
1826 dc
.SetPen(m_penCurrent
);
1827 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1829 dc
.DrawLine(x1
, y1
, x2
, y2
);
1833 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1837 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1838 m_owner
->GetViewStart( &x
, NULL
);
1840 // shift the DC origin to match the position of the main window horizontal
1841 // scrollbar: this allows us to always use logical coords
1842 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1845 #endif // defined(__WXMSW__)
1847 //-----------------------------------------------------------------------------
1848 // wxDataViewRenameTimer
1849 //-----------------------------------------------------------------------------
1851 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1856 void wxDataViewRenameTimer::Notify()
1858 m_owner
->OnRenameTimer();
1861 //-----------------------------------------------------------------------------
1862 // wxDataViewMainWindow
1863 //-----------------------------------------------------------------------------
1865 //The tree building helper, declared firstly
1866 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1868 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1870 if (row1
> row2
) return 1;
1871 if (row1
== row2
) return 0;
1876 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1878 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1879 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1880 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1881 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1882 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1883 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1886 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1887 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1888 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1889 m_selection( wxDataViewSelectionCmp
)
1894 m_lastOnSame
= false;
1895 m_renameTimer
= new wxDataViewRenameTimer( this );
1897 // TODO: user better initial values/nothing selected
1898 m_currentCol
= NULL
;
1901 // TODO: we need to calculate this smartly
1910 m_dragStart
= wxPoint(0,0);
1911 m_lineLastClicked
= (unsigned int) -1;
1912 m_lineBeforeLastClicked
= (unsigned int) -1;
1913 m_lineSelectSingleOnUp
= (unsigned int) -1;
1917 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1918 SetBackgroundColour( *wxWHITE
);
1920 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1922 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1923 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1924 //Some new added code to deal with the tree structure
1925 m_root
= new wxDataViewTreeNode( NULL
);
1926 m_root
->SetHasChildren(true);
1928 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1930 m_underMouse
= NULL
;
1934 wxDataViewMainWindow::~wxDataViewMainWindow()
1937 delete m_renameTimer
;
1940 void wxDataViewMainWindow::OnRenameTimer()
1942 // We have to call this here because changes may just have
1943 // been made and no screen update taken place.
1948 unsigned int cols
= GetOwner()->GetColumnCount();
1950 for (i
= 0; i
< cols
; i
++)
1952 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1954 continue; // skip it!
1956 if (c
== m_currentCol
)
1958 xpos
+= c
->GetWidth();
1960 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1961 m_currentCol
->GetWidth(), m_lineHeight
);
1963 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1964 &labelRect
.x
, &labelRect
.y
);
1966 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1967 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1971 //------------------------------------------------------------------
1972 // Helper class for do operation on the tree node
1973 //------------------------------------------------------------------
1980 //The return value control how the tree-walker tranverse the tree
1981 // 0: Job done, stop tranverse and return
1982 // 1: Ignore the current node's subtree and continue
1983 // 2: Job not done, continue
1984 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1985 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1986 virtual int operator() ( void * n
) = 0;
1989 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1994 switch( func( node
) )
2005 wxDataViewTreeNodes nodes
= node
->GetNodes();
2006 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2008 int len_nodes
= nodes
.GetCount();
2009 int len
= leaves
.GetCount();
2010 int i
= 0, nodes_i
= 0;
2012 for( ; i
< len
; i
++ )
2014 void * n
= leaves
[i
];
2015 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2017 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2020 if( Walker( nd
, func
) )
2039 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2043 wxDataViewTreeNode
* node
;
2044 node
= FindNode(parent
);
2049 node
->SetHasChildren( true );
2051 if( g_model
->IsContainer( item
) )
2053 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2054 newnode
->SetItem(item
);
2055 newnode
->SetHasChildren( true );
2056 node
->AddNode( newnode
);
2059 node
->AddLeaf( item
.GetID() );
2061 node
->ChangeSubTreeCount(1);
2069 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2071 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2072 const wxDataViewItem
& item
)
2074 wxDataViewTreeNode
* node
= FindNode(parent
);
2076 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2077 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2080 node
->GetChildren().Remove( item
.GetID() );
2081 //Manuplate selection
2082 if( m_selection
.GetCount() > 1 )
2084 m_selection
.Empty();
2086 bool isContainer
= false;
2087 wxDataViewTreeNodes nds
= node
->GetNodes();
2088 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2090 if (nds
[i
]->GetItem() == item
)
2098 wxDataViewTreeNode
* n
= NULL
;
2099 wxDataViewTreeNodes nodes
= node
->GetNodes();
2100 int len
= nodes
.GetCount();
2101 for( int i
= 0 ; i
< len
; i
++)
2103 if( nodes
[i
]->GetItem() == item
)
2110 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2112 node
->GetNodes().Remove( n
);
2113 sub
-= n
->GetSubTreeCount();
2114 DestroyTreeHelper(n
);
2116 //Make the row number invalid and get a new valid one when user call GetRowCount
2118 node
->ChangeSubTreeCount(sub
);
2119 if( node
->GetChildrenNumber() == 0)
2121 node
->GetParent()->GetNodes().Remove( node
);
2125 //Change the current row to the last row if the current exceed the max row number
2126 if( m_currentRow
> GetRowCount() )
2127 m_currentRow
= m_count
- 1;
2134 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2140 wxWindow
*parent
= GetParent();
2141 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2142 le
.SetEventObject(parent
);
2143 le
.SetModel(GetOwner()->GetModel());
2145 parent
->GetEventHandler()->ProcessEvent(le
);
2150 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2152 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2153 /*#define MAX_VIRTUAL_WIDTH 100000
2155 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2156 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2157 Refresh( true, &rect );
2165 wxWindow
*parent
= GetParent();
2166 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2167 le
.SetEventObject(parent
);
2168 le
.SetModel(GetOwner()->GetModel());
2171 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2172 parent
->GetEventHandler()->ProcessEvent(le
);
2177 bool wxDataViewMainWindow::Cleared()
2187 void wxDataViewMainWindow::UpdateDisplay()
2192 void wxDataViewMainWindow::OnInternalIdle()
2194 wxWindow::OnInternalIdle();
2198 RecalculateDisplay();
2203 void wxDataViewMainWindow::RecalculateDisplay()
2205 wxDataViewModel
*model
= GetOwner()->GetModel();
2212 int width
= GetEndOfLastCol();
2213 int height
= GetRowCount() * m_lineHeight
;
2215 SetVirtualSize( width
, height
);
2216 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2221 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2223 wxWindow::ScrollWindow( dx
, dy
, rect
);
2225 if (GetOwner()->m_headerArea
)
2226 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2229 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2232 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2233 int sy
= rows
*m_lineHeight
/y
;
2237 wxRect rect
= GetClientRect();
2239 int x_start
= 0, x_end
= 0, w
= 0;
2241 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2242 for (x_start
= 0; colnum
< column
; colnum
++)
2244 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2245 if (col
->IsHidden())
2246 continue; // skip it!
2248 w
= col
->GetWidth();
2252 x_end
= x_start
+ w
;
2253 xe
= xx
+ rect
.width
;
2256 sx
= ( xx
+ x_end
- xe
)/x
;
2263 m_owner
->Scroll( sx
, sy
);
2266 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2268 wxDataViewModel
*model
= GetOwner()->GetModel();
2269 wxAutoBufferedPaintDC
dc( this );
2272 dc
.SetBackground(GetBackgroundColour());
2274 GetOwner()->PrepareDC( dc
);
2275 dc
.SetFont( GetFont() );
2277 wxRect update
= GetUpdateRegion().GetBox();
2278 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2280 // compute which items needs to be redrawn
2281 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2282 unsigned int item_count
=
2283 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2284 (int)(GetRowCount( )- item_start
) );
2285 unsigned int item_last
= item_start
+ item_count
;
2287 // compute which columns needs to be redrawn
2288 unsigned int cols
= GetOwner()->GetColumnCount();
2289 unsigned int col_start
= 0;
2290 unsigned int x_start
= 0;
2291 for (x_start
= 0; col_start
< cols
; col_start
++)
2293 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2294 if (col
->IsHidden())
2295 continue; // skip it!
2297 unsigned int w
= col
->GetWidth();
2298 if (x_start
+w
>= (unsigned int)update
.x
)
2304 unsigned int col_last
= col_start
;
2305 unsigned int x_last
= x_start
;
2306 for (; col_last
< cols
; col_last
++)
2308 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2309 if (col
->IsHidden())
2310 continue; // skip it!
2312 if (x_last
> (unsigned int)update
.GetRight())
2315 x_last
+= col
->GetWidth();
2318 // Draw horizontal rules if required
2319 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2321 dc
.SetPen(m_penRule
);
2322 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2324 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2326 int y
= i
* m_lineHeight
;
2327 dc
.DrawLine(x_start
, y
, x_last
, y
);
2331 // Draw vertical rules if required
2332 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2334 dc
.SetPen(m_penRule
);
2335 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2338 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2340 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2341 if (col
->IsHidden())
2342 continue; // skip it
2344 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2345 x
, item_last
* m_lineHeight
);
2347 x
+= col
->GetWidth();
2350 // Draw last vertical rule
2351 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2352 x
, item_last
* m_lineHeight
);
2355 // redraw the background for the items which are selected/current
2356 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2358 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2359 if (selected
|| item
== m_currentRow
)
2361 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2362 if (item
== m_currentRow
)
2363 flags
|= wxCONTROL_CURRENT
;
2365 flags
|= wxCONTROL_FOCUSED
;
2367 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2368 wxRendererNative::Get().DrawItemSelectionRect
2378 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2381 // TODO: last column for RTL support
2382 expander
= GetOwner()->GetColumn( 0 );
2383 GetOwner()->SetExpanderColumn(expander
);
2386 // redraw all cells for all rows which must be repainted and for all columns
2388 cell_rect
.x
= x_start
;
2389 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2390 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2392 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2393 wxDataViewRenderer
*cell
= col
->GetRenderer();
2394 cell_rect
.width
= col
->GetWidth();
2396 if (col
->IsHidden())
2397 continue; // skipt it!
2400 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2402 // get the cell value and set it into the renderer
2404 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2410 wxDataViewItem dataitem
= node
->GetItem();
2411 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2412 cell
->SetValue( value
);
2414 // update the y offset
2415 cell_rect
.y
= item
* m_lineHeight
;
2417 //Draw the expander here.
2418 int indent
= node
->GetIndentLevel();
2419 if( col
== expander
)
2421 //Calculate the indent first
2422 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2424 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2425 // change the cell_rect.x to the appropriate pos
2426 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2427 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2428 dc
.SetPen( m_penExpander
);
2429 dc
.SetBrush( wxNullBrush
);
2430 if( node
->HasChildren() )
2432 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2434 if (m_underMouse
== node
)
2436 flag
|= wxCONTROL_CURRENT
;
2438 if( node
->IsOpen() )
2439 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2441 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2445 // I am wandering whether we should draw dot lines between tree nodes
2447 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2450 //force the expander column to left-center align
2451 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2455 // cannot be bigger than allocated space
2456 wxSize size
= cell
->GetSize();
2457 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2458 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2459 size
.y
= wxMin( size
.y
, cell_rect
.height
);
2461 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2462 int align
= cell
->GetAlignment();
2464 // horizontal alignment:
2465 item_rect
.x
= cell_rect
.x
;
2466 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2467 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2468 else if (align
& wxALIGN_RIGHT
)
2469 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2470 //else: wxALIGN_LEFT is the default
2472 // vertical alignment:
2473 item_rect
.y
= cell_rect
.y
;
2474 if (align
& wxALIGN_CENTER_VERTICAL
)
2475 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2476 else if (align
& wxALIGN_BOTTOM
)
2477 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2478 //else: wxALIGN_TOP is the default
2481 item_rect
.x
+= PADDING_RIGHTLEFT
;
2482 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2484 //Here we add the tree indent
2485 item_rect
.x
+= indent
;
2488 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2489 state
|= wxDATAVIEW_CELL_SELECTED
;
2491 // TODO: it would be much more efficient to create a clipping
2492 // region for the entire column being rendered (in the OnPaint
2493 // of wxDataViewMainWindow) instead of a single clip region for
2494 // each cell. However it would mean that each renderer should
2495 // respect the given wxRect's top & bottom coords, eventually
2496 // violating only the left & right coords - however the user can
2497 // make its own renderer and thus we cannot be sure of that.
2498 dc
.SetClippingRegion( item_rect
);
2499 cell
->Render( item_rect
, &dc
, state
);
2500 dc
.DestroyClippingRegion();
2503 cell_rect
.x
+= cell_rect
.width
;
2507 int wxDataViewMainWindow::GetCountPerPage() const
2509 wxSize size
= GetClientSize();
2510 return size
.y
/ m_lineHeight
;
2513 int wxDataViewMainWindow::GetEndOfLastCol() const
2517 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2519 const wxDataViewColumn
*c
=
2520 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2523 width
+= c
->GetWidth();
2528 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2532 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2534 return y
/ m_lineHeight
;
2537 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2539 wxSize client_size
= GetClientSize();
2540 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2541 &client_size
.x
, &client_size
.y
);
2543 //we should deal with the pixel here
2544 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2545 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2548 return wxMin( GetRowCount()-1, row
);
2551 unsigned int wxDataViewMainWindow::GetRowCount()
2553 if ( m_count
== -1 )
2555 m_count
= RecalculateCount();
2557 GetVirtualSize( &width
, &height
);
2558 height
= m_count
* m_lineHeight
;
2560 SetVirtualSize( width
, height
);
2565 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2572 void wxDataViewMainWindow::SelectAllRows( bool on
)
2579 m_selection
.Clear();
2580 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2581 m_selection
.Add( i
);
2586 unsigned int first_visible
= GetFirstVisibleRow();
2587 unsigned int last_visible
= GetLastVisibleRow();
2589 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2591 unsigned int row
= m_selection
[i
];
2592 if ((row
>= first_visible
) && (row
<= last_visible
))
2595 m_selection
.Clear();
2599 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2601 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2605 m_selection
.Add( row
);
2613 m_selection
.Remove( row
);
2619 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2623 unsigned int tmp
= from
;
2629 for (i
= from
; i
<= to
; i
++)
2631 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2634 m_selection
.Add( i
);
2639 m_selection
.Remove( i
);
2642 RefreshRows( from
, to
);
2645 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2647 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2649 int n
= aSelections
[i
];
2651 m_selection
.Add( n
);
2656 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2658 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2659 m_selection
.Add( row
);
2661 m_selection
.Remove( row
);
2665 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2667 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2670 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2672 wxWindow
*parent
= GetParent();
2673 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2675 le
.SetEventObject(parent
);
2676 le
.SetModel(GetOwner()->GetModel());
2679 parent
->GetEventHandler()->ProcessEvent(le
);
2682 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2684 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2685 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2687 wxSize client_size
= GetClientSize();
2688 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2689 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2690 if (intersect_rect
.width
> 0)
2691 Refresh( true, &intersect_rect
);
2694 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2698 unsigned int tmp
= to
;
2703 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2704 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2706 wxSize client_size
= GetClientSize();
2707 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2708 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2709 if (intersect_rect
.width
> 0)
2710 Refresh( true, &intersect_rect
);
2713 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2715 unsigned int count
= GetRowCount();
2716 if (firstRow
> count
)
2719 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2720 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2722 wxSize client_size
= GetClientSize();
2723 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2724 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2725 if (intersect_rect
.width
> 0)
2726 Refresh( true, &intersect_rect
);
2729 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2731 wxCHECK_RET( newCurrent
< GetRowCount(),
2732 _T("invalid item index in OnArrowChar()") );
2734 // if there is no selection, we cannot move it anywhere
2735 if (!HasCurrentRow())
2738 unsigned int oldCurrent
= m_currentRow
;
2740 // in single selection we just ignore Shift as we can't select several
2742 if ( event
.ShiftDown() && !IsSingleSel() )
2744 RefreshRow( oldCurrent
);
2746 ChangeCurrentRow( newCurrent
);
2748 // select all the items between the old and the new one
2749 if ( oldCurrent
> newCurrent
)
2751 newCurrent
= oldCurrent
;
2752 oldCurrent
= m_currentRow
;
2755 SelectRows( oldCurrent
, newCurrent
, true );
2756 if (oldCurrent
!=newCurrent
)
2757 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2761 RefreshRow( oldCurrent
);
2763 // all previously selected items are unselected unless ctrl is held
2764 if ( !event
.ControlDown() )
2765 SelectAllRows(false);
2767 ChangeCurrentRow( newCurrent
);
2769 if ( !event
.ControlDown() )
2771 SelectRow( m_currentRow
, true );
2772 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2775 RefreshRow( m_currentRow
);
2778 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2781 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2785 rect
.y
= m_lineHeight
* row
;
2786 rect
.width
= GetEndOfLastCol();
2787 rect
.height
= m_lineHeight
;
2792 class RowToItemJob
: public DoJob
2795 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2796 virtual ~RowToItemJob(){};
2798 virtual int operator() ( wxDataViewTreeNode
* node
)
2801 if( current
== static_cast<int>(row
))
2803 ret
= node
->GetItem() ;
2807 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2809 current
+= node
->GetSubTreeCount();
2814 //If the current has no child node, we can find the desired item of the row number directly.
2815 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2816 if( node
->GetNodes().GetCount() == 0)
2818 int index
= static_cast<int>(row
) - current
- 1;
2819 ret
= node
->GetChildren().Item( index
);
2826 virtual int operator() ( void * n
)
2829 if( current
== static_cast<int>(row
))
2831 ret
= wxDataViewItem( n
) ;
2836 wxDataViewItem
GetResult(){ return ret
; }
2843 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2845 RowToItemJob
job( row
, -2 );
2846 Walker( m_root
, job
);
2847 return job
.GetResult();
2850 class RowToTreeNodeJob
: public DoJob
2853 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2856 this->current
= current
;
2860 virtual ~RowToTreeNodeJob(){};
2862 virtual int operator() ( wxDataViewTreeNode
* node
)
2865 if( current
== static_cast<int>(row
))
2871 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2873 current
+= node
->GetSubTreeCount();
2879 //If the current has no child node, we can find the desired item of the row number directly.
2880 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2881 if( node
->GetNodes().GetCount() == 0)
2883 int index
= static_cast<int>(row
) - current
- 1;
2884 void * n
= node
->GetChildren().Item( index
);
2885 ret
= new wxDataViewTreeNode( parent
) ;
2886 ret
->SetItem( wxDataViewItem( n
));
2887 ret
->SetHasChildren(false);
2896 virtual int operator() ( void * n
)
2899 if( current
== static_cast<int>(row
))
2901 ret
= new wxDataViewTreeNode( parent
) ;
2902 ret
->SetItem( wxDataViewItem( n
));
2903 ret
->SetHasChildren(false);
2909 wxDataViewTreeNode
* GetResult(){ return ret
; }
2913 wxDataViewTreeNode
* ret
;
2914 wxDataViewTreeNode
* parent
;
2918 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2920 RowToTreeNodeJob
job( row
, -2, m_root
);
2921 Walker( m_root
, job
);
2922 return job
.GetResult();
2925 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2927 wxWindow
*parent
= GetParent();
2928 wxDataViewEvent
le(type
, parent
->GetId());
2930 le
.SetEventObject(parent
);
2931 le
.SetModel(GetOwner()->GetModel());
2934 parent
->GetEventHandler()->ProcessEvent(le
);
2938 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2940 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2943 if( node
->HasChildren())
2945 if( !node
->IsOpen())
2947 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2948 //Check if the user prevent expanding
2949 if( e
.GetSkipped() )
2953 //Here I build the children of current node
2954 if( node
->GetChildrenNumber() == 0 )
2957 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2961 //Send the expanded event
2962 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2966 SelectRow( row
, false );
2967 SelectRow( row
+ 1, true );
2968 ChangeCurrentRow( row
+ 1 );
2969 SendSelectionChangedEvent( GetItemByRow(row
+1));
2977 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2979 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2982 wxDataViewTreeNode
* nd
= node
;
2984 if( node
->HasChildren() && node
->IsOpen() )
2986 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2987 if( e
.GetSkipped() )
2992 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2996 node
= node
->GetParent();
2999 int parent
= GetRowByItem( node
->GetItem() ) ;
3002 SelectRow( row
, false);
3003 SelectRow(parent
, true );
3004 ChangeCurrentRow( parent
);
3005 SendSelectionChangedEvent( node
->GetItem() );
3009 if( !nd
->HasChildren())
3014 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3016 wxDataViewModel
* model
= GetOwner()->GetModel();
3020 //Compose the a parent-chain of the finding item
3022 list
.DeleteContents( true );
3023 wxDataViewItem
it( item
);
3026 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3027 list
.Insert( pItem
);
3028 it
= model
->GetParent( it
);
3031 //Find the item along the parent-chain.
3032 //This algorithm is designed to speed up the node-finding method
3033 wxDataViewTreeNode
* node
= m_root
;
3034 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3036 if( node
->HasChildren() )
3038 if( node
->GetChildrenNumber() == 0 )
3041 BuildTreeHelper(model
, node
->GetItem(), node
);
3044 wxDataViewTreeNodes nodes
= node
->GetNodes();
3046 for (; i
< nodes
.GetCount(); i
++)
3048 if (nodes
[i
]->GetItem() == (**iter
))
3054 if (i
== nodes
.GetCount())
3063 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3065 wxDataViewColumn
*col
= NULL
;
3066 unsigned int cols
= GetOwner()->GetColumnCount();
3067 unsigned int colnum
= 0;
3068 unsigned int x_start
= 0;
3070 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3071 for (x_start
= 0; colnum
< cols
; colnum
++)
3073 col
= GetOwner()->GetColumn(colnum
);
3074 if (col
->IsHidden())
3075 continue; // skip it!
3077 unsigned int w
= col
->GetWidth();
3078 if (x_start
+w
>= (unsigned int)x
)
3085 item
= GetItemByRow( y
/m_lineHeight
);
3088 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3090 int row
= GetRowByItem(item
);
3091 int y
= row
*m_lineHeight
;
3092 int h
= m_lineHeight
;
3094 wxDataViewColumn
*col
= NULL
;
3095 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3097 col
= GetOwner()->GetColumn( i
);
3098 x
+= col
->GetWidth();
3099 if( GetOwner()->GetColumn(i
+1) == column
)
3102 int w
= col
->GetWidth();
3103 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3104 return wxRect(x
, y
, w
, h
);
3107 int wxDataViewMainWindow::RecalculateCount()
3109 return m_root
->GetSubTreeCount();
3112 class ItemToRowJob
: public DoJob
3115 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3116 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3117 virtual ~ItemToRowJob(){};
3119 //Maybe binary search will help to speed up this process
3120 virtual int operator() ( wxDataViewTreeNode
* node
)
3123 if( node
->GetItem() == item
)
3128 if( node
->GetItem() == **m_iter
)
3135 ret
+= node
->GetSubTreeCount();
3141 virtual int operator() ( void * n
)
3144 if( n
== item
.GetID() )
3148 //the row number is begin from zero
3149 int GetResult(){ return ret
-1 ; }
3151 ItemList::const_iterator m_iter
;
3152 wxDataViewItem item
;
3157 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3159 wxDataViewModel
* model
= GetOwner()->GetModel();
3166 //Compose the a parent-chain of the finding item
3168 wxDataViewItem
* pItem
= NULL
;
3169 list
.DeleteContents( true );
3170 wxDataViewItem
it( item
);
3173 pItem
= new wxDataViewItem( it
);
3174 list
.Insert( pItem
);
3175 it
= model
->GetParent( it
);
3177 pItem
= new wxDataViewItem( );
3178 list
.Insert( pItem
);
3180 ItemToRowJob
job( item
, list
.begin() );
3181 Walker(m_root
, job
);
3182 return job
.GetResult();
3185 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3187 if( !model
->IsContainer( item
) )
3190 wxDataViewItemArray children
;
3191 unsigned int num
= model
->GetChildren( item
, children
);
3192 unsigned int index
= 0;
3193 while( index
< num
)
3195 if( model
->IsContainer( children
[index
] ) )
3197 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3198 n
->SetItem(children
[index
]);
3199 n
->SetHasChildren( true ) ;
3204 node
->AddLeaf( children
[index
].GetID() );
3208 node
->SetSubTreeCount( num
);
3209 wxDataViewTreeNode
* n
= node
->GetParent();
3211 n
->ChangeSubTreeCount(num
);
3215 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3217 //First we define a invalid item to fetch the top-level elements
3218 wxDataViewItem item
;
3220 BuildTreeHelper( model
, item
, m_root
);
3224 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3226 if( node
->GetNodeNumber() != 0 )
3228 int len
= node
->GetNodeNumber();
3230 wxDataViewTreeNodes nodes
= node
->GetNodes();
3231 for( ; i
< len
; i
++ )
3233 DestroyTreeHelper(nodes
[i
]);
3239 void wxDataViewMainWindow::DestroyTree()
3241 DestroyTreeHelper(m_root
);
3242 m_root
->SetSubTreeCount(0);
3246 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3248 if (event
.GetKeyCode() == WXK_TAB
)
3250 wxNavigationKeyEvent nevent
;
3251 nevent
.SetWindowChange( event
.ControlDown() );
3252 nevent
.SetDirection( !event
.ShiftDown() );
3253 nevent
.SetEventObject( GetParent()->GetParent() );
3254 nevent
.SetCurrentFocus( m_parent
);
3255 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3259 // no item -> nothing to do
3260 if (!HasCurrentRow())
3266 // don't use m_linesPerPage directly as it might not be computed yet
3267 const int pageSize
= GetCountPerPage();
3268 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3270 switch ( event
.GetKeyCode() )
3274 if (m_currentRow
> 0)
3276 wxWindow
*parent
= GetParent();
3277 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3278 le
.SetItem( GetItemByRow(m_currentRow
) );
3279 le
.SetEventObject(parent
);
3280 le
.SetModel(GetOwner()->GetModel());
3282 parent
->GetEventHandler()->ProcessEvent(le
);
3287 if ( m_currentRow
> 0 )
3288 OnArrowChar( m_currentRow
- 1, event
);
3292 if ( m_currentRow
< GetRowCount() - 1 )
3293 OnArrowChar( m_currentRow
+ 1, event
);
3295 //Add the process for tree expanding/collapsing
3297 OnCollapsing(m_currentRow
);
3300 OnExpanding( m_currentRow
);
3304 OnArrowChar( GetRowCount() - 1, event
);
3309 OnArrowChar( 0, event
);
3314 int steps
= pageSize
- 1;
3315 int index
= m_currentRow
- steps
;
3319 OnArrowChar( index
, event
);
3325 int steps
= pageSize
- 1;
3326 unsigned int index
= m_currentRow
+ steps
;
3327 unsigned int count
= GetRowCount();
3328 if ( index
>= count
)
3331 OnArrowChar( index
, event
);
3340 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3342 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3344 // let the base handle mouse wheel events.
3349 int x
= event
.GetX();
3350 int y
= event
.GetY();
3351 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3352 wxDataViewColumn
*col
= NULL
;
3355 unsigned int cols
= GetOwner()->GetColumnCount();
3357 for (i
= 0; i
< cols
; i
++)
3359 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3361 continue; // skip it!
3363 if (x
< xpos
+ c
->GetWidth())
3368 xpos
+= c
->GetWidth();
3373 wxDataViewRenderer
*cell
= col
->GetRenderer();
3374 unsigned int current
= y
/ m_lineHeight
;
3375 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3377 // Unselect all if below the last row ?
3381 //Test whether the mouse is hovered on the tree item button
3383 if (GetOwner()->GetExpanderColumn() == col
)
3385 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3386 if( node
!=NULL
&& node
->HasChildren() )
3388 int indent
= node
->GetIndentLevel();
3389 indent
= GetOwner()->GetIndent()*indent
;
3390 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3391 if( rect
.Contains( x
, y
) )
3393 //So the mouse is over the expander
3395 if (m_underMouse
&& m_underMouse
!= node
)
3397 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3398 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3400 if (m_underMouse
!= node
)
3402 //wxLogMessage("Do the row: %d", current);
3405 m_underMouse
= node
;
3408 if (node
!=NULL
&& !node
->HasChildren())
3413 if (m_underMouse
!= NULL
)
3415 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3416 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3417 m_underMouse
= NULL
;
3421 wxDataViewModel
*model
= GetOwner()->GetModel();
3423 if (event
.Dragging())
3425 if (m_dragCount
== 0)
3427 // we have to report the raw, physical coords as we want to be
3428 // able to call HitTest(event.m_pointDrag) from the user code to
3429 // get the item being dragged
3430 m_dragStart
= event
.GetPosition();
3435 if (m_dragCount
!= 3)
3438 if (event
.LeftIsDown())
3440 // Notify cell about drag
3449 bool forceClick
= false;
3451 if (event
.ButtonDClick())
3453 m_renameTimer
->Stop();
3454 m_lastOnSame
= false;
3457 if (event
.LeftDClick())
3459 if ( current
== m_lineLastClicked
)
3461 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3463 wxDataViewItem item
= GetItemByRow(current
);
3465 model
->GetValue( value
, item
, col
->GetModelColumn() );
3466 cell
->SetValue( value
);
3467 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3468 col
->GetWidth(), m_lineHeight
);
3469 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3474 wxWindow
*parent
= GetParent();
3475 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3476 le
.SetItem( GetItemByRow(current
) );
3477 le
.SetEventObject(parent
);
3478 le
.SetModel(GetOwner()->GetModel());
3480 parent
->GetEventHandler()->ProcessEvent(le
);
3486 // The first click was on another item, so don't interpret this as
3487 // a double click, but as a simple click instead
3494 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3496 // select single line
3497 SelectAllRows( false );
3498 SelectRow( m_lineSelectSingleOnUp
, true );
3501 //Process the event of user clicking the expander
3502 bool expander
= false;
3503 if (GetOwner()->GetExpanderColumn() == col
)
3505 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3506 if( node
!=NULL
&& node
->HasChildren() )
3508 int indent
= node
->GetIndentLevel();
3509 indent
= GetOwner()->GetIndent()*indent
;
3510 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3511 if( rect
.Contains( x
, y
) )
3514 if( node
->IsOpen() )
3515 OnCollapsing(current
);
3517 OnExpanding( current
);
3521 //If the user click the expander, we do not do editing even if the column with expander are editable
3522 if (m_lastOnSame
&& !expander
)
3524 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3525 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3527 m_renameTimer
->Start( 100, true );
3531 m_lastOnSame
= false;
3532 m_lineSelectSingleOnUp
= (unsigned int)-1;
3536 // This is necessary, because after a DnD operation in
3537 // from and to ourself, the up event is swallowed by the
3538 // DnD code. So on next non-up event (which means here and
3539 // now) m_lineSelectSingleOnUp should be reset.
3540 m_lineSelectSingleOnUp
= (unsigned int)-1;
3543 if (event
.RightDown())
3545 m_lineBeforeLastClicked
= m_lineLastClicked
;
3546 m_lineLastClicked
= current
;
3548 // If the item is already selected, do not update the selection.
3549 // Multi-selections should not be cleared if a selected item is clicked.
3550 if (!IsRowSelected(current
))
3552 SelectAllRows(false);
3553 ChangeCurrentRow(current
);
3554 SelectRow(m_currentRow
,true);
3555 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3558 // notify cell about right click
3561 // Allow generation of context menu event
3564 else if (event
.MiddleDown())
3566 // notify cell about middle click
3569 if (event
.LeftDown() || forceClick
)
3573 m_lineBeforeLastClicked
= m_lineLastClicked
;
3574 m_lineLastClicked
= current
;
3576 unsigned int oldCurrentRow
= m_currentRow
;
3577 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3579 bool cmdModifierDown
= event
.CmdDown();
3580 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3582 if ( IsSingleSel() || !IsRowSelected(current
) )
3584 SelectAllRows( false );
3585 ChangeCurrentRow(current
);
3586 SelectRow(m_currentRow
,true);
3587 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3589 else // multi sel & current is highlighted & no mod keys
3591 m_lineSelectSingleOnUp
= current
;
3592 ChangeCurrentRow(current
); // change focus
3595 else // multi sel & either ctrl or shift is down
3597 if (cmdModifierDown
)
3599 ChangeCurrentRow(current
);
3600 ReverseRowSelection(m_currentRow
);
3601 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3603 else if (event
.ShiftDown())
3605 ChangeCurrentRow(current
);
3607 unsigned int lineFrom
= oldCurrentRow
,
3610 if ( lineTo
< lineFrom
)
3613 lineFrom
= m_currentRow
;
3616 SelectRows(lineFrom
, lineTo
, true);
3617 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3619 else // !ctrl, !shift
3621 // test in the enclosing if should make it impossible
3622 wxFAIL_MSG( _T("how did we get here?") );
3626 if (m_currentRow
!= oldCurrentRow
)
3627 RefreshRow( oldCurrentRow
);
3629 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3631 // Update selection here...
3634 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3635 (current
== oldCurrentRow
)) && oldWasSelected
;
3639 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3643 if (HasCurrentRow())
3649 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3653 if (HasCurrentRow())
3659 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3661 if( m_selection
.GetCount() != 1 )
3662 return wxDataViewItem();
3664 return GetItemByRow( m_selection
.Item(0));
3667 //-----------------------------------------------------------------------------
3669 //-----------------------------------------------------------------------------
3670 WX_DEFINE_LIST(wxDataViewColumnList
);
3672 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3674 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3675 EVT_SIZE(wxDataViewCtrl::OnSize
)
3678 wxDataViewCtrl::~wxDataViewCtrl()
3681 GetModel()->RemoveNotifier( m_notifier
);
3684 void wxDataViewCtrl::Init()
3689 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3690 const wxPoint
& pos
, const wxSize
& size
,
3691 long style
, const wxValidator
& validator
)
3693 if (!wxControl::Create( parent
, id
, pos
, size
,
3694 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3700 MacSetClipChildren( true ) ;
3703 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3705 if (HasFlag(wxDV_NO_HEADER
))
3706 m_headerArea
= NULL
;
3708 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3710 SetTargetWindow( m_clientArea
);
3712 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3714 sizer
->Add( m_headerArea
, 0, wxGROW
);
3715 sizer
->Add( m_clientArea
, 1, wxGROW
);
3722 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3726 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3729 // we need to process arrows ourselves for scrolling
3730 if ( nMsg
== WM_GETDLGCODE
)
3732 rc
|= DLGC_WANTARROWS
;
3740 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3742 // We need to override OnSize so that our scrolled
3743 // window a) does call Layout() to use sizers for
3744 // positioning the controls but b) does not query
3745 // the sizer for their size and use that for setting
3746 // the scrollable area as set that ourselves by
3747 // calling SetScrollbar() further down.
3754 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3756 if (!wxDataViewCtrlBase::AssociateModel( model
))
3759 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3761 model
->AddNotifier( m_notifier
);
3763 m_clientArea
->BuildTree(model
);
3765 m_clientArea
->UpdateDisplay();
3770 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3772 if (!wxDataViewCtrlBase::AppendColumn(col
))
3775 m_cols
.Append( col
);
3780 void wxDataViewCtrl::OnColumnChange()
3783 m_headerArea
->UpdateDisplay();
3785 m_clientArea
->UpdateDisplay();
3788 void wxDataViewCtrl::DoSetExpanderColumn()
3790 m_clientArea
->UpdateDisplay();
3793 void wxDataViewCtrl::DoSetIndent()
3795 m_clientArea
->UpdateDisplay();
3798 unsigned int wxDataViewCtrl::GetColumnCount() const
3800 return m_cols
.GetCount();
3803 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3805 wxDataViewColumnList::const_iterator iter
;
3807 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3812 if ((*iter
)->IsHidden())
3819 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3821 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3832 bool wxDataViewCtrl::ClearColumns()
3839 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3841 int ret
= 0, dead
= 0;
3842 int len
= GetColumnCount();
3843 for (int i
=0; i
<len
; i
++)
3845 wxDataViewColumn
* col
= GetColumn(i
);
3846 if (col
->IsHidden())
3848 ret
+= col
->GetWidth();
3851 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3858 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3863 //Selection code with wxDataViewItem as parameters
3864 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3866 return m_clientArea
->GetSelection();
3869 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3872 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3873 int len
= selection
.GetCount();
3874 for( int i
= 0; i
< len
; i
++)
3876 unsigned int row
= selection
[i
];
3877 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3882 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3884 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3885 int len
= sel
.GetCount();
3886 for( int i
= 0; i
< len
; i
++ )
3888 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3890 selection
.Add( static_cast<unsigned int>(row
) );
3892 m_clientArea
->SetSelections( selection
);
3895 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3897 int row
= m_clientArea
->GetRowByItem( item
);
3900 //Unselect all rows before select another in the single select mode
3901 if (m_clientArea
->IsSingleSel())
3902 m_clientArea
->SelectAllRows(false);
3903 m_clientArea
->SelectRow(row
, true);
3907 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3909 int row
= m_clientArea
->GetRowByItem( item
);
3911 m_clientArea
->SelectRow(row
, false);
3914 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3916 int row
= m_clientArea
->GetRowByItem( item
);
3919 return m_clientArea
->IsRowSelected(row
);
3924 //Selection code with row number as parameter
3925 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3928 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3929 int len
= selection
.GetCount();
3930 for( int i
= 0; i
< len
; i
++)
3932 unsigned int row
= selection
[i
];
3938 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3940 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3941 int len
= sel
.GetCount();
3942 for( int i
= 0; i
< len
; i
++ )
3946 selection
.Add( static_cast<unsigned int>(row
) );
3948 m_clientArea
->SetSelections( selection
);
3951 void wxDataViewCtrl::Select( int row
)
3955 if (m_clientArea
->IsSingleSel())
3956 m_clientArea
->SelectAllRows(false);
3957 m_clientArea
->SelectRow( row
, true );
3961 void wxDataViewCtrl::Unselect( int row
)
3964 m_clientArea
->SelectRow(row
, false);
3967 bool wxDataViewCtrl::IsSelected( int row
) const
3970 return m_clientArea
->IsRowSelected(row
);
3974 void wxDataViewCtrl::SelectRange( int from
, int to
)
3977 for( int i
= from
; i
< to
; i
++ )
3979 m_clientArea
->Select(sel
);
3982 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3984 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3985 for( int i
= from
; i
< to
; i
++ )
3986 if( sel
.Index( i
) != wxNOT_FOUND
)
3988 m_clientArea
->SetSelections(sel
);
3991 void wxDataViewCtrl::SelectAll()
3993 m_clientArea
->SelectAllRows(true);
3996 void wxDataViewCtrl::UnselectAll()
3998 m_clientArea
->SelectAllRows(false);
4001 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4005 if( row
> (int) m_clientArea
->GetRowCount() )
4006 row
= m_clientArea
->GetRowCount();
4008 int first
= m_clientArea
->GetFirstVisibleRow();
4009 int last
= m_clientArea
->GetLastVisibleRow();
4011 m_clientArea
->ScrollTo( row
, column
);
4012 else if( row
> last
)
4013 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4015 m_clientArea
->ScrollTo( first
, column
);
4018 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4020 int row
= m_clientArea
->GetRowByItem(item
);
4023 if( column
== NULL
)
4024 EnsureVisible(row
, -1);
4028 int len
= GetColumnCount();
4029 for( int i
= 0; i
< len
; i
++ )
4030 if( GetColumn(i
) == column
)
4035 EnsureVisible( row
, col
);
4041 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4043 m_clientArea
->HitTest(point
, item
, column
);
4046 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4048 return m_clientArea
->GetItemRect(item
, column
);
4051 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4053 return m_clientArea
->GetItemByRow( row
);
4056 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4058 return m_clientArea
->GetRowByItem( item
);
4061 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4063 int row
= m_clientArea
->GetRowByItem( item
);
4065 m_clientArea
->Expand(row
);
4068 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4070 int row
= m_clientArea
->GetRowByItem( item
);
4072 m_clientArea
->Collapse(row
);
4076 // !wxUSE_GENERICDATAVIEWCTRL
4079 // wxUSE_DATAVIEWCTRL