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 cell padding on the top/bottom
60 static const int PADDING_TOPBOTTOM
= 1;
62 // the expander space margin
63 static const int EXPANDER_MARGIN
= 4;
65 //-----------------------------------------------------------------------------
66 // wxDataViewHeaderWindow
67 //-----------------------------------------------------------------------------
69 #define USE_NATIVE_HEADER_WINDOW 0
71 //Below is the compare stuff
72 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
73 static wxDataViewModel
* g_model
;
74 static int g_column
= -2;
75 static bool g_asending
= true;
77 // NB: for some reason, this class must be dllexport'ed or we get warnings from
79 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
82 wxDataViewHeaderWindowBase()
85 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
86 const wxPoint
&pos
, const wxSize
&size
,
89 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
92 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
93 wxDataViewCtrl
*GetOwner() { return m_owner
; }
95 // called on column addition/removal
96 virtual void UpdateDisplay() { /* by default, do nothing */ }
98 // returns the n-th column
99 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
102 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
109 wxDataViewCtrl
*m_owner
;
111 // sends an event generated from the n-th wxDataViewColumn
112 void SendEvent(wxEventType type
, unsigned int n
);
115 // on wxMSW the header window (only that part however) can be made native!
116 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
118 #define COLUMN_WIDTH_OFFSET 2
119 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
121 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
125 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
127 const wxPoint
&pos
= wxDefaultPosition
,
128 const wxSize
&size
= wxDefaultSize
,
129 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
131 Create(parent
, id
, pos
, size
, name
);
134 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
135 const wxPoint
&pos
, const wxSize
&size
,
136 const wxString
&name
);
138 ~wxDataViewHeaderWindowMSW();
140 // called when any column setting is changed and/or changed
142 virtual void UpdateDisplay();
144 // called when the main window gets scrolled
145 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
148 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
149 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
151 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
153 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
154 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
157 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
160 #else // !defined(__WXMSW__)
162 #define HEADER_WINDOW_HEIGHT 25
163 #define HEADER_HORIZ_BORDER 5
164 #define HEADER_VERT_BORDER 3
165 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
167 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
170 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
172 const wxPoint
&pos
= wxDefaultPosition
,
173 const wxSize
&size
= wxDefaultSize
,
174 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
177 Create(parent
, id
, pos
, size
, name
);
180 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
181 const wxPoint
&pos
, const wxSize
&size
,
182 const wxString
&name
);
184 ~wxGenericDataViewHeaderWindow()
186 delete m_resizeCursor
;
189 virtual void UpdateDisplay() { Refresh(); }
193 void OnPaint( wxPaintEvent
&event
);
194 void OnMouse( wxMouseEvent
&event
);
195 void OnSetFocus( wxFocusEvent
&event
);
200 // vars used for column resizing:
202 wxCursor
*m_resizeCursor
;
203 const wxCursor
*m_currentCursor
;
206 bool m_dirty
; // needs refresh?
207 int m_column
; // index of the column being resized
208 int m_currentX
; // divider line position in logical (unscrolled) coords
209 int m_minX
; // minimal position beyond which the divider line
210 // can't be dragged in logical coords
212 // the pen used to draw the current column width drag line
213 // when resizing the columsn
217 // internal utilities:
221 m_currentCursor
= (wxCursor
*) NULL
;
222 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
224 m_isDragging
= false;
227 m_column
= wxNOT_FOUND
;
231 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
232 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
236 void AdjustDC(wxDC
& dc
);
239 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
240 DECLARE_EVENT_TABLE()
243 #endif // defined(__WXMSW__)
245 //-----------------------------------------------------------------------------
246 // wxDataViewRenameTimer
247 //-----------------------------------------------------------------------------
249 class wxDataViewRenameTimer
: public wxTimer
252 wxDataViewMainWindow
*m_owner
;
255 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
259 //-----------------------------------------------------------------------------
260 // wxDataViewTreeNode
261 //-----------------------------------------------------------------------------
262 class wxDataViewTreeNode
;
263 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
264 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
266 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
267 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
269 class wxDataViewTreeNode
272 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
273 { this->parent
= parent
;
281 //I don't know what I need to do in the destructure
282 ~wxDataViewTreeNode()
287 wxDataViewTreeNode
* GetParent() { return parent
; }
288 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
289 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
290 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
292 void AddNode( wxDataViewTreeNode
* node
)
294 leaves
.Add( node
->GetItem().GetID() );
296 leaves
.Sort( &wxGenericTreeModelItemCmp
);
299 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
301 void AddLeaf( void * leaf
)
305 leaves
.Sort( &wxGenericTreeModelItemCmp
);
308 wxDataViewItem
& GetItem() { return item
; }
309 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
311 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
312 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
316 wxDataViewTreeNode
* node
= this;
317 while( node
->GetParent()->GetParent() != NULL
)
319 node
= node
->GetParent();
332 int len
= nodes
.GetCount();
334 for ( int i
= 0 ;i
< len
; i
++)
335 sum
+= nodes
[i
]->GetSubTreeCount();
337 sum
+= leaves
.GetCount();
340 ChangeSubTreeCount(-sum
);
346 ChangeSubTreeCount(sum
);
349 bool HasChildren() { return hasChildren
; }
350 void SetHasChildren( bool has
){ hasChildren
= has
; }
352 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
353 int GetSubTreeCount() { return subTreeCount
; }
354 void ChangeSubTreeCount( int num
)
360 parent
->ChangeSubTreeCount(num
);
367 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
368 int len
= nodes
.GetCount();
369 for (int i
= 0; i
< len
; i
++)
373 leaves
.Sort( &wxGenericTreeModelItemCmp
);
378 wxDataViewTreeNode
* parent
;
379 wxDataViewTreeNodes nodes
;
380 wxDataViewTreeLeaves leaves
;
387 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
389 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
392 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
394 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
399 //-----------------------------------------------------------------------------
400 // wxDataViewMainWindow
401 //-----------------------------------------------------------------------------
403 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
405 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
406 WX_DEFINE_LIST(ItemList
);
408 class wxDataViewMainWindow
: public wxWindow
411 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
413 const wxPoint
&pos
= wxDefaultPosition
,
414 const wxSize
&size
= wxDefaultSize
,
415 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
416 virtual ~wxDataViewMainWindow();
418 // notifications from wxDataViewModel
419 void SendModelEvent( wxEventType type
, const wxDataViewItem
& item
);
420 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
421 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
422 bool ItemChanged( const wxDataViewItem
&item
);
423 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
434 g_model
= GetOwner()->GetModel();
435 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
438 if (g_model
->HasDefaultCompare())
446 g_column
= col
->GetModelColumn();
447 g_asending
= col
->IsSortOrderAscending();
450 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
451 wxDataViewCtrl
*GetOwner() { return m_owner
; }
452 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
454 void OnPaint( wxPaintEvent
&event
);
455 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
456 void OnChar( wxKeyEvent
&event
);
457 void OnMouse( wxMouseEvent
&event
);
458 void OnSetFocus( wxFocusEvent
&event
);
459 void OnKillFocus( wxFocusEvent
&event
);
461 void UpdateDisplay();
462 void RecalculateDisplay();
463 void OnInternalIdle();
465 void OnRenameTimer();
467 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
468 void ScrollTo( int rows
, int column
);
470 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
471 void ChangeCurrentRow( unsigned int row
);
473 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
474 bool IsEmpty() { return GetRowCount() == 0; }
476 int GetCountPerPage() const;
477 int GetEndOfLastCol() const;
478 unsigned int GetFirstVisibleRow() const;
479 //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
480 unsigned int GetLastVisibleRow();
481 unsigned int GetRowCount() ;
483 wxDataViewItem
GetSelection() const;
484 wxDataViewSelection
GetSelections(){ return m_selection
; }
485 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
486 void Select( const wxArrayInt
& aSelections
);
487 void SelectAllRows( bool on
);
488 void SelectRow( unsigned int row
, bool on
);
489 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
490 void ReverseRowSelection( unsigned int row
);
491 bool IsRowSelected( unsigned int row
);
493 void RefreshRow( unsigned int row
);
494 void RefreshRows( unsigned int from
, unsigned int to
);
495 void RefreshRowsAfter( unsigned int firstRow
);
497 // returns the colour to be used for drawing the rules
498 wxColour
GetRuleColour() const
500 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
503 //void EnsureVisible( unsigned int row );
504 wxRect
GetLineRect( unsigned int row
) const;
506 //Some useful functions for row and item mapping
507 wxDataViewItem
GetItemByRow( unsigned int row
) const;
508 int GetRowByItem( const wxDataViewItem
& item
);
510 //Methods for building the mapping tree
511 void BuildTree( wxDataViewModel
* model
);
513 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
514 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
516 void Expand( unsigned int row
) { OnExpanding( row
); }
517 void Collapse( unsigned int row
) { OnCollapsing( row
); }
519 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
520 //We did not need this temporarily
521 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
523 int RecalculateCount() ;
525 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
526 void OnExpanding( unsigned int row
);
527 void OnCollapsing( unsigned int row
);
529 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
532 wxDataViewCtrl
*m_owner
;
536 wxDataViewColumn
*m_currentCol
;
537 unsigned int m_currentRow
;
538 wxDataViewSelection m_selection
;
540 wxDataViewRenameTimer
*m_renameTimer
;
548 // for double click logic
549 unsigned int m_lineLastClicked
,
550 m_lineBeforeLastClicked
,
551 m_lineSelectSingleOnUp
;
553 // the pen used to draw horiz/vertical rules
556 // the pen used to draw the expander and the lines
559 //This is the tree structure of the model
560 wxDataViewTreeNode
* m_root
;
562 //This is the tree node under the cursor
563 wxDataViewTreeNode
* m_underMouse
;
565 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
566 DECLARE_EVENT_TABLE()
569 // ---------------------------------------------------------
570 // wxGenericDataViewModelNotifier
571 // ---------------------------------------------------------
573 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
576 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
577 { m_mainWindow
= mainWindow
; }
579 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
580 { return m_mainWindow
->ItemAdded( parent
, item
); }
581 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
582 { return m_mainWindow
->ItemDeleted( parent
, item
); }
583 virtual bool ItemChanged( const wxDataViewItem
& item
)
584 { return m_mainWindow
->ItemChanged(item
); }
585 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
586 { return m_mainWindow
->ValueChanged( item
, col
); }
587 virtual bool Cleared()
588 { return m_mainWindow
->Cleared(); }
589 virtual void Resort()
590 { m_mainWindow
->Resort(); }
592 wxDataViewMainWindow
*m_mainWindow
;
595 // ---------------------------------------------------------
596 // wxDataViewRenderer
597 // ---------------------------------------------------------
599 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
601 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
602 wxDataViewCellMode mode
,
604 wxDataViewRendererBase( varianttype
, mode
, align
)
611 wxDataViewRenderer::~wxDataViewRenderer()
617 wxDC
*wxDataViewRenderer::GetDC()
621 if (GetOwner() == NULL
)
623 if (GetOwner()->GetOwner() == NULL
)
625 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
631 // ---------------------------------------------------------
632 // wxDataViewCustomRenderer
633 // ---------------------------------------------------------
635 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
637 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
638 wxDataViewCellMode mode
, int align
) :
639 wxDataViewRenderer( varianttype
, mode
, align
)
643 // ---------------------------------------------------------
644 // wxDataViewTextRenderer
645 // ---------------------------------------------------------
647 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
649 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
650 wxDataViewCellMode mode
, int align
) :
651 wxDataViewCustomRenderer( varianttype
, mode
, align
)
655 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
657 m_text
= value
.GetString();
662 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
667 bool wxDataViewTextRenderer::HasEditorCtrl()
672 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
673 wxRect labelRect
, const wxVariant
&value
)
675 return new wxTextCtrl( parent
, wxID_ANY
, value
,
676 wxPoint(labelRect
.x
,labelRect
.y
),
677 wxSize(labelRect
.width
,labelRect
.height
) );
680 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
682 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
683 value
= text
->GetValue();
687 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
689 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
690 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
691 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
692 view
->GetForegroundColour();
694 dc
->SetTextForeground(col
);
695 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
700 wxSize
wxDataViewTextRenderer::GetSize() const
702 const wxDataViewCtrl
*view
= GetView();
706 view
->GetTextExtent( m_text
, &x
, &y
);
707 return wxSize( x
, y
);
709 return wxSize(80,20);
712 // ---------------------------------------------------------
713 // wxDataViewBitmapRenderer
714 // ---------------------------------------------------------
716 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
718 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
719 wxDataViewCellMode mode
, int align
) :
720 wxDataViewCustomRenderer( varianttype
, mode
, align
)
724 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
726 if (value
.GetType() == wxT("wxBitmap"))
728 if (value
.GetType() == wxT("wxIcon"))
734 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
739 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
742 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
743 else if (m_icon
.Ok())
744 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
749 wxSize
wxDataViewBitmapRenderer::GetSize() const
752 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
753 else if (m_icon
.Ok())
754 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
756 return wxSize(16,16);
759 // ---------------------------------------------------------
760 // wxDataViewToggleRenderer
761 // ---------------------------------------------------------
763 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
765 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
766 wxDataViewCellMode mode
, int align
) :
767 wxDataViewCustomRenderer( varianttype
, mode
, align
)
772 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
774 m_toggle
= value
.GetBool();
779 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
784 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
786 // User wxRenderer here
789 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
791 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
796 flags
|= wxCONTROL_CHECKED
;
797 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
798 flags
|= wxCONTROL_DISABLED
;
800 wxRendererNative::Get().DrawCheckBox(
801 GetOwner()->GetOwner(),
809 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
810 wxDataViewModel
*model
,
811 const wxDataViewItem
& item
, unsigned int col
)
813 bool value
= !m_toggle
;
814 wxVariant variant
= value
;
815 model
->SetValue( variant
, item
, col
);
816 model
->ValueChanged( item
, col
);
820 wxSize
wxDataViewToggleRenderer::GetSize() const
822 return wxSize(20,20);
825 // ---------------------------------------------------------
826 // wxDataViewProgressRenderer
827 // ---------------------------------------------------------
829 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
831 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
832 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
833 wxDataViewCustomRenderer( varianttype
, mode
, align
)
839 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
843 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
845 m_value
= (long) value
;
847 if (m_value
< 0) m_value
= 0;
848 if (m_value
> 100) m_value
= 100;
853 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
855 value
= (long) m_value
;
859 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
861 double pct
= (double)m_value
/ 100.0;
863 bar
.width
= (int)(cell
.width
* pct
);
864 dc
->SetPen( *wxTRANSPARENT_PEN
);
865 dc
->SetBrush( *wxBLUE_BRUSH
);
866 dc
->DrawRectangle( bar
);
868 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
869 dc
->SetPen( *wxBLACK_PEN
);
870 dc
->DrawRectangle( cell
);
875 wxSize
wxDataViewProgressRenderer::GetSize() const
877 return wxSize(40,12);
880 // ---------------------------------------------------------
881 // wxDataViewDateRenderer
882 // ---------------------------------------------------------
884 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
886 #if wxUSE_DATE_RENDERER_POPUP
888 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
891 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
892 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
893 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
898 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
899 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
900 sizer
->Add( m_cal
, 1, wxGROW
);
905 void OnCalendar( wxCalendarEvent
&event
);
907 wxCalendarCtrl
*m_cal
;
908 wxDataViewModel
*m_model
;
910 const wxDataViewItem
& m_item
;
913 virtual void OnDismiss()
918 DECLARE_EVENT_TABLE()
921 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
922 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
925 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
927 wxDateTime date
= event
.GetDate();
928 wxVariant value
= date
;
929 m_model
->SetValue( value
, m_item
, m_col
);
930 m_model
->ValueChanged( m_item
, m_col
);
934 #endif // wxUSE_DATE_RENDERER_POPUP
936 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
938 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
939 wxDataViewCellMode mode
, int align
) :
940 wxDataViewCustomRenderer( varianttype
, mode
, align
)
944 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
946 m_date
= value
.GetDateTime();
951 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
957 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
959 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
960 wxString tmp
= m_date
.FormatDate();
961 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
966 wxSize
wxDataViewDateRenderer::GetSize() const
968 const wxDataViewCtrl
* view
= GetView();
969 wxString tmp
= m_date
.FormatDate();
971 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
972 return wxSize(x
,y
+d
);
975 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
976 const wxDataViewItem
& item
, unsigned int col
)
979 model
->GetValue( variant
, item
, col
);
980 wxDateTime value
= variant
.GetDateTime();
982 #if wxUSE_DATE_RENDERER_POPUP
983 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
984 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
985 wxPoint pos
= wxGetMousePosition();
988 popup
->Popup( popup
->m_cal
);
989 #else // !wxUSE_DATE_RENDERER_POPUP
990 wxMessageBox(value
.Format());
991 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
995 // ---------------------------------------------------------
996 // wxDataViewIconTextRenderer
997 // ---------------------------------------------------------
999 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1001 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1002 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1003 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1006 SetAlignment(align
);
1009 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1013 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1019 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1024 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1026 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1028 const wxIcon
&icon
= m_value
.GetIcon();
1031 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1032 cell
.x
+= icon
.GetWidth()+4;
1035 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1040 wxSize
wxDataViewIconTextRenderer::GetSize() const
1042 return wxSize(80,16); // TODO
1045 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1050 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1055 // ---------------------------------------------------------
1057 // ---------------------------------------------------------
1059 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1061 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1062 unsigned int model_column
,
1063 int width
, wxAlignment align
, int flags
) :
1064 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1066 SetAlignment(align
);
1070 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1073 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1074 unsigned int model_column
,
1075 int width
, wxAlignment align
, int flags
) :
1076 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1078 SetAlignment(align
);
1081 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1084 wxDataViewColumn::~wxDataViewColumn()
1088 void wxDataViewColumn::Init( int width
)
1091 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1095 void wxDataViewColumn::SetResizeable( bool resizeable
)
1098 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1100 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1103 void wxDataViewColumn::SetHidden( bool hidden
)
1106 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1108 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1110 // tell our owner to e.g. update its scrollbars:
1112 GetOwner()->OnColumnChange();
1115 void wxDataViewColumn::SetSortable( bool sortable
)
1118 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1120 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1122 // Update header button
1124 GetOwner()->OnColumnChange();
1127 void wxDataViewColumn::SetSortOrder( bool ascending
)
1129 m_ascending
= ascending
;
1131 // Update header button
1133 GetOwner()->OnColumnChange();
1136 bool wxDataViewColumn::IsSortOrderAscending() const
1141 void wxDataViewColumn::SetInternalWidth( int width
)
1145 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1146 if (m_owner
&& m_owner
->m_clientArea
)
1147 m_owner
->m_clientArea
->RecalculateDisplay();
1150 void wxDataViewColumn::SetWidth( int width
)
1152 m_owner
->m_headerArea
->UpdateDisplay();
1154 SetInternalWidth(width
);
1158 //-----------------------------------------------------------------------------
1159 // wxDataViewHeaderWindowBase
1160 //-----------------------------------------------------------------------------
1162 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1164 wxWindow
*parent
= GetParent();
1165 wxDataViewEvent
le(type
, parent
->GetId());
1167 le
.SetEventObject(parent
);
1169 le
.SetDataViewColumn(GetColumn(n
));
1170 le
.SetModel(GetOwner()->GetModel());
1172 // for events created by wxDataViewHeaderWindow the
1173 // row / value fields are not valid
1175 parent
->GetEventHandler()->ProcessEvent(le
);
1178 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1180 // implemented in msw/listctrl.cpp:
1181 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1183 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1185 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1186 const wxPoint
&pos
, const wxSize
&size
,
1187 const wxString
&name
)
1191 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1194 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1195 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1196 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1197 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1199 // create the native WC_HEADER window:
1200 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1201 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1202 m_hWnd
= CreateWindowEx(0,
1213 wxLogLastError(_T("CreateWindowEx"));
1217 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1218 // to call wxDataViewHeaderWindow::MSWOnNotify
1219 SubclassWin(m_hWnd
);
1221 // the following is required to get the default win's font for
1222 // header windows and must be done befor sending the HDM_LAYOUT msg
1229 // Retrieve the bounding rectangle of the parent window's
1230 // client area, and then request size and position values
1231 // from the header control.
1232 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1234 hdl
.prc
= &rcParent
;
1236 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1238 wxLogLastError(_T("SendMessage"));
1242 // Set the size, position, and visibility of the header control.
1243 SetWindowPos((HWND
)m_hWnd
,
1247 wp
.flags
| SWP_SHOWWINDOW
);
1249 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1250 // a wxBoxSizer and in order to avoid super-big header windows,
1251 // we need to set our height as fixed
1252 SetMinSize(wxSize(-1, wp
.cy
));
1253 SetMaxSize(wxSize(-1, wp
.cy
));
1258 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1263 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1265 // remove old columns
1266 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1267 Header_DeleteItem((HWND
)m_hWnd
, 0);
1269 // add the updated array of columns to the header control
1270 unsigned int cols
= GetOwner()->GetColumnCount();
1271 unsigned int added
= 0;
1272 wxDataViewModel
* model
= GetOwner()->GetModel();
1273 for (unsigned int i
= 0; i
< cols
; i
++)
1275 wxDataViewColumn
*col
= GetColumn( i
);
1276 if (col
->IsHidden())
1277 continue; // don't add it!
1280 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1281 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1282 hdi
.cxy
= col
->GetWidth();
1283 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1284 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1285 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1288 if(model
&& m_owner
->GetSortingColumn() == col
)
1290 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1291 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1292 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1296 // lParam is reserved for application's use:
1297 // we store there the column index to use it later in MSWOnNotify
1298 // (since columns may have been hidden)
1299 hdi
.lParam
= (LPARAM
)i
;
1301 // the native wxMSW implementation of the header window
1302 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1303 // on the right: to correct this effect we make the column
1304 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1306 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1308 switch (col
->GetAlignment())
1311 hdi
.fmt
|= HDF_LEFT
;
1313 case wxALIGN_CENTER
:
1314 case wxALIGN_CENTER_HORIZONTAL
:
1315 hdi
.fmt
|= HDF_CENTER
;
1318 hdi
.fmt
|= HDF_RIGHT
;
1322 // such alignment is not allowed for the column header!
1326 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1327 (WPARAM
)added
, (LPARAM
)&hdi
);
1332 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1336 // NOTE: we don't just return nmHDR->iItem because when there are
1337 // hidden columns, nmHDR->iItem may be different from
1338 // nmHDR->pitem->lParam
1340 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1342 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1347 item
.mask
= HDI_LPARAM
;
1348 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1350 return (unsigned int)item
.lParam
;
1353 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1355 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1357 // is it a message from the header?
1358 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1359 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1361 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1362 switch ( nmhdr
->code
)
1364 case HDN_BEGINTRACK
:
1365 // user has started to resize a column:
1366 // do we need to veto it?
1367 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1375 // user has started to reorder a column
1378 case HDN_ITEMCHANGING
:
1379 if (nmHDR
->pitem
!= NULL
&&
1380 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1382 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1383 if (nmHDR
->pitem
->cxy
< minWidth
)
1385 // do not allow the user to resize this column under
1386 // its minimal width:
1392 case HDN_ITEMCHANGED
: // user is resizing a column
1393 case HDN_ENDTRACK
: // user has finished resizing a column
1394 case HDN_ENDDRAG
: // user has finished reordering a column
1396 // update the width of the modified column:
1397 if (nmHDR
->pitem
!= NULL
&&
1398 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1400 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1401 unsigned int w
= nmHDR
->pitem
->cxy
;
1402 wxDataViewColumn
*col
= GetColumn(idx
);
1404 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1405 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1406 w
-= COLUMN_WIDTH_OFFSET
;
1408 if (w
>= (unsigned)col
->GetMinWidth())
1409 col
->SetInternalWidth(w
);
1415 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1416 wxDataViewModel
* model
= GetOwner()->GetModel();
1418 if(nmHDR
->iButton
== 0)
1420 wxDataViewColumn
*col
= GetColumn(idx
);
1421 if(col
->IsSortable())
1423 if(model
&& m_owner
->GetSortingColumn() == col
)
1425 bool order
= col
->IsSortOrderAscending();
1426 col
->SetSortOrder(!order
);
1430 m_owner
->SetSortingColumn(col
);
1438 wxEventType evt
= nmHDR
->iButton
== 0 ?
1439 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1440 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1441 SendEvent(evt
, idx
);
1447 // NOTE: for some reason (i.e. for a bug in Windows)
1448 // the HDN_ITEMCLICK notification is not sent on
1449 // right clicks, so we need to handle NM_RCLICK
1452 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1453 if (column
!= wxNOT_FOUND
)
1456 item
.mask
= HDI_LPARAM
;
1457 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1459 // 'idx' may be different from 'column' if there are
1460 // hidden columns...
1461 unsigned int idx
= (unsigned int)item
.lParam
;
1462 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1468 case HDN_GETDISPINFOW
:
1469 // see wxListCtrl::MSWOnNotify for more info!
1472 case HDN_ITEMDBLCLICK
:
1474 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1475 int w
= GetOwner()->GetBestColumnWidth(idx
);
1477 // update the native control:
1479 ZeroMemory(&hd
, sizeof(hd
));
1480 hd
.mask
= HDI_WIDTH
;
1482 Header_SetItem(GetHwnd(),
1483 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1486 // update the wxDataViewColumn class:
1487 GetColumn(idx
)->SetInternalWidth(w
);
1492 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1498 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1499 const wxRect
*WXUNUSED(rect
))
1501 wxSize ourSz
= GetClientSize();
1502 wxSize ownerSz
= m_owner
->GetClientSize();
1504 // where should the (logical) origin of this window be placed?
1506 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1508 // put this window on top of our parent and
1509 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1510 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1514 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1515 int WXUNUSED(w
), int WXUNUSED(h
),
1518 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1519 // the wxDataViewCtrl window gets resized: the following dummy call
1520 // to ScrollWindow() is required in order to get this header window
1521 // correctly repainted when it's (horizontally) scrolled:
1526 #else // !defined(__WXMSW__)
1528 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1529 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1530 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1531 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1532 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1535 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1536 const wxPoint
&pos
, const wxSize
&size
,
1537 const wxString
&name
)
1541 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1544 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1545 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1546 SetOwnForegroundColour( attr
.colFg
);
1547 SetOwnBackgroundColour( attr
.colBg
);
1549 SetOwnFont( attr
.font
);
1551 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1552 // a wxBoxSizer and in order to avoid super-big header windows,
1553 // we need to set our height as fixed
1554 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1555 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1560 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1563 GetClientSize( &w
, &h
);
1565 wxAutoBufferedPaintDC
dc( this );
1567 dc
.SetBackground(GetBackgroundColour());
1571 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1574 m_owner
->GetViewStart( &x
, NULL
);
1576 // account for the horz scrollbar offset
1577 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1579 dc
.SetFont( GetFont() );
1581 unsigned int cols
= GetOwner()->GetColumnCount();
1584 for (i
= 0; i
< cols
; i
++)
1586 wxDataViewColumn
*col
= GetColumn( i
);
1587 if (col
->IsHidden())
1588 continue; // skip it!
1590 int cw
= col
->GetWidth();
1593 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1594 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1596 if (col
->IsSortOrderAscending())
1597 sortArrow
= wxHDR_SORT_ICON_UP
;
1599 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1602 wxRendererNative::Get().DrawHeaderButton
1606 wxRect(xpos
, 0, cw
, ch
-1),
1607 m_parent
->IsEnabled() ? 0
1608 : (int)wxCONTROL_DISABLED
,
1612 // align as required the column title:
1614 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1615 switch (col
->GetAlignment())
1618 x
+= HEADER_HORIZ_BORDER
;
1620 case wxALIGN_CENTER
:
1621 case wxALIGN_CENTER_HORIZONTAL
:
1622 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1625 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1629 // always center the title vertically:
1630 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1632 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1634 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1635 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1636 dc
.DrawText( col
->GetTitle(), x
, y
);
1637 dc
.DestroyClippingRegion();
1643 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1645 GetParent()->SetFocus();
1649 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1651 // we want to work with logical coords
1653 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1654 int y
= event
.GetY();
1658 // we don't draw the line beyond our window,
1659 // but we allow dragging it there
1661 GetClientSize( &w
, NULL
);
1662 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1665 // erase the line if it was drawn
1669 if (event
.ButtonUp())
1671 m_isDragging
= false;
1677 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1680 GetOwner()->Refresh();
1684 m_currentX
= wxMax(m_minX
+ 7, x
);
1686 // draw in the new location
1687 if (m_currentX
< w
) DrawCurrent();
1691 else // not dragging
1694 m_column
= wxNOT_FOUND
;
1696 bool hit_border
= false;
1698 // end of the current column
1701 // find the column where this event occured
1702 int countCol
= m_owner
->GetColumnCount();
1703 for (int column
= 0; column
< countCol
; column
++)
1705 wxDataViewColumn
*p
= GetColumn(column
);
1708 continue; // skip if not shown
1710 xpos
+= p
->GetWidth();
1712 if ((abs(x
-xpos
) < 3) && (y
< 22))
1720 // inside the column
1727 if (m_column
== wxNOT_FOUND
)
1730 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1731 if (event
.LeftDClick() && resizeable
)
1733 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1736 else if (event
.LeftDown() || event
.RightUp())
1738 if (hit_border
&& event
.LeftDown() && resizeable
)
1740 m_isDragging
= true;
1745 else // click on a column
1747 wxDataViewModel
* model
= GetOwner()->GetModel();
1748 wxEventType evt
= event
.LeftDown() ?
1749 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1750 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1751 SendEvent(evt
, m_column
);
1753 //Left click the header
1754 if(event
.LeftDown())
1756 wxDataViewColumn
*col
= GetColumn(m_column
);
1757 if(col
->IsSortable())
1759 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1760 if(model
&& sortCol
== col
)
1762 bool order
= col
->IsSortOrderAscending();
1763 col
->SetSortOrder(!order
);
1767 m_owner
->SetSortingColumn(col
);
1773 //Send the column sorted event
1774 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1778 else if (event
.Moving())
1780 if (hit_border
&& resizeable
)
1781 m_currentCursor
= m_resizeCursor
;
1783 m_currentCursor
= wxSTANDARD_CURSOR
;
1785 SetCursor(*m_currentCursor
);
1790 void wxGenericDataViewHeaderWindow::DrawCurrent()
1792 int x1
= m_currentX
;
1794 ClientToScreen (&x1
, &y1
);
1796 int x2
= m_currentX
-1;
1798 ++x2
; // but why ????
1801 m_owner
->GetClientSize( NULL
, &y2
);
1802 m_owner
->ClientToScreen( &x2
, &y2
);
1805 dc
.SetLogicalFunction(wxINVERT
);
1806 dc
.SetPen(m_penCurrent
);
1807 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1809 dc
.DrawLine(x1
, y1
, x2
, y2
);
1812 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1816 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1817 m_owner
->GetViewStart( &x
, NULL
);
1819 // shift the DC origin to match the position of the main window horizontal
1820 // scrollbar: this allows us to always use logical coords
1821 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1824 #endif // defined(__WXMSW__)
1826 //-----------------------------------------------------------------------------
1827 // wxDataViewRenameTimer
1828 //-----------------------------------------------------------------------------
1830 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1835 void wxDataViewRenameTimer::Notify()
1837 m_owner
->OnRenameTimer();
1840 //-----------------------------------------------------------------------------
1841 // wxDataViewMainWindow
1842 //-----------------------------------------------------------------------------
1844 //The tree building helper, declared firstly
1845 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1847 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1849 if (row1
> row2
) return 1;
1850 if (row1
== row2
) return 0;
1855 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1857 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1858 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1859 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1860 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1861 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1862 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1865 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1866 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1867 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1868 m_selection( wxDataViewSelectionCmp
)
1873 m_lastOnSame
= false;
1874 m_renameTimer
= new wxDataViewRenameTimer( this );
1876 // TODO: user better initial values/nothing selected
1877 m_currentCol
= NULL
;
1880 // TODO: we need to calculate this smartly
1887 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1890 m_dragStart
= wxPoint(0,0);
1891 m_lineLastClicked
= (unsigned int) -1;
1892 m_lineBeforeLastClicked
= (unsigned int) -1;
1893 m_lineSelectSingleOnUp
= (unsigned int) -1;
1897 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1898 SetBackgroundColour( *wxWHITE
);
1900 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1902 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1903 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1904 //Some new added code to deal with the tree structure
1905 m_root
= new wxDataViewTreeNode( NULL
);
1906 m_root
->SetHasChildren(true);
1908 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1910 m_underMouse
= NULL
;
1914 wxDataViewMainWindow::~wxDataViewMainWindow()
1917 delete m_renameTimer
;
1920 void wxDataViewMainWindow::OnRenameTimer()
1922 // We have to call this here because changes may just have
1923 // been made and no screen update taken place.
1928 unsigned int cols
= GetOwner()->GetColumnCount();
1930 for (i
= 0; i
< cols
; i
++)
1932 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1934 continue; // skip it!
1936 if (c
== m_currentCol
)
1938 xpos
+= c
->GetWidth();
1940 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1941 m_currentCol
->GetWidth(), m_lineHeight
);
1943 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1944 &labelRect
.x
, &labelRect
.y
);
1946 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1947 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1951 //------------------------------------------------------------------
1952 // Helper class for do operation on the tree node
1953 //------------------------------------------------------------------
1960 //The return value control how the tree-walker tranverse the tree
1961 // 0: Job done, stop tranverse and return
1962 // 1: Ignore the current node's subtree and continue
1963 // 2: Job not done, continue
1964 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1965 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1966 virtual int operator() ( void * n
) = 0;
1969 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1974 switch( func( node
) )
1985 wxDataViewTreeNodes nodes
= node
->GetNodes();
1986 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1988 int len_nodes
= nodes
.GetCount();
1989 int len
= leaves
.GetCount();
1990 int i
= 0, nodes_i
= 0;
1992 for( ; i
< len
; i
++ )
1994 void * n
= leaves
[i
];
1995 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1997 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2000 if( Walker( nd
, func
) )
2019 void wxDataViewMainWindow::SendModelEvent( wxEventType type
, const wxDataViewItem
& item
)
2021 wxWindow
*parent
= GetParent();
2022 wxDataViewEvent
le(type
, parent
->GetId());
2024 le
.SetEventObject(parent
);
2025 le
.SetModel(GetOwner()->GetModel());
2028 parent
->GetEventHandler()->ProcessEvent(le
);
2031 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2035 wxDataViewTreeNode
* node
;
2036 node
= FindNode(parent
);
2037 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, item
);
2042 node
->SetHasChildren( true );
2044 if( g_model
->IsContainer( item
) )
2046 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2047 newnode
->SetItem(item
);
2048 newnode
->SetHasChildren( true );
2049 node
->AddNode( newnode
);
2052 node
->AddLeaf( item
.GetID() );
2054 node
->ChangeSubTreeCount(1);
2062 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2064 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2065 const wxDataViewItem
& item
)
2067 wxDataViewTreeNode
* node
= FindNode(parent
);
2069 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2070 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2073 node
->GetChildren().Remove( item
.GetID() );
2074 //Manuplate selection
2075 if( m_selection
.GetCount() > 1 )
2077 m_selection
.Empty();
2079 bool isContainer
= false;
2080 wxDataViewTreeNodes nds
= node
->GetNodes();
2081 for (int i
= 0; i
< nds
.GetCount(); i
++)
2083 if (nds
[i
]->GetItem() == item
)
2091 wxDataViewTreeNode
* n
= NULL
;
2092 wxDataViewTreeNodes nodes
= node
->GetNodes();
2093 int len
= nodes
.GetCount();
2094 for( int i
= 0 ; i
< len
; i
++)
2096 if( nodes
[i
]->GetItem() == item
)
2103 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2105 node
->GetNodes().Remove( n
);
2106 sub
-= n
->GetSubTreeCount();
2107 DestroyTreeHelper(n
);
2109 //Make the row number invalid and get a new valid one when user call GetRowCount
2111 node
->ChangeSubTreeCount(sub
);
2112 if( node
->GetChildrenNumber() == 0)
2114 node
->GetParent()->GetNodes().Remove( node
);
2118 //Change the current row to the last row if the current exceed the max row number
2119 if( m_currentRow
> GetRowCount() )
2120 m_currentRow
= m_count
- 1;
2124 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, item
);
2129 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2134 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
,item
);
2139 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2141 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2142 /*#define MAX_VIRTUAL_WIDTH 100000
2144 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2145 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2146 Refresh( true, &rect );
2154 wxWindow
*parent
= GetParent();
2155 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, parent
->GetId());
2156 le
.SetEventObject(parent
);
2157 le
.SetModel(GetOwner()->GetModel());
2160 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2161 parent
->GetEventHandler()->ProcessEvent(le
);
2166 bool wxDataViewMainWindow::Cleared()
2173 wxWindow
*parent
= GetParent();
2174 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, parent
->GetId());
2175 le
.SetEventObject(parent
);
2176 le
.SetModel(GetOwner()->GetModel());
2177 parent
->GetEventHandler()->ProcessEvent(le
);
2182 void wxDataViewMainWindow::UpdateDisplay()
2187 void wxDataViewMainWindow::OnInternalIdle()
2189 wxWindow::OnInternalIdle();
2193 RecalculateDisplay();
2198 void wxDataViewMainWindow::RecalculateDisplay()
2200 wxDataViewModel
*model
= GetOwner()->GetModel();
2207 int width
= GetEndOfLastCol();
2208 int height
= GetRowCount() * m_lineHeight
;
2210 SetVirtualSize( width
, height
);
2211 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2216 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2218 wxWindow::ScrollWindow( dx
, dy
, rect
);
2220 if (GetOwner()->m_headerArea
)
2221 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2224 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2227 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2228 int sy
= rows
*m_lineHeight
/y
;
2232 wxRect rect
= GetClientRect();
2233 unsigned int colnum
= 0;
2234 unsigned int x_start
= 0, x_end
= 0, w
= 0;
2236 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2237 for (x_start
= 0; colnum
< column
; colnum
++)
2239 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2240 if (col
->IsHidden())
2241 continue; // skip it!
2243 w
= col
->GetWidth();
2247 x_end
= x_start
+ w
;
2248 xe
= xx
+ rect
.width
;
2251 sx
= ( xx
+ x_end
- xe
)/x
;
2258 m_owner
->Scroll( sx
, sy
);
2261 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2263 wxDataViewModel
*model
= GetOwner()->GetModel();
2264 wxAutoBufferedPaintDC
dc( this );
2267 dc
.SetBackground(GetBackgroundColour());
2269 GetOwner()->PrepareDC( dc
);
2270 dc
.SetFont( GetFont() );
2272 wxRect update
= GetUpdateRegion().GetBox();
2273 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2275 // compute which items needs to be redrawn
2276 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2277 unsigned int item_count
=
2278 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2279 (int)(GetRowCount( )- item_start
) );
2280 unsigned int item_last
= item_start
+ item_count
;
2282 // compute which columns needs to be redrawn
2283 unsigned int cols
= GetOwner()->GetColumnCount();
2284 unsigned int col_start
= 0;
2285 unsigned int x_start
= 0;
2286 for (x_start
= 0; col_start
< cols
; col_start
++)
2288 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2289 if (col
->IsHidden())
2290 continue; // skip it!
2292 unsigned int w
= col
->GetWidth();
2293 if (x_start
+w
>= (unsigned int)update
.x
)
2299 unsigned int col_last
= col_start
;
2300 unsigned int x_last
= x_start
;
2301 for (; col_last
< cols
; col_last
++)
2303 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2304 if (col
->IsHidden())
2305 continue; // skip it!
2307 if (x_last
> (unsigned int)update
.GetRight())
2310 x_last
+= col
->GetWidth();
2313 // Draw horizontal rules if required
2314 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2316 dc
.SetPen(m_penRule
);
2317 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2319 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2321 int y
= i
* m_lineHeight
;
2322 dc
.DrawLine(x_start
, y
, x_last
, y
);
2326 // Draw vertical rules if required
2327 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2329 dc
.SetPen(m_penRule
);
2330 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2333 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2335 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2336 if (col
->IsHidden())
2337 continue; // skip it
2339 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2340 x
, item_last
* m_lineHeight
);
2342 x
+= col
->GetWidth();
2345 // Draw last vertical rule
2346 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2347 x
, item_last
* m_lineHeight
);
2350 // redraw the background for the items which are selected/current
2351 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2353 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2354 if (selected
|| item
== m_currentRow
)
2356 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2357 if (item
== m_currentRow
)
2358 flags
|= wxCONTROL_CURRENT
;
2360 flags
|= wxCONTROL_FOCUSED
;
2362 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2363 wxRendererNative::Get().DrawItemSelectionRect
2373 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2376 // TODO: last column for RTL support
2377 expander
= GetOwner()->GetColumn( 0 );
2378 GetOwner()->SetExpanderColumn(expander
);
2381 // redraw all cells for all rows which must be repainted and for all columns
2383 cell_rect
.x
= x_start
;
2384 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2385 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2387 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2388 wxDataViewRenderer
*cell
= col
->GetRenderer();
2389 cell_rect
.width
= col
->GetWidth();
2391 if (col
->IsHidden())
2392 continue; // skipt it!
2395 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2397 // get the cell value and set it into the renderer
2399 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2405 wxDataViewItem dataitem
= node
->GetItem();
2406 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2407 cell
->SetValue( value
);
2409 // update the y offset
2410 cell_rect
.y
= item
* m_lineHeight
;
2412 //Draw the expander here.
2413 int indent
= node
->GetIndentLevel();
2414 if( col
== expander
)
2416 //Calculate the indent first
2417 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2419 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2420 // change the cell_rect.x to the appropriate pos
2421 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2422 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2423 dc
.SetPen( m_penExpander
);
2424 dc
.SetBrush( wxNullBrush
);
2425 if( node
->HasChildren() )
2427 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2429 if (m_underMouse
== node
)
2431 flag
|= wxCONTROL_CURRENT
;
2433 if( node
->IsOpen() )
2434 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2436 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2440 // I am wandering whether we should draw dot lines between tree nodes
2442 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2445 //force the expander column to left-center align
2446 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2450 // cannot be bigger than allocated space
2451 wxSize size
= cell
->GetSize();
2452 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2453 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2454 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2456 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2457 int align
= cell
->GetAlignment();
2459 // horizontal alignment:
2460 item_rect
.x
= cell_rect
.x
;
2461 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2462 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2463 else if (align
& wxALIGN_RIGHT
)
2464 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2465 //else: wxALIGN_LEFT is the default
2467 // vertical alignment:
2468 item_rect
.y
= cell_rect
.y
;
2469 if (align
& wxALIGN_CENTER_VERTICAL
)
2470 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2471 else if (align
& wxALIGN_BOTTOM
)
2472 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2473 //else: wxALIGN_TOP is the default
2476 item_rect
.x
+= PADDING_RIGHTLEFT
;
2477 item_rect
.y
+= PADDING_TOPBOTTOM
;
2478 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2479 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2481 //Here we add the tree indent
2482 item_rect
.x
+= indent
;
2485 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2486 state
|= wxDATAVIEW_CELL_SELECTED
;
2488 // TODO: it would be much more efficient to create a clipping
2489 // region for the entire column being rendered (in the OnPaint
2490 // of wxDataViewMainWindow) instead of a single clip region for
2491 // each cell. However it would mean that each renderer should
2492 // respect the given wxRect's top & bottom coords, eventually
2493 // violating only the left & right coords - however the user can
2494 // make its own renderer and thus we cannot be sure of that.
2495 dc
.SetClippingRegion( item_rect
);
2496 cell
->Render( item_rect
, &dc
, state
);
2497 dc
.DestroyClippingRegion();
2500 cell_rect
.x
+= cell_rect
.width
;
2504 int wxDataViewMainWindow::GetCountPerPage() const
2506 wxSize size
= GetClientSize();
2507 return size
.y
/ m_lineHeight
;
2510 int wxDataViewMainWindow::GetEndOfLastCol() const
2514 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2516 const wxDataViewColumn
*c
=
2517 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2520 width
+= c
->GetWidth();
2525 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2529 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2531 return y
/ m_lineHeight
;
2534 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2536 wxSize client_size
= GetClientSize();
2537 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2538 &client_size
.x
, &client_size
.y
);
2540 //we should deal with the pixel here
2541 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2542 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2545 return wxMin( GetRowCount()-1, row
);
2548 unsigned int wxDataViewMainWindow::GetRowCount()
2550 if ( m_count
== -1 )
2552 m_count
= RecalculateCount();
2554 GetVirtualSize( &width
, &height
);
2555 height
= m_count
* m_lineHeight
;
2557 SetVirtualSize( width
, height
);
2562 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2569 void wxDataViewMainWindow::SelectAllRows( bool on
)
2576 m_selection
.Clear();
2577 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2578 m_selection
.Add( i
);
2583 unsigned int first_visible
= GetFirstVisibleRow();
2584 unsigned int last_visible
= GetLastVisibleRow();
2586 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2588 unsigned int row
= m_selection
[i
];
2589 if ((row
>= first_visible
) && (row
<= last_visible
))
2592 m_selection
.Clear();
2596 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2598 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2602 m_selection
.Add( row
);
2610 m_selection
.Remove( row
);
2616 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2620 unsigned int tmp
= from
;
2626 for (i
= from
; i
<= to
; i
++)
2628 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2631 m_selection
.Add( i
);
2636 m_selection
.Remove( i
);
2639 RefreshRows( from
, to
);
2642 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2644 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2646 int n
= aSelections
[i
];
2648 m_selection
.Add( n
);
2653 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2655 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2656 m_selection
.Add( row
);
2658 m_selection
.Remove( row
);
2662 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2664 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2667 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2669 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2670 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2672 wxSize client_size
= GetClientSize();
2673 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2674 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2675 if (intersect_rect
.width
> 0)
2676 Refresh( true, &intersect_rect
);
2679 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2683 unsigned int tmp
= to
;
2688 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2689 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2691 wxSize client_size
= GetClientSize();
2692 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2693 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2694 if (intersect_rect
.width
> 0)
2695 Refresh( true, &intersect_rect
);
2698 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2700 unsigned int count
= GetRowCount();
2701 if (firstRow
> count
)
2704 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2705 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2707 wxSize client_size
= GetClientSize();
2708 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2709 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2710 if (intersect_rect
.width
> 0)
2711 Refresh( true, &intersect_rect
);
2714 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2716 wxCHECK_RET( newCurrent
< GetRowCount(),
2717 _T("invalid item index in OnArrowChar()") );
2719 // if there is no selection, we cannot move it anywhere
2720 if (!HasCurrentRow())
2723 unsigned int oldCurrent
= m_currentRow
;
2725 // in single selection we just ignore Shift as we can't select several
2727 if ( event
.ShiftDown() && !IsSingleSel() )
2729 RefreshRow( oldCurrent
);
2731 ChangeCurrentRow( newCurrent
);
2733 // select all the items between the old and the new one
2734 if ( oldCurrent
> newCurrent
)
2736 newCurrent
= oldCurrent
;
2737 oldCurrent
= m_currentRow
;
2740 SelectRows( oldCurrent
, newCurrent
, true );
2744 RefreshRow( oldCurrent
);
2746 // all previously selected items are unselected unless ctrl is held
2747 if ( !event
.ControlDown() )
2748 SelectAllRows(false);
2750 ChangeCurrentRow( newCurrent
);
2752 if ( !event
.ControlDown() )
2753 SelectRow( m_currentRow
, true );
2755 RefreshRow( m_currentRow
);
2758 //EnsureVisible( m_currentRow );
2761 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2765 rect
.y
= m_lineHeight
* row
;
2766 rect
.width
= GetEndOfLastCol();
2767 rect
.height
= m_lineHeight
;
2772 class RowToItemJob
: public DoJob
2775 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2776 virtual ~RowToItemJob(){};
2778 virtual int operator() ( wxDataViewTreeNode
* node
)
2781 if( current
== static_cast<int>(row
))
2783 ret
= node
->GetItem() ;
2787 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2789 current
+= node
->GetSubTreeCount();
2794 //If the current has no child node, we can find the desired item of the row number directly.
2795 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2796 if( node
->GetNodes().GetCount() == 0)
2798 int index
= static_cast<int>(row
) - current
- 1;
2799 ret
= node
->GetChildren().Item( index
);
2806 virtual int operator() ( void * n
)
2809 if( current
== static_cast<int>(row
))
2811 ret
= wxDataViewItem( n
) ;
2816 wxDataViewItem
GetResult(){ return ret
; }
2823 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2825 RowToItemJob
job( row
, -2 );
2826 Walker( m_root
, job
);
2827 return job
.GetResult();
2830 class RowToTreeNodeJob
: public DoJob
2833 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2836 this->current
= current
;
2840 virtual ~RowToTreeNodeJob(){};
2842 virtual int operator() ( wxDataViewTreeNode
* node
)
2845 if( current
== static_cast<int>(row
))
2851 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2853 current
+= node
->GetSubTreeCount();
2859 //If the current has no child node, we can find the desired item of the row number directly.
2860 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2861 if( node
->GetNodes().GetCount() == 0)
2863 int index
= static_cast<int>(row
) - current
- 1;
2864 void * n
= node
->GetChildren().Item( index
);
2865 ret
= new wxDataViewTreeNode( parent
) ;
2866 ret
->SetItem( wxDataViewItem( n
));
2867 ret
->SetHasChildren(false);
2876 virtual int operator() ( void * n
)
2879 if( current
== static_cast<int>(row
))
2881 ret
= new wxDataViewTreeNode( parent
) ;
2882 ret
->SetItem( wxDataViewItem( n
));
2883 ret
->SetHasChildren(false);
2889 wxDataViewTreeNode
* GetResult(){ return ret
; }
2893 wxDataViewTreeNode
* ret
;
2894 wxDataViewTreeNode
* parent
;
2898 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2900 RowToTreeNodeJob
job( row
, -2, m_root
);
2901 Walker( m_root
, job
);
2902 return job
.GetResult();
2905 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2907 wxWindow
*parent
= GetParent();
2908 wxDataViewEvent
le(type
, parent
->GetId());
2910 le
.SetEventObject(parent
);
2911 le
.SetModel(GetOwner()->GetModel());
2914 parent
->GetEventHandler()->ProcessEvent(le
);
2918 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2920 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2923 if( node
->HasChildren())
2925 if( !node
->IsOpen())
2927 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2928 //Check if the user prevent expanding
2929 if( e
.GetSkipped() )
2933 //Here I build the children of current node
2934 if( node
->GetChildrenNumber() == 0 )
2937 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2941 //Send the expanded event
2942 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2946 SelectRow( row
, false );
2947 SelectRow( row
+ 1, true );
2948 ChangeCurrentRow( row
+ 1 );
2956 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2958 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2961 wxDataViewTreeNode
* nd
= node
;
2963 if( node
->HasChildren() && node
->IsOpen() )
2965 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2966 if( e
.GetSkipped() )
2971 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2975 node
= node
->GetParent();
2978 int parent
= GetRowByItem( node
->GetItem() ) ;
2981 SelectRow( row
, false);
2982 SelectRow(parent
, true );
2983 ChangeCurrentRow( parent
);
2987 if( !nd
->HasChildren())
2992 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2994 wxDataViewModel
* model
= GetOwner()->GetModel();
2998 //Compose the a parent-chain of the finding item
3000 list
.DeleteContents( true );
3001 wxDataViewItem
it( item
);
3004 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3005 list
.Insert( pItem
);
3006 it
= model
->GetParent( it
);
3009 //Find the item along the parent-chain.
3010 //This algorithm is designed to speed up the node-finding method
3011 wxDataViewTreeNode
* node
= m_root
;
3012 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
3014 if( node
->HasChildren() )
3016 if( node
->GetChildrenNumber() == 0 )
3019 BuildTreeHelper(model
, node
->GetItem(), node
);
3022 wxDataViewTreeNodes nodes
= node
->GetNodes();
3024 for (; i
< nodes
.GetCount(); i
++)
3026 if (nodes
[i
]->GetItem() == *(n
->GetData()))
3032 if (i
== nodes
.GetCount())
3041 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3043 wxDataViewColumn
*col
= NULL
;
3044 unsigned int cols
= GetOwner()->GetColumnCount();
3045 unsigned int colnum
= 0;
3046 unsigned int x_start
= 0;
3048 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3049 for (x_start
= 0; colnum
< cols
; colnum
++)
3051 col
= GetOwner()->GetColumn(colnum
);
3052 if (col
->IsHidden())
3053 continue; // skip it!
3055 unsigned int w
= col
->GetWidth();
3056 if (x_start
+w
>= (unsigned int)x
)
3063 item
= GetItemByRow( y
/m_lineHeight
);
3066 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3068 int row
= GetRowByItem(item
);
3069 int y
= row
*m_lineHeight
;
3070 int h
= m_lineHeight
;
3072 wxDataViewColumn
*col
= NULL
;
3073 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3075 col
= GetOwner()->GetColumn( i
);
3076 x
+= col
->GetWidth();
3077 if( GetOwner()->GetColumn(i
+1) == column
)
3080 int w
= col
->GetWidth();
3081 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3082 return wxRect(x
, y
, w
, h
);
3085 int wxDataViewMainWindow::RecalculateCount()
3087 return m_root
->GetSubTreeCount();
3090 class ItemToRowJob
: public DoJob
3093 ItemToRowJob(const wxDataViewItem
& item
, ItemList::Node
* node
)
3094 { this->item
= item
; ret
= -1 ; nd
= node
; }
3095 virtual ~ItemToRowJob(){};
3097 //Maybe binary search will help to speed up this process
3098 virtual int operator() ( wxDataViewTreeNode
* node
)
3101 if( node
->GetItem() == item
)
3106 if( nd
&& node
->GetItem() == *(nd
->GetData()))
3113 ret
+= node
->GetSubTreeCount();
3119 virtual int operator() ( void * n
)
3122 if( n
== item
.GetID() )
3126 //the row number is begin from zero
3127 int GetResult(){ return ret
-1 ; }
3129 ItemList::Node
* nd
;
3130 wxDataViewItem item
;
3135 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3137 wxDataViewModel
* model
= GetOwner()->GetModel();
3144 //Compose the a parent-chain of the finding item
3146 wxDataViewItem
* pItem
= NULL
;
3147 list
.DeleteContents( true );
3148 wxDataViewItem
it( item
);
3151 pItem
= new wxDataViewItem( it
);
3152 list
.Insert( pItem
);
3153 it
= model
->GetParent( it
);
3155 pItem
= new wxDataViewItem( );
3156 list
.Insert( pItem
);
3158 ItemToRowJob
job( item
, list
.GetFirst() );
3159 Walker(m_root
, job
);
3160 return job
.GetResult();
3163 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3165 if( !model
->IsContainer( item
) )
3168 wxDataViewItem i
= model
->GetFirstChild( item
);
3173 if( model
->IsContainer( i
) )
3175 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3177 n
->SetHasChildren( true ) ;
3182 node
->AddLeaf( i
.GetID() );
3184 i
= model
->GetNextSibling( i
);
3186 node
->SetSubTreeCount( num
);
3187 wxDataViewTreeNode
* n
= node
->GetParent();
3189 n
->ChangeSubTreeCount(num
);
3193 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3195 //First we define a invalid item to fetch the top-level elements
3196 wxDataViewItem item
;
3198 BuildTreeHelper( model
, item
, m_root
);
3202 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3204 if( node
->GetNodeNumber() != 0 )
3206 int len
= node
->GetNodeNumber();
3208 wxDataViewTreeNodes nodes
= node
->GetNodes();
3209 for( ; i
< len
; i
++ )
3211 DestroyTreeHelper(nodes
[i
]);
3217 void wxDataViewMainWindow::DestroyTree()
3219 DestroyTreeHelper(m_root
);
3220 m_root
->SetSubTreeCount(0);
3224 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3226 if (event
.GetKeyCode() == WXK_TAB
)
3228 wxNavigationKeyEvent nevent
;
3229 nevent
.SetWindowChange( event
.ControlDown() );
3230 nevent
.SetDirection( !event
.ShiftDown() );
3231 nevent
.SetEventObject( GetParent()->GetParent() );
3232 nevent
.SetCurrentFocus( m_parent
);
3233 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3237 // no item -> nothing to do
3238 if (!HasCurrentRow())
3244 // don't use m_linesPerPage directly as it might not be computed yet
3245 const int pageSize
= GetCountPerPage();
3246 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3248 switch ( event
.GetKeyCode() )
3251 if ( m_currentRow
> 0 )
3252 OnArrowChar( m_currentRow
- 1, event
);
3256 if ( m_currentRow
< GetRowCount() - 1 )
3257 OnArrowChar( m_currentRow
+ 1, event
);
3259 //Add the process for tree expanding/collapsing
3261 OnCollapsing(m_currentRow
);
3264 OnExpanding( m_currentRow
);
3268 OnArrowChar( GetRowCount() - 1, event
);
3273 OnArrowChar( 0, event
);
3278 int steps
= pageSize
- 1;
3279 int index
= m_currentRow
- steps
;
3283 OnArrowChar( index
, event
);
3289 int steps
= pageSize
- 1;
3290 unsigned int index
= m_currentRow
+ steps
;
3291 unsigned int count
= GetRowCount();
3292 if ( index
>= count
)
3295 OnArrowChar( index
, event
);
3304 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3306 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3308 // let the base handle mouse wheel events.
3313 int x
= event
.GetX();
3314 int y
= event
.GetY();
3315 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3316 wxDataViewColumn
*col
= NULL
;
3319 unsigned int cols
= GetOwner()->GetColumnCount();
3321 for (i
= 0; i
< cols
; i
++)
3323 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3325 continue; // skip it!
3327 if (x
< xpos
+ c
->GetWidth())
3332 xpos
+= c
->GetWidth();
3337 wxDataViewRenderer
*cell
= col
->GetRenderer();
3338 unsigned int current
= y
/ m_lineHeight
;
3339 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3341 // Unselect all if below the last row ?
3345 //Test whether the mouse is hovered on the tree item button
3347 if (GetOwner()->GetExpanderColumn() == col
)
3349 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3350 if( node
!=NULL
&& node
->HasChildren() )
3352 int indent
= node
->GetIndentLevel();
3353 indent
= GetOwner()->GetIndent()*indent
;
3354 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3355 if( rect
.Contains( x
, y
) )
3357 //So the mouse is over the expander
3359 if (m_underMouse
&& m_underMouse
!= node
)
3361 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3362 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3364 if (m_underMouse
!= node
)
3366 //wxLogMessage("Do the row: %d", current);
3369 m_underMouse
= node
;
3372 if (node
!=NULL
&& !node
->HasChildren())
3377 if (m_underMouse
!= NULL
)
3379 wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse
->GetItem()));
3380 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3381 m_underMouse
= NULL
;
3385 wxDataViewModel
*model
= GetOwner()->GetModel();
3387 if (event
.Dragging())
3389 if (m_dragCount
== 0)
3391 // we have to report the raw, physical coords as we want to be
3392 // able to call HitTest(event.m_pointDrag) from the user code to
3393 // get the item being dragged
3394 m_dragStart
= event
.GetPosition();
3399 if (m_dragCount
!= 3)
3402 if (event
.LeftIsDown())
3404 // Notify cell about drag
3413 bool forceClick
= false;
3415 if (event
.ButtonDClick())
3417 m_renameTimer
->Stop();
3418 m_lastOnSame
= false;
3421 if (event
.LeftDClick())
3423 if ( current
== m_lineLastClicked
)
3425 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3427 wxDataViewItem item
= GetItemByRow(current
);
3429 model
->GetValue( value
, item
, col
->GetModelColumn() );
3430 cell
->SetValue( value
);
3431 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3432 col
->GetWidth(), m_lineHeight
);
3433 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3435 wxWindow
*parent
= GetParent();
3436 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3438 le
.SetEventObject(parent
);
3439 le
.SetColumn(col
->GetModelColumn());
3440 le
.SetDataViewColumn(col
);
3441 le
.SetModel(GetOwner()->GetModel());
3443 parent
->GetEventHandler()->ProcessEvent(le
);
3449 // The first click was on another item, so don't interpret this as
3450 // a double click, but as a simple click instead
3457 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3459 // select single line
3460 SelectAllRows( false );
3461 SelectRow( m_lineSelectSingleOnUp
, true );
3464 //Process the event of user clicking the expander
3465 bool expander
= false;
3466 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3467 if( node
!=NULL
&& node
->HasChildren() )
3469 int indent
= node
->GetIndentLevel();
3470 indent
= GetOwner()->GetIndent()*indent
;
3471 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3472 if( rect
.Contains( x
, y
) )
3475 if( node
->IsOpen() )
3476 OnCollapsing(current
);
3478 OnExpanding( current
);
3482 //If the user click the expander, we do not do editing even if the column with expander are editable
3483 if (m_lastOnSame
&& !expander
)
3485 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3486 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3488 m_renameTimer
->Start( 100, true );
3492 m_lastOnSame
= false;
3493 m_lineSelectSingleOnUp
= (unsigned int)-1;
3497 // This is necessary, because after a DnD operation in
3498 // from and to ourself, the up event is swallowed by the
3499 // DnD code. So on next non-up event (which means here and
3500 // now) m_lineSelectSingleOnUp should be reset.
3501 m_lineSelectSingleOnUp
= (unsigned int)-1;
3504 if (event
.RightDown())
3506 m_lineBeforeLastClicked
= m_lineLastClicked
;
3507 m_lineLastClicked
= current
;
3509 // If the item is already selected, do not update the selection.
3510 // Multi-selections should not be cleared if a selected item is clicked.
3511 if (!IsRowSelected(current
))
3513 SelectAllRows(false);
3514 ChangeCurrentRow(current
);
3515 SelectRow(m_currentRow
,true);
3518 // notify cell about right click
3521 // Allow generation of context menu event
3524 else if (event
.MiddleDown())
3526 // notify cell about middle click
3529 if (event
.LeftDown() || forceClick
)
3533 m_lineBeforeLastClicked
= m_lineLastClicked
;
3534 m_lineLastClicked
= current
;
3536 unsigned int oldCurrentRow
= m_currentRow
;
3537 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3539 bool cmdModifierDown
= event
.CmdDown();
3540 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3542 if ( IsSingleSel() || !IsRowSelected(current
) )
3544 SelectAllRows( false );
3546 ChangeCurrentRow(current
);
3548 SelectRow(m_currentRow
,true);
3550 else // multi sel & current is highlighted & no mod keys
3552 m_lineSelectSingleOnUp
= current
;
3553 ChangeCurrentRow(current
); // change focus
3556 else // multi sel & either ctrl or shift is down
3558 if (cmdModifierDown
)
3560 ChangeCurrentRow(current
);
3562 ReverseRowSelection(m_currentRow
);
3564 else if (event
.ShiftDown())
3566 ChangeCurrentRow(current
);
3568 unsigned int lineFrom
= oldCurrentRow
,
3571 if ( lineTo
< lineFrom
)
3574 lineFrom
= m_currentRow
;
3577 SelectRows(lineFrom
, lineTo
, true);
3579 else // !ctrl, !shift
3581 // test in the enclosing if should make it impossible
3582 wxFAIL_MSG( _T("how did we get here?") );
3586 if (m_currentRow
!= oldCurrentRow
)
3587 RefreshRow( oldCurrentRow
);
3589 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3591 // Update selection here...
3594 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3595 (current
== oldCurrentRow
)) && oldWasSelected
;
3599 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3603 if (HasCurrentRow())
3609 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3613 if (HasCurrentRow())
3619 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3621 if( m_selection
.GetCount() != 1 )
3622 return wxDataViewItem();
3624 return GetItemByRow( m_selection
.Item(0));
3627 //-----------------------------------------------------------------------------
3629 //-----------------------------------------------------------------------------
3630 WX_DEFINE_LIST(wxDataViewColumnList
);
3632 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3634 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3635 EVT_SIZE(wxDataViewCtrl::OnSize
)
3638 wxDataViewCtrl::~wxDataViewCtrl()
3641 GetModel()->RemoveNotifier( m_notifier
);
3644 void wxDataViewCtrl::Init()
3649 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3650 const wxPoint
& pos
, const wxSize
& size
,
3651 long style
, const wxValidator
& validator
)
3653 if (!wxControl::Create( parent
, id
, pos
, size
,
3654 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3660 MacSetClipChildren( true ) ;
3663 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3665 if (HasFlag(wxDV_NO_HEADER
))
3666 m_headerArea
= NULL
;
3668 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3670 SetTargetWindow( m_clientArea
);
3672 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3674 sizer
->Add( m_headerArea
, 0, wxGROW
);
3675 sizer
->Add( m_clientArea
, 1, wxGROW
);
3682 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3686 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3689 // we need to process arrows ourselves for scrolling
3690 if ( nMsg
== WM_GETDLGCODE
)
3692 rc
|= DLGC_WANTARROWS
;
3700 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3702 // We need to override OnSize so that our scrolled
3703 // window a) does call Layout() to use sizers for
3704 // positioning the controls but b) does not query
3705 // the sizer for their size and use that for setting
3706 // the scrollable area as set that ourselves by
3707 // calling SetScrollbar() further down.
3714 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3716 if (!wxDataViewCtrlBase::AssociateModel( model
))
3719 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3721 model
->AddNotifier( m_notifier
);
3723 m_clientArea
->BuildTree(model
);
3725 m_clientArea
->UpdateDisplay();
3730 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3732 if (!wxDataViewCtrlBase::AppendColumn(col
))
3735 m_cols
.Append( col
);
3740 void wxDataViewCtrl::OnColumnChange()
3743 m_headerArea
->UpdateDisplay();
3745 m_clientArea
->UpdateDisplay();
3748 void wxDataViewCtrl::DoSetExpanderColumn()
3750 m_clientArea
->UpdateDisplay();
3753 void wxDataViewCtrl::DoSetIndent()
3755 m_clientArea
->UpdateDisplay();
3758 unsigned int wxDataViewCtrl::GetColumnCount() const
3760 return m_cols
.GetCount();
3763 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3765 wxDataViewColumnList::const_iterator iter
;
3767 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3772 if ((*iter
)->IsHidden())
3779 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3781 wxDataViewColumnList::Node
* ret
= m_cols
.Find( column
);
3792 bool wxDataViewCtrl::ClearColumns()
3799 //Selection code with wxDataViewItem as parameters
3800 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3802 return m_clientArea
->GetSelection();
3805 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3808 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3809 int len
= selection
.GetCount();
3810 for( int i
= 0; i
< len
; i
++)
3812 unsigned int row
= selection
[i
];
3813 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3818 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3820 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3821 int len
= sel
.GetCount();
3822 for( int i
= 0; i
< len
; i
++ )
3824 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3826 selection
.Add( static_cast<unsigned int>(row
) );
3828 m_clientArea
->SetSelections( selection
);
3831 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3833 int row
= m_clientArea
->GetRowByItem( item
);
3836 //Unselect all rows before select another in the single select mode
3837 if (m_clientArea
->IsSingleSel())
3838 m_clientArea
->SelectAllRows(false);
3839 m_clientArea
->SelectRow(row
, true);
3843 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3845 int row
= m_clientArea
->GetRowByItem( item
);
3847 m_clientArea
->SelectRow(row
, false);
3850 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3852 int row
= m_clientArea
->GetRowByItem( item
);
3855 return m_clientArea
->IsRowSelected(row
);
3860 //Selection code with row number as parameter
3861 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3864 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3865 int len
= selection
.GetCount();
3866 for( int i
= 0; i
< len
; i
++)
3868 unsigned int row
= selection
[i
];
3874 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3876 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3877 int len
= sel
.GetCount();
3878 for( int i
= 0; i
< len
; i
++ )
3882 selection
.Add( static_cast<unsigned int>(row
) );
3884 m_clientArea
->SetSelections( selection
);
3887 void wxDataViewCtrl::Select( int row
)
3891 if (m_clientArea
->IsSingleSel())
3892 m_clientArea
->SelectAllRows(false);
3893 m_clientArea
->SelectRow( row
, true );
3897 void wxDataViewCtrl::Unselect( int row
)
3900 m_clientArea
->SelectRow(row
, false);
3903 bool wxDataViewCtrl::IsSelected( int row
) const
3906 return m_clientArea
->IsRowSelected(row
);
3910 void wxDataViewCtrl::SelectRange( int from
, int to
)
3913 for( int i
= from
; i
< to
; i
++ )
3915 m_clientArea
->Select(sel
);
3918 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3920 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3921 for( int i
= from
; i
< to
; i
++ )
3922 if( sel
.Index( i
) != wxNOT_FOUND
)
3924 m_clientArea
->SetSelections(sel
);
3927 void wxDataViewCtrl::SelectAll()
3929 m_clientArea
->SelectAllRows(true);
3932 void wxDataViewCtrl::UnselectAll()
3934 m_clientArea
->SelectAllRows(false);
3937 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3941 if( row
> m_clientArea
->GetRowCount() )
3942 row
= m_clientArea
->GetRowCount();
3944 int first
= m_clientArea
->GetFirstVisibleRow();
3945 int last
= m_clientArea
->GetLastVisibleRow();
3947 m_clientArea
->ScrollTo( row
, column
);
3948 else if( row
> last
)
3949 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3951 m_clientArea
->ScrollTo( first
, column
);
3954 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3956 int row
= m_clientArea
->GetRowByItem(item
);
3959 if( column
== NULL
)
3960 return EnsureVisible(row
, -1);
3964 int len
= GetColumnCount();
3965 for( int i
= 0; i
< len
; i
++ )
3966 if( GetColumn(i
) == column
)
3971 EnsureVisible( row
, col
);
3977 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3979 m_clientArea
->HitTest(point
, item
, column
);
3982 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3984 return m_clientArea
->GetItemRect(item
, column
);
3987 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3989 return m_clientArea
->GetItemByRow( row
);
3992 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3994 return m_clientArea
->GetRowByItem( item
);
3997 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
3999 int row
= m_clientArea
->GetRowByItem( item
);
4001 m_clientArea
->Expand(row
);
4004 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4006 int row
= m_clientArea
->GetRowByItem( item
);
4008 m_clientArea
->Collapse(row
);
4012 // !wxUSE_GENERICDATAVIEWCTRL
4015 // wxUSE_DATAVIEWCTRL