1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 #define USE_NATIVE_HEADER_WINDOW 1
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called Refresh afterwards
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
147 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
149 wxSize
DoGetBestSize() const;
151 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
153 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
154 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
159 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
162 #else // !defined(__WXMSW__)
164 #define HEADER_WINDOW_HEIGHT 25
165 #define HEADER_HORIZ_BORDER 5
166 #define HEADER_VERT_BORDER 3
167 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
169 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
172 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
174 const wxPoint
&pos
= wxDefaultPosition
,
175 const wxSize
&size
= wxDefaultSize
,
176 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
179 Create(parent
, id
, pos
, size
, name
);
182 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
183 const wxPoint
&pos
, const wxSize
&size
,
184 const wxString
&name
);
186 ~wxGenericDataViewHeaderWindow()
188 delete m_resizeCursor
;
191 virtual void UpdateDisplay() { Refresh(); }
195 void OnPaint( wxPaintEvent
&event
);
196 void OnMouse( wxMouseEvent
&event
);
197 void OnSetFocus( wxFocusEvent
&event
);
202 // vars used for column resizing:
204 wxCursor
*m_resizeCursor
;
205 const wxCursor
*m_currentCursor
;
208 bool m_dirty
; // needs refresh?
209 int m_hover
; // index of the column under the mouse
210 int m_column
; // index of the column being resized
211 int m_currentX
; // divider line position in logical (unscrolled) coords
212 int m_minX
; // minimal position beyond which the divider line
213 // can't be dragged in logical coords
215 // the pen used to draw the current column width drag line
216 // when resizing the columsn
220 // internal utilities:
224 m_currentCursor
= (wxCursor
*) NULL
;
225 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
227 m_isDragging
= false;
230 m_hover
= wxNOT_FOUND
;
231 m_column
= wxNOT_FOUND
;
235 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
236 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
239 void AdjustDC(wxDC
& dc
);
242 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
243 DECLARE_EVENT_TABLE()
246 #endif // defined(__WXMSW__)
248 //-----------------------------------------------------------------------------
249 // wxDataViewRenameTimer
250 //-----------------------------------------------------------------------------
252 class wxDataViewRenameTimer
: public wxTimer
255 wxDataViewMainWindow
*m_owner
;
258 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
262 //-----------------------------------------------------------------------------
263 // wxDataViewTreeNode
264 //-----------------------------------------------------------------------------
265 class wxDataViewTreeNode
;
266 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
267 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
269 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
270 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
272 class wxDataViewTreeNode
275 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
282 m_hasChildren
= false;
286 ~wxDataViewTreeNode()
290 wxDataViewTreeNode
* GetParent() { return m_parent
; }
291 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
292 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
293 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
295 void AddNode( wxDataViewTreeNode
* node
)
297 m_leaves
.Add( node
->GetItem().GetID() );
299 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
302 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
304 void AddLeaf( void * leaf
)
306 m_leaves
.Add( leaf
);
308 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
311 wxDataViewItem
& GetItem() { return m_item
; }
312 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
314 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
315 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
319 wxDataViewTreeNode
* node
= this;
320 while( node
->GetParent()->GetParent() != NULL
)
322 node
= node
->GetParent();
335 int len
= m_nodes
.GetCount();
337 for ( int i
= 0 ;i
< len
; i
++)
338 sum
+= m_nodes
[i
]->GetSubTreeCount();
340 sum
+= m_leaves
.GetCount();
343 ChangeSubTreeCount(-sum
);
349 ChangeSubTreeCount(sum
);
352 bool HasChildren() { return m_hasChildren
; }
353 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
355 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
356 int GetSubTreeCount() { return m_subTreeCount
; }
357 void ChangeSubTreeCount( int num
)
361 m_subTreeCount
+= num
;
363 m_parent
->ChangeSubTreeCount(num
);
370 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
371 int len
= m_nodes
.GetCount();
372 for (int i
= 0; i
< len
; i
++)
373 m_nodes
[i
]->Resort();
374 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
379 wxDataViewTreeNode
*m_parent
;
380 wxDataViewTreeNodes m_nodes
;
381 wxDataViewTreeLeaves m_leaves
;
382 wxDataViewItem m_item
;
388 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
390 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
393 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
395 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
400 //-----------------------------------------------------------------------------
401 // wxDataViewMainWindow
402 //-----------------------------------------------------------------------------
404 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
406 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
407 WX_DEFINE_LIST(ItemList
);
409 class wxDataViewMainWindow
: public wxWindow
412 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
414 const wxPoint
&pos
= wxDefaultPosition
,
415 const wxSize
&size
= wxDefaultSize
,
416 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
417 virtual ~wxDataViewMainWindow();
419 // notifications from wxDataViewModel
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 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 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
645 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
646 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
647 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
648 view
->GetForegroundColour();
649 dc
->SetTextForeground(col
);
650 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
653 // ---------------------------------------------------------
654 // wxDataViewTextRenderer
655 // ---------------------------------------------------------
657 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
659 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
660 wxDataViewCellMode mode
, int align
) :
661 wxDataViewCustomRenderer( varianttype
, mode
, align
)
665 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
667 m_text
= value
.GetString();
672 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
677 bool wxDataViewTextRenderer::HasEditorCtrl()
682 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
683 wxRect labelRect
, const wxVariant
&value
)
685 return new wxTextCtrl( parent
, wxID_ANY
, value
,
686 wxPoint(labelRect
.x
,labelRect
.y
),
687 wxSize(labelRect
.width
,labelRect
.height
) );
690 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
692 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
693 value
= text
->GetValue();
697 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
699 RenderText( m_text
, 0, cell
, dc
, state
);
703 wxSize
wxDataViewTextRenderer::GetSize() const
705 const wxDataViewCtrl
*view
= GetView();
709 view
->GetTextExtent( m_text
, &x
, &y
);
710 return wxSize( x
, y
);
712 return wxSize(80,20);
715 // ---------------------------------------------------------
716 // wxDataViewBitmapRenderer
717 // ---------------------------------------------------------
719 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
721 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
722 wxDataViewCellMode mode
, int align
) :
723 wxDataViewCustomRenderer( varianttype
, mode
, align
)
727 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
729 if (value
.GetType() == wxT("wxBitmap"))
731 if (value
.GetType() == wxT("wxIcon"))
737 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
742 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
745 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
746 else if (m_icon
.Ok())
747 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
752 wxSize
wxDataViewBitmapRenderer::GetSize() const
755 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
756 else if (m_icon
.Ok())
757 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
759 return wxSize(16,16);
762 // ---------------------------------------------------------
763 // wxDataViewToggleRenderer
764 // ---------------------------------------------------------
766 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
768 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
769 wxDataViewCellMode mode
, int align
) :
770 wxDataViewCustomRenderer( varianttype
, mode
, align
)
775 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
777 m_toggle
= value
.GetBool();
782 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
787 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
789 // User wxRenderer here
792 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
794 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
799 flags
|= wxCONTROL_CHECKED
;
800 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
801 flags
|= wxCONTROL_DISABLED
;
803 wxRendererNative::Get().DrawCheckBox(
804 GetOwner()->GetOwner(),
812 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
813 wxDataViewModel
*model
,
814 const wxDataViewItem
& item
, unsigned int col
)
816 bool value
= !m_toggle
;
817 wxVariant variant
= value
;
818 model
->SetValue( variant
, item
, col
);
819 model
->ValueChanged( item
, col
);
823 wxSize
wxDataViewToggleRenderer::GetSize() const
825 return wxSize(20,20);
828 // ---------------------------------------------------------
829 // wxDataViewProgressRenderer
830 // ---------------------------------------------------------
832 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
834 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
835 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
836 wxDataViewCustomRenderer( varianttype
, mode
, align
)
842 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
846 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
848 m_value
= (long) value
;
850 if (m_value
< 0) m_value
= 0;
851 if (m_value
> 100) m_value
= 100;
856 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
858 value
= (long) m_value
;
862 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
864 double pct
= (double)m_value
/ 100.0;
866 bar
.width
= (int)(cell
.width
* pct
);
867 dc
->SetPen( *wxTRANSPARENT_PEN
);
868 dc
->SetBrush( *wxBLUE_BRUSH
);
869 dc
->DrawRectangle( bar
);
871 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
872 dc
->SetPen( *wxBLACK_PEN
);
873 dc
->DrawRectangle( cell
);
878 wxSize
wxDataViewProgressRenderer::GetSize() const
880 return wxSize(40,12);
883 // ---------------------------------------------------------
884 // wxDataViewDateRenderer
885 // ---------------------------------------------------------
887 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
889 #if wxUSE_DATE_RENDERER_POPUP
891 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
894 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
895 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
896 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
901 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
902 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
903 sizer
->Add( m_cal
, 1, wxGROW
);
908 void OnCalendar( wxCalendarEvent
&event
);
910 wxCalendarCtrl
*m_cal
;
911 wxDataViewModel
*m_model
;
913 const wxDataViewItem
& m_item
;
916 virtual void OnDismiss()
921 DECLARE_EVENT_TABLE()
924 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
925 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
928 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
930 wxDateTime date
= event
.GetDate();
931 wxVariant value
= date
;
932 m_model
->SetValue( value
, m_item
, m_col
);
933 m_model
->ValueChanged( m_item
, m_col
);
937 #endif // wxUSE_DATE_RENDERER_POPUP
939 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
941 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
942 wxDataViewCellMode mode
, int align
) :
943 wxDataViewCustomRenderer( varianttype
, mode
, align
)
947 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
949 m_date
= value
.GetDateTime();
954 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
960 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
962 wxString tmp
= m_date
.FormatDate();
963 RenderText( tmp
, 0, cell
, dc
, state
);
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
& WXUNUSED(value
) ) const
1025 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1028 const wxIcon
&icon
= m_value
.GetIcon();
1031 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1032 xoffset
= icon
.GetWidth()+4;
1035 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1040 wxSize
wxDataViewIconTextRenderer::GetSize() const
1042 const wxDataViewCtrl
*view
= GetView();
1043 if (!m_value
.GetText().empty())
1046 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1048 if (m_value
.GetIcon().IsOk())
1049 x
+= m_value
.GetIcon().GetWidth() + 4;
1050 return wxSize( x
, y
);
1052 return wxSize(80,20);
1056 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1057 wxRect
WXUNUSED(labelRect
),
1058 const wxVariant
& WXUNUSED(value
))
1064 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1065 wxVariant
& WXUNUSED(value
))
1070 // ---------------------------------------------------------
1072 // ---------------------------------------------------------
1074 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1076 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1077 unsigned int model_column
,
1078 int width
, wxAlignment align
, int flags
) :
1079 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1081 SetAlignment(align
);
1085 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1088 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1089 unsigned int model_column
,
1090 int width
, wxAlignment align
, int flags
) :
1091 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1093 SetAlignment(align
);
1096 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1099 wxDataViewColumn::~wxDataViewColumn()
1103 void wxDataViewColumn::Init( int width
)
1106 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1110 void wxDataViewColumn::SetResizeable( bool resizeable
)
1113 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1115 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1118 void wxDataViewColumn::SetHidden( bool hidden
)
1121 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1123 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1125 // tell our owner to e.g. update its scrollbars:
1127 GetOwner()->OnColumnChange();
1130 void wxDataViewColumn::SetSortable( bool sortable
)
1133 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1135 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1137 // Update header button
1139 GetOwner()->OnColumnChange();
1142 void wxDataViewColumn::SetSortOrder( bool ascending
)
1144 m_ascending
= ascending
;
1146 // Update header button
1148 GetOwner()->OnColumnChange();
1151 bool wxDataViewColumn::IsSortOrderAscending() const
1156 void wxDataViewColumn::SetInternalWidth( int width
)
1160 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1161 if (m_owner
&& m_owner
->m_clientArea
)
1162 m_owner
->m_clientArea
->RecalculateDisplay();
1165 void wxDataViewColumn::SetWidth( int width
)
1167 m_owner
->m_headerArea
->UpdateDisplay();
1169 SetInternalWidth(width
);
1173 //-----------------------------------------------------------------------------
1174 // wxDataViewHeaderWindowBase
1175 //-----------------------------------------------------------------------------
1177 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1179 wxWindow
*parent
= GetParent();
1180 wxDataViewEvent
le(type
, parent
->GetId());
1182 le
.SetEventObject(parent
);
1184 le
.SetDataViewColumn(GetColumn(n
));
1185 le
.SetModel(GetOwner()->GetModel());
1187 // for events created by wxDataViewHeaderWindow the
1188 // row / value fields are not valid
1190 parent
->GetEventHandler()->ProcessEvent(le
);
1193 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1195 // implemented in msw/listctrl.cpp:
1196 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1198 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1200 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1201 const wxPoint
&pos
, const wxSize
&size
,
1202 const wxString
&name
)
1206 m_scrollOffsetX
= 0;;
1208 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1209 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1210 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1211 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1213 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1216 // create the native WC_HEADER window:
1217 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1218 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1221 msStyle
|= WS_VISIBLE
;
1223 m_hWnd
= CreateWindowEx(0,
1234 wxLogLastError(_T("CreateWindowEx"));
1238 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1239 // to call wxDataViewHeaderWindow::MSWOnNotify
1240 SubclassWin(m_hWnd
);
1242 // the following is required to get the default win's font for
1243 // header windows and must be done befor sending the HDM_LAYOUT msg
1249 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1254 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1256 return wxSize(80, 22);
1260 #define HDF_SORTUP 0x0400
1262 #ifndef HDF_SORTDOWN
1263 #define HDF_SORTDOWN 0x0200
1266 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1268 // remove old columns
1269 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1270 Header_DeleteItem((HWND
)m_hWnd
, 0);
1272 // add the updated array of columns to the header control
1273 unsigned int cols
= GetOwner()->GetColumnCount();
1274 unsigned int added
= 0;
1275 for (unsigned int i
= 0; i
< cols
; i
++)
1277 wxDataViewColumn
*col
= GetColumn( i
);
1278 if (col
->IsHidden())
1279 continue; // don't add it!
1282 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1283 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1284 hdi
.cxy
= col
->GetWidth();
1285 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1286 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1287 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1289 if (col
->IsSortable() && GetOwner()->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 // VZ: works with 5.81
1294 hdi
.fmt
|= col
->IsSortOrderAscending() ? 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 dx
, int dy
, const wxRect
*rect
)
1501 m_scrollOffsetX
+= dx
;
1503 GetParent()->Layout();
1506 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1510 wxControl::DoSetSize( x
+m_scrollOffsetX
, y
, w
-m_scrollOffsetX
, h
, f
);
1513 #else // !defined(__WXMSW__)
1515 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1516 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1517 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1518 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1519 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1522 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1523 const wxPoint
&pos
, const wxSize
&size
,
1524 const wxString
&name
)
1528 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1531 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1532 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1533 SetOwnForegroundColour( attr
.colFg
);
1534 SetOwnBackgroundColour( attr
.colBg
);
1536 SetOwnFont( attr
.font
);
1538 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1539 // a wxBoxSizer and in order to avoid super-big header windows,
1540 // we need to set our height as fixed
1541 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1542 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1547 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1550 GetClientSize( &w
, &h
);
1552 wxAutoBufferedPaintDC
dc( this );
1554 dc
.SetBackground(GetBackgroundColour());
1558 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1561 m_owner
->GetViewStart( &x
, NULL
);
1563 // account for the horz scrollbar offset
1564 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1566 dc
.SetFont( GetFont() );
1568 unsigned int cols
= GetOwner()->GetColumnCount();
1571 for (i
= 0; i
< cols
; i
++)
1573 wxDataViewColumn
*col
= GetColumn( i
);
1574 if (col
->IsHidden())
1575 continue; // skip it!
1577 int cw
= col
->GetWidth();
1580 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1581 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1583 if (col
->IsSortOrderAscending())
1584 sortArrow
= wxHDR_SORT_ICON_UP
;
1586 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1590 if (m_parent
->IsEnabled())
1592 if ((int) i
== m_hover
)
1593 state
= wxCONTROL_CURRENT
;
1597 state
= (int) wxCONTROL_DISABLED
;
1600 wxRendererNative::Get().DrawHeaderButton
1604 wxRect(xpos
, 0, cw
, ch
-1),
1609 // align as required the column title:
1611 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1612 switch (col
->GetAlignment())
1615 x
+= HEADER_HORIZ_BORDER
;
1618 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1621 case wxALIGN_CENTER
:
1622 case wxALIGN_CENTER_HORIZONTAL
:
1623 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1627 // always center the title vertically:
1628 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1630 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1632 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1633 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1634 dc
.DrawText( col
->GetTitle(), x
, y
);
1635 dc
.DestroyClippingRegion();
1641 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1643 GetParent()->SetFocus();
1647 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1649 // we want to work with logical coords
1651 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1652 int y
= event
.GetY();
1656 // we don't draw the line beyond our window,
1657 // but we allow dragging it there
1659 GetClientSize( &w
, NULL
);
1660 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1663 if (event
.ButtonUp())
1665 m_isDragging
= false;
1671 m_currentX
= wxMax(m_minX
+ 7, x
);
1675 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1677 GetOwner()->Refresh();
1681 else // not dragging
1684 m_column
= wxNOT_FOUND
;
1686 bool hit_border
= false;
1688 // end of the current column
1691 // find the column where this event occured
1692 int countCol
= m_owner
->GetColumnCount();
1693 for (int column
= 0; column
< countCol
; column
++)
1695 wxDataViewColumn
*p
= GetColumn(column
);
1698 continue; // skip if not shown
1700 xpos
+= p
->GetWidth();
1702 if ((abs(x
-xpos
) < 3) && (y
< 22))
1710 // inside the column
1717 int old_hover
= m_hover
;
1719 if (event
.Leaving())
1720 m_hover
= wxNOT_FOUND
;
1721 if (old_hover
!= m_hover
)
1724 if (m_column
== wxNOT_FOUND
)
1727 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1728 if (event
.LeftDClick() && resizeable
)
1730 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1733 else if (event
.LeftDown() || event
.RightUp())
1735 if (hit_border
&& event
.LeftDown() && resizeable
)
1737 m_isDragging
= true;
1741 else // click on a column
1743 wxDataViewModel
* model
= GetOwner()->GetModel();
1744 wxEventType evt
= event
.LeftDown() ?
1745 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1746 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1747 SendEvent(evt
, m_column
);
1749 //Left click the header
1750 if(event
.LeftDown())
1752 wxDataViewColumn
*col
= GetColumn(m_column
);
1753 if(col
->IsSortable())
1755 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1756 if(model
&& sortCol
== col
)
1758 bool order
= col
->IsSortOrderAscending();
1759 col
->SetSortOrder(!order
);
1763 m_owner
->SetSortingColumn(col
);
1769 //Send the column sorted event
1770 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1774 else if (event
.Moving())
1776 if (hit_border
&& resizeable
)
1777 m_currentCursor
= m_resizeCursor
;
1779 m_currentCursor
= wxSTANDARD_CURSOR
;
1781 SetCursor(*m_currentCursor
);
1786 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1790 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1791 m_owner
->GetViewStart( &x
, NULL
);
1793 // shift the DC origin to match the position of the main window horizontal
1794 // scrollbar: this allows us to always use logical coords
1795 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1798 #endif // defined(__WXMSW__)
1800 //-----------------------------------------------------------------------------
1801 // wxDataViewRenameTimer
1802 //-----------------------------------------------------------------------------
1804 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1809 void wxDataViewRenameTimer::Notify()
1811 m_owner
->OnRenameTimer();
1814 //-----------------------------------------------------------------------------
1815 // wxDataViewMainWindow
1816 //-----------------------------------------------------------------------------
1818 //The tree building helper, declared firstly
1819 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1821 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1823 if (row1
> row2
) return 1;
1824 if (row1
== row2
) return 0;
1829 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1831 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1832 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1833 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1834 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1835 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1836 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1839 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1840 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1841 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1842 m_selection( wxDataViewSelectionCmp
)
1847 m_lastOnSame
= false;
1848 m_renameTimer
= new wxDataViewRenameTimer( this );
1850 // TODO: user better initial values/nothing selected
1851 m_currentCol
= NULL
;
1854 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1857 m_dragStart
= wxPoint(0,0);
1858 m_lineLastClicked
= (unsigned int) -1;
1859 m_lineBeforeLastClicked
= (unsigned int) -1;
1860 m_lineSelectSingleOnUp
= (unsigned int) -1;
1864 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1865 SetBackgroundColour( *wxWHITE
);
1867 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1869 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1870 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1871 //Some new added code to deal with the tree structure
1872 m_root
= new wxDataViewTreeNode( NULL
);
1873 m_root
->SetHasChildren(true);
1875 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1877 m_underMouse
= NULL
;
1881 wxDataViewMainWindow::~wxDataViewMainWindow()
1884 delete m_renameTimer
;
1887 void wxDataViewMainWindow::OnRenameTimer()
1889 // We have to call this here because changes may just have
1890 // been made and no screen update taken place.
1895 unsigned int cols
= GetOwner()->GetColumnCount();
1897 for (i
= 0; i
< cols
; i
++)
1899 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1901 continue; // skip it!
1903 if (c
== m_currentCol
)
1905 xpos
+= c
->GetWidth();
1907 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1908 m_currentCol
->GetWidth(), m_lineHeight
);
1910 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1911 &labelRect
.x
, &labelRect
.y
);
1913 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1914 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1918 //------------------------------------------------------------------
1919 // Helper class for do operation on the tree node
1920 //------------------------------------------------------------------
1927 //The return value control how the tree-walker tranverse the tree
1928 // 0: Job done, stop tranverse and return
1929 // 1: Ignore the current node's subtree and continue
1930 // 2: Job not done, continue
1931 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1932 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1933 virtual int operator() ( void * n
) = 0;
1936 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1941 switch( func( node
) )
1952 wxDataViewTreeNodes nodes
= node
->GetNodes();
1953 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1955 int len_nodes
= nodes
.GetCount();
1956 int len
= leaves
.GetCount();
1957 int i
= 0, nodes_i
= 0;
1959 for( ; i
< len
; i
++ )
1961 void * n
= leaves
[i
];
1962 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1964 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1967 if( Walker( nd
, func
) )
1986 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1990 wxDataViewTreeNode
* node
;
1991 node
= FindNode(parent
);
1996 node
->SetHasChildren( true );
1998 if( g_model
->IsContainer( item
) )
2000 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2001 newnode
->SetItem(item
);
2002 newnode
->SetHasChildren( true );
2003 node
->AddNode( newnode
);
2006 node
->AddLeaf( item
.GetID() );
2008 node
->ChangeSubTreeCount(1);
2016 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2018 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2019 const wxDataViewItem
& item
)
2021 wxDataViewTreeNode
* node
= FindNode(parent
);
2023 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2024 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2027 node
->GetChildren().Remove( item
.GetID() );
2028 //Manuplate selection
2029 if( m_selection
.GetCount() > 1 )
2031 m_selection
.Empty();
2033 bool isContainer
= false;
2034 wxDataViewTreeNodes nds
= node
->GetNodes();
2035 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2037 if (nds
[i
]->GetItem() == item
)
2045 wxDataViewTreeNode
* n
= NULL
;
2046 wxDataViewTreeNodes nodes
= node
->GetNodes();
2047 int len
= nodes
.GetCount();
2048 for( int i
= 0 ; i
< len
; i
++)
2050 if( nodes
[i
]->GetItem() == item
)
2057 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2059 node
->GetNodes().Remove( n
);
2060 sub
-= n
->GetSubTreeCount();
2061 DestroyTreeHelper(n
);
2063 //Make the row number invalid and get a new valid one when user call GetRowCount
2065 node
->ChangeSubTreeCount(sub
);
2066 if( node
->GetChildrenNumber() == 0)
2068 node
->GetParent()->GetNodes().Remove( node
);
2072 //Change the current row to the last row if the current exceed the max row number
2073 if( m_currentRow
> GetRowCount() )
2074 m_currentRow
= m_count
- 1;
2081 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2087 wxWindow
*parent
= GetParent();
2088 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2089 le
.SetEventObject(parent
);
2090 le
.SetModel(GetOwner()->GetModel());
2092 parent
->GetEventHandler()->ProcessEvent(le
);
2097 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2099 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2100 /*#define MAX_VIRTUAL_WIDTH 100000
2102 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2103 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2104 Refresh( true, &rect );
2112 wxWindow
*parent
= GetParent();
2113 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2114 le
.SetEventObject(parent
);
2115 le
.SetModel(GetOwner()->GetModel());
2118 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2119 parent
->GetEventHandler()->ProcessEvent(le
);
2124 bool wxDataViewMainWindow::Cleared()
2134 void wxDataViewMainWindow::UpdateDisplay()
2139 void wxDataViewMainWindow::OnInternalIdle()
2141 wxWindow::OnInternalIdle();
2145 RecalculateDisplay();
2150 void wxDataViewMainWindow::RecalculateDisplay()
2152 wxDataViewModel
*model
= GetOwner()->GetModel();
2159 int width
= GetEndOfLastCol();
2160 int height
= GetRowCount() * m_lineHeight
;
2162 SetVirtualSize( width
, height
);
2163 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2168 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2170 wxWindow::ScrollWindow( dx
, dy
, rect
);
2172 if (GetOwner()->m_headerArea
)
2173 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2176 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2179 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2180 int sy
= rows
*m_lineHeight
/y
;
2184 wxRect rect
= GetClientRect();
2186 int x_start
= 0, x_end
= 0, w
= 0;
2188 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2189 for (x_start
= 0; colnum
< column
; colnum
++)
2191 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2192 if (col
->IsHidden())
2193 continue; // skip it!
2195 w
= col
->GetWidth();
2199 x_end
= x_start
+ w
;
2200 xe
= xx
+ rect
.width
;
2203 sx
= ( xx
+ x_end
- xe
)/x
;
2210 m_owner
->Scroll( sx
, sy
);
2213 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2215 wxDataViewModel
*model
= GetOwner()->GetModel();
2216 wxAutoBufferedPaintDC
dc( this );
2219 dc
.SetBackground(GetBackgroundColour());
2221 GetOwner()->PrepareDC( dc
);
2222 dc
.SetFont( GetFont() );
2224 wxRect update
= GetUpdateRegion().GetBox();
2225 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2227 // compute which items needs to be redrawn
2228 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2229 unsigned int item_count
=
2230 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2231 (int)(GetRowCount( )- item_start
) );
2232 unsigned int item_last
= item_start
+ item_count
;
2234 // compute which columns needs to be redrawn
2235 unsigned int cols
= GetOwner()->GetColumnCount();
2236 unsigned int col_start
= 0;
2237 unsigned int x_start
= 0;
2238 for (x_start
= 0; col_start
< cols
; col_start
++)
2240 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2241 if (col
->IsHidden())
2242 continue; // skip it!
2244 unsigned int w
= col
->GetWidth();
2245 if (x_start
+w
>= (unsigned int)update
.x
)
2251 unsigned int col_last
= col_start
;
2252 unsigned int x_last
= x_start
;
2253 for (; col_last
< cols
; col_last
++)
2255 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2256 if (col
->IsHidden())
2257 continue; // skip it!
2259 if (x_last
> (unsigned int)update
.GetRight())
2262 x_last
+= col
->GetWidth();
2265 // Draw horizontal rules if required
2266 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2268 dc
.SetPen(m_penRule
);
2269 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2271 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2273 int y
= i
* m_lineHeight
;
2274 dc
.DrawLine(x_start
, y
, x_last
, y
);
2278 // Draw vertical rules if required
2279 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2281 dc
.SetPen(m_penRule
);
2282 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2285 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2287 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2288 if (col
->IsHidden())
2289 continue; // skip it
2291 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2292 x
, item_last
* m_lineHeight
);
2294 x
+= col
->GetWidth();
2297 // Draw last vertical rule
2298 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2299 x
, item_last
* m_lineHeight
);
2302 // redraw the background for the items which are selected/current
2303 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2305 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2306 if (selected
|| item
== m_currentRow
)
2308 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2309 if (item
== m_currentRow
)
2310 flags
|= wxCONTROL_CURRENT
;
2312 flags
|= wxCONTROL_FOCUSED
;
2314 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2315 wxRendererNative::Get().DrawItemSelectionRect
2325 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2328 // TODO: last column for RTL support
2329 expander
= GetOwner()->GetColumn( 0 );
2330 GetOwner()->SetExpanderColumn(expander
);
2333 // redraw all cells for all rows which must be repainted and for all columns
2335 cell_rect
.x
= x_start
;
2336 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2337 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2339 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2340 wxDataViewRenderer
*cell
= col
->GetRenderer();
2341 cell_rect
.width
= col
->GetWidth();
2343 if (col
->IsHidden())
2344 continue; // skipt it!
2347 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2349 // get the cell value and set it into the renderer
2351 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2355 wxDataViewItem dataitem
= node
->GetItem();
2357 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2360 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2361 cell
->SetValue( value
);
2363 // update the y offset
2364 cell_rect
.y
= item
* m_lineHeight
;
2366 //Draw the expander here.
2367 int indent
= node
->GetIndentLevel();
2368 if( col
== expander
)
2370 //Calculate the indent first
2371 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2373 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2374 // change the cell_rect.x to the appropriate pos
2375 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2376 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2377 dc
.SetPen( m_penExpander
);
2378 dc
.SetBrush( wxNullBrush
);
2379 if( node
->HasChildren() )
2381 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2383 if (m_underMouse
== node
)
2385 flag
|= wxCONTROL_CURRENT
;
2387 if( node
->IsOpen() )
2388 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2390 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2394 // I am wandering whether we should draw dot lines between tree nodes
2396 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2399 //force the expander column to left-center align
2400 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2404 // cannot be bigger than allocated space
2405 wxSize size
= cell
->GetSize();
2406 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2407 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2408 // size.y = wxMin( size.y, cell_rect.height );
2409 size
.y
= cell_rect
.height
;
2411 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2412 int align
= cell
->GetAlignment();
2414 // horizontal alignment:
2415 item_rect
.x
= cell_rect
.x
;
2416 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2417 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2418 else if (align
& wxALIGN_RIGHT
)
2419 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2420 //else: wxALIGN_LEFT is the default
2422 // vertical alignment:
2423 item_rect
.y
= cell_rect
.y
;
2424 if (align
& wxALIGN_CENTER_VERTICAL
)
2425 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2426 else if (align
& wxALIGN_BOTTOM
)
2427 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2428 //else: wxALIGN_TOP is the default
2431 item_rect
.x
+= PADDING_RIGHTLEFT
;
2432 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2434 //Here we add the tree indent
2435 item_rect
.x
+= indent
;
2438 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2439 state
|= wxDATAVIEW_CELL_SELECTED
;
2441 // TODO: it would be much more efficient to create a clipping
2442 // region for the entire column being rendered (in the OnPaint
2443 // of wxDataViewMainWindow) instead of a single clip region for
2444 // each cell. However it would mean that each renderer should
2445 // respect the given wxRect's top & bottom coords, eventually
2446 // violating only the left & right coords - however the user can
2447 // make its own renderer and thus we cannot be sure of that.
2448 dc
.SetClippingRegion( item_rect
);
2449 cell
->Render( item_rect
, &dc
, state
);
2450 dc
.DestroyClippingRegion();
2453 cell_rect
.x
+= cell_rect
.width
;
2457 int wxDataViewMainWindow::GetCountPerPage() const
2459 wxSize size
= GetClientSize();
2460 return size
.y
/ m_lineHeight
;
2463 int wxDataViewMainWindow::GetEndOfLastCol() const
2467 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2469 const wxDataViewColumn
*c
=
2470 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2473 width
+= c
->GetWidth();
2478 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2482 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2484 return y
/ m_lineHeight
;
2487 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2489 wxSize client_size
= GetClientSize();
2490 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2491 &client_size
.x
, &client_size
.y
);
2493 //we should deal with the pixel here
2494 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2496 return wxMin( GetRowCount()-1, row
);
2499 unsigned int wxDataViewMainWindow::GetRowCount()
2501 if ( m_count
== -1 )
2503 m_count
= RecalculateCount();
2505 GetVirtualSize( &width
, &height
);
2506 height
= m_count
* m_lineHeight
;
2508 SetVirtualSize( width
, height
);
2513 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2520 void wxDataViewMainWindow::SelectAllRows( bool on
)
2527 m_selection
.Clear();
2528 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2529 m_selection
.Add( i
);
2534 unsigned int first_visible
= GetFirstVisibleRow();
2535 unsigned int last_visible
= GetLastVisibleRow();
2537 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2539 unsigned int row
= m_selection
[i
];
2540 if ((row
>= first_visible
) && (row
<= last_visible
))
2543 m_selection
.Clear();
2547 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2549 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2553 m_selection
.Add( row
);
2561 m_selection
.Remove( row
);
2567 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2571 unsigned int tmp
= from
;
2577 for (i
= from
; i
<= to
; i
++)
2579 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2582 m_selection
.Add( i
);
2587 m_selection
.Remove( i
);
2590 RefreshRows( from
, to
);
2593 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2595 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2597 int n
= aSelections
[i
];
2599 m_selection
.Add( n
);
2604 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2606 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2607 m_selection
.Add( row
);
2609 m_selection
.Remove( row
);
2613 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2615 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2618 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2620 wxWindow
*parent
= GetParent();
2621 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2623 le
.SetEventObject(parent
);
2624 le
.SetModel(GetOwner()->GetModel());
2627 parent
->GetEventHandler()->ProcessEvent(le
);
2630 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2632 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2633 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2635 wxSize client_size
= GetClientSize();
2636 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2637 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2638 if (intersect_rect
.width
> 0)
2639 Refresh( true, &intersect_rect
);
2642 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2646 unsigned int tmp
= to
;
2651 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2652 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2654 wxSize client_size
= GetClientSize();
2655 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2656 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2657 if (intersect_rect
.width
> 0)
2658 Refresh( true, &intersect_rect
);
2661 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2663 unsigned int count
= GetRowCount();
2664 if (firstRow
> count
)
2667 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2668 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2670 wxSize client_size
= GetClientSize();
2671 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2672 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2673 if (intersect_rect
.width
> 0)
2674 Refresh( true, &intersect_rect
);
2677 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2679 wxCHECK_RET( newCurrent
< GetRowCount(),
2680 _T("invalid item index in OnArrowChar()") );
2682 // if there is no selection, we cannot move it anywhere
2683 if (!HasCurrentRow())
2686 unsigned int oldCurrent
= m_currentRow
;
2688 // in single selection we just ignore Shift as we can't select several
2690 if ( event
.ShiftDown() && !IsSingleSel() )
2692 RefreshRow( oldCurrent
);
2694 ChangeCurrentRow( newCurrent
);
2696 // select all the items between the old and the new one
2697 if ( oldCurrent
> newCurrent
)
2699 newCurrent
= oldCurrent
;
2700 oldCurrent
= m_currentRow
;
2703 SelectRows( oldCurrent
, newCurrent
, true );
2704 if (oldCurrent
!=newCurrent
)
2705 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2709 RefreshRow( oldCurrent
);
2711 // all previously selected items are unselected unless ctrl is held
2712 if ( !event
.ControlDown() )
2713 SelectAllRows(false);
2715 ChangeCurrentRow( newCurrent
);
2717 if ( !event
.ControlDown() )
2719 SelectRow( m_currentRow
, true );
2720 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2723 RefreshRow( m_currentRow
);
2726 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2729 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2733 rect
.y
= m_lineHeight
* row
;
2734 rect
.width
= GetEndOfLastCol();
2735 rect
.height
= m_lineHeight
;
2740 class RowToItemJob
: public DoJob
2743 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2744 virtual ~RowToItemJob(){};
2746 virtual int operator() ( wxDataViewTreeNode
* node
)
2749 if( current
== static_cast<int>(row
))
2751 ret
= node
->GetItem() ;
2755 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2757 current
+= node
->GetSubTreeCount();
2762 //If the current has no child node, we can find the desired item of the row number directly.
2763 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2764 if( node
->GetNodes().GetCount() == 0)
2766 int index
= static_cast<int>(row
) - current
- 1;
2767 ret
= node
->GetChildren().Item( index
);
2774 virtual int operator() ( void * n
)
2777 if( current
== static_cast<int>(row
))
2779 ret
= wxDataViewItem( n
) ;
2784 wxDataViewItem
GetResult(){ return ret
; }
2791 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2793 RowToItemJob
job( row
, -2 );
2794 Walker( m_root
, job
);
2795 return job
.GetResult();
2798 class RowToTreeNodeJob
: public DoJob
2801 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2804 this->current
= current
;
2808 virtual ~RowToTreeNodeJob(){};
2810 virtual int operator() ( wxDataViewTreeNode
* node
)
2813 if( current
== static_cast<int>(row
))
2819 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2821 current
+= node
->GetSubTreeCount();
2827 //If the current has no child node, we can find the desired item of the row number directly.
2828 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2829 if( node
->GetNodes().GetCount() == 0)
2831 int index
= static_cast<int>(row
) - current
- 1;
2832 void * n
= node
->GetChildren().Item( index
);
2833 ret
= new wxDataViewTreeNode( parent
) ;
2834 ret
->SetItem( wxDataViewItem( n
));
2835 ret
->SetHasChildren(false);
2844 virtual int operator() ( void * n
)
2847 if( current
== static_cast<int>(row
))
2849 ret
= new wxDataViewTreeNode( parent
) ;
2850 ret
->SetItem( wxDataViewItem( n
));
2851 ret
->SetHasChildren(false);
2857 wxDataViewTreeNode
* GetResult(){ return ret
; }
2861 wxDataViewTreeNode
* ret
;
2862 wxDataViewTreeNode
* parent
;
2866 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2868 RowToTreeNodeJob
job( row
, -2, m_root
);
2869 Walker( m_root
, job
);
2870 return job
.GetResult();
2873 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2875 wxWindow
*parent
= GetParent();
2876 wxDataViewEvent
le(type
, parent
->GetId());
2878 le
.SetEventObject(parent
);
2879 le
.SetModel(GetOwner()->GetModel());
2882 parent
->GetEventHandler()->ProcessEvent(le
);
2886 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2888 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2891 if( node
->HasChildren())
2893 if( !node
->IsOpen())
2895 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2896 //Check if the user prevent expanding
2897 if( e
.GetSkipped() )
2901 //Here I build the children of current node
2902 if( node
->GetChildrenNumber() == 0 )
2905 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2909 //Send the expanded event
2910 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2914 SelectRow( row
, false );
2915 SelectRow( row
+ 1, true );
2916 ChangeCurrentRow( row
+ 1 );
2917 SendSelectionChangedEvent( GetItemByRow(row
+1));
2925 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2927 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2930 wxDataViewTreeNode
* nd
= node
;
2932 if( node
->HasChildren() && node
->IsOpen() )
2934 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2935 if( e
.GetSkipped() )
2940 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2944 node
= node
->GetParent();
2947 int parent
= GetRowByItem( node
->GetItem() ) ;
2950 SelectRow( row
, false);
2951 SelectRow(parent
, true );
2952 ChangeCurrentRow( parent
);
2953 SendSelectionChangedEvent( node
->GetItem() );
2957 if( !nd
->HasChildren())
2962 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2964 wxDataViewModel
* model
= GetOwner()->GetModel();
2968 //Compose the a parent-chain of the finding item
2970 list
.DeleteContents( true );
2971 wxDataViewItem
it( item
);
2974 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2975 list
.Insert( pItem
);
2976 it
= model
->GetParent( it
);
2979 //Find the item along the parent-chain.
2980 //This algorithm is designed to speed up the node-finding method
2981 wxDataViewTreeNode
* node
= m_root
;
2982 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
2984 if( node
->HasChildren() )
2986 if( node
->GetChildrenNumber() == 0 )
2989 BuildTreeHelper(model
, node
->GetItem(), node
);
2992 wxDataViewTreeNodes nodes
= node
->GetNodes();
2994 for (; i
< nodes
.GetCount(); i
++)
2996 if (nodes
[i
]->GetItem() == (**iter
))
3002 if (i
== nodes
.GetCount())
3011 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3013 wxDataViewColumn
*col
= NULL
;
3014 unsigned int cols
= GetOwner()->GetColumnCount();
3015 unsigned int colnum
= 0;
3016 unsigned int x_start
= 0;
3018 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3019 for (x_start
= 0; colnum
< cols
; colnum
++)
3021 col
= GetOwner()->GetColumn(colnum
);
3022 if (col
->IsHidden())
3023 continue; // skip it!
3025 unsigned int w
= col
->GetWidth();
3026 if (x_start
+w
>= (unsigned int)x
)
3033 item
= GetItemByRow( y
/m_lineHeight
);
3036 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3038 int row
= GetRowByItem(item
);
3039 int y
= row
*m_lineHeight
;
3040 int h
= m_lineHeight
;
3042 wxDataViewColumn
*col
= NULL
;
3043 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3045 col
= GetOwner()->GetColumn( i
);
3046 x
+= col
->GetWidth();
3047 if( GetOwner()->GetColumn(i
+1) == column
)
3050 int w
= col
->GetWidth();
3051 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3052 return wxRect(x
, y
, w
, h
);
3055 int wxDataViewMainWindow::RecalculateCount()
3057 return m_root
->GetSubTreeCount();
3060 class ItemToRowJob
: public DoJob
3063 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3064 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3065 virtual ~ItemToRowJob(){};
3067 //Maybe binary search will help to speed up this process
3068 virtual int operator() ( wxDataViewTreeNode
* node
)
3071 if( node
->GetItem() == item
)
3076 if( node
->GetItem() == **m_iter
)
3083 ret
+= node
->GetSubTreeCount();
3089 virtual int operator() ( void * n
)
3092 if( n
== item
.GetID() )
3096 //the row number is begin from zero
3097 int GetResult(){ return ret
-1 ; }
3099 ItemList::const_iterator m_iter
;
3100 wxDataViewItem item
;
3105 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3107 wxDataViewModel
* model
= GetOwner()->GetModel();
3114 //Compose the a parent-chain of the finding item
3116 wxDataViewItem
* pItem
= NULL
;
3117 list
.DeleteContents( true );
3118 wxDataViewItem
it( item
);
3121 pItem
= new wxDataViewItem( it
);
3122 list
.Insert( pItem
);
3123 it
= model
->GetParent( it
);
3125 pItem
= new wxDataViewItem( );
3126 list
.Insert( pItem
);
3128 ItemToRowJob
job( item
, list
.begin() );
3129 Walker(m_root
, job
);
3130 return job
.GetResult();
3133 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3135 if( !model
->IsContainer( item
) )
3138 wxDataViewItemArray children
;
3139 unsigned int num
= model
->GetChildren( item
, children
);
3140 unsigned int index
= 0;
3141 while( index
< num
)
3143 if( model
->IsContainer( children
[index
] ) )
3145 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3146 n
->SetItem(children
[index
]);
3147 n
->SetHasChildren( true ) ;
3152 node
->AddLeaf( children
[index
].GetID() );
3156 node
->SetSubTreeCount( num
);
3157 wxDataViewTreeNode
* n
= node
->GetParent();
3159 n
->ChangeSubTreeCount(num
);
3163 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3165 //First we define a invalid item to fetch the top-level elements
3166 wxDataViewItem item
;
3168 BuildTreeHelper( model
, item
, m_root
);
3172 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3174 if( node
->GetNodeNumber() != 0 )
3176 int len
= node
->GetNodeNumber();
3178 wxDataViewTreeNodes nodes
= node
->GetNodes();
3179 for( ; i
< len
; i
++ )
3181 DestroyTreeHelper(nodes
[i
]);
3187 void wxDataViewMainWindow::DestroyTree()
3189 DestroyTreeHelper(m_root
);
3190 m_root
->SetSubTreeCount(0);
3194 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3196 if (event
.GetKeyCode() == WXK_TAB
)
3198 wxNavigationKeyEvent nevent
;
3199 nevent
.SetWindowChange( event
.ControlDown() );
3200 nevent
.SetDirection( !event
.ShiftDown() );
3201 nevent
.SetEventObject( GetParent()->GetParent() );
3202 nevent
.SetCurrentFocus( m_parent
);
3203 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3207 // no item -> nothing to do
3208 if (!HasCurrentRow())
3214 // don't use m_linesPerPage directly as it might not be computed yet
3215 const int pageSize
= GetCountPerPage();
3216 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3218 switch ( event
.GetKeyCode() )
3222 if (m_currentRow
> 0)
3224 wxWindow
*parent
= GetParent();
3225 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3226 le
.SetItem( GetItemByRow(m_currentRow
) );
3227 le
.SetEventObject(parent
);
3228 le
.SetModel(GetOwner()->GetModel());
3230 parent
->GetEventHandler()->ProcessEvent(le
);
3235 if ( m_currentRow
> 0 )
3236 OnArrowChar( m_currentRow
- 1, event
);
3240 if ( m_currentRow
< GetRowCount() - 1 )
3241 OnArrowChar( m_currentRow
+ 1, event
);
3243 //Add the process for tree expanding/collapsing
3245 OnCollapsing(m_currentRow
);
3248 OnExpanding( m_currentRow
);
3252 OnArrowChar( GetRowCount() - 1, event
);
3257 OnArrowChar( 0, event
);
3262 int steps
= pageSize
- 1;
3263 int index
= m_currentRow
- steps
;
3267 OnArrowChar( index
, event
);
3273 int steps
= pageSize
- 1;
3274 unsigned int index
= m_currentRow
+ steps
;
3275 unsigned int count
= GetRowCount();
3276 if ( index
>= count
)
3279 OnArrowChar( index
, event
);
3288 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3290 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3292 // let the base handle mouse wheel events.
3297 int x
= event
.GetX();
3298 int y
= event
.GetY();
3299 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3300 wxDataViewColumn
*col
= NULL
;
3303 unsigned int cols
= GetOwner()->GetColumnCount();
3305 for (i
= 0; i
< cols
; i
++)
3307 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3309 continue; // skip it!
3311 if (x
< xpos
+ c
->GetWidth())
3316 xpos
+= c
->GetWidth();
3321 wxDataViewRenderer
*cell
= col
->GetRenderer();
3322 unsigned int current
= y
/ m_lineHeight
;
3323 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3325 // Unselect all if below the last row ?
3329 //Test whether the mouse is hovered on the tree item button
3331 if (GetOwner()->GetExpanderColumn() == col
)
3333 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3334 if( node
!=NULL
&& node
->HasChildren() )
3336 int indent
= node
->GetIndentLevel();
3337 indent
= GetOwner()->GetIndent()*indent
;
3338 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3339 if( rect
.Contains( x
, y
) )
3341 //So the mouse is over the expander
3343 if (m_underMouse
&& m_underMouse
!= node
)
3345 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3346 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3348 if (m_underMouse
!= node
)
3350 //wxLogMessage("Do the row: %d", current);
3351 RefreshRow(current
);
3353 m_underMouse
= node
;
3356 if (node
!=NULL
&& !node
->HasChildren())
3361 if (m_underMouse
!= NULL
)
3363 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3364 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3365 m_underMouse
= NULL
;
3369 wxDataViewModel
*model
= GetOwner()->GetModel();
3371 if (event
.Dragging())
3373 if (m_dragCount
== 0)
3375 // we have to report the raw, physical coords as we want to be
3376 // able to call HitTest(event.m_pointDrag) from the user code to
3377 // get the item being dragged
3378 m_dragStart
= event
.GetPosition();
3383 if (m_dragCount
!= 3)
3386 if (event
.LeftIsDown())
3388 // Notify cell about drag
3397 bool forceClick
= false;
3399 if (event
.ButtonDClick())
3401 m_renameTimer
->Stop();
3402 m_lastOnSame
= false;
3405 wxDataViewItem item
= GetItemByRow(current
);
3406 bool ignore_other_columns
=
3407 ((GetOwner()->GetExpanderColumn() != col
) &&
3408 (model
->IsContainer(item
)) &&
3409 (!model
->HasContainerColumns(item
)));
3411 if (event
.LeftDClick())
3413 if ( current
== m_lineLastClicked
)
3415 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3418 model
->GetValue( value
, item
, col
->GetModelColumn() );
3419 cell
->SetValue( value
);
3420 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3421 col
->GetWidth(), m_lineHeight
);
3422 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3427 wxWindow
*parent
= GetParent();
3428 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3430 le
.SetEventObject(parent
);
3431 le
.SetModel(GetOwner()->GetModel());
3433 parent
->GetEventHandler()->ProcessEvent(le
);
3439 // The first click was on another item, so don't interpret this as
3440 // a double click, but as a simple click instead
3447 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3449 // select single line
3450 SelectAllRows( false );
3451 SelectRow( m_lineSelectSingleOnUp
, true );
3454 //Process the event of user clicking the expander
3455 bool expander
= false;
3456 if (GetOwner()->GetExpanderColumn() == col
)
3458 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3459 if( node
!=NULL
&& node
->HasChildren() )
3461 int indent
= node
->GetIndentLevel();
3462 indent
= GetOwner()->GetIndent()*indent
;
3463 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3464 if( rect
.Contains( x
, y
) )
3467 if( node
->IsOpen() )
3468 OnCollapsing(current
);
3470 OnExpanding( current
);
3474 //If the user click the expander, we do not do editing even if the column with expander are editable
3475 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3477 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3478 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3480 m_renameTimer
->Start( 100, true );
3484 m_lastOnSame
= false;
3485 m_lineSelectSingleOnUp
= (unsigned int)-1;
3489 // This is necessary, because after a DnD operation in
3490 // from and to ourself, the up event is swallowed by the
3491 // DnD code. So on next non-up event (which means here and
3492 // now) m_lineSelectSingleOnUp should be reset.
3493 m_lineSelectSingleOnUp
= (unsigned int)-1;
3496 if (event
.RightDown())
3498 m_lineBeforeLastClicked
= m_lineLastClicked
;
3499 m_lineLastClicked
= current
;
3501 // If the item is already selected, do not update the selection.
3502 // Multi-selections should not be cleared if a selected item is clicked.
3503 if (!IsRowSelected(current
))
3505 SelectAllRows(false);
3506 ChangeCurrentRow(current
);
3507 SelectRow(m_currentRow
,true);
3508 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3511 // notify cell about right click
3514 // Allow generation of context menu event
3517 else if (event
.MiddleDown())
3519 // notify cell about middle click
3522 if (event
.LeftDown() || forceClick
)
3526 m_lineBeforeLastClicked
= m_lineLastClicked
;
3527 m_lineLastClicked
= current
;
3529 unsigned int oldCurrentRow
= m_currentRow
;
3530 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3532 bool cmdModifierDown
= event
.CmdDown();
3533 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3535 if ( IsSingleSel() || !IsRowSelected(current
) )
3537 SelectAllRows( false );
3538 ChangeCurrentRow(current
);
3539 SelectRow(m_currentRow
,true);
3540 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3542 else // multi sel & current is highlighted & no mod keys
3544 m_lineSelectSingleOnUp
= current
;
3545 ChangeCurrentRow(current
); // change focus
3548 else // multi sel & either ctrl or shift is down
3550 if (cmdModifierDown
)
3552 ChangeCurrentRow(current
);
3553 ReverseRowSelection(m_currentRow
);
3554 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3556 else if (event
.ShiftDown())
3558 ChangeCurrentRow(current
);
3560 unsigned int lineFrom
= oldCurrentRow
,
3563 if ( lineTo
< lineFrom
)
3566 lineFrom
= m_currentRow
;
3569 SelectRows(lineFrom
, lineTo
, true);
3570 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3572 else // !ctrl, !shift
3574 // test in the enclosing if should make it impossible
3575 wxFAIL_MSG( _T("how did we get here?") );
3579 if (m_currentRow
!= oldCurrentRow
)
3580 RefreshRow( oldCurrentRow
);
3582 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3584 // Update selection here...
3587 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3588 (current
== oldCurrentRow
)) && oldWasSelected
;
3592 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3596 if (HasCurrentRow())
3602 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3606 if (HasCurrentRow())
3612 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3614 if( m_selection
.GetCount() != 1 )
3615 return wxDataViewItem();
3617 return GetItemByRow( m_selection
.Item(0));
3620 //-----------------------------------------------------------------------------
3622 //-----------------------------------------------------------------------------
3623 WX_DEFINE_LIST(wxDataViewColumnList
);
3625 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3627 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3628 EVT_SIZE(wxDataViewCtrl::OnSize
)
3631 wxDataViewCtrl::~wxDataViewCtrl()
3634 GetModel()->RemoveNotifier( m_notifier
);
3637 void wxDataViewCtrl::Init()
3642 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3643 const wxPoint
& pos
, const wxSize
& size
,
3644 long style
, const wxValidator
& validator
)
3646 if (!wxControl::Create( parent
, id
, pos
, size
,
3647 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3650 SetInitialSize(size
);
3655 MacSetClipChildren( true ) ;
3658 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3660 if (HasFlag(wxDV_NO_HEADER
))
3661 m_headerArea
= NULL
;
3663 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3665 SetTargetWindow( m_clientArea
);
3667 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3669 sizer
->Add( m_headerArea
, 0, wxGROW
);
3670 sizer
->Add( m_clientArea
, 1, wxGROW
);
3677 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3681 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3684 // we need to process arrows ourselves for scrolling
3685 if ( nMsg
== WM_GETDLGCODE
)
3687 rc
|= DLGC_WANTARROWS
;
3695 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3697 // We need to override OnSize so that our scrolled
3698 // window a) does call Layout() to use sizers for
3699 // positioning the controls but b) does not query
3700 // the sizer for their size and use that for setting
3701 // the scrollable area as set that ourselves by
3702 // calling SetScrollbar() further down.
3709 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3711 if (!wxDataViewCtrlBase::AssociateModel( model
))
3714 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3716 model
->AddNotifier( m_notifier
);
3718 m_clientArea
->BuildTree(model
);
3720 m_clientArea
->UpdateDisplay();
3725 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3727 if (!wxDataViewCtrlBase::AppendColumn(col
))
3730 m_cols
.Append( col
);
3735 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3737 if (!wxDataViewCtrlBase::PrependColumn(col
))
3740 m_cols
.Insert( col
);
3745 void wxDataViewCtrl::OnColumnChange()
3748 m_headerArea
->UpdateDisplay();
3750 m_clientArea
->UpdateDisplay();
3753 void wxDataViewCtrl::DoSetExpanderColumn()
3755 m_clientArea
->UpdateDisplay();
3758 void wxDataViewCtrl::DoSetIndent()
3760 m_clientArea
->UpdateDisplay();
3763 unsigned int wxDataViewCtrl::GetColumnCount() const
3765 return m_cols
.GetCount();
3768 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3770 wxDataViewColumnList::const_iterator iter
;
3772 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3777 if ((*iter
)->IsHidden())
3784 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3786 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3797 bool wxDataViewCtrl::ClearColumns()
3804 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3806 int ret
= 0, dead
= 0;
3807 int len
= GetColumnCount();
3808 for (int i
=0; i
<len
; i
++)
3810 wxDataViewColumn
* col
= GetColumn(i
);
3811 if (col
->IsHidden())
3813 ret
+= col
->GetWidth();
3816 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3823 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3828 //Selection code with wxDataViewItem as parameters
3829 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3831 return m_clientArea
->GetSelection();
3834 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3837 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3838 int len
= selection
.GetCount();
3839 for( int i
= 0; i
< len
; i
++)
3841 unsigned int row
= selection
[i
];
3842 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3847 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3849 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3850 int len
= sel
.GetCount();
3851 for( int i
= 0; i
< len
; i
++ )
3853 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3855 selection
.Add( static_cast<unsigned int>(row
) );
3857 m_clientArea
->SetSelections( selection
);
3860 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3862 int row
= m_clientArea
->GetRowByItem( item
);
3865 //Unselect all rows before select another in the single select mode
3866 if (m_clientArea
->IsSingleSel())
3867 m_clientArea
->SelectAllRows(false);
3868 m_clientArea
->SelectRow(row
, true);
3872 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3874 int row
= m_clientArea
->GetRowByItem( item
);
3876 m_clientArea
->SelectRow(row
, false);
3879 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3881 int row
= m_clientArea
->GetRowByItem( item
);
3884 return m_clientArea
->IsRowSelected(row
);
3889 //Selection code with row number as parameter
3890 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3893 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3894 int len
= selection
.GetCount();
3895 for( int i
= 0; i
< len
; i
++)
3897 unsigned int row
= selection
[i
];
3903 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3905 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3906 int len
= sel
.GetCount();
3907 for( int i
= 0; i
< len
; i
++ )
3911 selection
.Add( static_cast<unsigned int>(row
) );
3913 m_clientArea
->SetSelections( selection
);
3916 void wxDataViewCtrl::Select( int row
)
3920 if (m_clientArea
->IsSingleSel())
3921 m_clientArea
->SelectAllRows(false);
3922 m_clientArea
->SelectRow( row
, true );
3926 void wxDataViewCtrl::Unselect( int row
)
3929 m_clientArea
->SelectRow(row
, false);
3932 bool wxDataViewCtrl::IsSelected( int row
) const
3935 return m_clientArea
->IsRowSelected(row
);
3939 void wxDataViewCtrl::SelectRange( int from
, int to
)
3942 for( int i
= from
; i
< to
; i
++ )
3944 m_clientArea
->Select(sel
);
3947 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3949 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3950 for( int i
= from
; i
< to
; i
++ )
3951 if( sel
.Index( i
) != wxNOT_FOUND
)
3953 m_clientArea
->SetSelections(sel
);
3956 void wxDataViewCtrl::SelectAll()
3958 m_clientArea
->SelectAllRows(true);
3961 void wxDataViewCtrl::UnselectAll()
3963 m_clientArea
->SelectAllRows(false);
3966 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3970 if( row
> (int) m_clientArea
->GetRowCount() )
3971 row
= m_clientArea
->GetRowCount();
3973 int first
= m_clientArea
->GetFirstVisibleRow();
3974 int last
= m_clientArea
->GetLastVisibleRow();
3976 m_clientArea
->ScrollTo( row
, column
);
3977 else if( row
> last
)
3978 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3980 m_clientArea
->ScrollTo( first
, column
);
3983 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3985 int row
= m_clientArea
->GetRowByItem(item
);
3988 if( column
== NULL
)
3989 EnsureVisible(row
, -1);
3993 int len
= GetColumnCount();
3994 for( int i
= 0; i
< len
; i
++ )
3995 if( GetColumn(i
) == column
)
4000 EnsureVisible( row
, col
);
4006 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4008 m_clientArea
->HitTest(point
, item
, column
);
4011 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4013 return m_clientArea
->GetItemRect(item
, column
);
4016 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4018 return m_clientArea
->GetItemByRow( row
);
4021 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4023 return m_clientArea
->GetRowByItem( item
);
4026 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4028 int row
= m_clientArea
->GetRowByItem( item
);
4030 m_clientArea
->Expand(row
);
4033 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4035 int row
= m_clientArea
->GetRowByItem( item
);
4037 m_clientArea
->Collapse(row
);
4041 // !wxUSE_GENERICDATAVIEWCTRL
4044 // wxUSE_DATAVIEWCTRL