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 void OnPaint(wxPaintEvent
&event
);
139 // called when any column setting is changed and/or changed
141 virtual void UpdateDisplay();
143 // called Refresh afterwards
144 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
147 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
149 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
151 wxSize
DoGetBestSize() const;
153 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
155 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
156 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
161 DECLARE_EVENT_TABLE()
162 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
165 #else // !defined(__WXMSW__)
167 #define HEADER_WINDOW_HEIGHT 25
168 #define HEADER_HORIZ_BORDER 5
169 #define HEADER_VERT_BORDER 3
170 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
172 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
175 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
177 const wxPoint
&pos
= wxDefaultPosition
,
178 const wxSize
&size
= wxDefaultSize
,
179 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
182 Create(parent
, id
, pos
, size
, name
);
185 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
186 const wxPoint
&pos
, const wxSize
&size
,
187 const wxString
&name
);
189 ~wxGenericDataViewHeaderWindow()
191 delete m_resizeCursor
;
194 virtual void UpdateDisplay() { Refresh(); }
198 void OnPaint( wxPaintEvent
&event
);
199 void OnMouse( wxMouseEvent
&event
);
200 void OnSetFocus( wxFocusEvent
&event
);
205 // vars used for column resizing:
207 wxCursor
*m_resizeCursor
;
208 const wxCursor
*m_currentCursor
;
211 bool m_dirty
; // needs refresh?
212 int m_hover
; // index of the column under the mouse
213 int m_column
; // index of the column being resized
214 int m_currentX
; // divider line position in logical (unscrolled) coords
215 int m_minX
; // minimal position beyond which the divider line
216 // can't be dragged in logical coords
218 // the pen used to draw the current column width drag line
219 // when resizing the columsn
223 // internal utilities:
227 m_currentCursor
= (wxCursor
*) NULL
;
228 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
230 m_isDragging
= false;
233 m_hover
= wxNOT_FOUND
;
234 m_column
= wxNOT_FOUND
;
238 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
239 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
242 void AdjustDC(wxDC
& dc
);
245 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
246 DECLARE_EVENT_TABLE()
249 #endif // defined(__WXMSW__)
251 //-----------------------------------------------------------------------------
252 // wxDataViewRenameTimer
253 //-----------------------------------------------------------------------------
255 class wxDataViewRenameTimer
: public wxTimer
258 wxDataViewMainWindow
*m_owner
;
261 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
265 //-----------------------------------------------------------------------------
266 // wxDataViewTreeNode
267 //-----------------------------------------------------------------------------
268 class wxDataViewTreeNode
;
269 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
270 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
272 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
273 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
275 class wxDataViewTreeNode
278 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
285 m_hasChildren
= false;
289 ~wxDataViewTreeNode()
293 wxDataViewTreeNode
* GetParent() { return m_parent
; }
294 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
295 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
296 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
298 void AddNode( wxDataViewTreeNode
* node
)
300 m_leaves
.Add( node
->GetItem().GetID() );
302 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
305 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
307 void AddLeaf( void * leaf
)
309 m_leaves
.Add( leaf
);
311 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
314 wxDataViewItem
& GetItem() { return m_item
; }
315 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
317 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
318 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
322 wxDataViewTreeNode
* node
= this;
323 while( node
->GetParent()->GetParent() != NULL
)
325 node
= node
->GetParent();
338 int len
= m_nodes
.GetCount();
340 for ( int i
= 0 ;i
< len
; i
++)
341 sum
+= m_nodes
[i
]->GetSubTreeCount();
343 sum
+= m_leaves
.GetCount();
346 ChangeSubTreeCount(-sum
);
352 ChangeSubTreeCount(sum
);
355 bool HasChildren() { return m_hasChildren
; }
356 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
358 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
359 int GetSubTreeCount() { return m_subTreeCount
; }
360 void ChangeSubTreeCount( int num
)
364 m_subTreeCount
+= num
;
366 m_parent
->ChangeSubTreeCount(num
);
373 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
374 int len
= m_nodes
.GetCount();
375 for (int i
= 0; i
< len
; i
++)
376 m_nodes
[i
]->Resort();
377 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
382 wxDataViewTreeNode
*m_parent
;
383 wxDataViewTreeNodes m_nodes
;
384 wxDataViewTreeLeaves m_leaves
;
385 wxDataViewItem m_item
;
391 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
393 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
396 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
398 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
403 //-----------------------------------------------------------------------------
404 // wxDataViewMainWindow
405 //-----------------------------------------------------------------------------
407 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
409 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
410 WX_DEFINE_LIST(ItemList
);
412 class wxDataViewMainWindow
: public wxWindow
415 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
417 const wxPoint
&pos
= wxDefaultPosition
,
418 const wxSize
&size
= wxDefaultSize
,
419 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
420 virtual ~wxDataViewMainWindow();
422 // notifications from wxDataViewModel
423 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
424 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
425 bool ItemChanged( const wxDataViewItem
&item
);
426 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
437 g_model
= GetOwner()->GetModel();
438 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
441 if (g_model
->HasDefaultCompare())
449 g_column
= col
->GetModelColumn();
450 g_asending
= col
->IsSortOrderAscending();
453 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
454 wxDataViewCtrl
*GetOwner() { return m_owner
; }
455 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
457 void OnPaint( wxPaintEvent
&event
);
458 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
459 void OnChar( wxKeyEvent
&event
);
460 void OnMouse( wxMouseEvent
&event
);
461 void OnSetFocus( wxFocusEvent
&event
);
462 void OnKillFocus( wxFocusEvent
&event
);
464 void UpdateDisplay();
465 void RecalculateDisplay();
466 void OnInternalIdle();
468 void OnRenameTimer();
470 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
471 void ScrollTo( int rows
, int column
);
473 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
474 void ChangeCurrentRow( unsigned int row
);
476 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
477 bool IsEmpty() { return GetRowCount() == 0; }
479 int GetCountPerPage() const;
480 int GetEndOfLastCol() const;
481 unsigned int GetFirstVisibleRow() const;
482 //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
483 unsigned int GetLastVisibleRow();
484 unsigned int GetRowCount() ;
486 wxDataViewItem
GetSelection() const;
487 wxDataViewSelection
GetSelections(){ return m_selection
; }
488 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
489 void Select( const wxArrayInt
& aSelections
);
490 void SelectAllRows( bool on
);
491 void SelectRow( unsigned int row
, bool on
);
492 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
493 void ReverseRowSelection( unsigned int row
);
494 bool IsRowSelected( unsigned int row
);
495 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
497 void RefreshRow( unsigned int row
);
498 void RefreshRows( unsigned int from
, unsigned int to
);
499 void RefreshRowsAfter( unsigned int firstRow
);
501 // returns the colour to be used for drawing the rules
502 wxColour
GetRuleColour() const
504 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
507 wxRect
GetLineRect( unsigned int row
) const;
509 //Some useful functions for row and item mapping
510 wxDataViewItem
GetItemByRow( unsigned int row
) const;
511 int GetRowByItem( const wxDataViewItem
& item
);
513 //Methods for building the mapping tree
514 void BuildTree( wxDataViewModel
* model
);
516 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
517 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
519 void Expand( unsigned int row
) { OnExpanding( row
); }
520 void Collapse( unsigned int row
) { OnCollapsing( row
); }
522 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
523 //We did not need this temporarily
524 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
526 int RecalculateCount() ;
528 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
529 void OnExpanding( unsigned int row
);
530 void OnCollapsing( unsigned int row
);
532 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
535 wxDataViewCtrl
*m_owner
;
539 wxDataViewColumn
*m_currentCol
;
540 unsigned int m_currentRow
;
541 wxDataViewSelection m_selection
;
543 wxDataViewRenameTimer
*m_renameTimer
;
551 // for double click logic
552 unsigned int m_lineLastClicked
,
553 m_lineBeforeLastClicked
,
554 m_lineSelectSingleOnUp
;
556 // the pen used to draw horiz/vertical rules
559 // the pen used to draw the expander and the lines
562 //This is the tree structure of the model
563 wxDataViewTreeNode
* m_root
;
565 //This is the tree node under the cursor
566 wxDataViewTreeNode
* m_underMouse
;
568 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
569 DECLARE_EVENT_TABLE()
572 // ---------------------------------------------------------
573 // wxGenericDataViewModelNotifier
574 // ---------------------------------------------------------
576 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
579 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
580 { m_mainWindow
= mainWindow
; }
582 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
583 { return m_mainWindow
->ItemAdded( parent
, item
); }
584 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
585 { return m_mainWindow
->ItemDeleted( parent
, item
); }
586 virtual bool ItemChanged( const wxDataViewItem
& item
)
587 { return m_mainWindow
->ItemChanged(item
); }
588 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
589 { return m_mainWindow
->ValueChanged( item
, col
); }
590 virtual bool Cleared()
591 { return m_mainWindow
->Cleared(); }
592 virtual void Resort()
593 { m_mainWindow
->Resort(); }
595 wxDataViewMainWindow
*m_mainWindow
;
598 // ---------------------------------------------------------
599 // wxDataViewRenderer
600 // ---------------------------------------------------------
602 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
604 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
605 wxDataViewCellMode mode
,
607 wxDataViewRendererBase( varianttype
, mode
, align
)
614 wxDataViewRenderer::~wxDataViewRenderer()
620 wxDC
*wxDataViewRenderer::GetDC()
624 if (GetOwner() == NULL
)
626 if (GetOwner()->GetOwner() == NULL
)
628 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
634 // ---------------------------------------------------------
635 // wxDataViewCustomRenderer
636 // ---------------------------------------------------------
638 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
640 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
641 wxDataViewCellMode mode
, int align
) :
642 wxDataViewRenderer( varianttype
, mode
, align
)
646 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
648 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
649 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
650 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
651 view
->GetForegroundColour();
652 dc
->SetTextForeground(col
);
653 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
656 // ---------------------------------------------------------
657 // wxDataViewTextRenderer
658 // ---------------------------------------------------------
660 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
662 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
663 wxDataViewCellMode mode
, int align
) :
664 wxDataViewCustomRenderer( varianttype
, mode
, align
)
668 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
670 m_text
= value
.GetString();
675 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
680 bool wxDataViewTextRenderer::HasEditorCtrl()
685 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
686 wxRect labelRect
, const wxVariant
&value
)
688 return new wxTextCtrl( parent
, wxID_ANY
, value
,
689 wxPoint(labelRect
.x
,labelRect
.y
),
690 wxSize(labelRect
.width
,labelRect
.height
) );
693 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
695 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
696 value
= text
->GetValue();
700 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
702 RenderText( m_text
, 0, cell
, dc
, state
);
706 wxSize
wxDataViewTextRenderer::GetSize() const
708 const wxDataViewCtrl
*view
= GetView();
712 view
->GetTextExtent( m_text
, &x
, &y
);
713 return wxSize( x
, y
);
715 return wxSize(80,20);
718 // ---------------------------------------------------------
719 // wxDataViewBitmapRenderer
720 // ---------------------------------------------------------
722 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
724 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
725 wxDataViewCellMode mode
, int align
) :
726 wxDataViewCustomRenderer( varianttype
, mode
, align
)
730 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
732 if (value
.GetType() == wxT("wxBitmap"))
734 if (value
.GetType() == wxT("wxIcon"))
740 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
745 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
748 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
749 else if (m_icon
.Ok())
750 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
755 wxSize
wxDataViewBitmapRenderer::GetSize() const
758 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
759 else if (m_icon
.Ok())
760 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
762 return wxSize(16,16);
765 // ---------------------------------------------------------
766 // wxDataViewToggleRenderer
767 // ---------------------------------------------------------
769 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
771 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
772 wxDataViewCellMode mode
, int align
) :
773 wxDataViewCustomRenderer( varianttype
, mode
, align
)
778 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
780 m_toggle
= value
.GetBool();
785 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
790 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
792 // User wxRenderer here
795 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
797 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
802 flags
|= wxCONTROL_CHECKED
;
803 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
804 flags
|= wxCONTROL_DISABLED
;
806 wxRendererNative::Get().DrawCheckBox(
807 GetOwner()->GetOwner(),
815 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
816 wxDataViewModel
*model
,
817 const wxDataViewItem
& item
, unsigned int col
)
819 bool value
= !m_toggle
;
820 wxVariant variant
= value
;
821 model
->SetValue( variant
, item
, col
);
822 model
->ValueChanged( item
, col
);
826 wxSize
wxDataViewToggleRenderer::GetSize() const
828 return wxSize(20,20);
831 // ---------------------------------------------------------
832 // wxDataViewProgressRenderer
833 // ---------------------------------------------------------
835 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
837 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
838 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
839 wxDataViewCustomRenderer( varianttype
, mode
, align
)
845 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
849 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
851 m_value
= (long) value
;
853 if (m_value
< 0) m_value
= 0;
854 if (m_value
> 100) m_value
= 100;
859 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
861 value
= (long) m_value
;
865 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
867 double pct
= (double)m_value
/ 100.0;
869 bar
.width
= (int)(cell
.width
* pct
);
870 dc
->SetPen( *wxTRANSPARENT_PEN
);
871 dc
->SetBrush( *wxBLUE_BRUSH
);
872 dc
->DrawRectangle( bar
);
874 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
875 dc
->SetPen( *wxBLACK_PEN
);
876 dc
->DrawRectangle( cell
);
881 wxSize
wxDataViewProgressRenderer::GetSize() const
883 return wxSize(40,12);
886 // ---------------------------------------------------------
887 // wxDataViewDateRenderer
888 // ---------------------------------------------------------
890 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
892 #if wxUSE_DATE_RENDERER_POPUP
894 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
897 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
898 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
899 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
904 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
905 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
906 sizer
->Add( m_cal
, 1, wxGROW
);
911 void OnCalendar( wxCalendarEvent
&event
);
913 wxCalendarCtrl
*m_cal
;
914 wxDataViewModel
*m_model
;
916 const wxDataViewItem
& m_item
;
919 virtual void OnDismiss()
924 DECLARE_EVENT_TABLE()
927 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
928 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
931 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
933 wxDateTime date
= event
.GetDate();
934 wxVariant value
= date
;
935 m_model
->SetValue( value
, m_item
, m_col
);
936 m_model
->ValueChanged( m_item
, m_col
);
940 #endif // wxUSE_DATE_RENDERER_POPUP
942 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
944 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
945 wxDataViewCellMode mode
, int align
) :
946 wxDataViewCustomRenderer( varianttype
, mode
, align
)
950 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
952 m_date
= value
.GetDateTime();
957 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
963 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
965 wxString tmp
= m_date
.FormatDate();
966 RenderText( tmp
, 0, cell
, dc
, state
);
970 wxSize
wxDataViewDateRenderer::GetSize() const
972 const wxDataViewCtrl
* view
= GetView();
973 wxString tmp
= m_date
.FormatDate();
975 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
976 return wxSize(x
,y
+d
);
979 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
980 const wxDataViewItem
& item
, unsigned int col
)
983 model
->GetValue( variant
, item
, col
);
984 wxDateTime value
= variant
.GetDateTime();
986 #if wxUSE_DATE_RENDERER_POPUP
987 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
988 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
989 wxPoint pos
= wxGetMousePosition();
992 popup
->Popup( popup
->m_cal
);
993 #else // !wxUSE_DATE_RENDERER_POPUP
994 wxMessageBox(value
.Format());
995 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
999 // ---------------------------------------------------------
1000 // wxDataViewIconTextRenderer
1001 // ---------------------------------------------------------
1003 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1005 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1006 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1007 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1010 SetAlignment(align
);
1013 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1017 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1023 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1028 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1031 const wxIcon
&icon
= m_value
.GetIcon();
1034 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1035 xoffset
= icon
.GetWidth()+4;
1038 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1043 wxSize
wxDataViewIconTextRenderer::GetSize() const
1045 const wxDataViewCtrl
*view
= GetView();
1046 if (!m_value
.GetText().empty())
1049 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1051 if (m_value
.GetIcon().IsOk())
1052 x
+= m_value
.GetIcon().GetWidth() + 4;
1053 return wxSize( x
, y
);
1055 return wxSize(80,20);
1059 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1060 wxRect
WXUNUSED(labelRect
),
1061 const wxVariant
& WXUNUSED(value
))
1067 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1068 wxVariant
& WXUNUSED(value
))
1073 // ---------------------------------------------------------
1075 // ---------------------------------------------------------
1077 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1079 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1080 unsigned int model_column
,
1081 int width
, wxAlignment align
, int flags
) :
1082 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1084 SetAlignment(align
);
1088 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1091 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1092 unsigned int model_column
,
1093 int width
, wxAlignment align
, int flags
) :
1094 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1096 SetAlignment(align
);
1099 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1102 wxDataViewColumn::~wxDataViewColumn()
1106 void wxDataViewColumn::Init( int width
)
1109 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1113 void wxDataViewColumn::SetResizeable( bool resizeable
)
1116 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1118 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1121 void wxDataViewColumn::SetHidden( bool hidden
)
1124 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1126 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1128 // tell our owner to e.g. update its scrollbars:
1130 GetOwner()->OnColumnChange();
1133 void wxDataViewColumn::SetSortable( bool sortable
)
1136 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1138 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1140 // Update header button
1142 GetOwner()->OnColumnChange();
1145 void wxDataViewColumn::SetSortOrder( bool ascending
)
1147 m_ascending
= ascending
;
1149 // Update header button
1151 GetOwner()->OnColumnChange();
1154 bool wxDataViewColumn::IsSortOrderAscending() const
1159 void wxDataViewColumn::SetInternalWidth( int width
)
1163 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1164 if (m_owner
&& m_owner
->m_clientArea
)
1165 m_owner
->m_clientArea
->RecalculateDisplay();
1168 void wxDataViewColumn::SetWidth( int width
)
1170 m_owner
->m_headerArea
->UpdateDisplay();
1172 SetInternalWidth(width
);
1176 //-----------------------------------------------------------------------------
1177 // wxDataViewHeaderWindowBase
1178 //-----------------------------------------------------------------------------
1180 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1182 wxWindow
*parent
= GetParent();
1183 wxDataViewEvent
le(type
, parent
->GetId());
1185 le
.SetEventObject(parent
);
1187 le
.SetDataViewColumn(GetColumn(n
));
1188 le
.SetModel(GetOwner()->GetModel());
1190 // for events created by wxDataViewHeaderWindow the
1191 // row / value fields are not valid
1193 parent
->GetEventHandler()->ProcessEvent(le
);
1196 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1198 // implemented in msw/listctrl.cpp:
1199 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1201 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1203 BEGIN_EVENT_TABLE(wxDataViewHeaderWindowMSW
, wxDataViewHeaderWindowBase
)
1204 // EVT_PAINT (wxDataViewHeaderWindowMSW::OnPaint)
1207 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1208 const wxPoint
&pos
, const wxSize
&size
,
1209 const wxString
&name
)
1213 m_scrollOffsetX
= 0;;
1215 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1216 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1217 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1218 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1220 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1223 // create the native WC_HEADER window:
1224 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1225 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1228 msStyle
|= WS_VISIBLE
;
1230 m_hWnd
= CreateWindowEx(0,
1241 wxLogLastError(_T("CreateWindowEx"));
1245 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1246 // to call wxDataViewHeaderWindow::MSWOnNotify
1247 SubclassWin(m_hWnd
);
1249 // the following is required to get the default win's font for
1250 // header windows and must be done befor sending the HDM_LAYOUT msg
1256 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1261 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1263 return wxSize(80, 22);
1266 void wxDataViewHeaderWindowMSW::OnPaint(wxPaintEvent
&event
)
1268 wxClientDC
dc(this);
1270 int sortArrow
= wxHDR_SORT_ICON_UP
;
1272 wxRect
rect(0,0,80,22);
1274 // Draw an up or down arrow
1276 if (sortArrow
!= wxHDR_SORT_ICON_NONE
)
1280 // make a rect for the arrow
1283 ar
.y
+= (rect
.height
- ar
.height
)/2;
1284 ar
.x
= ar
.x
+ rect
.width
- 3*ar
.width
/2;
1285 arrowSpace
= 3*ar
.width
/2; // space to preserve when drawing the label
1288 if ( sortArrow
& wxHDR_SORT_ICON_UP
)
1290 triPt
[0].x
= ar
.width
/ 2;
1292 triPt
[1].x
= ar
.width
;
1293 triPt
[1].y
= ar
.height
;
1295 triPt
[2].y
= ar
.height
;
1301 triPt
[1].x
= ar
.width
;
1303 triPt
[2].x
= ar
.width
/ 2;
1304 triPt
[2].y
= ar
.height
;
1307 wxColour c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
1308 dc
.SetPen(wxPen(c
));
1309 dc
.SetBrush(wxBrush(c
));
1310 dc
.DrawPolygon( 3, triPt
, ar
.x
, ar
.y
);
1316 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1318 // remove old columns
1319 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1320 Header_DeleteItem((HWND
)m_hWnd
, 0);
1322 // add the updated array of columns to the header control
1323 unsigned int cols
= GetOwner()->GetColumnCount();
1324 unsigned int added
= 0;
1325 for (unsigned int i
= 0; i
< cols
; i
++)
1327 wxDataViewColumn
*col
= GetColumn( i
);
1328 if (col
->IsHidden())
1329 continue; // don't add it!
1332 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1333 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1334 hdi
.cxy
= col
->GetWidth();
1335 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1336 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1337 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1339 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1341 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1342 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1344 // if (col->IsSortOrderAscending())
1345 // hdi.fmt |= col->IsSortOrderAscending() ? HDF_SORTUP : HDF_SORTDOWN;
1349 // lParam is reserved for application's use:
1350 // we store there the column index to use it later in MSWOnNotify
1351 // (since columns may have been hidden)
1352 hdi
.lParam
= (LPARAM
)i
;
1354 // the native wxMSW implementation of the header window
1355 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1356 // on the right: to correct this effect we make the column
1357 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1359 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1361 switch (col
->GetAlignment())
1364 hdi
.fmt
|= HDF_LEFT
;
1366 case wxALIGN_CENTER
:
1367 case wxALIGN_CENTER_HORIZONTAL
:
1368 hdi
.fmt
|= HDF_CENTER
;
1371 hdi
.fmt
|= HDF_RIGHT
;
1375 // such alignment is not allowed for the column header!
1379 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1380 (WPARAM
)added
, (LPARAM
)&hdi
);
1385 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1389 // NOTE: we don't just return nmHDR->iItem because when there are
1390 // hidden columns, nmHDR->iItem may be different from
1391 // nmHDR->pitem->lParam
1393 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1395 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1400 item
.mask
= HDI_LPARAM
;
1401 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1403 return (unsigned int)item
.lParam
;
1406 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1408 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1410 // is it a message from the header?
1411 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1412 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1414 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1415 switch ( nmhdr
->code
)
1417 case HDN_BEGINTRACK
:
1418 // user has started to resize a column:
1419 // do we need to veto it?
1420 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1428 // user has started to reorder a column
1431 case HDN_ITEMCHANGING
:
1432 if (nmHDR
->pitem
!= NULL
&&
1433 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1435 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1436 if (nmHDR
->pitem
->cxy
< minWidth
)
1438 // do not allow the user to resize this column under
1439 // its minimal width:
1445 case HDN_ITEMCHANGED
: // user is resizing a column
1446 case HDN_ENDTRACK
: // user has finished resizing a column
1447 case HDN_ENDDRAG
: // user has finished reordering a column
1449 // update the width of the modified column:
1450 if (nmHDR
->pitem
!= NULL
&&
1451 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1453 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1454 unsigned int w
= nmHDR
->pitem
->cxy
;
1455 wxDataViewColumn
*col
= GetColumn(idx
);
1457 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1458 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1459 w
-= COLUMN_WIDTH_OFFSET
;
1461 if (w
>= (unsigned)col
->GetMinWidth())
1462 col
->SetInternalWidth(w
);
1468 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1469 wxDataViewModel
* model
= GetOwner()->GetModel();
1471 if(nmHDR
->iButton
== 0)
1473 wxDataViewColumn
*col
= GetColumn(idx
);
1474 if(col
->IsSortable())
1476 if(model
&& m_owner
->GetSortingColumn() == col
)
1478 bool order
= col
->IsSortOrderAscending();
1479 col
->SetSortOrder(!order
);
1483 m_owner
->SetSortingColumn(col
);
1491 wxEventType evt
= nmHDR
->iButton
== 0 ?
1492 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1493 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1494 SendEvent(evt
, idx
);
1500 // NOTE: for some reason (i.e. for a bug in Windows)
1501 // the HDN_ITEMCLICK notification is not sent on
1502 // right clicks, so we need to handle NM_RCLICK
1505 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1506 if (column
!= wxNOT_FOUND
)
1509 item
.mask
= HDI_LPARAM
;
1510 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1512 // 'idx' may be different from 'column' if there are
1513 // hidden columns...
1514 unsigned int idx
= (unsigned int)item
.lParam
;
1515 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1521 case HDN_GETDISPINFOW
:
1522 // see wxListCtrl::MSWOnNotify for more info!
1525 case HDN_ITEMDBLCLICK
:
1527 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1528 int w
= GetOwner()->GetBestColumnWidth(idx
);
1530 // update the native control:
1532 ZeroMemory(&hd
, sizeof(hd
));
1533 hd
.mask
= HDI_WIDTH
;
1535 Header_SetItem(GetHwnd(),
1536 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1539 // update the wxDataViewColumn class:
1540 GetColumn(idx
)->SetInternalWidth(w
);
1545 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1551 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
1553 m_scrollOffsetX
+= dx
;
1555 GetParent()->Layout();
1558 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1562 wxControl::DoSetSize( x
+m_scrollOffsetX
, y
, w
-m_scrollOffsetX
, h
, f
);
1565 #else // !defined(__WXMSW__)
1567 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1568 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1569 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1570 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1571 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1574 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1575 const wxPoint
&pos
, const wxSize
&size
,
1576 const wxString
&name
)
1580 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1583 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1584 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1585 SetOwnForegroundColour( attr
.colFg
);
1586 SetOwnBackgroundColour( attr
.colBg
);
1588 SetOwnFont( attr
.font
);
1590 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1591 // a wxBoxSizer and in order to avoid super-big header windows,
1592 // we need to set our height as fixed
1593 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1594 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1599 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1602 GetClientSize( &w
, &h
);
1604 wxAutoBufferedPaintDC
dc( this );
1606 dc
.SetBackground(GetBackgroundColour());
1610 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1613 m_owner
->GetViewStart( &x
, NULL
);
1615 // account for the horz scrollbar offset
1616 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1618 dc
.SetFont( GetFont() );
1620 unsigned int cols
= GetOwner()->GetColumnCount();
1623 for (i
= 0; i
< cols
; i
++)
1625 wxDataViewColumn
*col
= GetColumn( i
);
1626 if (col
->IsHidden())
1627 continue; // skip it!
1629 int cw
= col
->GetWidth();
1632 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1633 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1635 if (col
->IsSortOrderAscending())
1636 sortArrow
= wxHDR_SORT_ICON_UP
;
1638 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1642 if (m_parent
->IsEnabled())
1644 if ((int) i
== m_hover
)
1645 state
= wxCONTROL_CURRENT
;
1649 state
= (int) wxCONTROL_DISABLED
;
1652 wxRendererNative::Get().DrawHeaderButton
1656 wxRect(xpos
, 0, cw
, ch
-1),
1661 // align as required the column title:
1663 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1664 switch (col
->GetAlignment())
1667 x
+= HEADER_HORIZ_BORDER
;
1670 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1673 case wxALIGN_CENTER
:
1674 case wxALIGN_CENTER_HORIZONTAL
:
1675 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1679 // always center the title vertically:
1680 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1682 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1684 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1685 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1686 dc
.DrawText( col
->GetTitle(), x
, y
);
1687 dc
.DestroyClippingRegion();
1693 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1695 GetParent()->SetFocus();
1699 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1701 // we want to work with logical coords
1703 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1704 int y
= event
.GetY();
1708 // we don't draw the line beyond our window,
1709 // but we allow dragging it there
1711 GetClientSize( &w
, NULL
);
1712 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1715 if (event
.ButtonUp())
1717 m_isDragging
= false;
1723 m_currentX
= wxMax(m_minX
+ 7, x
);
1727 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1729 GetOwner()->Refresh();
1733 else // not dragging
1736 m_column
= wxNOT_FOUND
;
1738 bool hit_border
= false;
1740 // end of the current column
1743 // find the column where this event occured
1744 int countCol
= m_owner
->GetColumnCount();
1745 for (int column
= 0; column
< countCol
; column
++)
1747 wxDataViewColumn
*p
= GetColumn(column
);
1750 continue; // skip if not shown
1752 xpos
+= p
->GetWidth();
1754 if ((abs(x
-xpos
) < 3) && (y
< 22))
1762 // inside the column
1769 int old_hover
= m_hover
;
1771 if (event
.Leaving())
1772 m_hover
= wxNOT_FOUND
;
1773 if (old_hover
!= m_hover
)
1776 if (m_column
== wxNOT_FOUND
)
1779 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1780 if (event
.LeftDClick() && resizeable
)
1782 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1785 else if (event
.LeftDown() || event
.RightUp())
1787 if (hit_border
&& event
.LeftDown() && resizeable
)
1789 m_isDragging
= true;
1793 else // click on a column
1795 wxDataViewModel
* model
= GetOwner()->GetModel();
1796 wxEventType evt
= event
.LeftDown() ?
1797 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1798 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1799 SendEvent(evt
, m_column
);
1801 //Left click the header
1802 if(event
.LeftDown())
1804 wxDataViewColumn
*col
= GetColumn(m_column
);
1805 if(col
->IsSortable())
1807 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1808 if(model
&& sortCol
== col
)
1810 bool order
= col
->IsSortOrderAscending();
1811 col
->SetSortOrder(!order
);
1815 m_owner
->SetSortingColumn(col
);
1821 //Send the column sorted event
1822 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1826 else if (event
.Moving())
1828 if (hit_border
&& resizeable
)
1829 m_currentCursor
= m_resizeCursor
;
1831 m_currentCursor
= wxSTANDARD_CURSOR
;
1833 SetCursor(*m_currentCursor
);
1838 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1842 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1843 m_owner
->GetViewStart( &x
, NULL
);
1845 // shift the DC origin to match the position of the main window horizontal
1846 // scrollbar: this allows us to always use logical coords
1847 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1850 #endif // defined(__WXMSW__)
1852 //-----------------------------------------------------------------------------
1853 // wxDataViewRenameTimer
1854 //-----------------------------------------------------------------------------
1856 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1861 void wxDataViewRenameTimer::Notify()
1863 m_owner
->OnRenameTimer();
1866 //-----------------------------------------------------------------------------
1867 // wxDataViewMainWindow
1868 //-----------------------------------------------------------------------------
1870 //The tree building helper, declared firstly
1871 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1873 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1875 if (row1
> row2
) return 1;
1876 if (row1
== row2
) return 0;
1881 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1883 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1884 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1885 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1886 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1887 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1888 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1891 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1892 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1893 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1894 m_selection( wxDataViewSelectionCmp
)
1899 m_lastOnSame
= false;
1900 m_renameTimer
= new wxDataViewRenameTimer( this );
1902 // TODO: user better initial values/nothing selected
1903 m_currentCol
= NULL
;
1906 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1909 m_dragStart
= wxPoint(0,0);
1910 m_lineLastClicked
= (unsigned int) -1;
1911 m_lineBeforeLastClicked
= (unsigned int) -1;
1912 m_lineSelectSingleOnUp
= (unsigned int) -1;
1916 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1917 SetBackgroundColour( *wxWHITE
);
1919 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1921 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1922 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1923 //Some new added code to deal with the tree structure
1924 m_root
= new wxDataViewTreeNode( NULL
);
1925 m_root
->SetHasChildren(true);
1927 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1929 m_underMouse
= NULL
;
1933 wxDataViewMainWindow::~wxDataViewMainWindow()
1936 delete m_renameTimer
;
1939 void wxDataViewMainWindow::OnRenameTimer()
1941 // We have to call this here because changes may just have
1942 // been made and no screen update taken place.
1947 unsigned int cols
= GetOwner()->GetColumnCount();
1949 for (i
= 0; i
< cols
; i
++)
1951 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1953 continue; // skip it!
1955 if (c
== m_currentCol
)
1957 xpos
+= c
->GetWidth();
1959 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1960 m_currentCol
->GetWidth(), m_lineHeight
);
1962 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1963 &labelRect
.x
, &labelRect
.y
);
1965 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1966 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1970 //------------------------------------------------------------------
1971 // Helper class for do operation on the tree node
1972 //------------------------------------------------------------------
1979 //The return value control how the tree-walker tranverse the tree
1980 // 0: Job done, stop tranverse and return
1981 // 1: Ignore the current node's subtree and continue
1982 // 2: Job not done, continue
1983 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1984 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1985 virtual int operator() ( void * n
) = 0;
1988 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1993 switch( func( node
) )
2004 wxDataViewTreeNodes nodes
= node
->GetNodes();
2005 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2007 int len_nodes
= nodes
.GetCount();
2008 int len
= leaves
.GetCount();
2009 int i
= 0, nodes_i
= 0;
2011 for( ; i
< len
; i
++ )
2013 void * n
= leaves
[i
];
2014 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2016 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2019 if( Walker( nd
, func
) )
2038 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2042 wxDataViewTreeNode
* node
;
2043 node
= FindNode(parent
);
2048 node
->SetHasChildren( true );
2050 if( g_model
->IsContainer( item
) )
2052 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2053 newnode
->SetItem(item
);
2054 newnode
->SetHasChildren( true );
2055 node
->AddNode( newnode
);
2058 node
->AddLeaf( item
.GetID() );
2060 node
->ChangeSubTreeCount(1);
2068 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2070 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2071 const wxDataViewItem
& item
)
2073 wxDataViewTreeNode
* node
= FindNode(parent
);
2075 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2076 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2079 node
->GetChildren().Remove( item
.GetID() );
2080 //Manuplate selection
2081 if( m_selection
.GetCount() > 1 )
2083 m_selection
.Empty();
2085 bool isContainer
= false;
2086 wxDataViewTreeNodes nds
= node
->GetNodes();
2087 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2089 if (nds
[i
]->GetItem() == item
)
2097 wxDataViewTreeNode
* n
= NULL
;
2098 wxDataViewTreeNodes nodes
= node
->GetNodes();
2099 int len
= nodes
.GetCount();
2100 for( int i
= 0 ; i
< len
; i
++)
2102 if( nodes
[i
]->GetItem() == item
)
2109 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2111 node
->GetNodes().Remove( n
);
2112 sub
-= n
->GetSubTreeCount();
2113 DestroyTreeHelper(n
);
2115 //Make the row number invalid and get a new valid one when user call GetRowCount
2117 node
->ChangeSubTreeCount(sub
);
2118 if( node
->GetChildrenNumber() == 0)
2120 node
->GetParent()->GetNodes().Remove( node
);
2124 //Change the current row to the last row if the current exceed the max row number
2125 if( m_currentRow
> GetRowCount() )
2126 m_currentRow
= m_count
- 1;
2133 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2139 wxWindow
*parent
= GetParent();
2140 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2141 le
.SetEventObject(parent
);
2142 le
.SetModel(GetOwner()->GetModel());
2144 parent
->GetEventHandler()->ProcessEvent(le
);
2149 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2151 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2152 /*#define MAX_VIRTUAL_WIDTH 100000
2154 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2155 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2156 Refresh( true, &rect );
2164 wxWindow
*parent
= GetParent();
2165 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2166 le
.SetEventObject(parent
);
2167 le
.SetModel(GetOwner()->GetModel());
2170 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2171 parent
->GetEventHandler()->ProcessEvent(le
);
2176 bool wxDataViewMainWindow::Cleared()
2186 void wxDataViewMainWindow::UpdateDisplay()
2191 void wxDataViewMainWindow::OnInternalIdle()
2193 wxWindow::OnInternalIdle();
2197 RecalculateDisplay();
2202 void wxDataViewMainWindow::RecalculateDisplay()
2204 wxDataViewModel
*model
= GetOwner()->GetModel();
2211 int width
= GetEndOfLastCol();
2212 int height
= GetRowCount() * m_lineHeight
;
2214 SetVirtualSize( width
, height
);
2215 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2220 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2222 wxWindow::ScrollWindow( dx
, dy
, rect
);
2224 if (GetOwner()->m_headerArea
)
2225 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2228 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2231 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2232 int sy
= rows
*m_lineHeight
/y
;
2236 wxRect rect
= GetClientRect();
2238 int x_start
= 0, x_end
= 0, w
= 0;
2240 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2241 for (x_start
= 0; colnum
< column
; colnum
++)
2243 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2244 if (col
->IsHidden())
2245 continue; // skip it!
2247 w
= col
->GetWidth();
2251 x_end
= x_start
+ w
;
2252 xe
= xx
+ rect
.width
;
2255 sx
= ( xx
+ x_end
- xe
)/x
;
2262 m_owner
->Scroll( sx
, sy
);
2265 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2267 wxDataViewModel
*model
= GetOwner()->GetModel();
2268 wxAutoBufferedPaintDC
dc( this );
2271 dc
.SetBackground(GetBackgroundColour());
2273 GetOwner()->PrepareDC( dc
);
2274 dc
.SetFont( GetFont() );
2276 wxRect update
= GetUpdateRegion().GetBox();
2277 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2279 // compute which items needs to be redrawn
2280 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2281 unsigned int item_count
=
2282 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2283 (int)(GetRowCount( )- item_start
) );
2284 unsigned int item_last
= item_start
+ item_count
;
2286 // compute which columns needs to be redrawn
2287 unsigned int cols
= GetOwner()->GetColumnCount();
2288 unsigned int col_start
= 0;
2289 unsigned int x_start
= 0;
2290 for (x_start
= 0; col_start
< cols
; col_start
++)
2292 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2293 if (col
->IsHidden())
2294 continue; // skip it!
2296 unsigned int w
= col
->GetWidth();
2297 if (x_start
+w
>= (unsigned int)update
.x
)
2303 unsigned int col_last
= col_start
;
2304 unsigned int x_last
= x_start
;
2305 for (; col_last
< cols
; col_last
++)
2307 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2308 if (col
->IsHidden())
2309 continue; // skip it!
2311 if (x_last
> (unsigned int)update
.GetRight())
2314 x_last
+= col
->GetWidth();
2317 // Draw horizontal rules if required
2318 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2320 dc
.SetPen(m_penRule
);
2321 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2323 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2325 int y
= i
* m_lineHeight
;
2326 dc
.DrawLine(x_start
, y
, x_last
, y
);
2330 // Draw vertical rules if required
2331 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2333 dc
.SetPen(m_penRule
);
2334 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2337 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2339 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2340 if (col
->IsHidden())
2341 continue; // skip it
2343 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2344 x
, item_last
* m_lineHeight
);
2346 x
+= col
->GetWidth();
2349 // Draw last vertical rule
2350 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2351 x
, item_last
* m_lineHeight
);
2354 // redraw the background for the items which are selected/current
2355 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2357 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2358 if (selected
|| item
== m_currentRow
)
2360 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2361 if (item
== m_currentRow
)
2362 flags
|= wxCONTROL_CURRENT
;
2364 flags
|= wxCONTROL_FOCUSED
;
2366 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2367 wxRendererNative::Get().DrawItemSelectionRect
2377 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2380 // TODO: last column for RTL support
2381 expander
= GetOwner()->GetColumn( 0 );
2382 GetOwner()->SetExpanderColumn(expander
);
2385 // redraw all cells for all rows which must be repainted and for all columns
2387 cell_rect
.x
= x_start
;
2388 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2389 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2391 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2392 wxDataViewRenderer
*cell
= col
->GetRenderer();
2393 cell_rect
.width
= col
->GetWidth();
2395 if (col
->IsHidden())
2396 continue; // skipt it!
2399 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2401 // get the cell value and set it into the renderer
2403 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2407 wxDataViewItem dataitem
= node
->GetItem();
2409 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2412 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2413 cell
->SetValue( value
);
2415 // update the y offset
2416 cell_rect
.y
= item
* m_lineHeight
;
2418 //Draw the expander here.
2419 int indent
= node
->GetIndentLevel();
2420 if( col
== expander
)
2422 //Calculate the indent first
2423 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2425 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2426 // change the cell_rect.x to the appropriate pos
2427 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2428 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2429 dc
.SetPen( m_penExpander
);
2430 dc
.SetBrush( wxNullBrush
);
2431 if( node
->HasChildren() )
2433 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2435 if (m_underMouse
== node
)
2437 flag
|= wxCONTROL_CURRENT
;
2439 if( node
->IsOpen() )
2440 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2442 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2446 // I am wandering whether we should draw dot lines between tree nodes
2448 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2451 //force the expander column to left-center align
2452 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2456 // cannot be bigger than allocated space
2457 wxSize size
= cell
->GetSize();
2458 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2459 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2460 // size.y = wxMin( size.y, cell_rect.height );
2461 size
.y
= cell_rect
.height
;
2463 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2464 int align
= cell
->GetAlignment();
2466 // horizontal alignment:
2467 item_rect
.x
= cell_rect
.x
;
2468 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2469 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2470 else if (align
& wxALIGN_RIGHT
)
2471 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2472 //else: wxALIGN_LEFT is the default
2474 // vertical alignment:
2475 item_rect
.y
= cell_rect
.y
;
2476 if (align
& wxALIGN_CENTER_VERTICAL
)
2477 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2478 else if (align
& wxALIGN_BOTTOM
)
2479 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2480 //else: wxALIGN_TOP is the default
2483 item_rect
.x
+= PADDING_RIGHTLEFT
;
2484 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2486 //Here we add the tree indent
2487 item_rect
.x
+= indent
;
2490 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2491 state
|= wxDATAVIEW_CELL_SELECTED
;
2493 // TODO: it would be much more efficient to create a clipping
2494 // region for the entire column being rendered (in the OnPaint
2495 // of wxDataViewMainWindow) instead of a single clip region for
2496 // each cell. However it would mean that each renderer should
2497 // respect the given wxRect's top & bottom coords, eventually
2498 // violating only the left & right coords - however the user can
2499 // make its own renderer and thus we cannot be sure of that.
2500 dc
.SetClippingRegion( item_rect
);
2501 cell
->Render( item_rect
, &dc
, state
);
2502 dc
.DestroyClippingRegion();
2505 cell_rect
.x
+= cell_rect
.width
;
2509 int wxDataViewMainWindow::GetCountPerPage() const
2511 wxSize size
= GetClientSize();
2512 return size
.y
/ m_lineHeight
;
2515 int wxDataViewMainWindow::GetEndOfLastCol() const
2519 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2521 const wxDataViewColumn
*c
=
2522 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2525 width
+= c
->GetWidth();
2530 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2534 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2536 return y
/ m_lineHeight
;
2539 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2541 wxSize client_size
= GetClientSize();
2542 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2543 &client_size
.x
, &client_size
.y
);
2545 //we should deal with the pixel here
2546 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2548 return wxMin( GetRowCount()-1, row
);
2551 unsigned int wxDataViewMainWindow::GetRowCount()
2553 if ( m_count
== -1 )
2555 m_count
= RecalculateCount();
2557 GetVirtualSize( &width
, &height
);
2558 height
= m_count
* m_lineHeight
;
2560 SetVirtualSize( width
, height
);
2565 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2572 void wxDataViewMainWindow::SelectAllRows( bool on
)
2579 m_selection
.Clear();
2580 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2581 m_selection
.Add( i
);
2586 unsigned int first_visible
= GetFirstVisibleRow();
2587 unsigned int last_visible
= GetLastVisibleRow();
2589 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2591 unsigned int row
= m_selection
[i
];
2592 if ((row
>= first_visible
) && (row
<= last_visible
))
2595 m_selection
.Clear();
2599 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2601 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2605 m_selection
.Add( row
);
2613 m_selection
.Remove( row
);
2619 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2623 unsigned int tmp
= from
;
2629 for (i
= from
; i
<= to
; i
++)
2631 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2634 m_selection
.Add( i
);
2639 m_selection
.Remove( i
);
2642 RefreshRows( from
, to
);
2645 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2647 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2649 int n
= aSelections
[i
];
2651 m_selection
.Add( n
);
2656 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2658 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2659 m_selection
.Add( row
);
2661 m_selection
.Remove( row
);
2665 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2667 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2670 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2672 wxWindow
*parent
= GetParent();
2673 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2675 le
.SetEventObject(parent
);
2676 le
.SetModel(GetOwner()->GetModel());
2679 parent
->GetEventHandler()->ProcessEvent(le
);
2682 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2684 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2685 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2687 wxSize client_size
= GetClientSize();
2688 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2689 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2690 if (intersect_rect
.width
> 0)
2691 Refresh( true, &intersect_rect
);
2694 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2698 unsigned int tmp
= to
;
2703 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2704 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2706 wxSize client_size
= GetClientSize();
2707 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2708 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2709 if (intersect_rect
.width
> 0)
2710 Refresh( true, &intersect_rect
);
2713 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2715 unsigned int count
= GetRowCount();
2716 if (firstRow
> count
)
2719 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2720 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2722 wxSize client_size
= GetClientSize();
2723 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2724 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2725 if (intersect_rect
.width
> 0)
2726 Refresh( true, &intersect_rect
);
2729 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2731 wxCHECK_RET( newCurrent
< GetRowCount(),
2732 _T("invalid item index in OnArrowChar()") );
2734 // if there is no selection, we cannot move it anywhere
2735 if (!HasCurrentRow())
2738 unsigned int oldCurrent
= m_currentRow
;
2740 // in single selection we just ignore Shift as we can't select several
2742 if ( event
.ShiftDown() && !IsSingleSel() )
2744 RefreshRow( oldCurrent
);
2746 ChangeCurrentRow( newCurrent
);
2748 // select all the items between the old and the new one
2749 if ( oldCurrent
> newCurrent
)
2751 newCurrent
= oldCurrent
;
2752 oldCurrent
= m_currentRow
;
2755 SelectRows( oldCurrent
, newCurrent
, true );
2756 if (oldCurrent
!=newCurrent
)
2757 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2761 RefreshRow( oldCurrent
);
2763 // all previously selected items are unselected unless ctrl is held
2764 if ( !event
.ControlDown() )
2765 SelectAllRows(false);
2767 ChangeCurrentRow( newCurrent
);
2769 if ( !event
.ControlDown() )
2771 SelectRow( m_currentRow
, true );
2772 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2775 RefreshRow( m_currentRow
);
2778 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2781 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2785 rect
.y
= m_lineHeight
* row
;
2786 rect
.width
= GetEndOfLastCol();
2787 rect
.height
= m_lineHeight
;
2792 class RowToItemJob
: public DoJob
2795 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2796 virtual ~RowToItemJob(){};
2798 virtual int operator() ( wxDataViewTreeNode
* node
)
2801 if( current
== static_cast<int>(row
))
2803 ret
= node
->GetItem() ;
2807 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2809 current
+= node
->GetSubTreeCount();
2814 //If the current has no child node, we can find the desired item of the row number directly.
2815 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2816 if( node
->GetNodes().GetCount() == 0)
2818 int index
= static_cast<int>(row
) - current
- 1;
2819 ret
= node
->GetChildren().Item( index
);
2826 virtual int operator() ( void * n
)
2829 if( current
== static_cast<int>(row
))
2831 ret
= wxDataViewItem( n
) ;
2836 wxDataViewItem
GetResult(){ return ret
; }
2843 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2845 RowToItemJob
job( row
, -2 );
2846 Walker( m_root
, job
);
2847 return job
.GetResult();
2850 class RowToTreeNodeJob
: public DoJob
2853 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2856 this->current
= current
;
2860 virtual ~RowToTreeNodeJob(){};
2862 virtual int operator() ( wxDataViewTreeNode
* node
)
2865 if( current
== static_cast<int>(row
))
2871 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2873 current
+= node
->GetSubTreeCount();
2879 //If the current has no child node, we can find the desired item of the row number directly.
2880 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2881 if( node
->GetNodes().GetCount() == 0)
2883 int index
= static_cast<int>(row
) - current
- 1;
2884 void * n
= node
->GetChildren().Item( index
);
2885 ret
= new wxDataViewTreeNode( parent
) ;
2886 ret
->SetItem( wxDataViewItem( n
));
2887 ret
->SetHasChildren(false);
2896 virtual int operator() ( void * n
)
2899 if( current
== static_cast<int>(row
))
2901 ret
= new wxDataViewTreeNode( parent
) ;
2902 ret
->SetItem( wxDataViewItem( n
));
2903 ret
->SetHasChildren(false);
2909 wxDataViewTreeNode
* GetResult(){ return ret
; }
2913 wxDataViewTreeNode
* ret
;
2914 wxDataViewTreeNode
* parent
;
2918 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2920 RowToTreeNodeJob
job( row
, -2, m_root
);
2921 Walker( m_root
, job
);
2922 return job
.GetResult();
2925 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2927 wxWindow
*parent
= GetParent();
2928 wxDataViewEvent
le(type
, parent
->GetId());
2930 le
.SetEventObject(parent
);
2931 le
.SetModel(GetOwner()->GetModel());
2934 parent
->GetEventHandler()->ProcessEvent(le
);
2938 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2940 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2943 if( node
->HasChildren())
2945 if( !node
->IsOpen())
2947 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2948 //Check if the user prevent expanding
2949 if( e
.GetSkipped() )
2953 //Here I build the children of current node
2954 if( node
->GetChildrenNumber() == 0 )
2957 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2961 //Send the expanded event
2962 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2966 SelectRow( row
, false );
2967 SelectRow( row
+ 1, true );
2968 ChangeCurrentRow( row
+ 1 );
2969 SendSelectionChangedEvent( GetItemByRow(row
+1));
2977 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2979 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2982 wxDataViewTreeNode
* nd
= node
;
2984 if( node
->HasChildren() && node
->IsOpen() )
2986 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2987 if( e
.GetSkipped() )
2992 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2996 node
= node
->GetParent();
2999 int parent
= GetRowByItem( node
->GetItem() ) ;
3002 SelectRow( row
, false);
3003 SelectRow(parent
, true );
3004 ChangeCurrentRow( parent
);
3005 SendSelectionChangedEvent( node
->GetItem() );
3009 if( !nd
->HasChildren())
3014 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3016 wxDataViewModel
* model
= GetOwner()->GetModel();
3020 //Compose the a parent-chain of the finding item
3022 list
.DeleteContents( true );
3023 wxDataViewItem
it( item
);
3026 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3027 list
.Insert( pItem
);
3028 it
= model
->GetParent( it
);
3031 //Find the item along the parent-chain.
3032 //This algorithm is designed to speed up the node-finding method
3033 wxDataViewTreeNode
* node
= m_root
;
3034 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3036 if( node
->HasChildren() )
3038 if( node
->GetChildrenNumber() == 0 )
3041 BuildTreeHelper(model
, node
->GetItem(), node
);
3044 wxDataViewTreeNodes nodes
= node
->GetNodes();
3046 for (; i
< nodes
.GetCount(); i
++)
3048 if (nodes
[i
]->GetItem() == (**iter
))
3054 if (i
== nodes
.GetCount())
3063 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3065 wxDataViewColumn
*col
= NULL
;
3066 unsigned int cols
= GetOwner()->GetColumnCount();
3067 unsigned int colnum
= 0;
3068 unsigned int x_start
= 0;
3070 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3071 for (x_start
= 0; colnum
< cols
; colnum
++)
3073 col
= GetOwner()->GetColumn(colnum
);
3074 if (col
->IsHidden())
3075 continue; // skip it!
3077 unsigned int w
= col
->GetWidth();
3078 if (x_start
+w
>= (unsigned int)x
)
3085 item
= GetItemByRow( y
/m_lineHeight
);
3088 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3090 int row
= GetRowByItem(item
);
3091 int y
= row
*m_lineHeight
;
3092 int h
= m_lineHeight
;
3094 wxDataViewColumn
*col
= NULL
;
3095 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3097 col
= GetOwner()->GetColumn( i
);
3098 x
+= col
->GetWidth();
3099 if( GetOwner()->GetColumn(i
+1) == column
)
3102 int w
= col
->GetWidth();
3103 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3104 return wxRect(x
, y
, w
, h
);
3107 int wxDataViewMainWindow::RecalculateCount()
3109 return m_root
->GetSubTreeCount();
3112 class ItemToRowJob
: public DoJob
3115 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3116 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3117 virtual ~ItemToRowJob(){};
3119 //Maybe binary search will help to speed up this process
3120 virtual int operator() ( wxDataViewTreeNode
* node
)
3123 if( node
->GetItem() == item
)
3128 if( node
->GetItem() == **m_iter
)
3135 ret
+= node
->GetSubTreeCount();
3141 virtual int operator() ( void * n
)
3144 if( n
== item
.GetID() )
3148 //the row number is begin from zero
3149 int GetResult(){ return ret
-1 ; }
3151 ItemList::const_iterator m_iter
;
3152 wxDataViewItem item
;
3157 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3159 wxDataViewModel
* model
= GetOwner()->GetModel();
3166 //Compose the a parent-chain of the finding item
3168 wxDataViewItem
* pItem
= NULL
;
3169 list
.DeleteContents( true );
3170 wxDataViewItem
it( item
);
3173 pItem
= new wxDataViewItem( it
);
3174 list
.Insert( pItem
);
3175 it
= model
->GetParent( it
);
3177 pItem
= new wxDataViewItem( );
3178 list
.Insert( pItem
);
3180 ItemToRowJob
job( item
, list
.begin() );
3181 Walker(m_root
, job
);
3182 return job
.GetResult();
3185 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3187 if( !model
->IsContainer( item
) )
3190 wxDataViewItemArray children
;
3191 unsigned int num
= model
->GetChildren( item
, children
);
3192 unsigned int index
= 0;
3193 while( index
< num
)
3195 if( model
->IsContainer( children
[index
] ) )
3197 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3198 n
->SetItem(children
[index
]);
3199 n
->SetHasChildren( true ) ;
3204 node
->AddLeaf( children
[index
].GetID() );
3208 node
->SetSubTreeCount( num
);
3209 wxDataViewTreeNode
* n
= node
->GetParent();
3211 n
->ChangeSubTreeCount(num
);
3215 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3217 //First we define a invalid item to fetch the top-level elements
3218 wxDataViewItem item
;
3220 BuildTreeHelper( model
, item
, m_root
);
3224 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3226 if( node
->GetNodeNumber() != 0 )
3228 int len
= node
->GetNodeNumber();
3230 wxDataViewTreeNodes nodes
= node
->GetNodes();
3231 for( ; i
< len
; i
++ )
3233 DestroyTreeHelper(nodes
[i
]);
3239 void wxDataViewMainWindow::DestroyTree()
3241 DestroyTreeHelper(m_root
);
3242 m_root
->SetSubTreeCount(0);
3246 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3248 if (event
.GetKeyCode() == WXK_TAB
)
3250 wxNavigationKeyEvent nevent
;
3251 nevent
.SetWindowChange( event
.ControlDown() );
3252 nevent
.SetDirection( !event
.ShiftDown() );
3253 nevent
.SetEventObject( GetParent()->GetParent() );
3254 nevent
.SetCurrentFocus( m_parent
);
3255 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3259 // no item -> nothing to do
3260 if (!HasCurrentRow())
3266 // don't use m_linesPerPage directly as it might not be computed yet
3267 const int pageSize
= GetCountPerPage();
3268 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3270 switch ( event
.GetKeyCode() )
3274 if (m_currentRow
> 0)
3276 wxWindow
*parent
= GetParent();
3277 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3278 le
.SetItem( GetItemByRow(m_currentRow
) );
3279 le
.SetEventObject(parent
);
3280 le
.SetModel(GetOwner()->GetModel());
3282 parent
->GetEventHandler()->ProcessEvent(le
);
3287 if ( m_currentRow
> 0 )
3288 OnArrowChar( m_currentRow
- 1, event
);
3292 if ( m_currentRow
< GetRowCount() - 1 )
3293 OnArrowChar( m_currentRow
+ 1, event
);
3295 //Add the process for tree expanding/collapsing
3297 OnCollapsing(m_currentRow
);
3300 OnExpanding( m_currentRow
);
3304 OnArrowChar( GetRowCount() - 1, event
);
3309 OnArrowChar( 0, event
);
3314 int steps
= pageSize
- 1;
3315 int index
= m_currentRow
- steps
;
3319 OnArrowChar( index
, event
);
3325 int steps
= pageSize
- 1;
3326 unsigned int index
= m_currentRow
+ steps
;
3327 unsigned int count
= GetRowCount();
3328 if ( index
>= count
)
3331 OnArrowChar( index
, event
);
3340 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3342 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3344 // let the base handle mouse wheel events.
3349 int x
= event
.GetX();
3350 int y
= event
.GetY();
3351 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3352 wxDataViewColumn
*col
= NULL
;
3355 unsigned int cols
= GetOwner()->GetColumnCount();
3357 for (i
= 0; i
< cols
; i
++)
3359 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3361 continue; // skip it!
3363 if (x
< xpos
+ c
->GetWidth())
3368 xpos
+= c
->GetWidth();
3373 wxDataViewRenderer
*cell
= col
->GetRenderer();
3374 unsigned int current
= y
/ m_lineHeight
;
3375 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3377 // Unselect all if below the last row ?
3381 //Test whether the mouse is hovered on the tree item button
3383 if (GetOwner()->GetExpanderColumn() == col
)
3385 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3386 if( node
!=NULL
&& node
->HasChildren() )
3388 int indent
= node
->GetIndentLevel();
3389 indent
= GetOwner()->GetIndent()*indent
;
3390 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3391 if( rect
.Contains( x
, y
) )
3393 //So the mouse is over the expander
3395 if (m_underMouse
&& m_underMouse
!= node
)
3397 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3398 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3400 if (m_underMouse
!= node
)
3402 //wxLogMessage("Do the row: %d", current);
3403 RefreshRow(current
);
3405 m_underMouse
= node
;
3408 if (node
!=NULL
&& !node
->HasChildren())
3413 if (m_underMouse
!= NULL
)
3415 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3416 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3417 m_underMouse
= NULL
;
3421 wxDataViewModel
*model
= GetOwner()->GetModel();
3423 if (event
.Dragging())
3425 if (m_dragCount
== 0)
3427 // we have to report the raw, physical coords as we want to be
3428 // able to call HitTest(event.m_pointDrag) from the user code to
3429 // get the item being dragged
3430 m_dragStart
= event
.GetPosition();
3435 if (m_dragCount
!= 3)
3438 if (event
.LeftIsDown())
3440 // Notify cell about drag
3449 bool forceClick
= false;
3451 if (event
.ButtonDClick())
3453 m_renameTimer
->Stop();
3454 m_lastOnSame
= false;
3457 wxDataViewItem item
= GetItemByRow(current
);
3458 bool ignore_other_columns
=
3459 ((GetOwner()->GetExpanderColumn() != col
) &&
3460 (model
->IsContainer(item
)) &&
3461 (!model
->HasContainerColumns(item
)));
3463 if (event
.LeftDClick())
3465 if ( current
== m_lineLastClicked
)
3467 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3470 model
->GetValue( value
, item
, col
->GetModelColumn() );
3471 cell
->SetValue( value
);
3472 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3473 col
->GetWidth(), m_lineHeight
);
3474 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3479 wxWindow
*parent
= GetParent();
3480 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3482 le
.SetEventObject(parent
);
3483 le
.SetModel(GetOwner()->GetModel());
3485 parent
->GetEventHandler()->ProcessEvent(le
);
3491 // The first click was on another item, so don't interpret this as
3492 // a double click, but as a simple click instead
3499 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3501 // select single line
3502 SelectAllRows( false );
3503 SelectRow( m_lineSelectSingleOnUp
, true );
3506 //Process the event of user clicking the expander
3507 bool expander
= false;
3508 if (GetOwner()->GetExpanderColumn() == col
)
3510 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3511 if( node
!=NULL
&& node
->HasChildren() )
3513 int indent
= node
->GetIndentLevel();
3514 indent
= GetOwner()->GetIndent()*indent
;
3515 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3516 if( rect
.Contains( x
, y
) )
3519 if( node
->IsOpen() )
3520 OnCollapsing(current
);
3522 OnExpanding( current
);
3526 //If the user click the expander, we do not do editing even if the column with expander are editable
3527 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3529 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3530 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3532 m_renameTimer
->Start( 100, true );
3536 m_lastOnSame
= false;
3537 m_lineSelectSingleOnUp
= (unsigned int)-1;
3541 // This is necessary, because after a DnD operation in
3542 // from and to ourself, the up event is swallowed by the
3543 // DnD code. So on next non-up event (which means here and
3544 // now) m_lineSelectSingleOnUp should be reset.
3545 m_lineSelectSingleOnUp
= (unsigned int)-1;
3548 if (event
.RightDown())
3550 m_lineBeforeLastClicked
= m_lineLastClicked
;
3551 m_lineLastClicked
= current
;
3553 // If the item is already selected, do not update the selection.
3554 // Multi-selections should not be cleared if a selected item is clicked.
3555 if (!IsRowSelected(current
))
3557 SelectAllRows(false);
3558 ChangeCurrentRow(current
);
3559 SelectRow(m_currentRow
,true);
3560 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3563 // notify cell about right click
3566 // Allow generation of context menu event
3569 else if (event
.MiddleDown())
3571 // notify cell about middle click
3574 if (event
.LeftDown() || forceClick
)
3578 m_lineBeforeLastClicked
= m_lineLastClicked
;
3579 m_lineLastClicked
= current
;
3581 unsigned int oldCurrentRow
= m_currentRow
;
3582 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3584 bool cmdModifierDown
= event
.CmdDown();
3585 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3587 if ( IsSingleSel() || !IsRowSelected(current
) )
3589 SelectAllRows( false );
3590 ChangeCurrentRow(current
);
3591 SelectRow(m_currentRow
,true);
3592 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3594 else // multi sel & current is highlighted & no mod keys
3596 m_lineSelectSingleOnUp
= current
;
3597 ChangeCurrentRow(current
); // change focus
3600 else // multi sel & either ctrl or shift is down
3602 if (cmdModifierDown
)
3604 ChangeCurrentRow(current
);
3605 ReverseRowSelection(m_currentRow
);
3606 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3608 else if (event
.ShiftDown())
3610 ChangeCurrentRow(current
);
3612 unsigned int lineFrom
= oldCurrentRow
,
3615 if ( lineTo
< lineFrom
)
3618 lineFrom
= m_currentRow
;
3621 SelectRows(lineFrom
, lineTo
, true);
3622 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3624 else // !ctrl, !shift
3626 // test in the enclosing if should make it impossible
3627 wxFAIL_MSG( _T("how did we get here?") );
3631 if (m_currentRow
!= oldCurrentRow
)
3632 RefreshRow( oldCurrentRow
);
3634 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3636 // Update selection here...
3639 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3640 (current
== oldCurrentRow
)) && oldWasSelected
;
3644 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3648 if (HasCurrentRow())
3654 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3658 if (HasCurrentRow())
3664 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3666 if( m_selection
.GetCount() != 1 )
3667 return wxDataViewItem();
3669 return GetItemByRow( m_selection
.Item(0));
3672 //-----------------------------------------------------------------------------
3674 //-----------------------------------------------------------------------------
3675 WX_DEFINE_LIST(wxDataViewColumnList
);
3677 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3679 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3680 EVT_SIZE(wxDataViewCtrl::OnSize
)
3683 wxDataViewCtrl::~wxDataViewCtrl()
3686 GetModel()->RemoveNotifier( m_notifier
);
3689 void wxDataViewCtrl::Init()
3694 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3695 const wxPoint
& pos
, const wxSize
& size
,
3696 long style
, const wxValidator
& validator
)
3698 if (!wxControl::Create( parent
, id
, pos
, size
,
3699 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3702 SetInitialSize(size
);
3707 MacSetClipChildren( true ) ;
3710 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3712 if (HasFlag(wxDV_NO_HEADER
))
3713 m_headerArea
= NULL
;
3715 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3717 SetTargetWindow( m_clientArea
);
3719 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3721 sizer
->Add( m_headerArea
, 0, wxGROW
);
3722 sizer
->Add( m_clientArea
, 1, wxGROW
);
3729 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3733 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3736 // we need to process arrows ourselves for scrolling
3737 if ( nMsg
== WM_GETDLGCODE
)
3739 rc
|= DLGC_WANTARROWS
;
3747 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3749 // We need to override OnSize so that our scrolled
3750 // window a) does call Layout() to use sizers for
3751 // positioning the controls but b) does not query
3752 // the sizer for their size and use that for setting
3753 // the scrollable area as set that ourselves by
3754 // calling SetScrollbar() further down.
3761 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3763 if (!wxDataViewCtrlBase::AssociateModel( model
))
3766 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3768 model
->AddNotifier( m_notifier
);
3770 m_clientArea
->BuildTree(model
);
3772 m_clientArea
->UpdateDisplay();
3777 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3779 if (!wxDataViewCtrlBase::AppendColumn(col
))
3782 m_cols
.Append( col
);
3787 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3789 if (!wxDataViewCtrlBase::PrependColumn(col
))
3792 m_cols
.Insert( col
);
3797 void wxDataViewCtrl::OnColumnChange()
3800 m_headerArea
->UpdateDisplay();
3802 m_clientArea
->UpdateDisplay();
3805 void wxDataViewCtrl::DoSetExpanderColumn()
3807 m_clientArea
->UpdateDisplay();
3810 void wxDataViewCtrl::DoSetIndent()
3812 m_clientArea
->UpdateDisplay();
3815 unsigned int wxDataViewCtrl::GetColumnCount() const
3817 return m_cols
.GetCount();
3820 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3822 wxDataViewColumnList::const_iterator iter
;
3824 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3829 if ((*iter
)->IsHidden())
3836 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3838 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3849 bool wxDataViewCtrl::ClearColumns()
3856 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3858 int ret
= 0, dead
= 0;
3859 int len
= GetColumnCount();
3860 for (int i
=0; i
<len
; i
++)
3862 wxDataViewColumn
* col
= GetColumn(i
);
3863 if (col
->IsHidden())
3865 ret
+= col
->GetWidth();
3868 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3875 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3880 //Selection code with wxDataViewItem as parameters
3881 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3883 return m_clientArea
->GetSelection();
3886 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3889 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3890 int len
= selection
.GetCount();
3891 for( int i
= 0; i
< len
; i
++)
3893 unsigned int row
= selection
[i
];
3894 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3899 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3901 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3902 int len
= sel
.GetCount();
3903 for( int i
= 0; i
< len
; i
++ )
3905 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3907 selection
.Add( static_cast<unsigned int>(row
) );
3909 m_clientArea
->SetSelections( selection
);
3912 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3914 int row
= m_clientArea
->GetRowByItem( item
);
3917 //Unselect all rows before select another in the single select mode
3918 if (m_clientArea
->IsSingleSel())
3919 m_clientArea
->SelectAllRows(false);
3920 m_clientArea
->SelectRow(row
, true);
3924 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3926 int row
= m_clientArea
->GetRowByItem( item
);
3928 m_clientArea
->SelectRow(row
, false);
3931 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3933 int row
= m_clientArea
->GetRowByItem( item
);
3936 return m_clientArea
->IsRowSelected(row
);
3941 //Selection code with row number as parameter
3942 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3945 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3946 int len
= selection
.GetCount();
3947 for( int i
= 0; i
< len
; i
++)
3949 unsigned int row
= selection
[i
];
3955 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3957 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3958 int len
= sel
.GetCount();
3959 for( int i
= 0; i
< len
; i
++ )
3963 selection
.Add( static_cast<unsigned int>(row
) );
3965 m_clientArea
->SetSelections( selection
);
3968 void wxDataViewCtrl::Select( int row
)
3972 if (m_clientArea
->IsSingleSel())
3973 m_clientArea
->SelectAllRows(false);
3974 m_clientArea
->SelectRow( row
, true );
3978 void wxDataViewCtrl::Unselect( int row
)
3981 m_clientArea
->SelectRow(row
, false);
3984 bool wxDataViewCtrl::IsSelected( int row
) const
3987 return m_clientArea
->IsRowSelected(row
);
3991 void wxDataViewCtrl::SelectRange( int from
, int to
)
3994 for( int i
= from
; i
< to
; i
++ )
3996 m_clientArea
->Select(sel
);
3999 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4001 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4002 for( int i
= from
; i
< to
; i
++ )
4003 if( sel
.Index( i
) != wxNOT_FOUND
)
4005 m_clientArea
->SetSelections(sel
);
4008 void wxDataViewCtrl::SelectAll()
4010 m_clientArea
->SelectAllRows(true);
4013 void wxDataViewCtrl::UnselectAll()
4015 m_clientArea
->SelectAllRows(false);
4018 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4022 if( row
> (int) m_clientArea
->GetRowCount() )
4023 row
= m_clientArea
->GetRowCount();
4025 int first
= m_clientArea
->GetFirstVisibleRow();
4026 int last
= m_clientArea
->GetLastVisibleRow();
4028 m_clientArea
->ScrollTo( row
, column
);
4029 else if( row
> last
)
4030 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4032 m_clientArea
->ScrollTo( first
, column
);
4035 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4037 int row
= m_clientArea
->GetRowByItem(item
);
4040 if( column
== NULL
)
4041 EnsureVisible(row
, -1);
4045 int len
= GetColumnCount();
4046 for( int i
= 0; i
< len
; i
++ )
4047 if( GetColumn(i
) == column
)
4052 EnsureVisible( row
, col
);
4058 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4060 m_clientArea
->HitTest(point
, item
, column
);
4063 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4065 return m_clientArea
->GetItemRect(item
, column
);
4068 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4070 return m_clientArea
->GetItemByRow( row
);
4073 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4075 return m_clientArea
->GetRowByItem( item
);
4078 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4080 int row
= m_clientArea
->GetRowByItem( item
);
4082 m_clientArea
->Expand(row
);
4085 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4087 int row
= m_clientArea
->GetRowByItem( item
);
4089 m_clientArea
->Collapse(row
);
4093 // !wxUSE_GENERICDATAVIEWCTRL
4096 // wxUSE_DATAVIEWCTRL