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
);
492 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
494 void RefreshRow( unsigned int row
);
495 void RefreshRows( unsigned int from
, unsigned int to
);
496 void RefreshRowsAfter( unsigned int firstRow
);
498 // returns the colour to be used for drawing the rules
499 wxColour
GetRuleColour() const
501 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
504 //void EnsureVisible( unsigned int row );
505 wxRect
GetLineRect( unsigned int row
) const;
507 //Some useful functions for row and item mapping
508 wxDataViewItem
GetItemByRow( unsigned int row
) const;
509 int GetRowByItem( const wxDataViewItem
& item
);
511 //Methods for building the mapping tree
512 void BuildTree( wxDataViewModel
* model
);
514 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
515 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
517 void Expand( unsigned int row
) { OnExpanding( row
); }
518 void Collapse( unsigned int row
) { OnCollapsing( row
); }
520 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
521 //We did not need this temporarily
522 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
524 int RecalculateCount() ;
526 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
527 void OnExpanding( unsigned int row
);
528 void OnCollapsing( unsigned int row
);
530 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
533 wxDataViewCtrl
*m_owner
;
537 wxDataViewColumn
*m_currentCol
;
538 unsigned int m_currentRow
;
539 wxDataViewSelection m_selection
;
541 wxDataViewRenameTimer
*m_renameTimer
;
549 // for double click logic
550 unsigned int m_lineLastClicked
,
551 m_lineBeforeLastClicked
,
552 m_lineSelectSingleOnUp
;
554 // the pen used to draw horiz/vertical rules
557 // the pen used to draw the expander and the lines
560 //This is the tree structure of the model
561 wxDataViewTreeNode
* m_root
;
563 //This is the tree node under the cursor
564 wxDataViewTreeNode
* m_underMouse
;
566 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
567 DECLARE_EVENT_TABLE()
570 // ---------------------------------------------------------
571 // wxGenericDataViewModelNotifier
572 // ---------------------------------------------------------
574 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
577 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
578 { m_mainWindow
= mainWindow
; }
580 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
581 { return m_mainWindow
->ItemAdded( parent
, item
); }
582 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
583 { return m_mainWindow
->ItemDeleted( parent
, item
); }
584 virtual bool ItemChanged( const wxDataViewItem
& item
)
585 { return m_mainWindow
->ItemChanged(item
); }
586 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
587 { return m_mainWindow
->ValueChanged( item
, col
); }
588 virtual bool Cleared()
589 { return m_mainWindow
->Cleared(); }
590 virtual void Resort()
591 { m_mainWindow
->Resort(); }
593 wxDataViewMainWindow
*m_mainWindow
;
596 // ---------------------------------------------------------
597 // wxDataViewRenderer
598 // ---------------------------------------------------------
600 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
602 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
603 wxDataViewCellMode mode
,
605 wxDataViewRendererBase( varianttype
, mode
, align
)
612 wxDataViewRenderer::~wxDataViewRenderer()
618 wxDC
*wxDataViewRenderer::GetDC()
622 if (GetOwner() == NULL
)
624 if (GetOwner()->GetOwner() == NULL
)
626 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
632 // ---------------------------------------------------------
633 // wxDataViewCustomRenderer
634 // ---------------------------------------------------------
636 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
638 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
639 wxDataViewCellMode mode
, int align
) :
640 wxDataViewRenderer( varianttype
, mode
, align
)
644 // ---------------------------------------------------------
645 // wxDataViewTextRenderer
646 // ---------------------------------------------------------
648 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
650 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
651 wxDataViewCellMode mode
, int align
) :
652 wxDataViewCustomRenderer( varianttype
, mode
, align
)
656 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
658 m_text
= value
.GetString();
663 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
668 bool wxDataViewTextRenderer::HasEditorCtrl()
673 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
674 wxRect labelRect
, const wxVariant
&value
)
676 return new wxTextCtrl( parent
, wxID_ANY
, value
,
677 wxPoint(labelRect
.x
,labelRect
.y
),
678 wxSize(labelRect
.width
,labelRect
.height
) );
681 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
683 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
684 value
= text
->GetValue();
688 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
690 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
691 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
692 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
693 view
->GetForegroundColour();
695 dc
->SetTextForeground(col
);
696 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
701 wxSize
wxDataViewTextRenderer::GetSize() const
703 const wxDataViewCtrl
*view
= GetView();
707 view
->GetTextExtent( m_text
, &x
, &y
);
708 return wxSize( x
, y
);
710 return wxSize(80,20);
713 // ---------------------------------------------------------
714 // wxDataViewBitmapRenderer
715 // ---------------------------------------------------------
717 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
719 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
720 wxDataViewCellMode mode
, int align
) :
721 wxDataViewCustomRenderer( varianttype
, mode
, align
)
725 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
727 if (value
.GetType() == wxT("wxBitmap"))
729 if (value
.GetType() == wxT("wxIcon"))
735 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
740 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
743 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
744 else if (m_icon
.Ok())
745 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
750 wxSize
wxDataViewBitmapRenderer::GetSize() const
753 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
754 else if (m_icon
.Ok())
755 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
757 return wxSize(16,16);
760 // ---------------------------------------------------------
761 // wxDataViewToggleRenderer
762 // ---------------------------------------------------------
764 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
766 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
767 wxDataViewCellMode mode
, int align
) :
768 wxDataViewCustomRenderer( varianttype
, mode
, align
)
773 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
775 m_toggle
= value
.GetBool();
780 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
785 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
787 // User wxRenderer here
790 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
792 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
797 flags
|= wxCONTROL_CHECKED
;
798 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
799 flags
|= wxCONTROL_DISABLED
;
801 wxRendererNative::Get().DrawCheckBox(
802 GetOwner()->GetOwner(),
810 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
811 wxDataViewModel
*model
,
812 const wxDataViewItem
& item
, unsigned int col
)
814 bool value
= !m_toggle
;
815 wxVariant variant
= value
;
816 model
->SetValue( variant
, item
, col
);
817 model
->ValueChanged( item
, col
);
821 wxSize
wxDataViewToggleRenderer::GetSize() const
823 return wxSize(20,20);
826 // ---------------------------------------------------------
827 // wxDataViewProgressRenderer
828 // ---------------------------------------------------------
830 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
832 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
833 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
834 wxDataViewCustomRenderer( varianttype
, mode
, align
)
840 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
844 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
846 m_value
= (long) value
;
848 if (m_value
< 0) m_value
= 0;
849 if (m_value
> 100) m_value
= 100;
854 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
856 value
= (long) m_value
;
860 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
862 double pct
= (double)m_value
/ 100.0;
864 bar
.width
= (int)(cell
.width
* pct
);
865 dc
->SetPen( *wxTRANSPARENT_PEN
);
866 dc
->SetBrush( *wxBLUE_BRUSH
);
867 dc
->DrawRectangle( bar
);
869 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
870 dc
->SetPen( *wxBLACK_PEN
);
871 dc
->DrawRectangle( cell
);
876 wxSize
wxDataViewProgressRenderer::GetSize() const
878 return wxSize(40,12);
881 // ---------------------------------------------------------
882 // wxDataViewDateRenderer
883 // ---------------------------------------------------------
885 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
887 #if wxUSE_DATE_RENDERER_POPUP
889 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
892 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
893 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
894 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
899 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
900 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
901 sizer
->Add( m_cal
, 1, wxGROW
);
906 void OnCalendar( wxCalendarEvent
&event
);
908 wxCalendarCtrl
*m_cal
;
909 wxDataViewModel
*m_model
;
911 const wxDataViewItem
& m_item
;
914 virtual void OnDismiss()
919 DECLARE_EVENT_TABLE()
922 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
923 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
926 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
928 wxDateTime date
= event
.GetDate();
929 wxVariant value
= date
;
930 m_model
->SetValue( value
, m_item
, m_col
);
931 m_model
->ValueChanged( m_item
, m_col
);
935 #endif // wxUSE_DATE_RENDERER_POPUP
937 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
939 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
940 wxDataViewCellMode mode
, int align
) :
941 wxDataViewCustomRenderer( varianttype
, mode
, align
)
945 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
947 m_date
= value
.GetDateTime();
952 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
958 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
960 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
961 wxString tmp
= m_date
.FormatDate();
962 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
967 wxSize
wxDataViewDateRenderer::GetSize() const
969 const wxDataViewCtrl
* view
= GetView();
970 wxString tmp
= m_date
.FormatDate();
972 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
973 return wxSize(x
,y
+d
);
976 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
977 const wxDataViewItem
& item
, unsigned int col
)
980 model
->GetValue( variant
, item
, col
);
981 wxDateTime value
= variant
.GetDateTime();
983 #if wxUSE_DATE_RENDERER_POPUP
984 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
985 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
986 wxPoint pos
= wxGetMousePosition();
989 popup
->Popup( popup
->m_cal
);
990 #else // !wxUSE_DATE_RENDERER_POPUP
991 wxMessageBox(value
.Format());
992 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
996 // ---------------------------------------------------------
997 // wxDataViewIconTextRenderer
998 // ---------------------------------------------------------
1000 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1002 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1003 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1004 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1007 SetAlignment(align
);
1010 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1014 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1020 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1025 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1027 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1029 const wxIcon
&icon
= m_value
.GetIcon();
1032 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1033 cell
.x
+= icon
.GetWidth()+4;
1036 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1041 wxSize
wxDataViewIconTextRenderer::GetSize() const
1043 return wxSize(80,16); // TODO
1046 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1051 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1056 // ---------------------------------------------------------
1058 // ---------------------------------------------------------
1060 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1062 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1063 unsigned int model_column
,
1064 int width
, wxAlignment align
, int flags
) :
1065 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1067 SetAlignment(align
);
1071 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1074 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1075 unsigned int model_column
,
1076 int width
, wxAlignment align
, int flags
) :
1077 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1079 SetAlignment(align
);
1082 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1085 wxDataViewColumn::~wxDataViewColumn()
1089 void wxDataViewColumn::Init( int width
)
1092 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1096 void wxDataViewColumn::SetResizeable( bool resizeable
)
1099 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1101 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1104 void wxDataViewColumn::SetHidden( bool hidden
)
1107 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1109 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1111 // tell our owner to e.g. update its scrollbars:
1113 GetOwner()->OnColumnChange();
1116 void wxDataViewColumn::SetSortable( bool sortable
)
1119 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1121 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1123 // Update header button
1125 GetOwner()->OnColumnChange();
1128 void wxDataViewColumn::SetSortOrder( bool ascending
)
1130 m_ascending
= ascending
;
1132 // Update header button
1134 GetOwner()->OnColumnChange();
1137 bool wxDataViewColumn::IsSortOrderAscending() const
1142 void wxDataViewColumn::SetInternalWidth( int width
)
1146 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1147 if (m_owner
&& m_owner
->m_clientArea
)
1148 m_owner
->m_clientArea
->RecalculateDisplay();
1151 void wxDataViewColumn::SetWidth( int width
)
1153 m_owner
->m_headerArea
->UpdateDisplay();
1155 SetInternalWidth(width
);
1159 //-----------------------------------------------------------------------------
1160 // wxDataViewHeaderWindowBase
1161 //-----------------------------------------------------------------------------
1163 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1165 wxWindow
*parent
= GetParent();
1166 wxDataViewEvent
le(type
, parent
->GetId());
1168 le
.SetEventObject(parent
);
1170 le
.SetDataViewColumn(GetColumn(n
));
1171 le
.SetModel(GetOwner()->GetModel());
1173 // for events created by wxDataViewHeaderWindow the
1174 // row / value fields are not valid
1176 parent
->GetEventHandler()->ProcessEvent(le
);
1179 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1181 // implemented in msw/listctrl.cpp:
1182 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1184 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1186 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1187 const wxPoint
&pos
, const wxSize
&size
,
1188 const wxString
&name
)
1192 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1195 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1196 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1197 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1198 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1200 // create the native WC_HEADER window:
1201 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1202 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1203 m_hWnd
= CreateWindowEx(0,
1214 wxLogLastError(_T("CreateWindowEx"));
1218 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1219 // to call wxDataViewHeaderWindow::MSWOnNotify
1220 SubclassWin(m_hWnd
);
1222 // the following is required to get the default win's font for
1223 // header windows and must be done befor sending the HDM_LAYOUT msg
1230 // Retrieve the bounding rectangle of the parent window's
1231 // client area, and then request size and position values
1232 // from the header control.
1233 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1235 hdl
.prc
= &rcParent
;
1237 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1239 wxLogLastError(_T("SendMessage"));
1243 // Set the size, position, and visibility of the header control.
1244 SetWindowPos((HWND
)m_hWnd
,
1248 wp
.flags
| SWP_SHOWWINDOW
);
1250 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1251 // a wxBoxSizer and in order to avoid super-big header windows,
1252 // we need to set our height as fixed
1253 SetMinSize(wxSize(-1, wp
.cy
));
1254 SetMaxSize(wxSize(-1, wp
.cy
));
1259 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1264 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1266 // remove old columns
1267 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1268 Header_DeleteItem((HWND
)m_hWnd
, 0);
1270 // add the updated array of columns to the header control
1271 unsigned int cols
= GetOwner()->GetColumnCount();
1272 unsigned int added
= 0;
1273 wxDataViewModel
* model
= GetOwner()->GetModel();
1274 for (unsigned int i
= 0; i
< cols
; i
++)
1276 wxDataViewColumn
*col
= GetColumn( i
);
1277 if (col
->IsHidden())
1278 continue; // don't add it!
1281 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1282 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1283 hdi
.cxy
= col
->GetWidth();
1284 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1285 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1286 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1289 if(model
&& m_owner
->GetSortingColumn() == col
)
1291 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1292 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1293 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1297 // lParam is reserved for application's use:
1298 // we store there the column index to use it later in MSWOnNotify
1299 // (since columns may have been hidden)
1300 hdi
.lParam
= (LPARAM
)i
;
1302 // the native wxMSW implementation of the header window
1303 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1304 // on the right: to correct this effect we make the column
1305 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1307 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1309 switch (col
->GetAlignment())
1312 hdi
.fmt
|= HDF_LEFT
;
1314 case wxALIGN_CENTER
:
1315 case wxALIGN_CENTER_HORIZONTAL
:
1316 hdi
.fmt
|= HDF_CENTER
;
1319 hdi
.fmt
|= HDF_RIGHT
;
1323 // such alignment is not allowed for the column header!
1327 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1328 (WPARAM
)added
, (LPARAM
)&hdi
);
1333 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1337 // NOTE: we don't just return nmHDR->iItem because when there are
1338 // hidden columns, nmHDR->iItem may be different from
1339 // nmHDR->pitem->lParam
1341 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1343 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1348 item
.mask
= HDI_LPARAM
;
1349 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1351 return (unsigned int)item
.lParam
;
1354 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1356 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1358 // is it a message from the header?
1359 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1360 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1362 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1363 switch ( nmhdr
->code
)
1365 case HDN_BEGINTRACK
:
1366 // user has started to resize a column:
1367 // do we need to veto it?
1368 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1376 // user has started to reorder a column
1379 case HDN_ITEMCHANGING
:
1380 if (nmHDR
->pitem
!= NULL
&&
1381 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1383 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1384 if (nmHDR
->pitem
->cxy
< minWidth
)
1386 // do not allow the user to resize this column under
1387 // its minimal width:
1393 case HDN_ITEMCHANGED
: // user is resizing a column
1394 case HDN_ENDTRACK
: // user has finished resizing a column
1395 case HDN_ENDDRAG
: // user has finished reordering a column
1397 // update the width of the modified column:
1398 if (nmHDR
->pitem
!= NULL
&&
1399 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1401 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1402 unsigned int w
= nmHDR
->pitem
->cxy
;
1403 wxDataViewColumn
*col
= GetColumn(idx
);
1405 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1406 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1407 w
-= COLUMN_WIDTH_OFFSET
;
1409 if (w
>= (unsigned)col
->GetMinWidth())
1410 col
->SetInternalWidth(w
);
1416 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1417 wxDataViewModel
* model
= GetOwner()->GetModel();
1419 if(nmHDR
->iButton
== 0)
1421 wxDataViewColumn
*col
= GetColumn(idx
);
1422 if(col
->IsSortable())
1424 if(model
&& m_owner
->GetSortingColumn() == col
)
1426 bool order
= col
->IsSortOrderAscending();
1427 col
->SetSortOrder(!order
);
1431 m_owner
->SetSortingColumn(col
);
1439 wxEventType evt
= nmHDR
->iButton
== 0 ?
1440 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1441 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1442 SendEvent(evt
, idx
);
1448 // NOTE: for some reason (i.e. for a bug in Windows)
1449 // the HDN_ITEMCLICK notification is not sent on
1450 // right clicks, so we need to handle NM_RCLICK
1453 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1454 if (column
!= wxNOT_FOUND
)
1457 item
.mask
= HDI_LPARAM
;
1458 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1460 // 'idx' may be different from 'column' if there are
1461 // hidden columns...
1462 unsigned int idx
= (unsigned int)item
.lParam
;
1463 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1469 case HDN_GETDISPINFOW
:
1470 // see wxListCtrl::MSWOnNotify for more info!
1473 case HDN_ITEMDBLCLICK
:
1475 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1476 int w
= GetOwner()->GetBestColumnWidth(idx
);
1478 // update the native control:
1480 ZeroMemory(&hd
, sizeof(hd
));
1481 hd
.mask
= HDI_WIDTH
;
1483 Header_SetItem(GetHwnd(),
1484 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1487 // update the wxDataViewColumn class:
1488 GetColumn(idx
)->SetInternalWidth(w
);
1493 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1499 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1500 const wxRect
*WXUNUSED(rect
))
1502 wxSize ourSz
= GetClientSize();
1503 wxSize ownerSz
= m_owner
->GetClientSize();
1505 // where should the (logical) origin of this window be placed?
1507 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1509 // put this window on top of our parent and
1510 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1511 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1515 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1516 int WXUNUSED(w
), int WXUNUSED(h
),
1519 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1520 // the wxDataViewCtrl window gets resized: the following dummy call
1521 // to ScrollWindow() is required in order to get this header window
1522 // correctly repainted when it's (horizontally) scrolled:
1527 #else // !defined(__WXMSW__)
1529 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1530 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1531 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1532 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1533 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1536 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1537 const wxPoint
&pos
, const wxSize
&size
,
1538 const wxString
&name
)
1542 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1545 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1546 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1547 SetOwnForegroundColour( attr
.colFg
);
1548 SetOwnBackgroundColour( attr
.colBg
);
1550 SetOwnFont( attr
.font
);
1552 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1553 // a wxBoxSizer and in order to avoid super-big header windows,
1554 // we need to set our height as fixed
1555 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1556 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1561 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1564 GetClientSize( &w
, &h
);
1566 wxAutoBufferedPaintDC
dc( this );
1568 dc
.SetBackground(GetBackgroundColour());
1572 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1575 m_owner
->GetViewStart( &x
, NULL
);
1577 // account for the horz scrollbar offset
1578 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1580 dc
.SetFont( GetFont() );
1582 unsigned int cols
= GetOwner()->GetColumnCount();
1585 for (i
= 0; i
< cols
; i
++)
1587 wxDataViewColumn
*col
= GetColumn( i
);
1588 if (col
->IsHidden())
1589 continue; // skip it!
1591 int cw
= col
->GetWidth();
1594 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1595 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1597 if (col
->IsSortOrderAscending())
1598 sortArrow
= wxHDR_SORT_ICON_UP
;
1600 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1603 wxRendererNative::Get().DrawHeaderButton
1607 wxRect(xpos
, 0, cw
, ch
-1),
1608 m_parent
->IsEnabled() ? 0
1609 : (int)wxCONTROL_DISABLED
,
1613 // align as required the column title:
1615 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1616 switch (col
->GetAlignment())
1619 x
+= HEADER_HORIZ_BORDER
;
1621 case wxALIGN_CENTER
:
1622 case wxALIGN_CENTER_HORIZONTAL
:
1623 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1626 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1630 // always center the title vertically:
1631 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1633 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1635 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1636 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1637 dc
.DrawText( col
->GetTitle(), x
, y
);
1638 dc
.DestroyClippingRegion();
1644 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1646 GetParent()->SetFocus();
1650 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1652 // we want to work with logical coords
1654 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1655 int y
= event
.GetY();
1659 // we don't draw the line beyond our window,
1660 // but we allow dragging it there
1662 GetClientSize( &w
, NULL
);
1663 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1666 if (event
.ButtonUp())
1668 m_isDragging
= false;
1674 m_currentX
= wxMax(m_minX
+ 7, x
);
1678 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1680 GetOwner()->Refresh();
1684 else // not dragging
1687 m_column
= wxNOT_FOUND
;
1689 bool hit_border
= false;
1691 // end of the current column
1694 // find the column where this event occured
1695 int countCol
= m_owner
->GetColumnCount();
1696 for (int column
= 0; column
< countCol
; column
++)
1698 wxDataViewColumn
*p
= GetColumn(column
);
1701 continue; // skip if not shown
1703 xpos
+= p
->GetWidth();
1705 if ((abs(x
-xpos
) < 3) && (y
< 22))
1713 // inside the column
1720 if (m_column
== wxNOT_FOUND
)
1723 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1724 if (event
.LeftDClick() && resizeable
)
1726 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1729 else if (event
.LeftDown() || event
.RightUp())
1731 if (hit_border
&& event
.LeftDown() && resizeable
)
1733 m_isDragging
= true;
1737 else // click on a column
1739 wxDataViewModel
* model
= GetOwner()->GetModel();
1740 wxEventType evt
= event
.LeftDown() ?
1741 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1742 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1743 SendEvent(evt
, m_column
);
1745 //Left click the header
1746 if(event
.LeftDown())
1748 wxDataViewColumn
*col
= GetColumn(m_column
);
1749 if(col
->IsSortable())
1751 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1752 if(model
&& sortCol
== col
)
1754 bool order
= col
->IsSortOrderAscending();
1755 col
->SetSortOrder(!order
);
1759 m_owner
->SetSortingColumn(col
);
1765 //Send the column sorted event
1766 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1770 else if (event
.Moving())
1772 if (hit_border
&& resizeable
)
1773 m_currentCursor
= m_resizeCursor
;
1775 m_currentCursor
= wxSTANDARD_CURSOR
;
1777 SetCursor(*m_currentCursor
);
1782 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1783 void wxGenericDataViewHeaderWindow::DrawCurrent()
1786 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1788 int x1
= m_currentX
;
1790 ClientToScreen (&x1
, &y1
);
1792 int x2
= m_currentX
-1;
1794 ++x2
; // but why ????
1797 m_owner
->GetClientSize( NULL
, &y2
);
1798 m_owner
->ClientToScreen( &x2
, &y2
);
1801 dc
.SetLogicalFunction(wxINVERT
);
1802 dc
.SetPen(m_penCurrent
);
1803 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1805 dc
.DrawLine(x1
, y1
, x2
, y2
);
1809 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1813 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1814 m_owner
->GetViewStart( &x
, NULL
);
1816 // shift the DC origin to match the position of the main window horizontal
1817 // scrollbar: this allows us to always use logical coords
1818 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1821 #endif // defined(__WXMSW__)
1823 //-----------------------------------------------------------------------------
1824 // wxDataViewRenameTimer
1825 //-----------------------------------------------------------------------------
1827 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1832 void wxDataViewRenameTimer::Notify()
1834 m_owner
->OnRenameTimer();
1837 //-----------------------------------------------------------------------------
1838 // wxDataViewMainWindow
1839 //-----------------------------------------------------------------------------
1841 //The tree building helper, declared firstly
1842 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1844 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1846 if (row1
> row2
) return 1;
1847 if (row1
== row2
) return 0;
1852 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1854 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1855 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1856 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1857 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1858 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1859 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1862 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1863 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1864 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1865 m_selection( wxDataViewSelectionCmp
)
1870 m_lastOnSame
= false;
1871 m_renameTimer
= new wxDataViewRenameTimer( this );
1873 // TODO: user better initial values/nothing selected
1874 m_currentCol
= NULL
;
1877 // TODO: we need to calculate this smartly
1884 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1887 m_dragStart
= wxPoint(0,0);
1888 m_lineLastClicked
= (unsigned int) -1;
1889 m_lineBeforeLastClicked
= (unsigned int) -1;
1890 m_lineSelectSingleOnUp
= (unsigned int) -1;
1894 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1895 SetBackgroundColour( *wxWHITE
);
1897 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1899 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1900 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1901 //Some new added code to deal with the tree structure
1902 m_root
= new wxDataViewTreeNode( NULL
);
1903 m_root
->SetHasChildren(true);
1905 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1907 m_underMouse
= NULL
;
1911 wxDataViewMainWindow::~wxDataViewMainWindow()
1914 delete m_renameTimer
;
1917 void wxDataViewMainWindow::OnRenameTimer()
1919 // We have to call this here because changes may just have
1920 // been made and no screen update taken place.
1925 unsigned int cols
= GetOwner()->GetColumnCount();
1927 for (i
= 0; i
< cols
; i
++)
1929 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1931 continue; // skip it!
1933 if (c
== m_currentCol
)
1935 xpos
+= c
->GetWidth();
1937 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1938 m_currentCol
->GetWidth(), m_lineHeight
);
1940 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1941 &labelRect
.x
, &labelRect
.y
);
1943 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1944 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1948 //------------------------------------------------------------------
1949 // Helper class for do operation on the tree node
1950 //------------------------------------------------------------------
1957 //The return value control how the tree-walker tranverse the tree
1958 // 0: Job done, stop tranverse and return
1959 // 1: Ignore the current node's subtree and continue
1960 // 2: Job not done, continue
1961 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1962 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1963 virtual int operator() ( void * n
) = 0;
1966 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1971 switch( func( node
) )
1982 wxDataViewTreeNodes nodes
= node
->GetNodes();
1983 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1985 int len_nodes
= nodes
.GetCount();
1986 int len
= leaves
.GetCount();
1987 int i
= 0, nodes_i
= 0;
1989 for( ; i
< len
; i
++ )
1991 void * n
= leaves
[i
];
1992 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1994 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1997 if( Walker( nd
, func
) )
2016 void wxDataViewMainWindow::SendModelEvent( wxEventType type
, const wxDataViewItem
& item
)
2018 wxWindow
*parent
= GetParent();
2019 wxDataViewEvent
le(type
, parent
->GetId());
2021 le
.SetEventObject(parent
);
2022 le
.SetModel(GetOwner()->GetModel());
2025 parent
->GetEventHandler()->ProcessEvent(le
);
2028 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2032 wxDataViewTreeNode
* node
;
2033 node
= FindNode(parent
);
2034 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, item
);
2039 node
->SetHasChildren( true );
2041 if( g_model
->IsContainer( item
) )
2043 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2044 newnode
->SetItem(item
);
2045 newnode
->SetHasChildren( true );
2046 node
->AddNode( newnode
);
2049 node
->AddLeaf( item
.GetID() );
2051 node
->ChangeSubTreeCount(1);
2059 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2061 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2062 const wxDataViewItem
& item
)
2064 wxDataViewTreeNode
* node
= FindNode(parent
);
2066 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2067 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2070 node
->GetChildren().Remove( item
.GetID() );
2071 //Manuplate selection
2072 if( m_selection
.GetCount() > 1 )
2074 m_selection
.Empty();
2076 bool isContainer
= false;
2077 wxDataViewTreeNodes nds
= node
->GetNodes();
2078 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2080 if (nds
[i
]->GetItem() == item
)
2088 wxDataViewTreeNode
* n
= NULL
;
2089 wxDataViewTreeNodes nodes
= node
->GetNodes();
2090 int len
= nodes
.GetCount();
2091 for( int i
= 0 ; i
< len
; i
++)
2093 if( nodes
[i
]->GetItem() == item
)
2100 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2102 node
->GetNodes().Remove( n
);
2103 sub
-= n
->GetSubTreeCount();
2104 DestroyTreeHelper(n
);
2106 //Make the row number invalid and get a new valid one when user call GetRowCount
2108 node
->ChangeSubTreeCount(sub
);
2109 if( node
->GetChildrenNumber() == 0)
2111 node
->GetParent()->GetNodes().Remove( node
);
2115 //Change the current row to the last row if the current exceed the max row number
2116 if( m_currentRow
> GetRowCount() )
2117 m_currentRow
= m_count
- 1;
2121 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, item
);
2126 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2131 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
,item
);
2136 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2138 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2139 /*#define MAX_VIRTUAL_WIDTH 100000
2141 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2142 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2143 Refresh( true, &rect );
2151 wxWindow
*parent
= GetParent();
2152 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, parent
->GetId());
2153 le
.SetEventObject(parent
);
2154 le
.SetModel(GetOwner()->GetModel());
2157 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2158 parent
->GetEventHandler()->ProcessEvent(le
);
2163 bool wxDataViewMainWindow::Cleared()
2170 wxWindow
*parent
= GetParent();
2171 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, parent
->GetId());
2172 le
.SetEventObject(parent
);
2173 le
.SetModel(GetOwner()->GetModel());
2174 parent
->GetEventHandler()->ProcessEvent(le
);
2179 void wxDataViewMainWindow::UpdateDisplay()
2184 void wxDataViewMainWindow::OnInternalIdle()
2186 wxWindow::OnInternalIdle();
2190 RecalculateDisplay();
2195 void wxDataViewMainWindow::RecalculateDisplay()
2197 wxDataViewModel
*model
= GetOwner()->GetModel();
2204 int width
= GetEndOfLastCol();
2205 int height
= GetRowCount() * m_lineHeight
;
2207 SetVirtualSize( width
, height
);
2208 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2213 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2215 wxWindow::ScrollWindow( dx
, dy
, rect
);
2217 if (GetOwner()->m_headerArea
)
2218 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2221 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2224 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2225 int sy
= rows
*m_lineHeight
/y
;
2229 wxRect rect
= GetClientRect();
2230 unsigned int colnum
= 0;
2231 unsigned int x_start
= 0, x_end
= 0, w
= 0;
2233 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2234 for (x_start
= 0; colnum
< column
; colnum
++)
2236 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2237 if (col
->IsHidden())
2238 continue; // skip it!
2240 w
= col
->GetWidth();
2244 x_end
= x_start
+ w
;
2245 xe
= xx
+ rect
.width
;
2248 sx
= ( xx
+ x_end
- xe
)/x
;
2255 m_owner
->Scroll( sx
, sy
);
2258 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2260 wxDataViewModel
*model
= GetOwner()->GetModel();
2261 wxAutoBufferedPaintDC
dc( this );
2264 dc
.SetBackground(GetBackgroundColour());
2266 GetOwner()->PrepareDC( dc
);
2267 dc
.SetFont( GetFont() );
2269 wxRect update
= GetUpdateRegion().GetBox();
2270 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2272 // compute which items needs to be redrawn
2273 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2274 unsigned int item_count
=
2275 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2276 (int)(GetRowCount( )- item_start
) );
2277 unsigned int item_last
= item_start
+ item_count
;
2279 // compute which columns needs to be redrawn
2280 unsigned int cols
= GetOwner()->GetColumnCount();
2281 unsigned int col_start
= 0;
2282 unsigned int x_start
= 0;
2283 for (x_start
= 0; col_start
< cols
; col_start
++)
2285 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2286 if (col
->IsHidden())
2287 continue; // skip it!
2289 unsigned int w
= col
->GetWidth();
2290 if (x_start
+w
>= (unsigned int)update
.x
)
2296 unsigned int col_last
= col_start
;
2297 unsigned int x_last
= x_start
;
2298 for (; col_last
< cols
; col_last
++)
2300 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2301 if (col
->IsHidden())
2302 continue; // skip it!
2304 if (x_last
> (unsigned int)update
.GetRight())
2307 x_last
+= col
->GetWidth();
2310 // Draw horizontal rules if required
2311 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2313 dc
.SetPen(m_penRule
);
2314 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2316 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2318 int y
= i
* m_lineHeight
;
2319 dc
.DrawLine(x_start
, y
, x_last
, y
);
2323 // Draw vertical rules if required
2324 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2326 dc
.SetPen(m_penRule
);
2327 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2330 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2332 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2333 if (col
->IsHidden())
2334 continue; // skip it
2336 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2337 x
, item_last
* m_lineHeight
);
2339 x
+= col
->GetWidth();
2342 // Draw last vertical rule
2343 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2344 x
, item_last
* m_lineHeight
);
2347 // redraw the background for the items which are selected/current
2348 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2350 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2351 if (selected
|| item
== m_currentRow
)
2353 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2354 if (item
== m_currentRow
)
2355 flags
|= wxCONTROL_CURRENT
;
2357 flags
|= wxCONTROL_FOCUSED
;
2359 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2360 wxRendererNative::Get().DrawItemSelectionRect
2370 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2373 // TODO: last column for RTL support
2374 expander
= GetOwner()->GetColumn( 0 );
2375 GetOwner()->SetExpanderColumn(expander
);
2378 // redraw all cells for all rows which must be repainted and for all columns
2380 cell_rect
.x
= x_start
;
2381 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2382 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2384 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2385 wxDataViewRenderer
*cell
= col
->GetRenderer();
2386 cell_rect
.width
= col
->GetWidth();
2388 if (col
->IsHidden())
2389 continue; // skipt it!
2392 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2394 // get the cell value and set it into the renderer
2396 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2402 wxDataViewItem dataitem
= node
->GetItem();
2403 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2404 cell
->SetValue( value
);
2406 // update the y offset
2407 cell_rect
.y
= item
* m_lineHeight
;
2409 //Draw the expander here.
2410 int indent
= node
->GetIndentLevel();
2411 if( col
== expander
)
2413 //Calculate the indent first
2414 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2416 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2417 // change the cell_rect.x to the appropriate pos
2418 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2419 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2420 dc
.SetPen( m_penExpander
);
2421 dc
.SetBrush( wxNullBrush
);
2422 if( node
->HasChildren() )
2424 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2426 if (m_underMouse
== node
)
2428 flag
|= wxCONTROL_CURRENT
;
2430 if( node
->IsOpen() )
2431 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2433 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2437 // I am wandering whether we should draw dot lines between tree nodes
2439 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2442 //force the expander column to left-center align
2443 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2447 // cannot be bigger than allocated space
2448 wxSize size
= cell
->GetSize();
2449 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2450 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2451 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2453 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2454 int align
= cell
->GetAlignment();
2456 // horizontal alignment:
2457 item_rect
.x
= cell_rect
.x
;
2458 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2459 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2460 else if (align
& wxALIGN_RIGHT
)
2461 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2462 //else: wxALIGN_LEFT is the default
2464 // vertical alignment:
2465 item_rect
.y
= cell_rect
.y
;
2466 if (align
& wxALIGN_CENTER_VERTICAL
)
2467 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2468 else if (align
& wxALIGN_BOTTOM
)
2469 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2470 //else: wxALIGN_TOP is the default
2473 item_rect
.x
+= PADDING_RIGHTLEFT
;
2474 item_rect
.y
+= PADDING_TOPBOTTOM
;
2475 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2476 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2478 //Here we add the tree indent
2479 item_rect
.x
+= indent
;
2482 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2483 state
|= wxDATAVIEW_CELL_SELECTED
;
2485 // TODO: it would be much more efficient to create a clipping
2486 // region for the entire column being rendered (in the OnPaint
2487 // of wxDataViewMainWindow) instead of a single clip region for
2488 // each cell. However it would mean that each renderer should
2489 // respect the given wxRect's top & bottom coords, eventually
2490 // violating only the left & right coords - however the user can
2491 // make its own renderer and thus we cannot be sure of that.
2492 dc
.SetClippingRegion( item_rect
);
2493 cell
->Render( item_rect
, &dc
, state
);
2494 dc
.DestroyClippingRegion();
2497 cell_rect
.x
+= cell_rect
.width
;
2501 int wxDataViewMainWindow::GetCountPerPage() const
2503 wxSize size
= GetClientSize();
2504 return size
.y
/ m_lineHeight
;
2507 int wxDataViewMainWindow::GetEndOfLastCol() const
2511 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2513 const wxDataViewColumn
*c
=
2514 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2517 width
+= c
->GetWidth();
2522 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2526 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2528 return y
/ m_lineHeight
;
2531 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2533 wxSize client_size
= GetClientSize();
2534 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2535 &client_size
.x
, &client_size
.y
);
2537 //we should deal with the pixel here
2538 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2539 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2542 return wxMin( GetRowCount()-1, row
);
2545 unsigned int wxDataViewMainWindow::GetRowCount()
2547 if ( m_count
== -1 )
2549 m_count
= RecalculateCount();
2551 GetVirtualSize( &width
, &height
);
2552 height
= m_count
* m_lineHeight
;
2554 SetVirtualSize( width
, height
);
2559 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2566 void wxDataViewMainWindow::SelectAllRows( bool on
)
2573 m_selection
.Clear();
2574 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2575 m_selection
.Add( i
);
2580 unsigned int first_visible
= GetFirstVisibleRow();
2581 unsigned int last_visible
= GetLastVisibleRow();
2583 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2585 unsigned int row
= m_selection
[i
];
2586 if ((row
>= first_visible
) && (row
<= last_visible
))
2589 m_selection
.Clear();
2593 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2595 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2599 m_selection
.Add( row
);
2607 m_selection
.Remove( row
);
2613 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2617 unsigned int tmp
= from
;
2623 for (i
= from
; i
<= to
; i
++)
2625 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2628 m_selection
.Add( i
);
2633 m_selection
.Remove( i
);
2636 RefreshRows( from
, to
);
2639 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2641 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2643 int n
= aSelections
[i
];
2645 m_selection
.Add( n
);
2650 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2652 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2653 m_selection
.Add( row
);
2655 m_selection
.Remove( row
);
2659 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2661 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2664 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2666 wxWindow
*parent
= GetParent();
2667 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2669 le
.SetEventObject(parent
);
2670 le
.SetModel(GetOwner()->GetModel());
2673 parent
->GetEventHandler()->ProcessEvent(le
);
2676 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2678 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2679 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2681 wxSize client_size
= GetClientSize();
2682 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2683 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2684 if (intersect_rect
.width
> 0)
2685 Refresh( true, &intersect_rect
);
2688 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2692 unsigned int tmp
= to
;
2697 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2698 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2700 wxSize client_size
= GetClientSize();
2701 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2702 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2703 if (intersect_rect
.width
> 0)
2704 Refresh( true, &intersect_rect
);
2707 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2709 unsigned int count
= GetRowCount();
2710 if (firstRow
> count
)
2713 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2714 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2716 wxSize client_size
= GetClientSize();
2717 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2718 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2719 if (intersect_rect
.width
> 0)
2720 Refresh( true, &intersect_rect
);
2723 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2725 wxCHECK_RET( newCurrent
< GetRowCount(),
2726 _T("invalid item index in OnArrowChar()") );
2728 // if there is no selection, we cannot move it anywhere
2729 if (!HasCurrentRow())
2732 unsigned int oldCurrent
= m_currentRow
;
2734 // in single selection we just ignore Shift as we can't select several
2736 if ( event
.ShiftDown() && !IsSingleSel() )
2738 RefreshRow( oldCurrent
);
2740 ChangeCurrentRow( newCurrent
);
2742 // select all the items between the old and the new one
2743 if ( oldCurrent
> newCurrent
)
2745 newCurrent
= oldCurrent
;
2746 oldCurrent
= m_currentRow
;
2749 SelectRows( oldCurrent
, newCurrent
, true );
2750 if (oldCurrent
!=newCurrent
)
2751 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2755 RefreshRow( oldCurrent
);
2757 // all previously selected items are unselected unless ctrl is held
2758 if ( !event
.ControlDown() )
2759 SelectAllRows(false);
2761 ChangeCurrentRow( newCurrent
);
2763 if ( !event
.ControlDown() )
2765 SelectRow( m_currentRow
, true );
2766 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2769 RefreshRow( m_currentRow
);
2772 //EnsureVisible( m_currentRow );
2775 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2779 rect
.y
= m_lineHeight
* row
;
2780 rect
.width
= GetEndOfLastCol();
2781 rect
.height
= m_lineHeight
;
2786 class RowToItemJob
: public DoJob
2789 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2790 virtual ~RowToItemJob(){};
2792 virtual int operator() ( wxDataViewTreeNode
* node
)
2795 if( current
== static_cast<int>(row
))
2797 ret
= node
->GetItem() ;
2801 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2803 current
+= node
->GetSubTreeCount();
2808 //If the current has no child node, we can find the desired item of the row number directly.
2809 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2810 if( node
->GetNodes().GetCount() == 0)
2812 int index
= static_cast<int>(row
) - current
- 1;
2813 ret
= node
->GetChildren().Item( index
);
2820 virtual int operator() ( void * n
)
2823 if( current
== static_cast<int>(row
))
2825 ret
= wxDataViewItem( n
) ;
2830 wxDataViewItem
GetResult(){ return ret
; }
2837 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2839 RowToItemJob
job( row
, -2 );
2840 Walker( m_root
, job
);
2841 return job
.GetResult();
2844 class RowToTreeNodeJob
: public DoJob
2847 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2850 this->current
= current
;
2854 virtual ~RowToTreeNodeJob(){};
2856 virtual int operator() ( wxDataViewTreeNode
* node
)
2859 if( current
== static_cast<int>(row
))
2865 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2867 current
+= node
->GetSubTreeCount();
2873 //If the current has no child node, we can find the desired item of the row number directly.
2874 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2875 if( node
->GetNodes().GetCount() == 0)
2877 int index
= static_cast<int>(row
) - current
- 1;
2878 void * n
= node
->GetChildren().Item( index
);
2879 ret
= new wxDataViewTreeNode( parent
) ;
2880 ret
->SetItem( wxDataViewItem( n
));
2881 ret
->SetHasChildren(false);
2890 virtual int operator() ( void * n
)
2893 if( current
== static_cast<int>(row
))
2895 ret
= new wxDataViewTreeNode( parent
) ;
2896 ret
->SetItem( wxDataViewItem( n
));
2897 ret
->SetHasChildren(false);
2903 wxDataViewTreeNode
* GetResult(){ return ret
; }
2907 wxDataViewTreeNode
* ret
;
2908 wxDataViewTreeNode
* parent
;
2912 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2914 RowToTreeNodeJob
job( row
, -2, m_root
);
2915 Walker( m_root
, job
);
2916 return job
.GetResult();
2919 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2921 wxWindow
*parent
= GetParent();
2922 wxDataViewEvent
le(type
, parent
->GetId());
2924 le
.SetEventObject(parent
);
2925 le
.SetModel(GetOwner()->GetModel());
2928 parent
->GetEventHandler()->ProcessEvent(le
);
2932 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2934 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2937 if( node
->HasChildren())
2939 if( !node
->IsOpen())
2941 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2942 //Check if the user prevent expanding
2943 if( e
.GetSkipped() )
2947 //Here I build the children of current node
2948 if( node
->GetChildrenNumber() == 0 )
2951 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2955 //Send the expanded event
2956 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2960 SelectRow( row
, false );
2961 SelectRow( row
+ 1, true );
2962 ChangeCurrentRow( row
+ 1 );
2963 SendSelectionChangedEvent( GetItemByRow(row
+1));
2971 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2973 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2976 wxDataViewTreeNode
* nd
= node
;
2978 if( node
->HasChildren() && node
->IsOpen() )
2980 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2981 if( e
.GetSkipped() )
2986 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2990 node
= node
->GetParent();
2993 int parent
= GetRowByItem( node
->GetItem() ) ;
2996 SelectRow( row
, false);
2997 SelectRow(parent
, true );
2998 ChangeCurrentRow( parent
);
2999 SendSelectionChangedEvent( node
->GetItem() );
3003 if( !nd
->HasChildren())
3008 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3010 wxDataViewModel
* model
= GetOwner()->GetModel();
3014 //Compose the a parent-chain of the finding item
3016 list
.DeleteContents( true );
3017 wxDataViewItem
it( item
);
3020 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3021 list
.Insert( pItem
);
3022 it
= model
->GetParent( it
);
3025 //Find the item along the parent-chain.
3026 //This algorithm is designed to speed up the node-finding method
3027 wxDataViewTreeNode
* node
= m_root
;
3028 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3030 if( node
->HasChildren() )
3032 if( node
->GetChildrenNumber() == 0 )
3035 BuildTreeHelper(model
, node
->GetItem(), node
);
3038 wxDataViewTreeNodes nodes
= node
->GetNodes();
3040 for (; i
< nodes
.GetCount(); i
++)
3042 if (nodes
[i
]->GetItem() == (**iter
))
3048 if (i
== nodes
.GetCount())
3057 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3059 wxDataViewColumn
*col
= NULL
;
3060 unsigned int cols
= GetOwner()->GetColumnCount();
3061 unsigned int colnum
= 0;
3062 unsigned int x_start
= 0;
3064 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3065 for (x_start
= 0; colnum
< cols
; colnum
++)
3067 col
= GetOwner()->GetColumn(colnum
);
3068 if (col
->IsHidden())
3069 continue; // skip it!
3071 unsigned int w
= col
->GetWidth();
3072 if (x_start
+w
>= (unsigned int)x
)
3079 item
= GetItemByRow( y
/m_lineHeight
);
3082 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3084 int row
= GetRowByItem(item
);
3085 int y
= row
*m_lineHeight
;
3086 int h
= m_lineHeight
;
3088 wxDataViewColumn
*col
= NULL
;
3089 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3091 col
= GetOwner()->GetColumn( i
);
3092 x
+= col
->GetWidth();
3093 if( GetOwner()->GetColumn(i
+1) == column
)
3096 int w
= col
->GetWidth();
3097 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3098 return wxRect(x
, y
, w
, h
);
3101 int wxDataViewMainWindow::RecalculateCount()
3103 return m_root
->GetSubTreeCount();
3106 class ItemToRowJob
: public DoJob
3109 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3110 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3111 virtual ~ItemToRowJob(){};
3113 //Maybe binary search will help to speed up this process
3114 virtual int operator() ( wxDataViewTreeNode
* node
)
3117 if( node
->GetItem() == item
)
3122 if( node
->GetItem() == **m_iter
)
3129 ret
+= node
->GetSubTreeCount();
3135 virtual int operator() ( void * n
)
3138 if( n
== item
.GetID() )
3142 //the row number is begin from zero
3143 int GetResult(){ return ret
-1 ; }
3145 ItemList::const_iterator m_iter
;
3146 wxDataViewItem item
;
3151 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3153 wxDataViewModel
* model
= GetOwner()->GetModel();
3160 //Compose the a parent-chain of the finding item
3162 wxDataViewItem
* pItem
= NULL
;
3163 list
.DeleteContents( true );
3164 wxDataViewItem
it( item
);
3167 pItem
= new wxDataViewItem( it
);
3168 list
.Insert( pItem
);
3169 it
= model
->GetParent( it
);
3171 pItem
= new wxDataViewItem( );
3172 list
.Insert( pItem
);
3174 ItemToRowJob
job( item
, list
.begin() );
3175 Walker(m_root
, job
);
3176 return job
.GetResult();
3179 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3181 if( !model
->IsContainer( item
) )
3184 wxDataViewItemArray children
;
3185 unsigned int num
= model
->GetChildren( item
, children
);
3187 while( index
< num
)
3189 if( model
->IsContainer( children
[index
] ) )
3191 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3192 n
->SetItem(children
[index
]);
3193 n
->SetHasChildren( true ) ;
3198 node
->AddLeaf( children
[index
].GetID() );
3202 node
->SetSubTreeCount( num
);
3203 wxDataViewTreeNode
* n
= node
->GetParent();
3205 n
->ChangeSubTreeCount(num
);
3209 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3211 //First we define a invalid item to fetch the top-level elements
3212 wxDataViewItem item
;
3214 BuildTreeHelper( model
, item
, m_root
);
3218 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3220 if( node
->GetNodeNumber() != 0 )
3222 int len
= node
->GetNodeNumber();
3224 wxDataViewTreeNodes nodes
= node
->GetNodes();
3225 for( ; i
< len
; i
++ )
3227 DestroyTreeHelper(nodes
[i
]);
3233 void wxDataViewMainWindow::DestroyTree()
3235 DestroyTreeHelper(m_root
);
3236 m_root
->SetSubTreeCount(0);
3240 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3242 if (event
.GetKeyCode() == WXK_TAB
)
3244 wxNavigationKeyEvent nevent
;
3245 nevent
.SetWindowChange( event
.ControlDown() );
3246 nevent
.SetDirection( !event
.ShiftDown() );
3247 nevent
.SetEventObject( GetParent()->GetParent() );
3248 nevent
.SetCurrentFocus( m_parent
);
3249 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3253 // no item -> nothing to do
3254 if (!HasCurrentRow())
3260 // don't use m_linesPerPage directly as it might not be computed yet
3261 const int pageSize
= GetCountPerPage();
3262 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3264 switch ( event
.GetKeyCode() )
3267 if ( m_currentRow
> 0 )
3268 OnArrowChar( m_currentRow
- 1, event
);
3272 if ( m_currentRow
< GetRowCount() - 1 )
3273 OnArrowChar( m_currentRow
+ 1, event
);
3275 //Add the process for tree expanding/collapsing
3277 OnCollapsing(m_currentRow
);
3280 OnExpanding( m_currentRow
);
3284 OnArrowChar( GetRowCount() - 1, event
);
3289 OnArrowChar( 0, event
);
3294 int steps
= pageSize
- 1;
3295 int index
= m_currentRow
- steps
;
3299 OnArrowChar( index
, event
);
3305 int steps
= pageSize
- 1;
3306 unsigned int index
= m_currentRow
+ steps
;
3307 unsigned int count
= GetRowCount();
3308 if ( index
>= count
)
3311 OnArrowChar( index
, event
);
3320 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3322 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3324 // let the base handle mouse wheel events.
3329 int x
= event
.GetX();
3330 int y
= event
.GetY();
3331 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3332 wxDataViewColumn
*col
= NULL
;
3335 unsigned int cols
= GetOwner()->GetColumnCount();
3337 for (i
= 0; i
< cols
; i
++)
3339 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3341 continue; // skip it!
3343 if (x
< xpos
+ c
->GetWidth())
3348 xpos
+= c
->GetWidth();
3353 wxDataViewRenderer
*cell
= col
->GetRenderer();
3354 unsigned int current
= y
/ m_lineHeight
;
3355 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3357 // Unselect all if below the last row ?
3361 //Test whether the mouse is hovered on the tree item button
3363 if (GetOwner()->GetExpanderColumn() == col
)
3365 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3366 if( node
!=NULL
&& node
->HasChildren() )
3368 int indent
= node
->GetIndentLevel();
3369 indent
= GetOwner()->GetIndent()*indent
;
3370 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3371 if( rect
.Contains( x
, y
) )
3373 //So the mouse is over the expander
3375 if (m_underMouse
&& m_underMouse
!= node
)
3377 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3378 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3380 if (m_underMouse
!= node
)
3382 //wxLogMessage("Do the row: %d", current);
3385 m_underMouse
= node
;
3388 if (node
!=NULL
&& !node
->HasChildren())
3393 if (m_underMouse
!= NULL
)
3395 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3396 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3397 m_underMouse
= NULL
;
3401 wxDataViewModel
*model
= GetOwner()->GetModel();
3403 if (event
.Dragging())
3405 if (m_dragCount
== 0)
3407 // we have to report the raw, physical coords as we want to be
3408 // able to call HitTest(event.m_pointDrag) from the user code to
3409 // get the item being dragged
3410 m_dragStart
= event
.GetPosition();
3415 if (m_dragCount
!= 3)
3418 if (event
.LeftIsDown())
3420 // Notify cell about drag
3429 bool forceClick
= false;
3431 if (event
.ButtonDClick())
3433 m_renameTimer
->Stop();
3434 m_lastOnSame
= false;
3437 if (event
.LeftDClick())
3439 if ( current
== m_lineLastClicked
)
3441 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3443 wxDataViewItem item
= GetItemByRow(current
);
3445 model
->GetValue( value
, item
, col
->GetModelColumn() );
3446 cell
->SetValue( value
);
3447 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3448 col
->GetWidth(), m_lineHeight
);
3449 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3451 wxWindow
*parent
= GetParent();
3452 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3454 le
.SetEventObject(parent
);
3455 le
.SetColumn(col
->GetModelColumn());
3456 le
.SetDataViewColumn(col
);
3457 le
.SetModel(GetOwner()->GetModel());
3459 parent
->GetEventHandler()->ProcessEvent(le
);
3465 // The first click was on another item, so don't interpret this as
3466 // a double click, but as a simple click instead
3473 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3475 // select single line
3476 SelectAllRows( false );
3477 SelectRow( m_lineSelectSingleOnUp
, true );
3480 //Process the event of user clicking the expander
3481 bool expander
= false;
3482 if (GetOwner()->GetExpanderColumn() == col
)
3484 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3485 if( node
!=NULL
&& node
->HasChildren() )
3487 int indent
= node
->GetIndentLevel();
3488 indent
= GetOwner()->GetIndent()*indent
;
3489 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3490 if( rect
.Contains( x
, y
) )
3493 if( node
->IsOpen() )
3494 OnCollapsing(current
);
3496 OnExpanding( current
);
3500 //If the user click the expander, we do not do editing even if the column with expander are editable
3501 if (m_lastOnSame
&& !expander
)
3503 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3504 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3506 m_renameTimer
->Start( 100, true );
3510 m_lastOnSame
= false;
3511 m_lineSelectSingleOnUp
= (unsigned int)-1;
3515 // This is necessary, because after a DnD operation in
3516 // from and to ourself, the up event is swallowed by the
3517 // DnD code. So on next non-up event (which means here and
3518 // now) m_lineSelectSingleOnUp should be reset.
3519 m_lineSelectSingleOnUp
= (unsigned int)-1;
3522 if (event
.RightDown())
3524 m_lineBeforeLastClicked
= m_lineLastClicked
;
3525 m_lineLastClicked
= current
;
3527 // If the item is already selected, do not update the selection.
3528 // Multi-selections should not be cleared if a selected item is clicked.
3529 if (!IsRowSelected(current
))
3531 SelectAllRows(false);
3532 ChangeCurrentRow(current
);
3533 SelectRow(m_currentRow
,true);
3534 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3537 // notify cell about right click
3540 // Allow generation of context menu event
3543 else if (event
.MiddleDown())
3545 // notify cell about middle click
3548 if (event
.LeftDown() || forceClick
)
3552 m_lineBeforeLastClicked
= m_lineLastClicked
;
3553 m_lineLastClicked
= current
;
3555 unsigned int oldCurrentRow
= m_currentRow
;
3556 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3558 bool cmdModifierDown
= event
.CmdDown();
3559 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3561 if ( IsSingleSel() || !IsRowSelected(current
) )
3563 SelectAllRows( false );
3564 ChangeCurrentRow(current
);
3565 SelectRow(m_currentRow
,true);
3566 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3568 else // multi sel & current is highlighted & no mod keys
3570 m_lineSelectSingleOnUp
= current
;
3571 ChangeCurrentRow(current
); // change focus
3574 else // multi sel & either ctrl or shift is down
3576 if (cmdModifierDown
)
3578 ChangeCurrentRow(current
);
3579 ReverseRowSelection(m_currentRow
);
3580 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3582 else if (event
.ShiftDown())
3584 ChangeCurrentRow(current
);
3586 unsigned int lineFrom
= oldCurrentRow
,
3589 if ( lineTo
< lineFrom
)
3592 lineFrom
= m_currentRow
;
3595 SelectRows(lineFrom
, lineTo
, true);
3596 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3598 else // !ctrl, !shift
3600 // test in the enclosing if should make it impossible
3601 wxFAIL_MSG( _T("how did we get here?") );
3605 if (m_currentRow
!= oldCurrentRow
)
3606 RefreshRow( oldCurrentRow
);
3608 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3610 // Update selection here...
3613 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3614 (current
== oldCurrentRow
)) && oldWasSelected
;
3618 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3622 if (HasCurrentRow())
3628 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3632 if (HasCurrentRow())
3638 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3640 if( m_selection
.GetCount() != 1 )
3641 return wxDataViewItem();
3643 return GetItemByRow( m_selection
.Item(0));
3646 //-----------------------------------------------------------------------------
3648 //-----------------------------------------------------------------------------
3649 WX_DEFINE_LIST(wxDataViewColumnList
);
3651 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3653 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3654 EVT_SIZE(wxDataViewCtrl::OnSize
)
3657 wxDataViewCtrl::~wxDataViewCtrl()
3660 GetModel()->RemoveNotifier( m_notifier
);
3663 void wxDataViewCtrl::Init()
3668 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3669 const wxPoint
& pos
, const wxSize
& size
,
3670 long style
, const wxValidator
& validator
)
3672 if (!wxControl::Create( parent
, id
, pos
, size
,
3673 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3679 MacSetClipChildren( true ) ;
3682 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3684 if (HasFlag(wxDV_NO_HEADER
))
3685 m_headerArea
= NULL
;
3687 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3689 SetTargetWindow( m_clientArea
);
3691 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3693 sizer
->Add( m_headerArea
, 0, wxGROW
);
3694 sizer
->Add( m_clientArea
, 1, wxGROW
);
3701 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3705 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3708 // we need to process arrows ourselves for scrolling
3709 if ( nMsg
== WM_GETDLGCODE
)
3711 rc
|= DLGC_WANTARROWS
;
3719 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3721 // We need to override OnSize so that our scrolled
3722 // window a) does call Layout() to use sizers for
3723 // positioning the controls but b) does not query
3724 // the sizer for their size and use that for setting
3725 // the scrollable area as set that ourselves by
3726 // calling SetScrollbar() further down.
3733 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3735 if (!wxDataViewCtrlBase::AssociateModel( model
))
3738 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3740 model
->AddNotifier( m_notifier
);
3742 m_clientArea
->BuildTree(model
);
3744 m_clientArea
->UpdateDisplay();
3749 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3751 if (!wxDataViewCtrlBase::AppendColumn(col
))
3754 m_cols
.Append( col
);
3759 void wxDataViewCtrl::OnColumnChange()
3762 m_headerArea
->UpdateDisplay();
3764 m_clientArea
->UpdateDisplay();
3767 void wxDataViewCtrl::DoSetExpanderColumn()
3769 m_clientArea
->UpdateDisplay();
3772 void wxDataViewCtrl::DoSetIndent()
3774 m_clientArea
->UpdateDisplay();
3777 unsigned int wxDataViewCtrl::GetColumnCount() const
3779 return m_cols
.GetCount();
3782 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3784 wxDataViewColumnList::const_iterator iter
;
3786 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3791 if ((*iter
)->IsHidden())
3798 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3800 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3811 bool wxDataViewCtrl::ClearColumns()
3818 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3820 int ret
= 0, dead
= 0;
3821 int len
= GetColumnCount();
3822 for (int i
=0; i
<len
; i
++)
3824 wxDataViewColumn
* col
= GetColumn(i
);
3825 if (col
->IsHidden())
3827 ret
+= col
->GetWidth();
3830 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3837 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3842 //Selection code with wxDataViewItem as parameters
3843 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3845 return m_clientArea
->GetSelection();
3848 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3851 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3852 int len
= selection
.GetCount();
3853 for( int i
= 0; i
< len
; i
++)
3855 unsigned int row
= selection
[i
];
3856 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3861 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3863 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3864 int len
= sel
.GetCount();
3865 for( int i
= 0; i
< len
; i
++ )
3867 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3869 selection
.Add( static_cast<unsigned int>(row
) );
3871 m_clientArea
->SetSelections( selection
);
3874 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3876 int row
= m_clientArea
->GetRowByItem( item
);
3879 //Unselect all rows before select another in the single select mode
3880 if (m_clientArea
->IsSingleSel())
3881 m_clientArea
->SelectAllRows(false);
3882 m_clientArea
->SelectRow(row
, true);
3886 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3888 int row
= m_clientArea
->GetRowByItem( item
);
3890 m_clientArea
->SelectRow(row
, false);
3893 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3895 int row
= m_clientArea
->GetRowByItem( item
);
3898 return m_clientArea
->IsRowSelected(row
);
3903 //Selection code with row number as parameter
3904 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3907 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3908 int len
= selection
.GetCount();
3909 for( int i
= 0; i
< len
; i
++)
3911 unsigned int row
= selection
[i
];
3917 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3919 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3920 int len
= sel
.GetCount();
3921 for( int i
= 0; i
< len
; i
++ )
3925 selection
.Add( static_cast<unsigned int>(row
) );
3927 m_clientArea
->SetSelections( selection
);
3930 void wxDataViewCtrl::Select( int row
)
3934 if (m_clientArea
->IsSingleSel())
3935 m_clientArea
->SelectAllRows(false);
3936 m_clientArea
->SelectRow( row
, true );
3940 void wxDataViewCtrl::Unselect( int row
)
3943 m_clientArea
->SelectRow(row
, false);
3946 bool wxDataViewCtrl::IsSelected( int row
) const
3949 return m_clientArea
->IsRowSelected(row
);
3953 void wxDataViewCtrl::SelectRange( int from
, int to
)
3956 for( int i
= from
; i
< to
; i
++ )
3958 m_clientArea
->Select(sel
);
3961 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3963 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3964 for( int i
= from
; i
< to
; i
++ )
3965 if( sel
.Index( i
) != wxNOT_FOUND
)
3967 m_clientArea
->SetSelections(sel
);
3970 void wxDataViewCtrl::SelectAll()
3972 m_clientArea
->SelectAllRows(true);
3975 void wxDataViewCtrl::UnselectAll()
3977 m_clientArea
->SelectAllRows(false);
3980 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3984 if( row
> m_clientArea
->GetRowCount() )
3985 row
= m_clientArea
->GetRowCount();
3987 int first
= m_clientArea
->GetFirstVisibleRow();
3988 int last
= m_clientArea
->GetLastVisibleRow();
3990 m_clientArea
->ScrollTo( row
, column
);
3991 else if( row
> last
)
3992 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3994 m_clientArea
->ScrollTo( first
, column
);
3997 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3999 int row
= m_clientArea
->GetRowByItem(item
);
4002 if( column
== NULL
)
4003 EnsureVisible(row
, -1);
4007 int len
= GetColumnCount();
4008 for( int i
= 0; i
< len
; i
++ )
4009 if( GetColumn(i
) == column
)
4014 EnsureVisible( row
, col
);
4020 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4022 m_clientArea
->HitTest(point
, item
, column
);
4025 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4027 return m_clientArea
->GetItemRect(item
, column
);
4030 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4032 return m_clientArea
->GetItemByRow( row
);
4035 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4037 return m_clientArea
->GetRowByItem( item
);
4040 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4042 int row
= m_clientArea
->GetRowByItem( item
);
4044 m_clientArea
->Expand(row
);
4047 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4049 int row
= m_clientArea
->GetRowByItem( item
);
4051 m_clientArea
->Collapse(row
);
4055 // !wxUSE_GENERICDATAVIEWCTRL
4058 // wxUSE_DATAVIEWCTRL