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 wxRect
GetLineRect( unsigned int row
) const;
504 //Some useful functions for row and item mapping
505 wxDataViewItem
GetItemByRow( unsigned int row
) const;
506 int GetRowByItem( const wxDataViewItem
& item
);
508 //Methods for building the mapping tree
509 void BuildTree( wxDataViewModel
* model
);
511 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
512 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
514 void Expand( unsigned int row
) { OnExpanding( row
); }
515 void Collapse( unsigned int row
) { OnCollapsing( row
); }
517 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
518 //We did not need this temporarily
519 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
521 int RecalculateCount() ;
523 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
524 void OnExpanding( unsigned int row
);
525 void OnCollapsing( unsigned int row
);
527 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
530 wxDataViewCtrl
*m_owner
;
534 wxDataViewColumn
*m_currentCol
;
535 unsigned int m_currentRow
;
536 wxDataViewSelection m_selection
;
538 wxDataViewRenameTimer
*m_renameTimer
;
546 // for double click logic
547 unsigned int m_lineLastClicked
,
548 m_lineBeforeLastClicked
,
549 m_lineSelectSingleOnUp
;
551 // the pen used to draw horiz/vertical rules
554 // the pen used to draw the expander and the lines
557 //This is the tree structure of the model
558 wxDataViewTreeNode
* m_root
;
560 //This is the tree node under the cursor
561 wxDataViewTreeNode
* m_underMouse
;
563 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
564 DECLARE_EVENT_TABLE()
567 // ---------------------------------------------------------
568 // wxGenericDataViewModelNotifier
569 // ---------------------------------------------------------
571 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
574 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
575 { m_mainWindow
= mainWindow
; }
577 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
578 { return m_mainWindow
->ItemAdded( parent
, item
); }
579 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
580 { return m_mainWindow
->ItemDeleted( parent
, item
); }
581 virtual bool ItemChanged( const wxDataViewItem
& item
)
582 { return m_mainWindow
->ItemChanged(item
); }
583 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
584 { return m_mainWindow
->ValueChanged( item
, col
); }
585 virtual bool Cleared()
586 { return m_mainWindow
->Cleared(); }
587 virtual void Resort()
588 { m_mainWindow
->Resort(); }
590 wxDataViewMainWindow
*m_mainWindow
;
593 // ---------------------------------------------------------
594 // wxDataViewRenderer
595 // ---------------------------------------------------------
597 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
599 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
600 wxDataViewCellMode mode
,
602 wxDataViewRendererBase( varianttype
, mode
, align
)
609 wxDataViewRenderer::~wxDataViewRenderer()
615 wxDC
*wxDataViewRenderer::GetDC()
619 if (GetOwner() == NULL
)
621 if (GetOwner()->GetOwner() == NULL
)
623 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
629 // ---------------------------------------------------------
630 // wxDataViewCustomRenderer
631 // ---------------------------------------------------------
633 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
635 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
636 wxDataViewCellMode mode
, int align
) :
637 wxDataViewRenderer( varianttype
, mode
, align
)
641 // ---------------------------------------------------------
642 // wxDataViewTextRenderer
643 // ---------------------------------------------------------
645 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
647 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
648 wxDataViewCellMode mode
, int align
) :
649 wxDataViewCustomRenderer( varianttype
, mode
, align
)
653 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
655 m_text
= value
.GetString();
660 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
665 bool wxDataViewTextRenderer::HasEditorCtrl()
670 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
671 wxRect labelRect
, const wxVariant
&value
)
673 return new wxTextCtrl( parent
, wxID_ANY
, value
,
674 wxPoint(labelRect
.x
,labelRect
.y
),
675 wxSize(labelRect
.width
,labelRect
.height
) );
678 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
680 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
681 value
= text
->GetValue();
685 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
687 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
688 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
689 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
690 view
->GetForegroundColour();
692 dc
->SetTextForeground(col
);
693 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
698 wxSize
wxDataViewTextRenderer::GetSize() const
700 const wxDataViewCtrl
*view
= GetView();
704 view
->GetTextExtent( m_text
, &x
, &y
);
705 return wxSize( x
, y
);
707 return wxSize(80,20);
710 // ---------------------------------------------------------
711 // wxDataViewBitmapRenderer
712 // ---------------------------------------------------------
714 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
716 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
717 wxDataViewCellMode mode
, int align
) :
718 wxDataViewCustomRenderer( varianttype
, mode
, align
)
722 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
724 if (value
.GetType() == wxT("wxBitmap"))
726 if (value
.GetType() == wxT("wxIcon"))
732 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
737 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
740 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
741 else if (m_icon
.Ok())
742 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
747 wxSize
wxDataViewBitmapRenderer::GetSize() const
750 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
751 else if (m_icon
.Ok())
752 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
754 return wxSize(16,16);
757 // ---------------------------------------------------------
758 // wxDataViewToggleRenderer
759 // ---------------------------------------------------------
761 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
763 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
764 wxDataViewCellMode mode
, int align
) :
765 wxDataViewCustomRenderer( varianttype
, mode
, align
)
770 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
772 m_toggle
= value
.GetBool();
777 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
782 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
784 // User wxRenderer here
787 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
789 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
794 flags
|= wxCONTROL_CHECKED
;
795 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
796 flags
|= wxCONTROL_DISABLED
;
798 wxRendererNative::Get().DrawCheckBox(
799 GetOwner()->GetOwner(),
807 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
808 wxDataViewModel
*model
,
809 const wxDataViewItem
& item
, unsigned int col
)
811 bool value
= !m_toggle
;
812 wxVariant variant
= value
;
813 model
->SetValue( variant
, item
, col
);
814 model
->ValueChanged( item
, col
);
818 wxSize
wxDataViewToggleRenderer::GetSize() const
820 return wxSize(20,20);
823 // ---------------------------------------------------------
824 // wxDataViewProgressRenderer
825 // ---------------------------------------------------------
827 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
829 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
830 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
831 wxDataViewCustomRenderer( varianttype
, mode
, align
)
837 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
841 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
843 m_value
= (long) value
;
845 if (m_value
< 0) m_value
= 0;
846 if (m_value
> 100) m_value
= 100;
851 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
853 value
= (long) m_value
;
857 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
859 double pct
= (double)m_value
/ 100.0;
861 bar
.width
= (int)(cell
.width
* pct
);
862 dc
->SetPen( *wxTRANSPARENT_PEN
);
863 dc
->SetBrush( *wxBLUE_BRUSH
);
864 dc
->DrawRectangle( bar
);
866 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
867 dc
->SetPen( *wxBLACK_PEN
);
868 dc
->DrawRectangle( cell
);
873 wxSize
wxDataViewProgressRenderer::GetSize() const
875 return wxSize(40,12);
878 // ---------------------------------------------------------
879 // wxDataViewDateRenderer
880 // ---------------------------------------------------------
882 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
884 #if wxUSE_DATE_RENDERER_POPUP
886 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
889 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
890 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
891 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
896 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
897 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
898 sizer
->Add( m_cal
, 1, wxGROW
);
903 void OnCalendar( wxCalendarEvent
&event
);
905 wxCalendarCtrl
*m_cal
;
906 wxDataViewModel
*m_model
;
908 const wxDataViewItem
& m_item
;
911 virtual void OnDismiss()
916 DECLARE_EVENT_TABLE()
919 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
920 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
923 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
925 wxDateTime date
= event
.GetDate();
926 wxVariant value
= date
;
927 m_model
->SetValue( value
, m_item
, m_col
);
928 m_model
->ValueChanged( m_item
, m_col
);
932 #endif // wxUSE_DATE_RENDERER_POPUP
934 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
936 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
937 wxDataViewCellMode mode
, int align
) :
938 wxDataViewCustomRenderer( varianttype
, mode
, align
)
942 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
944 m_date
= value
.GetDateTime();
949 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
955 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
957 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
958 wxString tmp
= m_date
.FormatDate();
959 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
964 wxSize
wxDataViewDateRenderer::GetSize() const
966 const wxDataViewCtrl
* view
= GetView();
967 wxString tmp
= m_date
.FormatDate();
969 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
970 return wxSize(x
,y
+d
);
973 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
974 const wxDataViewItem
& item
, unsigned int col
)
977 model
->GetValue( variant
, item
, col
);
978 wxDateTime value
= variant
.GetDateTime();
980 #if wxUSE_DATE_RENDERER_POPUP
981 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
982 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
983 wxPoint pos
= wxGetMousePosition();
986 popup
->Popup( popup
->m_cal
);
987 #else // !wxUSE_DATE_RENDERER_POPUP
988 wxMessageBox(value
.Format());
989 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
993 // ---------------------------------------------------------
994 // wxDataViewIconTextRenderer
995 // ---------------------------------------------------------
997 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
999 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1000 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1001 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1004 SetAlignment(align
);
1007 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1011 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1017 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1022 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1024 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1026 dc
->SetFont( view
->GetFont() );
1028 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1029 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1030 view
->GetForegroundColour();
1032 dc
->SetTextForeground(col
);
1034 const wxIcon
&icon
= m_value
.GetIcon();
1037 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1038 cell
.x
+= icon
.GetWidth()+4;
1041 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1046 wxSize
wxDataViewIconTextRenderer::GetSize() const
1048 return wxSize(80,16); // TODO
1051 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1056 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1061 // ---------------------------------------------------------
1063 // ---------------------------------------------------------
1065 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1067 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1068 unsigned int model_column
,
1069 int width
, wxAlignment align
, int flags
) :
1070 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1072 SetAlignment(align
);
1076 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1079 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1080 unsigned int model_column
,
1081 int width
, wxAlignment align
, int flags
) :
1082 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1084 SetAlignment(align
);
1087 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1090 wxDataViewColumn::~wxDataViewColumn()
1094 void wxDataViewColumn::Init( int width
)
1097 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1101 void wxDataViewColumn::SetResizeable( bool resizeable
)
1104 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1106 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1109 void wxDataViewColumn::SetHidden( bool hidden
)
1112 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1114 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1116 // tell our owner to e.g. update its scrollbars:
1118 GetOwner()->OnColumnChange();
1121 void wxDataViewColumn::SetSortable( bool sortable
)
1124 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1126 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1128 // Update header button
1130 GetOwner()->OnColumnChange();
1133 void wxDataViewColumn::SetSortOrder( bool ascending
)
1135 m_ascending
= ascending
;
1137 // Update header button
1139 GetOwner()->OnColumnChange();
1142 bool wxDataViewColumn::IsSortOrderAscending() const
1147 void wxDataViewColumn::SetInternalWidth( int width
)
1151 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1152 if (m_owner
&& m_owner
->m_clientArea
)
1153 m_owner
->m_clientArea
->RecalculateDisplay();
1156 void wxDataViewColumn::SetWidth( int width
)
1158 m_owner
->m_headerArea
->UpdateDisplay();
1160 SetInternalWidth(width
);
1164 //-----------------------------------------------------------------------------
1165 // wxDataViewHeaderWindowBase
1166 //-----------------------------------------------------------------------------
1168 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1170 wxWindow
*parent
= GetParent();
1171 wxDataViewEvent
le(type
, parent
->GetId());
1173 le
.SetEventObject(parent
);
1175 le
.SetDataViewColumn(GetColumn(n
));
1176 le
.SetModel(GetOwner()->GetModel());
1178 // for events created by wxDataViewHeaderWindow the
1179 // row / value fields are not valid
1181 parent
->GetEventHandler()->ProcessEvent(le
);
1184 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1186 // implemented in msw/listctrl.cpp:
1187 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1189 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1191 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1192 const wxPoint
&pos
, const wxSize
&size
,
1193 const wxString
&name
)
1197 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1200 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1201 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1202 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1203 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1205 // create the native WC_HEADER window:
1206 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1207 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1208 m_hWnd
= CreateWindowEx(0,
1219 wxLogLastError(_T("CreateWindowEx"));
1223 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1224 // to call wxDataViewHeaderWindow::MSWOnNotify
1225 SubclassWin(m_hWnd
);
1227 // the following is required to get the default win's font for
1228 // header windows and must be done befor sending the HDM_LAYOUT msg
1235 // Retrieve the bounding rectangle of the parent window's
1236 // client area, and then request size and position values
1237 // from the header control.
1238 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1240 hdl
.prc
= &rcParent
;
1242 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1244 wxLogLastError(_T("SendMessage"));
1248 // Set the size, position, and visibility of the header control.
1249 SetWindowPos((HWND
)m_hWnd
,
1253 wp
.flags
| SWP_SHOWWINDOW
);
1255 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1256 // a wxBoxSizer and in order to avoid super-big header windows,
1257 // we need to set our height as fixed
1258 SetMinSize(wxSize(-1, wp
.cy
));
1259 SetMaxSize(wxSize(-1, wp
.cy
));
1264 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1269 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1271 // remove old columns
1272 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1273 Header_DeleteItem((HWND
)m_hWnd
, 0);
1275 // add the updated array of columns to the header control
1276 unsigned int cols
= GetOwner()->GetColumnCount();
1277 unsigned int added
= 0;
1278 wxDataViewModel
* model
= GetOwner()->GetModel();
1279 for (unsigned int i
= 0; i
< cols
; i
++)
1281 wxDataViewColumn
*col
= GetColumn( i
);
1282 if (col
->IsHidden())
1283 continue; // don't add it!
1286 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1287 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1288 hdi
.cxy
= col
->GetWidth();
1289 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1290 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1291 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1294 if(model
&& m_owner
->GetSortingColumn() == col
)
1296 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1297 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1298 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1302 // lParam is reserved for application's use:
1303 // we store there the column index to use it later in MSWOnNotify
1304 // (since columns may have been hidden)
1305 hdi
.lParam
= (LPARAM
)i
;
1307 // the native wxMSW implementation of the header window
1308 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1309 // on the right: to correct this effect we make the column
1310 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1312 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1314 switch (col
->GetAlignment())
1317 hdi
.fmt
|= HDF_LEFT
;
1319 case wxALIGN_CENTER
:
1320 case wxALIGN_CENTER_HORIZONTAL
:
1321 hdi
.fmt
|= HDF_CENTER
;
1324 hdi
.fmt
|= HDF_RIGHT
;
1328 // such alignment is not allowed for the column header!
1332 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1333 (WPARAM
)added
, (LPARAM
)&hdi
);
1338 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1342 // NOTE: we don't just return nmHDR->iItem because when there are
1343 // hidden columns, nmHDR->iItem may be different from
1344 // nmHDR->pitem->lParam
1346 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1348 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1353 item
.mask
= HDI_LPARAM
;
1354 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1356 return (unsigned int)item
.lParam
;
1359 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1361 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1363 // is it a message from the header?
1364 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1365 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1367 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1368 switch ( nmhdr
->code
)
1370 case HDN_BEGINTRACK
:
1371 // user has started to resize a column:
1372 // do we need to veto it?
1373 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1381 // user has started to reorder a column
1384 case HDN_ITEMCHANGING
:
1385 if (nmHDR
->pitem
!= NULL
&&
1386 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1388 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1389 if (nmHDR
->pitem
->cxy
< minWidth
)
1391 // do not allow the user to resize this column under
1392 // its minimal width:
1398 case HDN_ITEMCHANGED
: // user is resizing a column
1399 case HDN_ENDTRACK
: // user has finished resizing a column
1400 case HDN_ENDDRAG
: // user has finished reordering a column
1402 // update the width of the modified column:
1403 if (nmHDR
->pitem
!= NULL
&&
1404 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1406 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1407 unsigned int w
= nmHDR
->pitem
->cxy
;
1408 wxDataViewColumn
*col
= GetColumn(idx
);
1410 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1411 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1412 w
-= COLUMN_WIDTH_OFFSET
;
1414 if (w
>= (unsigned)col
->GetMinWidth())
1415 col
->SetInternalWidth(w
);
1421 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1422 wxDataViewModel
* model
= GetOwner()->GetModel();
1424 if(nmHDR
->iButton
== 0)
1426 wxDataViewColumn
*col
= GetColumn(idx
);
1427 if(col
->IsSortable())
1429 if(model
&& m_owner
->GetSortingColumn() == col
)
1431 bool order
= col
->IsSortOrderAscending();
1432 col
->SetSortOrder(!order
);
1436 m_owner
->SetSortingColumn(col
);
1444 wxEventType evt
= nmHDR
->iButton
== 0 ?
1445 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1446 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1447 SendEvent(evt
, idx
);
1453 // NOTE: for some reason (i.e. for a bug in Windows)
1454 // the HDN_ITEMCLICK notification is not sent on
1455 // right clicks, so we need to handle NM_RCLICK
1458 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1459 if (column
!= wxNOT_FOUND
)
1462 item
.mask
= HDI_LPARAM
;
1463 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1465 // 'idx' may be different from 'column' if there are
1466 // hidden columns...
1467 unsigned int idx
= (unsigned int)item
.lParam
;
1468 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1474 case HDN_GETDISPINFOW
:
1475 // see wxListCtrl::MSWOnNotify for more info!
1478 case HDN_ITEMDBLCLICK
:
1480 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1481 int w
= GetOwner()->GetBestColumnWidth(idx
);
1483 // update the native control:
1485 ZeroMemory(&hd
, sizeof(hd
));
1486 hd
.mask
= HDI_WIDTH
;
1488 Header_SetItem(GetHwnd(),
1489 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1492 // update the wxDataViewColumn class:
1493 GetColumn(idx
)->SetInternalWidth(w
);
1498 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1504 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1505 const wxRect
*WXUNUSED(rect
))
1507 wxSize ourSz
= GetClientSize();
1508 wxSize ownerSz
= m_owner
->GetClientSize();
1510 // where should the (logical) origin of this window be placed?
1512 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1514 // put this window on top of our parent and
1515 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1516 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1520 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1521 int WXUNUSED(w
), int WXUNUSED(h
),
1524 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1525 // the wxDataViewCtrl window gets resized: the following dummy call
1526 // to ScrollWindow() is required in order to get this header window
1527 // correctly repainted when it's (horizontally) scrolled:
1532 #else // !defined(__WXMSW__)
1534 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1535 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1536 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1537 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1538 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1541 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1542 const wxPoint
&pos
, const wxSize
&size
,
1543 const wxString
&name
)
1547 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1550 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1551 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1552 SetOwnForegroundColour( attr
.colFg
);
1553 SetOwnBackgroundColour( attr
.colBg
);
1555 SetOwnFont( attr
.font
);
1557 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1558 // a wxBoxSizer and in order to avoid super-big header windows,
1559 // we need to set our height as fixed
1560 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1561 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1566 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1569 GetClientSize( &w
, &h
);
1571 wxAutoBufferedPaintDC
dc( this );
1573 dc
.SetBackground(GetBackgroundColour());
1577 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1580 m_owner
->GetViewStart( &x
, NULL
);
1582 // account for the horz scrollbar offset
1583 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1585 dc
.SetFont( GetFont() );
1587 unsigned int cols
= GetOwner()->GetColumnCount();
1590 for (i
= 0; i
< cols
; i
++)
1592 wxDataViewColumn
*col
= GetColumn( i
);
1593 if (col
->IsHidden())
1594 continue; // skip it!
1596 int cw
= col
->GetWidth();
1599 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1600 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1602 if (col
->IsSortOrderAscending())
1603 sortArrow
= wxHDR_SORT_ICON_UP
;
1605 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1609 if (m_parent
->IsEnabled())
1611 if ((int) i
== m_hover
)
1612 state
= wxCONTROL_CURRENT
;
1616 state
= (int) wxCONTROL_DISABLED
;
1619 wxRendererNative::Get().DrawHeaderButton
1623 wxRect(xpos
, 0, cw
, ch
-1),
1628 // align as required the column title:
1630 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1631 switch (col
->GetAlignment())
1634 x
+= HEADER_HORIZ_BORDER
;
1637 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1640 case wxALIGN_CENTER
:
1641 case wxALIGN_CENTER_HORIZONTAL
:
1642 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1646 // always center the title vertically:
1647 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1649 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1651 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1652 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1653 dc
.DrawText( col
->GetTitle(), x
, y
);
1654 dc
.DestroyClippingRegion();
1660 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1662 GetParent()->SetFocus();
1666 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1668 // we want to work with logical coords
1670 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1671 int y
= event
.GetY();
1675 // we don't draw the line beyond our window,
1676 // but we allow dragging it there
1678 GetClientSize( &w
, NULL
);
1679 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1682 if (event
.ButtonUp())
1684 m_isDragging
= false;
1690 m_currentX
= wxMax(m_minX
+ 7, x
);
1694 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1696 GetOwner()->Refresh();
1700 else // not dragging
1703 m_column
= wxNOT_FOUND
;
1705 bool hit_border
= false;
1707 // end of the current column
1710 // find the column where this event occured
1711 int countCol
= m_owner
->GetColumnCount();
1712 for (int column
= 0; column
< countCol
; column
++)
1714 wxDataViewColumn
*p
= GetColumn(column
);
1717 continue; // skip if not shown
1719 xpos
+= p
->GetWidth();
1721 if ((abs(x
-xpos
) < 3) && (y
< 22))
1729 // inside the column
1736 int old_hover
= m_hover
;
1738 if (event
.Leaving())
1739 m_hover
= wxNOT_FOUND
;
1740 if (old_hover
!= m_hover
)
1743 if (m_column
== wxNOT_FOUND
)
1746 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1747 if (event
.LeftDClick() && resizeable
)
1749 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1752 else if (event
.LeftDown() || event
.RightUp())
1754 if (hit_border
&& event
.LeftDown() && resizeable
)
1756 m_isDragging
= true;
1760 else // click on a column
1762 wxDataViewModel
* model
= GetOwner()->GetModel();
1763 wxEventType evt
= event
.LeftDown() ?
1764 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1765 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1766 SendEvent(evt
, m_column
);
1768 //Left click the header
1769 if(event
.LeftDown())
1771 wxDataViewColumn
*col
= GetColumn(m_column
);
1772 if(col
->IsSortable())
1774 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1775 if(model
&& sortCol
== col
)
1777 bool order
= col
->IsSortOrderAscending();
1778 col
->SetSortOrder(!order
);
1782 m_owner
->SetSortingColumn(col
);
1788 //Send the column sorted event
1789 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1793 else if (event
.Moving())
1795 if (hit_border
&& resizeable
)
1796 m_currentCursor
= m_resizeCursor
;
1798 m_currentCursor
= wxSTANDARD_CURSOR
;
1800 SetCursor(*m_currentCursor
);
1805 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1806 void wxGenericDataViewHeaderWindow::DrawCurrent()
1809 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1811 int x1
= m_currentX
;
1813 ClientToScreen (&x1
, &y1
);
1815 int x2
= m_currentX
-1;
1817 ++x2
; // but why ????
1820 m_owner
->GetClientSize( NULL
, &y2
);
1821 m_owner
->ClientToScreen( &x2
, &y2
);
1824 dc
.SetLogicalFunction(wxINVERT
);
1825 dc
.SetPen(m_penCurrent
);
1826 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1828 dc
.DrawLine(x1
, y1
, x2
, y2
);
1832 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1836 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1837 m_owner
->GetViewStart( &x
, NULL
);
1839 // shift the DC origin to match the position of the main window horizontal
1840 // scrollbar: this allows us to always use logical coords
1841 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1844 #endif // defined(__WXMSW__)
1846 //-----------------------------------------------------------------------------
1847 // wxDataViewRenameTimer
1848 //-----------------------------------------------------------------------------
1850 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1855 void wxDataViewRenameTimer::Notify()
1857 m_owner
->OnRenameTimer();
1860 //-----------------------------------------------------------------------------
1861 // wxDataViewMainWindow
1862 //-----------------------------------------------------------------------------
1864 //The tree building helper, declared firstly
1865 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1867 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1869 if (row1
> row2
) return 1;
1870 if (row1
== row2
) return 0;
1875 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1877 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1878 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1879 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1880 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1881 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1882 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1885 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1886 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1887 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1888 m_selection( wxDataViewSelectionCmp
)
1893 m_lastOnSame
= false;
1894 m_renameTimer
= new wxDataViewRenameTimer( this );
1896 // TODO: user better initial values/nothing selected
1897 m_currentCol
= NULL
;
1900 // TODO: we need to calculate this smartly
1909 m_dragStart
= wxPoint(0,0);
1910 m_lineLastClicked
= (unsigned int) -1;
1911 m_lineBeforeLastClicked
= (unsigned int) -1;
1912 m_lineSelectSingleOnUp
= (unsigned int) -1;
1916 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1917 SetBackgroundColour( *wxWHITE
);
1919 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1921 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1922 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1923 //Some new added code to deal with the tree structure
1924 m_root
= new wxDataViewTreeNode( NULL
);
1925 m_root
->SetHasChildren(true);
1927 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1929 m_underMouse
= NULL
;
1933 wxDataViewMainWindow::~wxDataViewMainWindow()
1936 delete m_renameTimer
;
1939 void wxDataViewMainWindow::OnRenameTimer()
1941 // We have to call this here because changes may just have
1942 // been made and no screen update taken place.
1947 unsigned int cols
= GetOwner()->GetColumnCount();
1949 for (i
= 0; i
< cols
; i
++)
1951 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1953 continue; // skip it!
1955 if (c
== m_currentCol
)
1957 xpos
+= c
->GetWidth();
1959 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1960 m_currentCol
->GetWidth(), m_lineHeight
);
1962 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1963 &labelRect
.x
, &labelRect
.y
);
1965 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1966 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1970 //------------------------------------------------------------------
1971 // Helper class for do operation on the tree node
1972 //------------------------------------------------------------------
1979 //The return value control how the tree-walker tranverse the tree
1980 // 0: Job done, stop tranverse and return
1981 // 1: Ignore the current node's subtree and continue
1982 // 2: Job not done, continue
1983 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1984 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1985 virtual int operator() ( void * n
) = 0;
1988 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1993 switch( func( node
) )
2004 wxDataViewTreeNodes nodes
= node
->GetNodes();
2005 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2007 int len_nodes
= nodes
.GetCount();
2008 int len
= leaves
.GetCount();
2009 int i
= 0, nodes_i
= 0;
2011 for( ; i
< len
; i
++ )
2013 void * n
= leaves
[i
];
2014 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2016 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2019 if( Walker( nd
, func
) )
2038 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2042 wxDataViewTreeNode
* node
;
2043 node
= FindNode(parent
);
2048 node
->SetHasChildren( true );
2050 if( g_model
->IsContainer( item
) )
2052 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2053 newnode
->SetItem(item
);
2054 newnode
->SetHasChildren( true );
2055 node
->AddNode( newnode
);
2058 node
->AddLeaf( item
.GetID() );
2060 node
->ChangeSubTreeCount(1);
2068 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2070 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2071 const wxDataViewItem
& item
)
2073 wxDataViewTreeNode
* node
= FindNode(parent
);
2075 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2076 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2079 node
->GetChildren().Remove( item
.GetID() );
2080 //Manuplate selection
2081 if( m_selection
.GetCount() > 1 )
2083 m_selection
.Empty();
2085 bool isContainer
= false;
2086 wxDataViewTreeNodes nds
= node
->GetNodes();
2087 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2089 if (nds
[i
]->GetItem() == item
)
2097 wxDataViewTreeNode
* n
= NULL
;
2098 wxDataViewTreeNodes nodes
= node
->GetNodes();
2099 int len
= nodes
.GetCount();
2100 for( int i
= 0 ; i
< len
; i
++)
2102 if( nodes
[i
]->GetItem() == item
)
2109 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2111 node
->GetNodes().Remove( n
);
2112 sub
-= n
->GetSubTreeCount();
2113 DestroyTreeHelper(n
);
2115 //Make the row number invalid and get a new valid one when user call GetRowCount
2117 node
->ChangeSubTreeCount(sub
);
2118 if( node
->GetChildrenNumber() == 0)
2120 node
->GetParent()->GetNodes().Remove( node
);
2124 //Change the current row to the last row if the current exceed the max row number
2125 if( m_currentRow
> GetRowCount() )
2126 m_currentRow
= m_count
- 1;
2133 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2139 wxWindow
*parent
= GetParent();
2140 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2141 le
.SetEventObject(parent
);
2142 le
.SetModel(GetOwner()->GetModel());
2144 parent
->GetEventHandler()->ProcessEvent(le
);
2149 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2151 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2152 /*#define MAX_VIRTUAL_WIDTH 100000
2154 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2155 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2156 Refresh( true, &rect );
2164 wxWindow
*parent
= GetParent();
2165 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2166 le
.SetEventObject(parent
);
2167 le
.SetModel(GetOwner()->GetModel());
2170 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2171 parent
->GetEventHandler()->ProcessEvent(le
);
2176 bool wxDataViewMainWindow::Cleared()
2186 void wxDataViewMainWindow::UpdateDisplay()
2191 void wxDataViewMainWindow::OnInternalIdle()
2193 wxWindow::OnInternalIdle();
2197 RecalculateDisplay();
2202 void wxDataViewMainWindow::RecalculateDisplay()
2204 wxDataViewModel
*model
= GetOwner()->GetModel();
2211 int width
= GetEndOfLastCol();
2212 int height
= GetRowCount() * m_lineHeight
;
2214 SetVirtualSize( width
, height
);
2215 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2220 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2222 wxWindow::ScrollWindow( dx
, dy
, rect
);
2224 if (GetOwner()->m_headerArea
)
2225 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2228 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2231 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2232 int sy
= rows
*m_lineHeight
/y
;
2236 wxRect rect
= GetClientRect();
2238 int x_start
= 0, x_end
= 0, w
= 0;
2240 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2241 for (x_start
= 0; colnum
< column
; colnum
++)
2243 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2244 if (col
->IsHidden())
2245 continue; // skip it!
2247 w
= col
->GetWidth();
2251 x_end
= x_start
+ w
;
2252 xe
= xx
+ rect
.width
;
2255 sx
= ( xx
+ x_end
- xe
)/x
;
2262 m_owner
->Scroll( sx
, sy
);
2265 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2267 wxDataViewModel
*model
= GetOwner()->GetModel();
2268 wxAutoBufferedPaintDC
dc( this );
2271 dc
.SetBackground(GetBackgroundColour());
2273 GetOwner()->PrepareDC( dc
);
2274 dc
.SetFont( GetFont() );
2276 wxRect update
= GetUpdateRegion().GetBox();
2277 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2279 // compute which items needs to be redrawn
2280 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2281 unsigned int item_count
=
2282 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2283 (int)(GetRowCount( )- item_start
) );
2284 unsigned int item_last
= item_start
+ item_count
;
2286 // compute which columns needs to be redrawn
2287 unsigned int cols
= GetOwner()->GetColumnCount();
2288 unsigned int col_start
= 0;
2289 unsigned int x_start
= 0;
2290 for (x_start
= 0; col_start
< cols
; col_start
++)
2292 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2293 if (col
->IsHidden())
2294 continue; // skip it!
2296 unsigned int w
= col
->GetWidth();
2297 if (x_start
+w
>= (unsigned int)update
.x
)
2303 unsigned int col_last
= col_start
;
2304 unsigned int x_last
= x_start
;
2305 for (; col_last
< cols
; col_last
++)
2307 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2308 if (col
->IsHidden())
2309 continue; // skip it!
2311 if (x_last
> (unsigned int)update
.GetRight())
2314 x_last
+= col
->GetWidth();
2317 // Draw horizontal rules if required
2318 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2320 dc
.SetPen(m_penRule
);
2321 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2323 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2325 int y
= i
* m_lineHeight
;
2326 dc
.DrawLine(x_start
, y
, x_last
, y
);
2330 // Draw vertical rules if required
2331 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2333 dc
.SetPen(m_penRule
);
2334 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2337 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2339 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2340 if (col
->IsHidden())
2341 continue; // skip it
2343 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2344 x
, item_last
* m_lineHeight
);
2346 x
+= col
->GetWidth();
2349 // Draw last vertical rule
2350 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2351 x
, item_last
* m_lineHeight
);
2354 bool has_focus
= (FindFocus() == this);
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
) && has_focus
)
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
) - 1;
2546 return wxMin( GetRowCount()-1, row
);
2549 unsigned int wxDataViewMainWindow::GetRowCount()
2551 if ( m_count
== -1 )
2553 m_count
= RecalculateCount();
2555 GetVirtualSize( &width
, &height
);
2556 height
= m_count
* m_lineHeight
;
2558 SetVirtualSize( width
, height
);
2563 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2570 void wxDataViewMainWindow::SelectAllRows( bool on
)
2577 m_selection
.Clear();
2578 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2579 m_selection
.Add( i
);
2584 unsigned int first_visible
= GetFirstVisibleRow();
2585 unsigned int last_visible
= GetLastVisibleRow();
2587 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2589 unsigned int row
= m_selection
[i
];
2590 if ((row
>= first_visible
) && (row
<= last_visible
))
2593 m_selection
.Clear();
2597 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2599 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2603 m_selection
.Add( row
);
2611 m_selection
.Remove( row
);
2617 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2621 unsigned int tmp
= from
;
2627 for (i
= from
; i
<= to
; i
++)
2629 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2632 m_selection
.Add( i
);
2637 m_selection
.Remove( i
);
2640 RefreshRows( from
, to
);
2643 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2645 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2647 int n
= aSelections
[i
];
2649 m_selection
.Add( n
);
2654 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2656 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2657 m_selection
.Add( row
);
2659 m_selection
.Remove( row
);
2663 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2665 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2668 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2670 wxWindow
*parent
= GetParent();
2671 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2673 le
.SetEventObject(parent
);
2674 le
.SetModel(GetOwner()->GetModel());
2677 parent
->GetEventHandler()->ProcessEvent(le
);
2680 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2682 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2683 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2685 wxSize client_size
= GetClientSize();
2686 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2687 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2688 if (intersect_rect
.width
> 0)
2689 Refresh( true, &intersect_rect
);
2692 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2696 unsigned int tmp
= to
;
2701 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * 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::RefreshRowsAfter( unsigned int firstRow
)
2713 unsigned int count
= GetRowCount();
2714 if (firstRow
> count
)
2717 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2718 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2720 wxSize client_size
= GetClientSize();
2721 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2722 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2723 if (intersect_rect
.width
> 0)
2724 Refresh( true, &intersect_rect
);
2727 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2729 wxCHECK_RET( newCurrent
< GetRowCount(),
2730 _T("invalid item index in OnArrowChar()") );
2732 // if there is no selection, we cannot move it anywhere
2733 if (!HasCurrentRow())
2736 unsigned int oldCurrent
= m_currentRow
;
2738 // in single selection we just ignore Shift as we can't select several
2740 if ( event
.ShiftDown() && !IsSingleSel() )
2742 RefreshRow( oldCurrent
);
2744 ChangeCurrentRow( newCurrent
);
2746 // select all the items between the old and the new one
2747 if ( oldCurrent
> newCurrent
)
2749 newCurrent
= oldCurrent
;
2750 oldCurrent
= m_currentRow
;
2753 SelectRows( oldCurrent
, newCurrent
, true );
2754 if (oldCurrent
!=newCurrent
)
2755 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2759 RefreshRow( oldCurrent
);
2761 // all previously selected items are unselected unless ctrl is held
2762 if ( !event
.ControlDown() )
2763 SelectAllRows(false);
2765 ChangeCurrentRow( newCurrent
);
2767 if ( !event
.ControlDown() )
2769 SelectRow( m_currentRow
, true );
2770 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2773 RefreshRow( m_currentRow
);
2776 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2779 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2783 rect
.y
= m_lineHeight
* row
;
2784 rect
.width
= GetEndOfLastCol();
2785 rect
.height
= m_lineHeight
;
2790 class RowToItemJob
: public DoJob
2793 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2794 virtual ~RowToItemJob(){};
2796 virtual int operator() ( wxDataViewTreeNode
* node
)
2799 if( current
== static_cast<int>(row
))
2801 ret
= node
->GetItem() ;
2805 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2807 current
+= node
->GetSubTreeCount();
2812 //If the current has no child node, we can find the desired item of the row number directly.
2813 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2814 if( node
->GetNodes().GetCount() == 0)
2816 int index
= static_cast<int>(row
) - current
- 1;
2817 ret
= node
->GetChildren().Item( index
);
2824 virtual int operator() ( void * n
)
2827 if( current
== static_cast<int>(row
))
2829 ret
= wxDataViewItem( n
) ;
2834 wxDataViewItem
GetResult(){ return ret
; }
2841 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2843 RowToItemJob
job( row
, -2 );
2844 Walker( m_root
, job
);
2845 return job
.GetResult();
2848 class RowToTreeNodeJob
: public DoJob
2851 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2854 this->current
= current
;
2858 virtual ~RowToTreeNodeJob(){};
2860 virtual int operator() ( wxDataViewTreeNode
* node
)
2863 if( current
== static_cast<int>(row
))
2869 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2871 current
+= node
->GetSubTreeCount();
2877 //If the current has no child node, we can find the desired item of the row number directly.
2878 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2879 if( node
->GetNodes().GetCount() == 0)
2881 int index
= static_cast<int>(row
) - current
- 1;
2882 void * n
= node
->GetChildren().Item( index
);
2883 ret
= new wxDataViewTreeNode( parent
) ;
2884 ret
->SetItem( wxDataViewItem( n
));
2885 ret
->SetHasChildren(false);
2894 virtual int operator() ( void * n
)
2897 if( current
== static_cast<int>(row
))
2899 ret
= new wxDataViewTreeNode( parent
) ;
2900 ret
->SetItem( wxDataViewItem( n
));
2901 ret
->SetHasChildren(false);
2907 wxDataViewTreeNode
* GetResult(){ return ret
; }
2911 wxDataViewTreeNode
* ret
;
2912 wxDataViewTreeNode
* parent
;
2916 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2918 RowToTreeNodeJob
job( row
, -2, m_root
);
2919 Walker( m_root
, job
);
2920 return job
.GetResult();
2923 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2925 wxWindow
*parent
= GetParent();
2926 wxDataViewEvent
le(type
, parent
->GetId());
2928 le
.SetEventObject(parent
);
2929 le
.SetModel(GetOwner()->GetModel());
2932 parent
->GetEventHandler()->ProcessEvent(le
);
2936 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2938 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2941 if( node
->HasChildren())
2943 if( !node
->IsOpen())
2945 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2946 //Check if the user prevent expanding
2947 if( e
.GetSkipped() )
2951 //Here I build the children of current node
2952 if( node
->GetChildrenNumber() == 0 )
2955 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2959 //Send the expanded event
2960 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2964 SelectRow( row
, false );
2965 SelectRow( row
+ 1, true );
2966 ChangeCurrentRow( row
+ 1 );
2967 SendSelectionChangedEvent( GetItemByRow(row
+1));
2975 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2977 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2980 wxDataViewTreeNode
* nd
= node
;
2982 if( node
->HasChildren() && node
->IsOpen() )
2984 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2985 if( e
.GetSkipped() )
2990 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2994 node
= node
->GetParent();
2997 int parent
= GetRowByItem( node
->GetItem() ) ;
3000 SelectRow( row
, false);
3001 SelectRow(parent
, true );
3002 ChangeCurrentRow( parent
);
3003 SendSelectionChangedEvent( node
->GetItem() );
3007 if( !nd
->HasChildren())
3012 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3014 wxDataViewModel
* model
= GetOwner()->GetModel();
3018 //Compose the a parent-chain of the finding item
3020 list
.DeleteContents( true );
3021 wxDataViewItem
it( item
);
3024 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3025 list
.Insert( pItem
);
3026 it
= model
->GetParent( it
);
3029 //Find the item along the parent-chain.
3030 //This algorithm is designed to speed up the node-finding method
3031 wxDataViewTreeNode
* node
= m_root
;
3032 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3034 if( node
->HasChildren() )
3036 if( node
->GetChildrenNumber() == 0 )
3039 BuildTreeHelper(model
, node
->GetItem(), node
);
3042 wxDataViewTreeNodes nodes
= node
->GetNodes();
3044 for (; i
< nodes
.GetCount(); i
++)
3046 if (nodes
[i
]->GetItem() == (**iter
))
3052 if (i
== nodes
.GetCount())
3061 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3063 wxDataViewColumn
*col
= NULL
;
3064 unsigned int cols
= GetOwner()->GetColumnCount();
3065 unsigned int colnum
= 0;
3066 unsigned int x_start
= 0;
3068 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3069 for (x_start
= 0; colnum
< cols
; colnum
++)
3071 col
= GetOwner()->GetColumn(colnum
);
3072 if (col
->IsHidden())
3073 continue; // skip it!
3075 unsigned int w
= col
->GetWidth();
3076 if (x_start
+w
>= (unsigned int)x
)
3083 item
= GetItemByRow( y
/m_lineHeight
);
3086 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3088 int row
= GetRowByItem(item
);
3089 int y
= row
*m_lineHeight
;
3090 int h
= m_lineHeight
;
3092 wxDataViewColumn
*col
= NULL
;
3093 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3095 col
= GetOwner()->GetColumn( i
);
3096 x
+= col
->GetWidth();
3097 if( GetOwner()->GetColumn(i
+1) == column
)
3100 int w
= col
->GetWidth();
3101 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3102 return wxRect(x
, y
, w
, h
);
3105 int wxDataViewMainWindow::RecalculateCount()
3107 return m_root
->GetSubTreeCount();
3110 class ItemToRowJob
: public DoJob
3113 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3114 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3115 virtual ~ItemToRowJob(){};
3117 //Maybe binary search will help to speed up this process
3118 virtual int operator() ( wxDataViewTreeNode
* node
)
3121 if( node
->GetItem() == item
)
3126 if( node
->GetItem() == **m_iter
)
3133 ret
+= node
->GetSubTreeCount();
3139 virtual int operator() ( void * n
)
3142 if( n
== item
.GetID() )
3146 //the row number is begin from zero
3147 int GetResult(){ return ret
-1 ; }
3149 ItemList::const_iterator m_iter
;
3150 wxDataViewItem item
;
3155 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3157 wxDataViewModel
* model
= GetOwner()->GetModel();
3164 //Compose the a parent-chain of the finding item
3166 wxDataViewItem
* pItem
= NULL
;
3167 list
.DeleteContents( true );
3168 wxDataViewItem
it( item
);
3171 pItem
= new wxDataViewItem( it
);
3172 list
.Insert( pItem
);
3173 it
= model
->GetParent( it
);
3175 pItem
= new wxDataViewItem( );
3176 list
.Insert( pItem
);
3178 ItemToRowJob
job( item
, list
.begin() );
3179 Walker(m_root
, job
);
3180 return job
.GetResult();
3183 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3185 if( !model
->IsContainer( item
) )
3188 wxDataViewItemArray children
;
3189 unsigned int num
= model
->GetChildren( item
, children
);
3190 unsigned int index
= 0;
3191 while( index
< num
)
3193 if( model
->IsContainer( children
[index
] ) )
3195 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3196 n
->SetItem(children
[index
]);
3197 n
->SetHasChildren( true ) ;
3202 node
->AddLeaf( children
[index
].GetID() );
3206 node
->SetSubTreeCount( num
);
3207 wxDataViewTreeNode
* n
= node
->GetParent();
3209 n
->ChangeSubTreeCount(num
);
3213 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3215 //First we define a invalid item to fetch the top-level elements
3216 wxDataViewItem item
;
3218 BuildTreeHelper( model
, item
, m_root
);
3222 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3224 if( node
->GetNodeNumber() != 0 )
3226 int len
= node
->GetNodeNumber();
3228 wxDataViewTreeNodes nodes
= node
->GetNodes();
3229 for( ; i
< len
; i
++ )
3231 DestroyTreeHelper(nodes
[i
]);
3237 void wxDataViewMainWindow::DestroyTree()
3239 DestroyTreeHelper(m_root
);
3240 m_root
->SetSubTreeCount(0);
3244 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3246 if (event
.GetKeyCode() == WXK_TAB
)
3248 wxNavigationKeyEvent nevent
;
3249 nevent
.SetWindowChange( event
.ControlDown() );
3250 nevent
.SetDirection( !event
.ShiftDown() );
3251 nevent
.SetEventObject( GetParent()->GetParent() );
3252 nevent
.SetCurrentFocus( m_parent
);
3253 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3257 // no item -> nothing to do
3258 if (!HasCurrentRow())
3264 // don't use m_linesPerPage directly as it might not be computed yet
3265 const int pageSize
= GetCountPerPage();
3266 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3268 switch ( event
.GetKeyCode() )
3272 if (m_currentRow
> 0)
3274 wxWindow
*parent
= GetParent();
3275 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3276 le
.SetItem( GetItemByRow(m_currentRow
) );
3277 le
.SetEventObject(parent
);
3278 le
.SetModel(GetOwner()->GetModel());
3280 parent
->GetEventHandler()->ProcessEvent(le
);
3285 if ( m_currentRow
> 0 )
3286 OnArrowChar( m_currentRow
- 1, event
);
3290 if ( m_currentRow
< GetRowCount() - 1 )
3291 OnArrowChar( m_currentRow
+ 1, event
);
3293 //Add the process for tree expanding/collapsing
3295 OnCollapsing(m_currentRow
);
3298 OnExpanding( m_currentRow
);
3302 OnArrowChar( GetRowCount() - 1, event
);
3307 OnArrowChar( 0, event
);
3312 int steps
= pageSize
- 1;
3313 int index
= m_currentRow
- steps
;
3317 OnArrowChar( index
, event
);
3323 int steps
= pageSize
- 1;
3324 unsigned int index
= m_currentRow
+ steps
;
3325 unsigned int count
= GetRowCount();
3326 if ( index
>= count
)
3329 OnArrowChar( index
, event
);
3338 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3340 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3342 // let the base handle mouse wheel events.
3347 int x
= event
.GetX();
3348 int y
= event
.GetY();
3349 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3350 wxDataViewColumn
*col
= NULL
;
3353 unsigned int cols
= GetOwner()->GetColumnCount();
3355 for (i
= 0; i
< cols
; i
++)
3357 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3359 continue; // skip it!
3361 if (x
< xpos
+ c
->GetWidth())
3366 xpos
+= c
->GetWidth();
3371 wxDataViewRenderer
*cell
= col
->GetRenderer();
3372 unsigned int current
= y
/ m_lineHeight
;
3373 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3375 // Unselect all if below the last row ?
3379 //Test whether the mouse is hovered on the tree item button
3381 if (GetOwner()->GetExpanderColumn() == col
)
3383 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3384 if( node
!=NULL
&& node
->HasChildren() )
3386 int indent
= node
->GetIndentLevel();
3387 indent
= GetOwner()->GetIndent()*indent
;
3388 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3389 if( rect
.Contains( x
, y
) )
3391 //So the mouse is over the expander
3393 if (m_underMouse
&& m_underMouse
!= node
)
3395 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3396 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3398 if (m_underMouse
!= node
)
3400 //wxLogMessage("Do the row: %d", current);
3403 m_underMouse
= node
;
3406 if (node
!=NULL
&& !node
->HasChildren())
3411 if (m_underMouse
!= NULL
)
3413 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3414 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3415 m_underMouse
= NULL
;
3419 wxDataViewModel
*model
= GetOwner()->GetModel();
3421 if (event
.Dragging())
3423 if (m_dragCount
== 0)
3425 // we have to report the raw, physical coords as we want to be
3426 // able to call HitTest(event.m_pointDrag) from the user code to
3427 // get the item being dragged
3428 m_dragStart
= event
.GetPosition();
3433 if (m_dragCount
!= 3)
3436 if (event
.LeftIsDown())
3438 // Notify cell about drag
3447 bool forceClick
= false;
3449 if (event
.ButtonDClick())
3451 m_renameTimer
->Stop();
3452 m_lastOnSame
= false;
3455 if (event
.LeftDClick())
3457 if ( current
== m_lineLastClicked
)
3459 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3461 wxDataViewItem item
= GetItemByRow(current
);
3463 model
->GetValue( value
, item
, col
->GetModelColumn() );
3464 cell
->SetValue( value
);
3465 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3466 col
->GetWidth(), m_lineHeight
);
3467 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3472 wxWindow
*parent
= GetParent();
3473 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3474 le
.SetItem( GetItemByRow(current
) );
3475 le
.SetEventObject(parent
);
3476 le
.SetModel(GetOwner()->GetModel());
3478 parent
->GetEventHandler()->ProcessEvent(le
);
3484 // The first click was on another item, so don't interpret this as
3485 // a double click, but as a simple click instead
3492 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3494 // select single line
3495 SelectAllRows( false );
3496 SelectRow( m_lineSelectSingleOnUp
, true );
3499 //Process the event of user clicking the expander
3500 bool expander
= false;
3501 if (GetOwner()->GetExpanderColumn() == col
)
3503 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3504 if( node
!=NULL
&& node
->HasChildren() )
3506 int indent
= node
->GetIndentLevel();
3507 indent
= GetOwner()->GetIndent()*indent
;
3508 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3509 if( rect
.Contains( x
, y
) )
3512 if( node
->IsOpen() )
3513 OnCollapsing(current
);
3515 OnExpanding( current
);
3519 //If the user click the expander, we do not do editing even if the column with expander are editable
3520 if (m_lastOnSame
&& !expander
)
3522 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3523 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3525 m_renameTimer
->Start( 100, true );
3529 m_lastOnSame
= false;
3530 m_lineSelectSingleOnUp
= (unsigned int)-1;
3534 // This is necessary, because after a DnD operation in
3535 // from and to ourself, the up event is swallowed by the
3536 // DnD code. So on next non-up event (which means here and
3537 // now) m_lineSelectSingleOnUp should be reset.
3538 m_lineSelectSingleOnUp
= (unsigned int)-1;
3541 if (event
.RightDown())
3543 m_lineBeforeLastClicked
= m_lineLastClicked
;
3544 m_lineLastClicked
= current
;
3546 // If the item is already selected, do not update the selection.
3547 // Multi-selections should not be cleared if a selected item is clicked.
3548 if (!IsRowSelected(current
))
3550 SelectAllRows(false);
3551 ChangeCurrentRow(current
);
3552 SelectRow(m_currentRow
,true);
3553 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3556 // notify cell about right click
3559 // Allow generation of context menu event
3562 else if (event
.MiddleDown())
3564 // notify cell about middle click
3567 if (event
.LeftDown() || forceClick
)
3571 m_lineBeforeLastClicked
= m_lineLastClicked
;
3572 m_lineLastClicked
= current
;
3574 unsigned int oldCurrentRow
= m_currentRow
;
3575 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3577 bool cmdModifierDown
= event
.CmdDown();
3578 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3580 if ( IsSingleSel() || !IsRowSelected(current
) )
3582 SelectAllRows( false );
3583 ChangeCurrentRow(current
);
3584 SelectRow(m_currentRow
,true);
3585 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3587 else // multi sel & current is highlighted & no mod keys
3589 m_lineSelectSingleOnUp
= current
;
3590 ChangeCurrentRow(current
); // change focus
3593 else // multi sel & either ctrl or shift is down
3595 if (cmdModifierDown
)
3597 ChangeCurrentRow(current
);
3598 ReverseRowSelection(m_currentRow
);
3599 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3601 else if (event
.ShiftDown())
3603 ChangeCurrentRow(current
);
3605 unsigned int lineFrom
= oldCurrentRow
,
3608 if ( lineTo
< lineFrom
)
3611 lineFrom
= m_currentRow
;
3614 SelectRows(lineFrom
, lineTo
, true);
3615 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3617 else // !ctrl, !shift
3619 // test in the enclosing if should make it impossible
3620 wxFAIL_MSG( _T("how did we get here?") );
3624 if (m_currentRow
!= oldCurrentRow
)
3625 RefreshRow( oldCurrentRow
);
3627 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3629 // Update selection here...
3632 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3633 (current
== oldCurrentRow
)) && oldWasSelected
;
3637 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3641 if (HasCurrentRow())
3647 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3651 if (HasCurrentRow())
3657 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3659 if( m_selection
.GetCount() != 1 )
3660 return wxDataViewItem();
3662 return GetItemByRow( m_selection
.Item(0));
3665 //-----------------------------------------------------------------------------
3667 //-----------------------------------------------------------------------------
3668 WX_DEFINE_LIST(wxDataViewColumnList
);
3670 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3672 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3673 EVT_SIZE(wxDataViewCtrl::OnSize
)
3676 wxDataViewCtrl::~wxDataViewCtrl()
3679 GetModel()->RemoveNotifier( m_notifier
);
3682 void wxDataViewCtrl::Init()
3687 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3688 const wxPoint
& pos
, const wxSize
& size
,
3689 long style
, const wxValidator
& validator
)
3691 if (!wxControl::Create( parent
, id
, pos
, size
,
3692 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3698 MacSetClipChildren( true ) ;
3701 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3703 if (HasFlag(wxDV_NO_HEADER
))
3704 m_headerArea
= NULL
;
3706 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3708 SetTargetWindow( m_clientArea
);
3710 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3712 sizer
->Add( m_headerArea
, 0, wxGROW
);
3713 sizer
->Add( m_clientArea
, 1, wxGROW
);
3720 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3724 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3727 // we need to process arrows ourselves for scrolling
3728 if ( nMsg
== WM_GETDLGCODE
)
3730 rc
|= DLGC_WANTARROWS
;
3738 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3740 // We need to override OnSize so that our scrolled
3741 // window a) does call Layout() to use sizers for
3742 // positioning the controls but b) does not query
3743 // the sizer for their size and use that for setting
3744 // the scrollable area as set that ourselves by
3745 // calling SetScrollbar() further down.
3752 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3754 if (!wxDataViewCtrlBase::AssociateModel( model
))
3757 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3759 model
->AddNotifier( m_notifier
);
3761 m_clientArea
->BuildTree(model
);
3763 m_clientArea
->UpdateDisplay();
3768 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3770 if (!wxDataViewCtrlBase::AppendColumn(col
))
3773 m_cols
.Append( col
);
3778 void wxDataViewCtrl::OnColumnChange()
3781 m_headerArea
->UpdateDisplay();
3783 m_clientArea
->UpdateDisplay();
3786 void wxDataViewCtrl::DoSetExpanderColumn()
3788 m_clientArea
->UpdateDisplay();
3791 void wxDataViewCtrl::DoSetIndent()
3793 m_clientArea
->UpdateDisplay();
3796 unsigned int wxDataViewCtrl::GetColumnCount() const
3798 return m_cols
.GetCount();
3801 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3803 wxDataViewColumnList::const_iterator iter
;
3805 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3810 if ((*iter
)->IsHidden())
3817 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3819 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3830 bool wxDataViewCtrl::ClearColumns()
3837 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3839 int ret
= 0, dead
= 0;
3840 int len
= GetColumnCount();
3841 for (int i
=0; i
<len
; i
++)
3843 wxDataViewColumn
* col
= GetColumn(i
);
3844 if (col
->IsHidden())
3846 ret
+= col
->GetWidth();
3849 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3856 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3861 //Selection code with wxDataViewItem as parameters
3862 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3864 return m_clientArea
->GetSelection();
3867 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3870 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3871 int len
= selection
.GetCount();
3872 for( int i
= 0; i
< len
; i
++)
3874 unsigned int row
= selection
[i
];
3875 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3880 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3882 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3883 int len
= sel
.GetCount();
3884 for( int i
= 0; i
< len
; i
++ )
3886 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3888 selection
.Add( static_cast<unsigned int>(row
) );
3890 m_clientArea
->SetSelections( selection
);
3893 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3895 int row
= m_clientArea
->GetRowByItem( item
);
3898 //Unselect all rows before select another in the single select mode
3899 if (m_clientArea
->IsSingleSel())
3900 m_clientArea
->SelectAllRows(false);
3901 m_clientArea
->SelectRow(row
, true);
3905 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3907 int row
= m_clientArea
->GetRowByItem( item
);
3909 m_clientArea
->SelectRow(row
, false);
3912 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3914 int row
= m_clientArea
->GetRowByItem( item
);
3917 return m_clientArea
->IsRowSelected(row
);
3922 //Selection code with row number as parameter
3923 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3926 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3927 int len
= selection
.GetCount();
3928 for( int i
= 0; i
< len
; i
++)
3930 unsigned int row
= selection
[i
];
3936 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3938 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3939 int len
= sel
.GetCount();
3940 for( int i
= 0; i
< len
; i
++ )
3944 selection
.Add( static_cast<unsigned int>(row
) );
3946 m_clientArea
->SetSelections( selection
);
3949 void wxDataViewCtrl::Select( int row
)
3953 if (m_clientArea
->IsSingleSel())
3954 m_clientArea
->SelectAllRows(false);
3955 m_clientArea
->SelectRow( row
, true );
3959 void wxDataViewCtrl::Unselect( int row
)
3962 m_clientArea
->SelectRow(row
, false);
3965 bool wxDataViewCtrl::IsSelected( int row
) const
3968 return m_clientArea
->IsRowSelected(row
);
3972 void wxDataViewCtrl::SelectRange( int from
, int to
)
3975 for( int i
= from
; i
< to
; i
++ )
3977 m_clientArea
->Select(sel
);
3980 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3982 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3983 for( int i
= from
; i
< to
; i
++ )
3984 if( sel
.Index( i
) != wxNOT_FOUND
)
3986 m_clientArea
->SetSelections(sel
);
3989 void wxDataViewCtrl::SelectAll()
3991 m_clientArea
->SelectAllRows(true);
3994 void wxDataViewCtrl::UnselectAll()
3996 m_clientArea
->SelectAllRows(false);
3999 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4003 if( row
> (int) m_clientArea
->GetRowCount() )
4004 row
= m_clientArea
->GetRowCount();
4006 int first
= m_clientArea
->GetFirstVisibleRow();
4007 int last
= m_clientArea
->GetLastVisibleRow();
4009 m_clientArea
->ScrollTo( row
, column
);
4010 else if( row
> last
)
4011 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4013 m_clientArea
->ScrollTo( first
, column
);
4016 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4018 int row
= m_clientArea
->GetRowByItem(item
);
4021 if( column
== NULL
)
4022 EnsureVisible(row
, -1);
4026 int len
= GetColumnCount();
4027 for( int i
= 0; i
< len
; i
++ )
4028 if( GetColumn(i
) == column
)
4033 EnsureVisible( row
, col
);
4039 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4041 m_clientArea
->HitTest(point
, item
, column
);
4044 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4046 return m_clientArea
->GetItemRect(item
, column
);
4049 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4051 return m_clientArea
->GetItemByRow( row
);
4054 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4056 return m_clientArea
->GetRowByItem( item
);
4059 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4061 int row
= m_clientArea
->GetRowByItem( item
);
4063 m_clientArea
->Expand(row
);
4066 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4068 int row
= m_clientArea
->GetRowByItem( item
);
4070 m_clientArea
->Collapse(row
);
4074 // !wxUSE_GENERICDATAVIEWCTRL
4077 // wxUSE_DATAVIEWCTRL