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 if (event
.ButtonUp())
1667 m_isDragging
= false;
1673 m_currentX
= wxMax(m_minX
+ 7, x
);
1677 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1679 GetOwner()->Refresh();
1683 else // not dragging
1686 m_column
= wxNOT_FOUND
;
1688 bool hit_border
= false;
1690 // end of the current column
1693 // find the column where this event occured
1694 int countCol
= m_owner
->GetColumnCount();
1695 for (int column
= 0; column
< countCol
; column
++)
1697 wxDataViewColumn
*p
= GetColumn(column
);
1700 continue; // skip if not shown
1702 xpos
+= p
->GetWidth();
1704 if ((abs(x
-xpos
) < 3) && (y
< 22))
1712 // inside the column
1719 if (m_column
== wxNOT_FOUND
)
1722 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1723 if (event
.LeftDClick() && resizeable
)
1725 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1728 else if (event
.LeftDown() || event
.RightUp())
1730 if (hit_border
&& event
.LeftDown() && resizeable
)
1732 m_isDragging
= true;
1736 else // click on a column
1738 wxDataViewModel
* model
= GetOwner()->GetModel();
1739 wxEventType evt
= event
.LeftDown() ?
1740 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1741 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1742 SendEvent(evt
, m_column
);
1744 //Left click the header
1745 if(event
.LeftDown())
1747 wxDataViewColumn
*col
= GetColumn(m_column
);
1748 if(col
->IsSortable())
1750 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1751 if(model
&& sortCol
== col
)
1753 bool order
= col
->IsSortOrderAscending();
1754 col
->SetSortOrder(!order
);
1758 m_owner
->SetSortingColumn(col
);
1764 //Send the column sorted event
1765 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1769 else if (event
.Moving())
1771 if (hit_border
&& resizeable
)
1772 m_currentCursor
= m_resizeCursor
;
1774 m_currentCursor
= wxSTANDARD_CURSOR
;
1776 SetCursor(*m_currentCursor
);
1781 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1782 void wxGenericDataViewHeaderWindow::DrawCurrent()
1785 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1787 int x1
= m_currentX
;
1789 ClientToScreen (&x1
, &y1
);
1791 int x2
= m_currentX
-1;
1793 ++x2
; // but why ????
1796 m_owner
->GetClientSize( NULL
, &y2
);
1797 m_owner
->ClientToScreen( &x2
, &y2
);
1800 dc
.SetLogicalFunction(wxINVERT
);
1801 dc
.SetPen(m_penCurrent
);
1802 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1804 dc
.DrawLine(x1
, y1
, x2
, y2
);
1808 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1812 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1813 m_owner
->GetViewStart( &x
, NULL
);
1815 // shift the DC origin to match the position of the main window horizontal
1816 // scrollbar: this allows us to always use logical coords
1817 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1820 #endif // defined(__WXMSW__)
1822 //-----------------------------------------------------------------------------
1823 // wxDataViewRenameTimer
1824 //-----------------------------------------------------------------------------
1826 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1831 void wxDataViewRenameTimer::Notify()
1833 m_owner
->OnRenameTimer();
1836 //-----------------------------------------------------------------------------
1837 // wxDataViewMainWindow
1838 //-----------------------------------------------------------------------------
1840 //The tree building helper, declared firstly
1841 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1843 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1845 if (row1
> row2
) return 1;
1846 if (row1
== row2
) return 0;
1851 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1853 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1854 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1855 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1856 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1857 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1858 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1861 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1862 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1863 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1864 m_selection( wxDataViewSelectionCmp
)
1869 m_lastOnSame
= false;
1870 m_renameTimer
= new wxDataViewRenameTimer( this );
1872 // TODO: user better initial values/nothing selected
1873 m_currentCol
= NULL
;
1876 // TODO: we need to calculate this smartly
1883 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1886 m_dragStart
= wxPoint(0,0);
1887 m_lineLastClicked
= (unsigned int) -1;
1888 m_lineBeforeLastClicked
= (unsigned int) -1;
1889 m_lineSelectSingleOnUp
= (unsigned int) -1;
1893 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1894 SetBackgroundColour( *wxWHITE
);
1896 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1898 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1899 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1900 //Some new added code to deal with the tree structure
1901 m_root
= new wxDataViewTreeNode( NULL
);
1902 m_root
->SetHasChildren(true);
1904 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1906 m_underMouse
= NULL
;
1910 wxDataViewMainWindow::~wxDataViewMainWindow()
1913 delete m_renameTimer
;
1916 void wxDataViewMainWindow::OnRenameTimer()
1918 // We have to call this here because changes may just have
1919 // been made and no screen update taken place.
1924 unsigned int cols
= GetOwner()->GetColumnCount();
1926 for (i
= 0; i
< cols
; i
++)
1928 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1930 continue; // skip it!
1932 if (c
== m_currentCol
)
1934 xpos
+= c
->GetWidth();
1936 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1937 m_currentCol
->GetWidth(), m_lineHeight
);
1939 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1940 &labelRect
.x
, &labelRect
.y
);
1942 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1943 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1947 //------------------------------------------------------------------
1948 // Helper class for do operation on the tree node
1949 //------------------------------------------------------------------
1956 //The return value control how the tree-walker tranverse the tree
1957 // 0: Job done, stop tranverse and return
1958 // 1: Ignore the current node's subtree and continue
1959 // 2: Job not done, continue
1960 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1961 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1962 virtual int operator() ( void * n
) = 0;
1965 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1970 switch( func( node
) )
1981 wxDataViewTreeNodes nodes
= node
->GetNodes();
1982 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1984 int len_nodes
= nodes
.GetCount();
1985 int len
= leaves
.GetCount();
1986 int i
= 0, nodes_i
= 0;
1988 for( ; i
< len
; i
++ )
1990 void * n
= leaves
[i
];
1991 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1993 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1996 if( Walker( nd
, func
) )
2015 void wxDataViewMainWindow::SendModelEvent( wxEventType type
, const wxDataViewItem
& item
)
2017 wxWindow
*parent
= GetParent();
2018 wxDataViewEvent
le(type
, parent
->GetId());
2020 le
.SetEventObject(parent
);
2021 le
.SetModel(GetOwner()->GetModel());
2024 parent
->GetEventHandler()->ProcessEvent(le
);
2027 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2031 wxDataViewTreeNode
* node
;
2032 node
= FindNode(parent
);
2033 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, item
);
2038 node
->SetHasChildren( true );
2040 if( g_model
->IsContainer( item
) )
2042 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2043 newnode
->SetItem(item
);
2044 newnode
->SetHasChildren( true );
2045 node
->AddNode( newnode
);
2048 node
->AddLeaf( item
.GetID() );
2050 node
->ChangeSubTreeCount(1);
2058 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2060 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2061 const wxDataViewItem
& item
)
2063 wxDataViewTreeNode
* node
= FindNode(parent
);
2065 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2066 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2069 node
->GetChildren().Remove( item
.GetID() );
2070 //Manuplate selection
2071 if( m_selection
.GetCount() > 1 )
2073 m_selection
.Empty();
2075 bool isContainer
= false;
2076 wxDataViewTreeNodes nds
= node
->GetNodes();
2077 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2079 if (nds
[i
]->GetItem() == item
)
2087 wxDataViewTreeNode
* n
= NULL
;
2088 wxDataViewTreeNodes nodes
= node
->GetNodes();
2089 int len
= nodes
.GetCount();
2090 for( int i
= 0 ; i
< len
; i
++)
2092 if( nodes
[i
]->GetItem() == item
)
2099 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2101 node
->GetNodes().Remove( n
);
2102 sub
-= n
->GetSubTreeCount();
2103 DestroyTreeHelper(n
);
2105 //Make the row number invalid and get a new valid one when user call GetRowCount
2107 node
->ChangeSubTreeCount(sub
);
2108 if( node
->GetChildrenNumber() == 0)
2110 node
->GetParent()->GetNodes().Remove( node
);
2114 //Change the current row to the last row if the current exceed the max row number
2115 if( m_currentRow
> GetRowCount() )
2116 m_currentRow
= m_count
- 1;
2120 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, item
);
2125 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2130 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
,item
);
2135 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2137 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2138 /*#define MAX_VIRTUAL_WIDTH 100000
2140 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2141 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2142 Refresh( true, &rect );
2150 wxWindow
*parent
= GetParent();
2151 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, parent
->GetId());
2152 le
.SetEventObject(parent
);
2153 le
.SetModel(GetOwner()->GetModel());
2156 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2157 parent
->GetEventHandler()->ProcessEvent(le
);
2162 bool wxDataViewMainWindow::Cleared()
2169 wxWindow
*parent
= GetParent();
2170 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, parent
->GetId());
2171 le
.SetEventObject(parent
);
2172 le
.SetModel(GetOwner()->GetModel());
2173 parent
->GetEventHandler()->ProcessEvent(le
);
2178 void wxDataViewMainWindow::UpdateDisplay()
2183 void wxDataViewMainWindow::OnInternalIdle()
2185 wxWindow::OnInternalIdle();
2189 RecalculateDisplay();
2194 void wxDataViewMainWindow::RecalculateDisplay()
2196 wxDataViewModel
*model
= GetOwner()->GetModel();
2203 int width
= GetEndOfLastCol();
2204 int height
= GetRowCount() * m_lineHeight
;
2206 SetVirtualSize( width
, height
);
2207 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2212 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2214 wxWindow::ScrollWindow( dx
, dy
, rect
);
2216 if (GetOwner()->m_headerArea
)
2217 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2220 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2223 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2224 int sy
= rows
*m_lineHeight
/y
;
2228 wxRect rect
= GetClientRect();
2229 unsigned int colnum
= 0;
2230 unsigned int x_start
= 0, x_end
= 0, w
= 0;
2232 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2233 for (x_start
= 0; colnum
< column
; colnum
++)
2235 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2236 if (col
->IsHidden())
2237 continue; // skip it!
2239 w
= col
->GetWidth();
2243 x_end
= x_start
+ w
;
2244 xe
= xx
+ rect
.width
;
2247 sx
= ( xx
+ x_end
- xe
)/x
;
2254 m_owner
->Scroll( sx
, sy
);
2257 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2259 wxDataViewModel
*model
= GetOwner()->GetModel();
2260 wxAutoBufferedPaintDC
dc( this );
2263 dc
.SetBackground(GetBackgroundColour());
2265 GetOwner()->PrepareDC( dc
);
2266 dc
.SetFont( GetFont() );
2268 wxRect update
= GetUpdateRegion().GetBox();
2269 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2271 // compute which items needs to be redrawn
2272 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2273 unsigned int item_count
=
2274 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2275 (int)(GetRowCount( )- item_start
) );
2276 unsigned int item_last
= item_start
+ item_count
;
2278 // compute which columns needs to be redrawn
2279 unsigned int cols
= GetOwner()->GetColumnCount();
2280 unsigned int col_start
= 0;
2281 unsigned int x_start
= 0;
2282 for (x_start
= 0; col_start
< cols
; col_start
++)
2284 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2285 if (col
->IsHidden())
2286 continue; // skip it!
2288 unsigned int w
= col
->GetWidth();
2289 if (x_start
+w
>= (unsigned int)update
.x
)
2295 unsigned int col_last
= col_start
;
2296 unsigned int x_last
= x_start
;
2297 for (; col_last
< cols
; col_last
++)
2299 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2300 if (col
->IsHidden())
2301 continue; // skip it!
2303 if (x_last
> (unsigned int)update
.GetRight())
2306 x_last
+= col
->GetWidth();
2309 // Draw horizontal rules if required
2310 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2312 dc
.SetPen(m_penRule
);
2313 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2315 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2317 int y
= i
* m_lineHeight
;
2318 dc
.DrawLine(x_start
, y
, x_last
, y
);
2322 // Draw vertical rules if required
2323 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2325 dc
.SetPen(m_penRule
);
2326 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2329 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2331 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2332 if (col
->IsHidden())
2333 continue; // skip it
2335 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2336 x
, item_last
* m_lineHeight
);
2338 x
+= col
->GetWidth();
2341 // Draw last vertical rule
2342 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2343 x
, item_last
* m_lineHeight
);
2346 // redraw the background for the items which are selected/current
2347 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2349 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2350 if (selected
|| item
== m_currentRow
)
2352 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2353 if (item
== m_currentRow
)
2354 flags
|= wxCONTROL_CURRENT
;
2356 flags
|= wxCONTROL_FOCUSED
;
2358 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2359 wxRendererNative::Get().DrawItemSelectionRect
2369 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2372 // TODO: last column for RTL support
2373 expander
= GetOwner()->GetColumn( 0 );
2374 GetOwner()->SetExpanderColumn(expander
);
2377 // redraw all cells for all rows which must be repainted and for all columns
2379 cell_rect
.x
= x_start
;
2380 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2381 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2383 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2384 wxDataViewRenderer
*cell
= col
->GetRenderer();
2385 cell_rect
.width
= col
->GetWidth();
2387 if (col
->IsHidden())
2388 continue; // skipt it!
2391 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2393 // get the cell value and set it into the renderer
2395 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2401 wxDataViewItem dataitem
= node
->GetItem();
2402 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2403 cell
->SetValue( value
);
2405 // update the y offset
2406 cell_rect
.y
= item
* m_lineHeight
;
2408 //Draw the expander here.
2409 int indent
= node
->GetIndentLevel();
2410 if( col
== expander
)
2412 //Calculate the indent first
2413 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2415 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2416 // change the cell_rect.x to the appropriate pos
2417 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2418 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2419 dc
.SetPen( m_penExpander
);
2420 dc
.SetBrush( wxNullBrush
);
2421 if( node
->HasChildren() )
2423 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2425 if (m_underMouse
== node
)
2427 flag
|= wxCONTROL_CURRENT
;
2429 if( node
->IsOpen() )
2430 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2432 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2436 // I am wandering whether we should draw dot lines between tree nodes
2438 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2441 //force the expander column to left-center align
2442 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2446 // cannot be bigger than allocated space
2447 wxSize size
= cell
->GetSize();
2448 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2449 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2450 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2452 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2453 int align
= cell
->GetAlignment();
2455 // horizontal alignment:
2456 item_rect
.x
= cell_rect
.x
;
2457 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2458 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2459 else if (align
& wxALIGN_RIGHT
)
2460 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2461 //else: wxALIGN_LEFT is the default
2463 // vertical alignment:
2464 item_rect
.y
= cell_rect
.y
;
2465 if (align
& wxALIGN_CENTER_VERTICAL
)
2466 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2467 else if (align
& wxALIGN_BOTTOM
)
2468 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2469 //else: wxALIGN_TOP is the default
2472 item_rect
.x
+= PADDING_RIGHTLEFT
;
2473 item_rect
.y
+= PADDING_TOPBOTTOM
;
2474 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2475 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2477 //Here we add the tree indent
2478 item_rect
.x
+= indent
;
2481 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2482 state
|= wxDATAVIEW_CELL_SELECTED
;
2484 // TODO: it would be much more efficient to create a clipping
2485 // region for the entire column being rendered (in the OnPaint
2486 // of wxDataViewMainWindow) instead of a single clip region for
2487 // each cell. However it would mean that each renderer should
2488 // respect the given wxRect's top & bottom coords, eventually
2489 // violating only the left & right coords - however the user can
2490 // make its own renderer and thus we cannot be sure of that.
2491 dc
.SetClippingRegion( item_rect
);
2492 cell
->Render( item_rect
, &dc
, state
);
2493 dc
.DestroyClippingRegion();
2496 cell_rect
.x
+= cell_rect
.width
;
2500 int wxDataViewMainWindow::GetCountPerPage() const
2502 wxSize size
= GetClientSize();
2503 return size
.y
/ m_lineHeight
;
2506 int wxDataViewMainWindow::GetEndOfLastCol() const
2510 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2512 const wxDataViewColumn
*c
=
2513 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2516 width
+= c
->GetWidth();
2521 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2525 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2527 return y
/ m_lineHeight
;
2530 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2532 wxSize client_size
= GetClientSize();
2533 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2534 &client_size
.x
, &client_size
.y
);
2536 //we should deal with the pixel here
2537 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2538 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2541 return wxMin( GetRowCount()-1, row
);
2544 unsigned int wxDataViewMainWindow::GetRowCount()
2546 if ( m_count
== -1 )
2548 m_count
= RecalculateCount();
2550 GetVirtualSize( &width
, &height
);
2551 height
= m_count
* m_lineHeight
;
2553 SetVirtualSize( width
, height
);
2558 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2565 void wxDataViewMainWindow::SelectAllRows( bool on
)
2572 m_selection
.Clear();
2573 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2574 m_selection
.Add( i
);
2579 unsigned int first_visible
= GetFirstVisibleRow();
2580 unsigned int last_visible
= GetLastVisibleRow();
2582 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2584 unsigned int row
= m_selection
[i
];
2585 if ((row
>= first_visible
) && (row
<= last_visible
))
2588 m_selection
.Clear();
2592 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2594 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2598 m_selection
.Add( row
);
2606 m_selection
.Remove( row
);
2612 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2616 unsigned int tmp
= from
;
2622 for (i
= from
; i
<= to
; i
++)
2624 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2627 m_selection
.Add( i
);
2632 m_selection
.Remove( i
);
2635 RefreshRows( from
, to
);
2638 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2640 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2642 int n
= aSelections
[i
];
2644 m_selection
.Add( n
);
2649 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2651 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2652 m_selection
.Add( row
);
2654 m_selection
.Remove( row
);
2658 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2660 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2663 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2665 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2666 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2668 wxSize client_size
= GetClientSize();
2669 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2670 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2671 if (intersect_rect
.width
> 0)
2672 Refresh( true, &intersect_rect
);
2675 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2679 unsigned int tmp
= to
;
2684 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2685 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2687 wxSize client_size
= GetClientSize();
2688 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2689 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2690 if (intersect_rect
.width
> 0)
2691 Refresh( true, &intersect_rect
);
2694 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2696 unsigned int count
= GetRowCount();
2697 if (firstRow
> count
)
2700 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2701 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2703 wxSize client_size
= GetClientSize();
2704 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2705 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2706 if (intersect_rect
.width
> 0)
2707 Refresh( true, &intersect_rect
);
2710 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2712 wxCHECK_RET( newCurrent
< GetRowCount(),
2713 _T("invalid item index in OnArrowChar()") );
2715 // if there is no selection, we cannot move it anywhere
2716 if (!HasCurrentRow())
2719 unsigned int oldCurrent
= m_currentRow
;
2721 // in single selection we just ignore Shift as we can't select several
2723 if ( event
.ShiftDown() && !IsSingleSel() )
2725 RefreshRow( oldCurrent
);
2727 ChangeCurrentRow( newCurrent
);
2729 // select all the items between the old and the new one
2730 if ( oldCurrent
> newCurrent
)
2732 newCurrent
= oldCurrent
;
2733 oldCurrent
= m_currentRow
;
2736 SelectRows( oldCurrent
, newCurrent
, true );
2740 RefreshRow( oldCurrent
);
2742 // all previously selected items are unselected unless ctrl is held
2743 if ( !event
.ControlDown() )
2744 SelectAllRows(false);
2746 ChangeCurrentRow( newCurrent
);
2748 if ( !event
.ControlDown() )
2749 SelectRow( m_currentRow
, true );
2751 RefreshRow( m_currentRow
);
2754 //EnsureVisible( m_currentRow );
2757 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2761 rect
.y
= m_lineHeight
* row
;
2762 rect
.width
= GetEndOfLastCol();
2763 rect
.height
= m_lineHeight
;
2768 class RowToItemJob
: public DoJob
2771 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2772 virtual ~RowToItemJob(){};
2774 virtual int operator() ( wxDataViewTreeNode
* node
)
2777 if( current
== static_cast<int>(row
))
2779 ret
= node
->GetItem() ;
2783 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2785 current
+= node
->GetSubTreeCount();
2790 //If the current has no child node, we can find the desired item of the row number directly.
2791 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2792 if( node
->GetNodes().GetCount() == 0)
2794 int index
= static_cast<int>(row
) - current
- 1;
2795 ret
= node
->GetChildren().Item( index
);
2802 virtual int operator() ( void * n
)
2805 if( current
== static_cast<int>(row
))
2807 ret
= wxDataViewItem( n
) ;
2812 wxDataViewItem
GetResult(){ return ret
; }
2819 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2821 RowToItemJob
job( row
, -2 );
2822 Walker( m_root
, job
);
2823 return job
.GetResult();
2826 class RowToTreeNodeJob
: public DoJob
2829 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2832 this->current
= current
;
2836 virtual ~RowToTreeNodeJob(){};
2838 virtual int operator() ( wxDataViewTreeNode
* node
)
2841 if( current
== static_cast<int>(row
))
2847 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2849 current
+= node
->GetSubTreeCount();
2855 //If the current has no child node, we can find the desired item of the row number directly.
2856 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2857 if( node
->GetNodes().GetCount() == 0)
2859 int index
= static_cast<int>(row
) - current
- 1;
2860 void * n
= node
->GetChildren().Item( index
);
2861 ret
= new wxDataViewTreeNode( parent
) ;
2862 ret
->SetItem( wxDataViewItem( n
));
2863 ret
->SetHasChildren(false);
2872 virtual int operator() ( void * n
)
2875 if( current
== static_cast<int>(row
))
2877 ret
= new wxDataViewTreeNode( parent
) ;
2878 ret
->SetItem( wxDataViewItem( n
));
2879 ret
->SetHasChildren(false);
2885 wxDataViewTreeNode
* GetResult(){ return ret
; }
2889 wxDataViewTreeNode
* ret
;
2890 wxDataViewTreeNode
* parent
;
2894 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2896 RowToTreeNodeJob
job( row
, -2, m_root
);
2897 Walker( m_root
, job
);
2898 return job
.GetResult();
2901 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2903 wxWindow
*parent
= GetParent();
2904 wxDataViewEvent
le(type
, parent
->GetId());
2906 le
.SetEventObject(parent
);
2907 le
.SetModel(GetOwner()->GetModel());
2910 parent
->GetEventHandler()->ProcessEvent(le
);
2914 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2916 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2919 if( node
->HasChildren())
2921 if( !node
->IsOpen())
2923 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2924 //Check if the user prevent expanding
2925 if( e
.GetSkipped() )
2929 //Here I build the children of current node
2930 if( node
->GetChildrenNumber() == 0 )
2933 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2937 //Send the expanded event
2938 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2942 SelectRow( row
, false );
2943 SelectRow( row
+ 1, true );
2944 ChangeCurrentRow( row
+ 1 );
2952 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2954 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2957 wxDataViewTreeNode
* nd
= node
;
2959 if( node
->HasChildren() && node
->IsOpen() )
2961 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2962 if( e
.GetSkipped() )
2967 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2971 node
= node
->GetParent();
2974 int parent
= GetRowByItem( node
->GetItem() ) ;
2977 SelectRow( row
, false);
2978 SelectRow(parent
, true );
2979 ChangeCurrentRow( parent
);
2983 if( !nd
->HasChildren())
2988 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2990 wxDataViewModel
* model
= GetOwner()->GetModel();
2994 //Compose the a parent-chain of the finding item
2996 list
.DeleteContents( true );
2997 wxDataViewItem
it( item
);
3000 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3001 list
.Insert( pItem
);
3002 it
= model
->GetParent( it
);
3005 //Find the item along the parent-chain.
3006 //This algorithm is designed to speed up the node-finding method
3007 wxDataViewTreeNode
* node
= m_root
;
3008 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3010 if( node
->HasChildren() )
3012 if( node
->GetChildrenNumber() == 0 )
3015 BuildTreeHelper(model
, node
->GetItem(), node
);
3018 wxDataViewTreeNodes nodes
= node
->GetNodes();
3020 for (; i
< nodes
.GetCount(); i
++)
3022 if (nodes
[i
]->GetItem() == (**iter
))
3028 if (i
== nodes
.GetCount())
3037 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3039 wxDataViewColumn
*col
= NULL
;
3040 unsigned int cols
= GetOwner()->GetColumnCount();
3041 unsigned int colnum
= 0;
3042 unsigned int x_start
= 0;
3044 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3045 for (x_start
= 0; colnum
< cols
; colnum
++)
3047 col
= GetOwner()->GetColumn(colnum
);
3048 if (col
->IsHidden())
3049 continue; // skip it!
3051 unsigned int w
= col
->GetWidth();
3052 if (x_start
+w
>= (unsigned int)x
)
3059 item
= GetItemByRow( y
/m_lineHeight
);
3062 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3064 int row
= GetRowByItem(item
);
3065 int y
= row
*m_lineHeight
;
3066 int h
= m_lineHeight
;
3068 wxDataViewColumn
*col
= NULL
;
3069 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3071 col
= GetOwner()->GetColumn( i
);
3072 x
+= col
->GetWidth();
3073 if( GetOwner()->GetColumn(i
+1) == column
)
3076 int w
= col
->GetWidth();
3077 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3078 return wxRect(x
, y
, w
, h
);
3081 int wxDataViewMainWindow::RecalculateCount()
3083 return m_root
->GetSubTreeCount();
3086 class ItemToRowJob
: public DoJob
3089 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3090 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3091 virtual ~ItemToRowJob(){};
3093 //Maybe binary search will help to speed up this process
3094 virtual int operator() ( wxDataViewTreeNode
* node
)
3097 if( node
->GetItem() == item
)
3102 if( node
->GetItem() == **m_iter
)
3109 ret
+= node
->GetSubTreeCount();
3115 virtual int operator() ( void * n
)
3118 if( n
== item
.GetID() )
3122 //the row number is begin from zero
3123 int GetResult(){ return ret
-1 ; }
3125 ItemList::const_iterator m_iter
;
3126 wxDataViewItem item
;
3131 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3133 wxDataViewModel
* model
= GetOwner()->GetModel();
3140 //Compose the a parent-chain of the finding item
3142 wxDataViewItem
* pItem
= NULL
;
3143 list
.DeleteContents( true );
3144 wxDataViewItem
it( item
);
3147 pItem
= new wxDataViewItem( it
);
3148 list
.Insert( pItem
);
3149 it
= model
->GetParent( it
);
3151 pItem
= new wxDataViewItem( );
3152 list
.Insert( pItem
);
3154 ItemToRowJob
job( item
, list
.begin() );
3155 Walker(m_root
, job
);
3156 return job
.GetResult();
3159 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3161 if( !model
->IsContainer( item
) )
3164 wxDataViewItemArray children
;
3165 unsigned int num
= model
->GetChildren( item
, children
);
3167 while( index
< num
)
3169 if( model
->IsContainer( children
[index
] ) )
3171 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3172 n
->SetItem(children
[index
]);
3173 n
->SetHasChildren( true ) ;
3178 node
->AddLeaf( children
[index
].GetID() );
3182 node
->SetSubTreeCount( num
);
3183 wxDataViewTreeNode
* n
= node
->GetParent();
3185 n
->ChangeSubTreeCount(num
);
3189 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3191 //First we define a invalid item to fetch the top-level elements
3192 wxDataViewItem item
;
3194 BuildTreeHelper( model
, item
, m_root
);
3198 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3200 if( node
->GetNodeNumber() != 0 )
3202 int len
= node
->GetNodeNumber();
3204 wxDataViewTreeNodes nodes
= node
->GetNodes();
3205 for( ; i
< len
; i
++ )
3207 DestroyTreeHelper(nodes
[i
]);
3213 void wxDataViewMainWindow::DestroyTree()
3215 DestroyTreeHelper(m_root
);
3216 m_root
->SetSubTreeCount(0);
3220 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3222 if (event
.GetKeyCode() == WXK_TAB
)
3224 wxNavigationKeyEvent nevent
;
3225 nevent
.SetWindowChange( event
.ControlDown() );
3226 nevent
.SetDirection( !event
.ShiftDown() );
3227 nevent
.SetEventObject( GetParent()->GetParent() );
3228 nevent
.SetCurrentFocus( m_parent
);
3229 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3233 // no item -> nothing to do
3234 if (!HasCurrentRow())
3240 // don't use m_linesPerPage directly as it might not be computed yet
3241 const int pageSize
= GetCountPerPage();
3242 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3244 switch ( event
.GetKeyCode() )
3247 if ( m_currentRow
> 0 )
3248 OnArrowChar( m_currentRow
- 1, event
);
3252 if ( m_currentRow
< GetRowCount() - 1 )
3253 OnArrowChar( m_currentRow
+ 1, event
);
3255 //Add the process for tree expanding/collapsing
3257 OnCollapsing(m_currentRow
);
3260 OnExpanding( m_currentRow
);
3264 OnArrowChar( GetRowCount() - 1, event
);
3269 OnArrowChar( 0, event
);
3274 int steps
= pageSize
- 1;
3275 int index
= m_currentRow
- steps
;
3279 OnArrowChar( index
, event
);
3285 int steps
= pageSize
- 1;
3286 unsigned int index
= m_currentRow
+ steps
;
3287 unsigned int count
= GetRowCount();
3288 if ( index
>= count
)
3291 OnArrowChar( index
, event
);
3300 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3302 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3304 // let the base handle mouse wheel events.
3309 int x
= event
.GetX();
3310 int y
= event
.GetY();
3311 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3312 wxDataViewColumn
*col
= NULL
;
3315 unsigned int cols
= GetOwner()->GetColumnCount();
3317 for (i
= 0; i
< cols
; i
++)
3319 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3321 continue; // skip it!
3323 if (x
< xpos
+ c
->GetWidth())
3328 xpos
+= c
->GetWidth();
3333 wxDataViewRenderer
*cell
= col
->GetRenderer();
3334 unsigned int current
= y
/ m_lineHeight
;
3335 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3337 // Unselect all if below the last row ?
3341 //Test whether the mouse is hovered on the tree item button
3343 if (GetOwner()->GetExpanderColumn() == col
)
3345 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3346 if( node
!=NULL
&& node
->HasChildren() )
3348 int indent
= node
->GetIndentLevel();
3349 indent
= GetOwner()->GetIndent()*indent
;
3350 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3351 if( rect
.Contains( x
, y
) )
3353 //So the mouse is over the expander
3355 if (m_underMouse
&& m_underMouse
!= node
)
3357 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3358 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3360 if (m_underMouse
!= node
)
3362 //wxLogMessage("Do the row: %d", current);
3365 m_underMouse
= node
;
3368 if (node
!=NULL
&& !node
->HasChildren())
3373 if (m_underMouse
!= NULL
)
3375 wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse
->GetItem()));
3376 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3377 m_underMouse
= NULL
;
3381 wxDataViewModel
*model
= GetOwner()->GetModel();
3383 if (event
.Dragging())
3385 if (m_dragCount
== 0)
3387 // we have to report the raw, physical coords as we want to be
3388 // able to call HitTest(event.m_pointDrag) from the user code to
3389 // get the item being dragged
3390 m_dragStart
= event
.GetPosition();
3395 if (m_dragCount
!= 3)
3398 if (event
.LeftIsDown())
3400 // Notify cell about drag
3409 bool forceClick
= false;
3411 if (event
.ButtonDClick())
3413 m_renameTimer
->Stop();
3414 m_lastOnSame
= false;
3417 if (event
.LeftDClick())
3419 if ( current
== m_lineLastClicked
)
3421 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3423 wxDataViewItem item
= GetItemByRow(current
);
3425 model
->GetValue( value
, item
, col
->GetModelColumn() );
3426 cell
->SetValue( value
);
3427 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3428 col
->GetWidth(), m_lineHeight
);
3429 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3431 wxWindow
*parent
= GetParent();
3432 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3434 le
.SetEventObject(parent
);
3435 le
.SetColumn(col
->GetModelColumn());
3436 le
.SetDataViewColumn(col
);
3437 le
.SetModel(GetOwner()->GetModel());
3439 parent
->GetEventHandler()->ProcessEvent(le
);
3445 // The first click was on another item, so don't interpret this as
3446 // a double click, but as a simple click instead
3453 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3455 // select single line
3456 SelectAllRows( false );
3457 SelectRow( m_lineSelectSingleOnUp
, true );
3460 //Process the event of user clicking the expander
3461 bool expander
= false;
3462 if (GetOwner()->GetExpanderColumn() == col
)
3464 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3465 if( node
!=NULL
&& node
->HasChildren() )
3467 int indent
= node
->GetIndentLevel();
3468 indent
= GetOwner()->GetIndent()*indent
;
3469 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3470 if( rect
.Contains( x
, y
) )
3473 if( node
->IsOpen() )
3474 OnCollapsing(current
);
3476 OnExpanding( current
);
3480 //If the user click the expander, we do not do editing even if the column with expander are editable
3481 if (m_lastOnSame
&& !expander
)
3483 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3484 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3486 m_renameTimer
->Start( 100, true );
3490 m_lastOnSame
= false;
3491 m_lineSelectSingleOnUp
= (unsigned int)-1;
3495 // This is necessary, because after a DnD operation in
3496 // from and to ourself, the up event is swallowed by the
3497 // DnD code. So on next non-up event (which means here and
3498 // now) m_lineSelectSingleOnUp should be reset.
3499 m_lineSelectSingleOnUp
= (unsigned int)-1;
3502 if (event
.RightDown())
3504 m_lineBeforeLastClicked
= m_lineLastClicked
;
3505 m_lineLastClicked
= current
;
3507 // If the item is already selected, do not update the selection.
3508 // Multi-selections should not be cleared if a selected item is clicked.
3509 if (!IsRowSelected(current
))
3511 SelectAllRows(false);
3512 ChangeCurrentRow(current
);
3513 SelectRow(m_currentRow
,true);
3516 // notify cell about right click
3519 // Allow generation of context menu event
3522 else if (event
.MiddleDown())
3524 // notify cell about middle click
3527 if (event
.LeftDown() || forceClick
)
3531 m_lineBeforeLastClicked
= m_lineLastClicked
;
3532 m_lineLastClicked
= current
;
3534 unsigned int oldCurrentRow
= m_currentRow
;
3535 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3537 bool cmdModifierDown
= event
.CmdDown();
3538 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3540 if ( IsSingleSel() || !IsRowSelected(current
) )
3542 SelectAllRows( false );
3544 ChangeCurrentRow(current
);
3546 SelectRow(m_currentRow
,true);
3548 else // multi sel & current is highlighted & no mod keys
3550 m_lineSelectSingleOnUp
= current
;
3551 ChangeCurrentRow(current
); // change focus
3554 else // multi sel & either ctrl or shift is down
3556 if (cmdModifierDown
)
3558 ChangeCurrentRow(current
);
3560 ReverseRowSelection(m_currentRow
);
3562 else if (event
.ShiftDown())
3564 ChangeCurrentRow(current
);
3566 unsigned int lineFrom
= oldCurrentRow
,
3569 if ( lineTo
< lineFrom
)
3572 lineFrom
= m_currentRow
;
3575 SelectRows(lineFrom
, lineTo
, true);
3577 else // !ctrl, !shift
3579 // test in the enclosing if should make it impossible
3580 wxFAIL_MSG( _T("how did we get here?") );
3584 if (m_currentRow
!= oldCurrentRow
)
3585 RefreshRow( oldCurrentRow
);
3587 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3589 // Update selection here...
3592 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3593 (current
== oldCurrentRow
)) && oldWasSelected
;
3597 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3601 if (HasCurrentRow())
3607 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3611 if (HasCurrentRow())
3617 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3619 if( m_selection
.GetCount() != 1 )
3620 return wxDataViewItem();
3622 return GetItemByRow( m_selection
.Item(0));
3625 //-----------------------------------------------------------------------------
3627 //-----------------------------------------------------------------------------
3628 WX_DEFINE_LIST(wxDataViewColumnList
);
3630 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3632 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3633 EVT_SIZE(wxDataViewCtrl::OnSize
)
3636 wxDataViewCtrl::~wxDataViewCtrl()
3639 GetModel()->RemoveNotifier( m_notifier
);
3642 void wxDataViewCtrl::Init()
3647 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3648 const wxPoint
& pos
, const wxSize
& size
,
3649 long style
, const wxValidator
& validator
)
3651 if (!wxControl::Create( parent
, id
, pos
, size
,
3652 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3658 MacSetClipChildren( true ) ;
3661 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3663 if (HasFlag(wxDV_NO_HEADER
))
3664 m_headerArea
= NULL
;
3666 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3668 SetTargetWindow( m_clientArea
);
3670 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3672 sizer
->Add( m_headerArea
, 0, wxGROW
);
3673 sizer
->Add( m_clientArea
, 1, wxGROW
);
3680 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3684 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3687 // we need to process arrows ourselves for scrolling
3688 if ( nMsg
== WM_GETDLGCODE
)
3690 rc
|= DLGC_WANTARROWS
;
3698 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3700 // We need to override OnSize so that our scrolled
3701 // window a) does call Layout() to use sizers for
3702 // positioning the controls but b) does not query
3703 // the sizer for their size and use that for setting
3704 // the scrollable area as set that ourselves by
3705 // calling SetScrollbar() further down.
3712 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3714 if (!wxDataViewCtrlBase::AssociateModel( model
))
3717 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3719 model
->AddNotifier( m_notifier
);
3721 m_clientArea
->BuildTree(model
);
3723 m_clientArea
->UpdateDisplay();
3728 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3730 if (!wxDataViewCtrlBase::AppendColumn(col
))
3733 m_cols
.Append( col
);
3738 void wxDataViewCtrl::OnColumnChange()
3741 m_headerArea
->UpdateDisplay();
3743 m_clientArea
->UpdateDisplay();
3746 void wxDataViewCtrl::DoSetExpanderColumn()
3748 m_clientArea
->UpdateDisplay();
3751 void wxDataViewCtrl::DoSetIndent()
3753 m_clientArea
->UpdateDisplay();
3756 unsigned int wxDataViewCtrl::GetColumnCount() const
3758 return m_cols
.GetCount();
3761 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3763 wxDataViewColumnList::const_iterator iter
;
3765 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3770 if ((*iter
)->IsHidden())
3777 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3779 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3790 bool wxDataViewCtrl::ClearColumns()
3797 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3802 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3807 //Selection code with wxDataViewItem as parameters
3808 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3810 return m_clientArea
->GetSelection();
3813 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3816 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3817 int len
= selection
.GetCount();
3818 for( int i
= 0; i
< len
; i
++)
3820 unsigned int row
= selection
[i
];
3821 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3826 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3828 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3829 int len
= sel
.GetCount();
3830 for( int i
= 0; i
< len
; i
++ )
3832 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3834 selection
.Add( static_cast<unsigned int>(row
) );
3836 m_clientArea
->SetSelections( selection
);
3839 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3841 int row
= m_clientArea
->GetRowByItem( item
);
3844 //Unselect all rows before select another in the single select mode
3845 if (m_clientArea
->IsSingleSel())
3846 m_clientArea
->SelectAllRows(false);
3847 m_clientArea
->SelectRow(row
, true);
3851 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3853 int row
= m_clientArea
->GetRowByItem( item
);
3855 m_clientArea
->SelectRow(row
, false);
3858 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3860 int row
= m_clientArea
->GetRowByItem( item
);
3863 return m_clientArea
->IsRowSelected(row
);
3868 //Selection code with row number as parameter
3869 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3872 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3873 int len
= selection
.GetCount();
3874 for( int i
= 0; i
< len
; i
++)
3876 unsigned int row
= selection
[i
];
3882 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3884 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3885 int len
= sel
.GetCount();
3886 for( int i
= 0; i
< len
; i
++ )
3890 selection
.Add( static_cast<unsigned int>(row
) );
3892 m_clientArea
->SetSelections( selection
);
3895 void wxDataViewCtrl::Select( int row
)
3899 if (m_clientArea
->IsSingleSel())
3900 m_clientArea
->SelectAllRows(false);
3901 m_clientArea
->SelectRow( row
, true );
3905 void wxDataViewCtrl::Unselect( int row
)
3908 m_clientArea
->SelectRow(row
, false);
3911 bool wxDataViewCtrl::IsSelected( int row
) const
3914 return m_clientArea
->IsRowSelected(row
);
3918 void wxDataViewCtrl::SelectRange( int from
, int to
)
3921 for( int i
= from
; i
< to
; i
++ )
3923 m_clientArea
->Select(sel
);
3926 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3928 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3929 for( int i
= from
; i
< to
; i
++ )
3930 if( sel
.Index( i
) != wxNOT_FOUND
)
3932 m_clientArea
->SetSelections(sel
);
3935 void wxDataViewCtrl::SelectAll()
3937 m_clientArea
->SelectAllRows(true);
3940 void wxDataViewCtrl::UnselectAll()
3942 m_clientArea
->SelectAllRows(false);
3945 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3949 if( row
> m_clientArea
->GetRowCount() )
3950 row
= m_clientArea
->GetRowCount();
3952 int first
= m_clientArea
->GetFirstVisibleRow();
3953 int last
= m_clientArea
->GetLastVisibleRow();
3955 m_clientArea
->ScrollTo( row
, column
);
3956 else if( row
> last
)
3957 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3959 m_clientArea
->ScrollTo( first
, column
);
3962 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3964 int row
= m_clientArea
->GetRowByItem(item
);
3967 if( column
== NULL
)
3968 return EnsureVisible(row
, -1);
3972 int len
= GetColumnCount();
3973 for( int i
= 0; i
< len
; i
++ )
3974 if( GetColumn(i
) == column
)
3979 EnsureVisible( row
, col
);
3985 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3987 m_clientArea
->HitTest(point
, item
, column
);
3990 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3992 return m_clientArea
->GetItemRect(item
, column
);
3995 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3997 return m_clientArea
->GetItemByRow( row
);
4000 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4002 return m_clientArea
->GetRowByItem( item
);
4005 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4007 int row
= m_clientArea
->GetRowByItem( item
);
4009 m_clientArea
->Expand(row
);
4012 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4014 int row
= m_clientArea
->GetRowByItem( item
);
4016 m_clientArea
->Collapse(row
);
4020 // !wxUSE_GENERICDATAVIEWCTRL
4023 // wxUSE_DATAVIEWCTRL