1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the cell padding on the top/bottom
60 static const int PADDING_TOPBOTTOM
= 1;
62 // the expander space margin
63 static const int EXPANDER_MARGIN
= 4;
65 //-----------------------------------------------------------------------------
66 // wxDataViewHeaderWindow
67 //-----------------------------------------------------------------------------
69 #define USE_NATIVE_HEADER_WINDOW 0
71 //Below is the compare stuff
72 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
73 static wxDataViewModel
* g_model
;
74 static int g_column
= -2;
75 static bool g_asending
= true;
77 // NB: for some reason, this class must be dllexport'ed or we get warnings from
79 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
82 wxDataViewHeaderWindowBase()
85 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
86 const wxPoint
&pos
, const wxSize
&size
,
89 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
92 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
93 wxDataViewCtrl
*GetOwner() { return m_owner
; }
95 // called on column addition/removal
96 virtual void UpdateDisplay() { /* by default, do nothing */ }
98 // returns the n-th column
99 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
102 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
109 wxDataViewCtrl
*m_owner
;
111 // sends an event generated from the n-th wxDataViewColumn
112 void SendEvent(wxEventType type
, unsigned int n
);
115 // on wxMSW the header window (only that part however) can be made native!
116 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
118 #define COLUMN_WIDTH_OFFSET 2
119 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
121 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
125 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
127 const wxPoint
&pos
= wxDefaultPosition
,
128 const wxSize
&size
= wxDefaultSize
,
129 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
131 Create(parent
, id
, pos
, size
, name
);
134 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
135 const wxPoint
&pos
, const wxSize
&size
,
136 const wxString
&name
);
138 ~wxDataViewHeaderWindowMSW();
140 // called when any column setting is changed and/or changed
142 virtual void UpdateDisplay();
144 // called when the main window gets scrolled
145 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
148 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
149 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
151 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
153 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
154 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
157 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
160 #else // !defined(__WXMSW__)
162 #define HEADER_WINDOW_HEIGHT 25
163 #define HEADER_HORIZ_BORDER 5
164 #define HEADER_VERT_BORDER 3
165 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
167 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
170 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
172 const wxPoint
&pos
= wxDefaultPosition
,
173 const wxSize
&size
= wxDefaultSize
,
174 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
177 Create(parent
, id
, pos
, size
, name
);
180 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
181 const wxPoint
&pos
, const wxSize
&size
,
182 const wxString
&name
);
184 ~wxGenericDataViewHeaderWindow()
186 delete m_resizeCursor
;
189 virtual void UpdateDisplay() { Refresh(); }
193 void OnPaint( wxPaintEvent
&event
);
194 void OnMouse( wxMouseEvent
&event
);
195 void OnSetFocus( wxFocusEvent
&event
);
200 // vars used for column resizing:
202 wxCursor
*m_resizeCursor
;
203 const wxCursor
*m_currentCursor
;
206 bool m_dirty
; // needs refresh?
207 int m_hover
; // index of the column under the mouse
208 int m_column
; // index of the column being resized
209 int m_currentX
; // divider line position in logical (unscrolled) coords
210 int m_minX
; // minimal position beyond which the divider line
211 // can't be dragged in logical coords
213 // the pen used to draw the current column width drag line
214 // when resizing the columsn
218 // internal utilities:
222 m_currentCursor
= (wxCursor
*) NULL
;
223 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
225 m_isDragging
= false;
228 m_hover
= wxNOT_FOUND
;
229 m_column
= wxNOT_FOUND
;
233 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
234 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
238 void AdjustDC(wxDC
& dc
);
241 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
242 DECLARE_EVENT_TABLE()
245 #endif // defined(__WXMSW__)
247 //-----------------------------------------------------------------------------
248 // wxDataViewRenameTimer
249 //-----------------------------------------------------------------------------
251 class wxDataViewRenameTimer
: public wxTimer
254 wxDataViewMainWindow
*m_owner
;
257 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
261 //-----------------------------------------------------------------------------
262 // wxDataViewTreeNode
263 //-----------------------------------------------------------------------------
264 class wxDataViewTreeNode
;
265 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
266 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
268 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
269 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
271 class wxDataViewTreeNode
274 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
275 { this->parent
= parent
;
283 //I don't know what I need to do in the destructure
284 ~wxDataViewTreeNode()
289 wxDataViewTreeNode
* GetParent() { return parent
; }
290 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
291 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
292 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
294 void AddNode( wxDataViewTreeNode
* node
)
296 leaves
.Add( node
->GetItem().GetID() );
298 leaves
.Sort( &wxGenericTreeModelItemCmp
);
301 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
303 void AddLeaf( void * leaf
)
307 leaves
.Sort( &wxGenericTreeModelItemCmp
);
310 wxDataViewItem
& GetItem() { return item
; }
311 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
313 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
314 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
318 wxDataViewTreeNode
* node
= this;
319 while( node
->GetParent()->GetParent() != NULL
)
321 node
= node
->GetParent();
334 int len
= nodes
.GetCount();
336 for ( int i
= 0 ;i
< len
; i
++)
337 sum
+= nodes
[i
]->GetSubTreeCount();
339 sum
+= leaves
.GetCount();
342 ChangeSubTreeCount(-sum
);
348 ChangeSubTreeCount(sum
);
351 bool HasChildren() { return hasChildren
; }
352 void SetHasChildren( bool has
){ hasChildren
= has
; }
354 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
355 int GetSubTreeCount() { return subTreeCount
; }
356 void ChangeSubTreeCount( int num
)
362 parent
->ChangeSubTreeCount(num
);
369 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
370 int len
= nodes
.GetCount();
371 for (int i
= 0; i
< len
; i
++)
375 leaves
.Sort( &wxGenericTreeModelItemCmp
);
380 wxDataViewTreeNode
* parent
;
381 wxDataViewTreeNodes nodes
;
382 wxDataViewTreeLeaves leaves
;
389 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
391 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
394 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
396 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
401 //-----------------------------------------------------------------------------
402 // wxDataViewMainWindow
403 //-----------------------------------------------------------------------------
405 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
407 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
408 WX_DEFINE_LIST(ItemList
);
410 class wxDataViewMainWindow
: public wxWindow
413 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
415 const wxPoint
&pos
= wxDefaultPosition
,
416 const wxSize
&size
= wxDefaultSize
,
417 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
418 virtual ~wxDataViewMainWindow();
420 // notifications from wxDataViewModel
421 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
422 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
423 bool ItemChanged( const wxDataViewItem
&item
);
424 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
435 g_model
= GetOwner()->GetModel();
436 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
439 if (g_model
->HasDefaultCompare())
447 g_column
= col
->GetModelColumn();
448 g_asending
= col
->IsSortOrderAscending();
451 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
452 wxDataViewCtrl
*GetOwner() { return m_owner
; }
453 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
455 void OnPaint( wxPaintEvent
&event
);
456 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
457 void OnChar( wxKeyEvent
&event
);
458 void OnMouse( wxMouseEvent
&event
);
459 void OnSetFocus( wxFocusEvent
&event
);
460 void OnKillFocus( wxFocusEvent
&event
);
462 void UpdateDisplay();
463 void RecalculateDisplay();
464 void OnInternalIdle();
466 void OnRenameTimer();
468 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
469 void ScrollTo( int rows
, int column
);
471 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
472 void ChangeCurrentRow( unsigned int row
);
474 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
475 bool IsEmpty() { return GetRowCount() == 0; }
477 int GetCountPerPage() const;
478 int GetEndOfLastCol() const;
479 unsigned int GetFirstVisibleRow() const;
480 //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
481 unsigned int GetLastVisibleRow();
482 unsigned int GetRowCount() ;
484 wxDataViewItem
GetSelection() const;
485 wxDataViewSelection
GetSelections(){ return m_selection
; }
486 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
487 void Select( const wxArrayInt
& aSelections
);
488 void SelectAllRows( bool on
);
489 void SelectRow( unsigned int row
, bool on
);
490 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
491 void ReverseRowSelection( unsigned int row
);
492 bool IsRowSelected( unsigned int row
);
493 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
495 void RefreshRow( unsigned int row
);
496 void RefreshRows( unsigned int from
, unsigned int to
);
497 void RefreshRowsAfter( unsigned int firstRow
);
499 // returns the colour to be used for drawing the rules
500 wxColour
GetRuleColour() const
502 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
505 //void EnsureVisible( unsigned int row );
506 wxRect
GetLineRect( unsigned int row
) const;
508 //Some useful functions for row and item mapping
509 wxDataViewItem
GetItemByRow( unsigned int row
) const;
510 int GetRowByItem( const wxDataViewItem
& item
);
512 //Methods for building the mapping tree
513 void BuildTree( wxDataViewModel
* model
);
515 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
516 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
518 void Expand( unsigned int row
) { OnExpanding( row
); }
519 void Collapse( unsigned int row
) { OnCollapsing( row
); }
521 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
522 //We did not need this temporarily
523 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
525 int RecalculateCount() ;
527 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
528 void OnExpanding( unsigned int row
);
529 void OnCollapsing( unsigned int row
);
531 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
534 wxDataViewCtrl
*m_owner
;
538 wxDataViewColumn
*m_currentCol
;
539 unsigned int m_currentRow
;
540 wxDataViewSelection m_selection
;
542 wxDataViewRenameTimer
*m_renameTimer
;
550 // for double click logic
551 unsigned int m_lineLastClicked
,
552 m_lineBeforeLastClicked
,
553 m_lineSelectSingleOnUp
;
555 // the pen used to draw horiz/vertical rules
558 // the pen used to draw the expander and the lines
561 //This is the tree structure of the model
562 wxDataViewTreeNode
* m_root
;
564 //This is the tree node under the cursor
565 wxDataViewTreeNode
* m_underMouse
;
567 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
568 DECLARE_EVENT_TABLE()
571 // ---------------------------------------------------------
572 // wxGenericDataViewModelNotifier
573 // ---------------------------------------------------------
575 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
578 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
579 { m_mainWindow
= mainWindow
; }
581 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
582 { return m_mainWindow
->ItemAdded( parent
, item
); }
583 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
584 { return m_mainWindow
->ItemDeleted( parent
, item
); }
585 virtual bool ItemChanged( const wxDataViewItem
& item
)
586 { return m_mainWindow
->ItemChanged(item
); }
587 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
588 { return m_mainWindow
->ValueChanged( item
, col
); }
589 virtual bool Cleared()
590 { return m_mainWindow
->Cleared(); }
591 virtual void Resort()
592 { m_mainWindow
->Resort(); }
594 wxDataViewMainWindow
*m_mainWindow
;
597 // ---------------------------------------------------------
598 // wxDataViewRenderer
599 // ---------------------------------------------------------
601 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
603 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
604 wxDataViewCellMode mode
,
606 wxDataViewRendererBase( varianttype
, mode
, align
)
613 wxDataViewRenderer::~wxDataViewRenderer()
619 wxDC
*wxDataViewRenderer::GetDC()
623 if (GetOwner() == NULL
)
625 if (GetOwner()->GetOwner() == NULL
)
627 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
633 // ---------------------------------------------------------
634 // wxDataViewCustomRenderer
635 // ---------------------------------------------------------
637 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
639 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
640 wxDataViewCellMode mode
, int align
) :
641 wxDataViewRenderer( varianttype
, mode
, align
)
645 // ---------------------------------------------------------
646 // wxDataViewTextRenderer
647 // ---------------------------------------------------------
649 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
651 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
652 wxDataViewCellMode mode
, int align
) :
653 wxDataViewCustomRenderer( varianttype
, mode
, align
)
657 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
659 m_text
= value
.GetString();
664 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
669 bool wxDataViewTextRenderer::HasEditorCtrl()
674 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
675 wxRect labelRect
, const wxVariant
&value
)
677 return new wxTextCtrl( parent
, wxID_ANY
, value
,
678 wxPoint(labelRect
.x
,labelRect
.y
),
679 wxSize(labelRect
.width
,labelRect
.height
) );
682 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
684 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
685 value
= text
->GetValue();
689 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
691 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
692 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
693 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
694 view
->GetForegroundColour();
696 dc
->SetTextForeground(col
);
697 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
702 wxSize
wxDataViewTextRenderer::GetSize() const
704 const wxDataViewCtrl
*view
= GetView();
708 view
->GetTextExtent( m_text
, &x
, &y
);
709 return wxSize( x
, y
);
711 return wxSize(80,20);
714 // ---------------------------------------------------------
715 // wxDataViewBitmapRenderer
716 // ---------------------------------------------------------
718 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
720 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
721 wxDataViewCellMode mode
, int align
) :
722 wxDataViewCustomRenderer( varianttype
, mode
, align
)
726 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
728 if (value
.GetType() == wxT("wxBitmap"))
730 if (value
.GetType() == wxT("wxIcon"))
736 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
741 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
744 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
745 else if (m_icon
.Ok())
746 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
751 wxSize
wxDataViewBitmapRenderer::GetSize() const
754 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
755 else if (m_icon
.Ok())
756 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
758 return wxSize(16,16);
761 // ---------------------------------------------------------
762 // wxDataViewToggleRenderer
763 // ---------------------------------------------------------
765 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
767 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
768 wxDataViewCellMode mode
, int align
) :
769 wxDataViewCustomRenderer( varianttype
, mode
, align
)
774 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
776 m_toggle
= value
.GetBool();
781 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
786 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
788 // User wxRenderer here
791 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
793 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
798 flags
|= wxCONTROL_CHECKED
;
799 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
800 flags
|= wxCONTROL_DISABLED
;
802 wxRendererNative::Get().DrawCheckBox(
803 GetOwner()->GetOwner(),
811 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
812 wxDataViewModel
*model
,
813 const wxDataViewItem
& item
, unsigned int col
)
815 bool value
= !m_toggle
;
816 wxVariant variant
= value
;
817 model
->SetValue( variant
, item
, col
);
818 model
->ValueChanged( item
, col
);
822 wxSize
wxDataViewToggleRenderer::GetSize() const
824 return wxSize(20,20);
827 // ---------------------------------------------------------
828 // wxDataViewProgressRenderer
829 // ---------------------------------------------------------
831 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
833 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
834 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
835 wxDataViewCustomRenderer( varianttype
, mode
, align
)
841 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
845 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
847 m_value
= (long) value
;
849 if (m_value
< 0) m_value
= 0;
850 if (m_value
> 100) m_value
= 100;
855 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
857 value
= (long) m_value
;
861 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
863 double pct
= (double)m_value
/ 100.0;
865 bar
.width
= (int)(cell
.width
* pct
);
866 dc
->SetPen( *wxTRANSPARENT_PEN
);
867 dc
->SetBrush( *wxBLUE_BRUSH
);
868 dc
->DrawRectangle( bar
);
870 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
871 dc
->SetPen( *wxBLACK_PEN
);
872 dc
->DrawRectangle( cell
);
877 wxSize
wxDataViewProgressRenderer::GetSize() const
879 return wxSize(40,12);
882 // ---------------------------------------------------------
883 // wxDataViewDateRenderer
884 // ---------------------------------------------------------
886 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
888 #if wxUSE_DATE_RENDERER_POPUP
890 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
893 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
894 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
895 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
900 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
901 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
902 sizer
->Add( m_cal
, 1, wxGROW
);
907 void OnCalendar( wxCalendarEvent
&event
);
909 wxCalendarCtrl
*m_cal
;
910 wxDataViewModel
*m_model
;
912 const wxDataViewItem
& m_item
;
915 virtual void OnDismiss()
920 DECLARE_EVENT_TABLE()
923 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
924 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
927 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
929 wxDateTime date
= event
.GetDate();
930 wxVariant value
= date
;
931 m_model
->SetValue( value
, m_item
, m_col
);
932 m_model
->ValueChanged( m_item
, m_col
);
936 #endif // wxUSE_DATE_RENDERER_POPUP
938 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
940 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
941 wxDataViewCellMode mode
, int align
) :
942 wxDataViewCustomRenderer( varianttype
, mode
, align
)
946 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
948 m_date
= value
.GetDateTime();
953 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
959 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
961 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
962 wxString tmp
= m_date
.FormatDate();
963 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
968 wxSize
wxDataViewDateRenderer::GetSize() const
970 const wxDataViewCtrl
* view
= GetView();
971 wxString tmp
= m_date
.FormatDate();
973 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
974 return wxSize(x
,y
+d
);
977 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
978 const wxDataViewItem
& item
, unsigned int col
)
981 model
->GetValue( variant
, item
, col
);
982 wxDateTime value
= variant
.GetDateTime();
984 #if wxUSE_DATE_RENDERER_POPUP
985 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
986 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
987 wxPoint pos
= wxGetMousePosition();
990 popup
->Popup( popup
->m_cal
);
991 #else // !wxUSE_DATE_RENDERER_POPUP
992 wxMessageBox(value
.Format());
993 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
997 // ---------------------------------------------------------
998 // wxDataViewIconTextRenderer
999 // ---------------------------------------------------------
1001 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1003 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1004 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1005 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1008 SetAlignment(align
);
1011 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1015 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1021 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1026 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1028 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1030 dc
->SetFont( view
->GetFont() );
1032 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1033 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1034 view
->GetForegroundColour();
1036 dc
->SetTextForeground(col
);
1038 const wxIcon
&icon
= m_value
.GetIcon();
1041 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1042 cell
.x
+= icon
.GetWidth()+4;
1045 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1050 wxSize
wxDataViewIconTextRenderer::GetSize() const
1052 return wxSize(80,16); // TODO
1055 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1060 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1065 // ---------------------------------------------------------
1067 // ---------------------------------------------------------
1069 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1071 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1072 unsigned int model_column
,
1073 int width
, wxAlignment align
, int flags
) :
1074 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1076 SetAlignment(align
);
1080 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1083 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1084 unsigned int model_column
,
1085 int width
, wxAlignment align
, int flags
) :
1086 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1088 SetAlignment(align
);
1091 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1094 wxDataViewColumn::~wxDataViewColumn()
1098 void wxDataViewColumn::Init( int width
)
1101 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1105 void wxDataViewColumn::SetResizeable( bool resizeable
)
1108 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1110 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1113 void wxDataViewColumn::SetHidden( bool hidden
)
1116 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1118 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1120 // tell our owner to e.g. update its scrollbars:
1122 GetOwner()->OnColumnChange();
1125 void wxDataViewColumn::SetSortable( bool sortable
)
1128 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1130 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1132 // Update header button
1134 GetOwner()->OnColumnChange();
1137 void wxDataViewColumn::SetSortOrder( bool ascending
)
1139 m_ascending
= ascending
;
1141 // Update header button
1143 GetOwner()->OnColumnChange();
1146 bool wxDataViewColumn::IsSortOrderAscending() const
1151 void wxDataViewColumn::SetInternalWidth( int width
)
1155 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1156 if (m_owner
&& m_owner
->m_clientArea
)
1157 m_owner
->m_clientArea
->RecalculateDisplay();
1160 void wxDataViewColumn::SetWidth( int width
)
1162 m_owner
->m_headerArea
->UpdateDisplay();
1164 SetInternalWidth(width
);
1168 //-----------------------------------------------------------------------------
1169 // wxDataViewHeaderWindowBase
1170 //-----------------------------------------------------------------------------
1172 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1174 wxWindow
*parent
= GetParent();
1175 wxDataViewEvent
le(type
, parent
->GetId());
1177 le
.SetEventObject(parent
);
1179 le
.SetDataViewColumn(GetColumn(n
));
1180 le
.SetModel(GetOwner()->GetModel());
1182 // for events created by wxDataViewHeaderWindow the
1183 // row / value fields are not valid
1185 parent
->GetEventHandler()->ProcessEvent(le
);
1188 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1190 // implemented in msw/listctrl.cpp:
1191 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1193 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1195 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1196 const wxPoint
&pos
, const wxSize
&size
,
1197 const wxString
&name
)
1201 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1204 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1205 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1206 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1207 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1209 // create the native WC_HEADER window:
1210 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1211 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1212 m_hWnd
= CreateWindowEx(0,
1223 wxLogLastError(_T("CreateWindowEx"));
1227 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1228 // to call wxDataViewHeaderWindow::MSWOnNotify
1229 SubclassWin(m_hWnd
);
1231 // the following is required to get the default win's font for
1232 // header windows and must be done befor sending the HDM_LAYOUT msg
1239 // Retrieve the bounding rectangle of the parent window's
1240 // client area, and then request size and position values
1241 // from the header control.
1242 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1244 hdl
.prc
= &rcParent
;
1246 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1248 wxLogLastError(_T("SendMessage"));
1252 // Set the size, position, and visibility of the header control.
1253 SetWindowPos((HWND
)m_hWnd
,
1257 wp
.flags
| SWP_SHOWWINDOW
);
1259 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1260 // a wxBoxSizer and in order to avoid super-big header windows,
1261 // we need to set our height as fixed
1262 SetMinSize(wxSize(-1, wp
.cy
));
1263 SetMaxSize(wxSize(-1, wp
.cy
));
1268 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1273 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1275 // remove old columns
1276 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1277 Header_DeleteItem((HWND
)m_hWnd
, 0);
1279 // add the updated array of columns to the header control
1280 unsigned int cols
= GetOwner()->GetColumnCount();
1281 unsigned int added
= 0;
1282 wxDataViewModel
* model
= GetOwner()->GetModel();
1283 for (unsigned int i
= 0; i
< cols
; i
++)
1285 wxDataViewColumn
*col
= GetColumn( i
);
1286 if (col
->IsHidden())
1287 continue; // don't add it!
1290 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1291 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1292 hdi
.cxy
= col
->GetWidth();
1293 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1294 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1295 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1298 if(model
&& m_owner
->GetSortingColumn() == col
)
1300 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1301 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1302 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1306 // lParam is reserved for application's use:
1307 // we store there the column index to use it later in MSWOnNotify
1308 // (since columns may have been hidden)
1309 hdi
.lParam
= (LPARAM
)i
;
1311 // the native wxMSW implementation of the header window
1312 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1313 // on the right: to correct this effect we make the column
1314 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1316 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1318 switch (col
->GetAlignment())
1321 hdi
.fmt
|= HDF_LEFT
;
1323 case wxALIGN_CENTER
:
1324 case wxALIGN_CENTER_HORIZONTAL
:
1325 hdi
.fmt
|= HDF_CENTER
;
1328 hdi
.fmt
|= HDF_RIGHT
;
1332 // such alignment is not allowed for the column header!
1336 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1337 (WPARAM
)added
, (LPARAM
)&hdi
);
1342 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1346 // NOTE: we don't just return nmHDR->iItem because when there are
1347 // hidden columns, nmHDR->iItem may be different from
1348 // nmHDR->pitem->lParam
1350 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1352 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1357 item
.mask
= HDI_LPARAM
;
1358 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1360 return (unsigned int)item
.lParam
;
1363 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1365 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1367 // is it a message from the header?
1368 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1369 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1371 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1372 switch ( nmhdr
->code
)
1374 case HDN_BEGINTRACK
:
1375 // user has started to resize a column:
1376 // do we need to veto it?
1377 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1385 // user has started to reorder a column
1388 case HDN_ITEMCHANGING
:
1389 if (nmHDR
->pitem
!= NULL
&&
1390 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1392 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1393 if (nmHDR
->pitem
->cxy
< minWidth
)
1395 // do not allow the user to resize this column under
1396 // its minimal width:
1402 case HDN_ITEMCHANGED
: // user is resizing a column
1403 case HDN_ENDTRACK
: // user has finished resizing a column
1404 case HDN_ENDDRAG
: // user has finished reordering a column
1406 // update the width of the modified column:
1407 if (nmHDR
->pitem
!= NULL
&&
1408 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1410 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1411 unsigned int w
= nmHDR
->pitem
->cxy
;
1412 wxDataViewColumn
*col
= GetColumn(idx
);
1414 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1415 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1416 w
-= COLUMN_WIDTH_OFFSET
;
1418 if (w
>= (unsigned)col
->GetMinWidth())
1419 col
->SetInternalWidth(w
);
1425 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1426 wxDataViewModel
* model
= GetOwner()->GetModel();
1428 if(nmHDR
->iButton
== 0)
1430 wxDataViewColumn
*col
= GetColumn(idx
);
1431 if(col
->IsSortable())
1433 if(model
&& m_owner
->GetSortingColumn() == col
)
1435 bool order
= col
->IsSortOrderAscending();
1436 col
->SetSortOrder(!order
);
1440 m_owner
->SetSortingColumn(col
);
1448 wxEventType evt
= nmHDR
->iButton
== 0 ?
1449 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1450 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1451 SendEvent(evt
, idx
);
1457 // NOTE: for some reason (i.e. for a bug in Windows)
1458 // the HDN_ITEMCLICK notification is not sent on
1459 // right clicks, so we need to handle NM_RCLICK
1462 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1463 if (column
!= wxNOT_FOUND
)
1466 item
.mask
= HDI_LPARAM
;
1467 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1469 // 'idx' may be different from 'column' if there are
1470 // hidden columns...
1471 unsigned int idx
= (unsigned int)item
.lParam
;
1472 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1478 case HDN_GETDISPINFOW
:
1479 // see wxListCtrl::MSWOnNotify for more info!
1482 case HDN_ITEMDBLCLICK
:
1484 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1485 int w
= GetOwner()->GetBestColumnWidth(idx
);
1487 // update the native control:
1489 ZeroMemory(&hd
, sizeof(hd
));
1490 hd
.mask
= HDI_WIDTH
;
1492 Header_SetItem(GetHwnd(),
1493 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1496 // update the wxDataViewColumn class:
1497 GetColumn(idx
)->SetInternalWidth(w
);
1502 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1508 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1509 const wxRect
*WXUNUSED(rect
))
1511 wxSize ourSz
= GetClientSize();
1512 wxSize ownerSz
= m_owner
->GetClientSize();
1514 // where should the (logical) origin of this window be placed?
1516 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1518 // put this window on top of our parent and
1519 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1520 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1524 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1525 int WXUNUSED(w
), int WXUNUSED(h
),
1528 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1529 // the wxDataViewCtrl window gets resized: the following dummy call
1530 // to ScrollWindow() is required in order to get this header window
1531 // correctly repainted when it's (horizontally) scrolled:
1536 #else // !defined(__WXMSW__)
1538 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1539 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1540 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1541 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1542 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1545 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1546 const wxPoint
&pos
, const wxSize
&size
,
1547 const wxString
&name
)
1551 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1554 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1555 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1556 SetOwnForegroundColour( attr
.colFg
);
1557 SetOwnBackgroundColour( attr
.colBg
);
1559 SetOwnFont( attr
.font
);
1561 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1562 // a wxBoxSizer and in order to avoid super-big header windows,
1563 // we need to set our height as fixed
1564 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1565 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1570 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1573 GetClientSize( &w
, &h
);
1575 wxAutoBufferedPaintDC
dc( this );
1577 dc
.SetBackground(GetBackgroundColour());
1581 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1584 m_owner
->GetViewStart( &x
, NULL
);
1586 // account for the horz scrollbar offset
1587 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1589 dc
.SetFont( GetFont() );
1591 unsigned int cols
= GetOwner()->GetColumnCount();
1594 for (i
= 0; i
< cols
; i
++)
1596 wxDataViewColumn
*col
= GetColumn( i
);
1597 if (col
->IsHidden())
1598 continue; // skip it!
1600 int cw
= col
->GetWidth();
1603 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1604 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1606 if (col
->IsSortOrderAscending())
1607 sortArrow
= wxHDR_SORT_ICON_UP
;
1609 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1613 if (m_parent
->IsEnabled())
1616 state
= wxCONTROL_CURRENT
;
1620 state
= (int) wxCONTROL_DISABLED
;
1623 wxRendererNative::Get().DrawHeaderButton
1627 wxRect(xpos
, 0, cw
, ch
-1),
1632 // align as required the column title:
1634 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1635 switch (col
->GetAlignment())
1638 x
+= HEADER_HORIZ_BORDER
;
1640 case wxALIGN_CENTER
:
1641 case wxALIGN_CENTER_HORIZONTAL
:
1642 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1645 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1649 // always center the title vertically:
1650 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1652 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1654 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1655 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1656 dc
.DrawText( col
->GetTitle(), x
, y
);
1657 dc
.DestroyClippingRegion();
1663 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1665 GetParent()->SetFocus();
1669 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1671 // we want to work with logical coords
1673 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1674 int y
= event
.GetY();
1678 // we don't draw the line beyond our window,
1679 // but we allow dragging it there
1681 GetClientSize( &w
, NULL
);
1682 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1685 if (event
.ButtonUp())
1687 m_isDragging
= false;
1693 m_currentX
= wxMax(m_minX
+ 7, x
);
1697 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1699 GetOwner()->Refresh();
1703 else // not dragging
1706 m_column
= wxNOT_FOUND
;
1708 bool hit_border
= false;
1710 // end of the current column
1713 // find the column where this event occured
1714 int countCol
= m_owner
->GetColumnCount();
1715 for (int column
= 0; column
< countCol
; column
++)
1717 wxDataViewColumn
*p
= GetColumn(column
);
1720 continue; // skip if not shown
1722 xpos
+= p
->GetWidth();
1724 if ((abs(x
-xpos
) < 3) && (y
< 22))
1732 // inside the column
1739 int old_hover
= m_hover
;
1741 if (event
.Leaving())
1742 m_hover
= wxNOT_FOUND
;
1743 if (old_hover
!= m_hover
)
1746 if (m_column
== wxNOT_FOUND
)
1749 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1750 if (event
.LeftDClick() && resizeable
)
1752 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1755 else if (event
.LeftDown() || event
.RightUp())
1757 if (hit_border
&& event
.LeftDown() && resizeable
)
1759 m_isDragging
= true;
1763 else // click on a column
1765 wxDataViewModel
* model
= GetOwner()->GetModel();
1766 wxEventType evt
= event
.LeftDown() ?
1767 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1768 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1769 SendEvent(evt
, m_column
);
1771 //Left click the header
1772 if(event
.LeftDown())
1774 wxDataViewColumn
*col
= GetColumn(m_column
);
1775 if(col
->IsSortable())
1777 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1778 if(model
&& sortCol
== col
)
1780 bool order
= col
->IsSortOrderAscending();
1781 col
->SetSortOrder(!order
);
1785 m_owner
->SetSortingColumn(col
);
1791 //Send the column sorted event
1792 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1796 else if (event
.Moving())
1798 if (hit_border
&& resizeable
)
1799 m_currentCursor
= m_resizeCursor
;
1801 m_currentCursor
= wxSTANDARD_CURSOR
;
1803 SetCursor(*m_currentCursor
);
1808 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1809 void wxGenericDataViewHeaderWindow::DrawCurrent()
1812 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1814 int x1
= m_currentX
;
1816 ClientToScreen (&x1
, &y1
);
1818 int x2
= m_currentX
-1;
1820 ++x2
; // but why ????
1823 m_owner
->GetClientSize( NULL
, &y2
);
1824 m_owner
->ClientToScreen( &x2
, &y2
);
1827 dc
.SetLogicalFunction(wxINVERT
);
1828 dc
.SetPen(m_penCurrent
);
1829 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1831 dc
.DrawLine(x1
, y1
, x2
, y2
);
1835 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1839 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1840 m_owner
->GetViewStart( &x
, NULL
);
1842 // shift the DC origin to match the position of the main window horizontal
1843 // scrollbar: this allows us to always use logical coords
1844 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1847 #endif // defined(__WXMSW__)
1849 //-----------------------------------------------------------------------------
1850 // wxDataViewRenameTimer
1851 //-----------------------------------------------------------------------------
1853 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1858 void wxDataViewRenameTimer::Notify()
1860 m_owner
->OnRenameTimer();
1863 //-----------------------------------------------------------------------------
1864 // wxDataViewMainWindow
1865 //-----------------------------------------------------------------------------
1867 //The tree building helper, declared firstly
1868 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1870 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1872 if (row1
> row2
) return 1;
1873 if (row1
== row2
) return 0;
1878 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1880 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1881 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1882 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1883 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1884 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1885 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1888 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1889 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1890 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1891 m_selection( wxDataViewSelectionCmp
)
1896 m_lastOnSame
= false;
1897 m_renameTimer
= new wxDataViewRenameTimer( this );
1899 // TODO: user better initial values/nothing selected
1900 m_currentCol
= NULL
;
1903 // TODO: we need to calculate this smartly
1910 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1913 m_dragStart
= wxPoint(0,0);
1914 m_lineLastClicked
= (unsigned int) -1;
1915 m_lineBeforeLastClicked
= (unsigned int) -1;
1916 m_lineSelectSingleOnUp
= (unsigned int) -1;
1920 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1921 SetBackgroundColour( *wxWHITE
);
1923 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1925 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1926 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1927 //Some new added code to deal with the tree structure
1928 m_root
= new wxDataViewTreeNode( NULL
);
1929 m_root
->SetHasChildren(true);
1931 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1933 m_underMouse
= NULL
;
1937 wxDataViewMainWindow::~wxDataViewMainWindow()
1940 delete m_renameTimer
;
1943 void wxDataViewMainWindow::OnRenameTimer()
1945 // We have to call this here because changes may just have
1946 // been made and no screen update taken place.
1951 unsigned int cols
= GetOwner()->GetColumnCount();
1953 for (i
= 0; i
< cols
; i
++)
1955 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1957 continue; // skip it!
1959 if (c
== m_currentCol
)
1961 xpos
+= c
->GetWidth();
1963 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1964 m_currentCol
->GetWidth(), m_lineHeight
);
1966 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1967 &labelRect
.x
, &labelRect
.y
);
1969 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1970 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1974 //------------------------------------------------------------------
1975 // Helper class for do operation on the tree node
1976 //------------------------------------------------------------------
1983 //The return value control how the tree-walker tranverse the tree
1984 // 0: Job done, stop tranverse and return
1985 // 1: Ignore the current node's subtree and continue
1986 // 2: Job not done, continue
1987 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1988 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1989 virtual int operator() ( void * n
) = 0;
1992 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1997 switch( func( node
) )
2008 wxDataViewTreeNodes nodes
= node
->GetNodes();
2009 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2011 int len_nodes
= nodes
.GetCount();
2012 int len
= leaves
.GetCount();
2013 int i
= 0, nodes_i
= 0;
2015 for( ; i
< len
; i
++ )
2017 void * n
= leaves
[i
];
2018 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2020 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2023 if( Walker( nd
, func
) )
2042 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2046 wxDataViewTreeNode
* node
;
2047 node
= FindNode(parent
);
2052 node
->SetHasChildren( true );
2054 if( g_model
->IsContainer( item
) )
2056 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2057 newnode
->SetItem(item
);
2058 newnode
->SetHasChildren( true );
2059 node
->AddNode( newnode
);
2062 node
->AddLeaf( item
.GetID() );
2064 node
->ChangeSubTreeCount(1);
2072 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2074 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2075 const wxDataViewItem
& item
)
2077 wxDataViewTreeNode
* node
= FindNode(parent
);
2079 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2080 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2083 node
->GetChildren().Remove( item
.GetID() );
2084 //Manuplate selection
2085 if( m_selection
.GetCount() > 1 )
2087 m_selection
.Empty();
2089 bool isContainer
= false;
2090 wxDataViewTreeNodes nds
= node
->GetNodes();
2091 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2093 if (nds
[i
]->GetItem() == item
)
2101 wxDataViewTreeNode
* n
= NULL
;
2102 wxDataViewTreeNodes nodes
= node
->GetNodes();
2103 int len
= nodes
.GetCount();
2104 for( int i
= 0 ; i
< len
; i
++)
2106 if( nodes
[i
]->GetItem() == item
)
2113 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2115 node
->GetNodes().Remove( n
);
2116 sub
-= n
->GetSubTreeCount();
2117 DestroyTreeHelper(n
);
2119 //Make the row number invalid and get a new valid one when user call GetRowCount
2121 node
->ChangeSubTreeCount(sub
);
2122 if( node
->GetChildrenNumber() == 0)
2124 node
->GetParent()->GetNodes().Remove( node
);
2128 //Change the current row to the last row if the current exceed the max row number
2129 if( m_currentRow
> GetRowCount() )
2130 m_currentRow
= m_count
- 1;
2137 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2143 wxWindow
*parent
= GetParent();
2144 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2145 le
.SetEventObject(parent
);
2146 le
.SetModel(GetOwner()->GetModel());
2148 parent
->GetEventHandler()->ProcessEvent(le
);
2153 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2155 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2156 /*#define MAX_VIRTUAL_WIDTH 100000
2158 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2159 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2160 Refresh( true, &rect );
2168 wxWindow
*parent
= GetParent();
2169 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2170 le
.SetEventObject(parent
);
2171 le
.SetModel(GetOwner()->GetModel());
2174 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2175 parent
->GetEventHandler()->ProcessEvent(le
);
2180 bool wxDataViewMainWindow::Cleared()
2190 void wxDataViewMainWindow::UpdateDisplay()
2195 void wxDataViewMainWindow::OnInternalIdle()
2197 wxWindow::OnInternalIdle();
2201 RecalculateDisplay();
2206 void wxDataViewMainWindow::RecalculateDisplay()
2208 wxDataViewModel
*model
= GetOwner()->GetModel();
2215 int width
= GetEndOfLastCol();
2216 int height
= GetRowCount() * m_lineHeight
;
2218 SetVirtualSize( width
, height
);
2219 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2224 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2226 wxWindow::ScrollWindow( dx
, dy
, rect
);
2228 if (GetOwner()->m_headerArea
)
2229 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2232 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2235 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2236 int sy
= rows
*m_lineHeight
/y
;
2240 wxRect rect
= GetClientRect();
2241 unsigned int colnum
= 0;
2242 unsigned int x_start
= 0, x_end
= 0, w
= 0;
2244 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2245 for (x_start
= 0; colnum
< column
; colnum
++)
2247 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2248 if (col
->IsHidden())
2249 continue; // skip it!
2251 w
= col
->GetWidth();
2255 x_end
= x_start
+ w
;
2256 xe
= xx
+ rect
.width
;
2259 sx
= ( xx
+ x_end
- xe
)/x
;
2266 m_owner
->Scroll( sx
, sy
);
2269 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2271 wxDataViewModel
*model
= GetOwner()->GetModel();
2272 wxAutoBufferedPaintDC
dc( this );
2275 dc
.SetBackground(GetBackgroundColour());
2277 GetOwner()->PrepareDC( dc
);
2278 dc
.SetFont( GetFont() );
2280 wxRect update
= GetUpdateRegion().GetBox();
2281 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2283 // compute which items needs to be redrawn
2284 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2285 unsigned int item_count
=
2286 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2287 (int)(GetRowCount( )- item_start
) );
2288 unsigned int item_last
= item_start
+ item_count
;
2290 // compute which columns needs to be redrawn
2291 unsigned int cols
= GetOwner()->GetColumnCount();
2292 unsigned int col_start
= 0;
2293 unsigned int x_start
= 0;
2294 for (x_start
= 0; col_start
< cols
; col_start
++)
2296 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2297 if (col
->IsHidden())
2298 continue; // skip it!
2300 unsigned int w
= col
->GetWidth();
2301 if (x_start
+w
>= (unsigned int)update
.x
)
2307 unsigned int col_last
= col_start
;
2308 unsigned int x_last
= x_start
;
2309 for (; col_last
< cols
; col_last
++)
2311 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2312 if (col
->IsHidden())
2313 continue; // skip it!
2315 if (x_last
> (unsigned int)update
.GetRight())
2318 x_last
+= col
->GetWidth();
2321 // Draw horizontal rules if required
2322 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2324 dc
.SetPen(m_penRule
);
2325 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2327 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2329 int y
= i
* m_lineHeight
;
2330 dc
.DrawLine(x_start
, y
, x_last
, y
);
2334 // Draw vertical rules if required
2335 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2337 dc
.SetPen(m_penRule
);
2338 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2341 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2343 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2344 if (col
->IsHidden())
2345 continue; // skip it
2347 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2348 x
, item_last
* m_lineHeight
);
2350 x
+= col
->GetWidth();
2353 // Draw last vertical rule
2354 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2355 x
, item_last
* m_lineHeight
);
2358 // redraw the background for the items which are selected/current
2359 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2361 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2362 if (selected
|| item
== m_currentRow
)
2364 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2365 if (item
== m_currentRow
)
2366 flags
|= wxCONTROL_CURRENT
;
2368 flags
|= wxCONTROL_FOCUSED
;
2370 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2371 wxRendererNative::Get().DrawItemSelectionRect
2381 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2384 // TODO: last column for RTL support
2385 expander
= GetOwner()->GetColumn( 0 );
2386 GetOwner()->SetExpanderColumn(expander
);
2389 // redraw all cells for all rows which must be repainted and for all columns
2391 cell_rect
.x
= x_start
;
2392 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2393 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2395 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2396 wxDataViewRenderer
*cell
= col
->GetRenderer();
2397 cell_rect
.width
= col
->GetWidth();
2399 if (col
->IsHidden())
2400 continue; // skipt it!
2403 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2405 // get the cell value and set it into the renderer
2407 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2413 wxDataViewItem dataitem
= node
->GetItem();
2414 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2415 cell
->SetValue( value
);
2417 // update the y offset
2418 cell_rect
.y
= item
* m_lineHeight
;
2420 //Draw the expander here.
2421 int indent
= node
->GetIndentLevel();
2422 if( col
== expander
)
2424 //Calculate the indent first
2425 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2427 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2428 // change the cell_rect.x to the appropriate pos
2429 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2430 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2431 dc
.SetPen( m_penExpander
);
2432 dc
.SetBrush( wxNullBrush
);
2433 if( node
->HasChildren() )
2435 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2437 if (m_underMouse
== node
)
2439 flag
|= wxCONTROL_CURRENT
;
2441 if( node
->IsOpen() )
2442 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2444 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2448 // I am wandering whether we should draw dot lines between tree nodes
2450 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2453 //force the expander column to left-center align
2454 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2458 // cannot be bigger than allocated space
2459 wxSize size
= cell
->GetSize();
2460 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2461 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2462 size
.y
= wxMin( size
.y
+ 1*PADDING_TOPBOTTOM
, cell_rect
.height
);
2464 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2465 int align
= cell
->GetAlignment();
2467 // horizontal alignment:
2468 item_rect
.x
= cell_rect
.x
;
2469 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2470 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2471 else if (align
& wxALIGN_RIGHT
)
2472 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2473 //else: wxALIGN_LEFT is the default
2475 // vertical alignment:
2476 item_rect
.y
= cell_rect
.y
;
2477 if (align
& wxALIGN_CENTER_VERTICAL
)
2478 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2479 else if (align
& wxALIGN_BOTTOM
)
2480 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2481 //else: wxALIGN_TOP is the default
2484 item_rect
.x
+= PADDING_RIGHTLEFT
;
2485 //item_rect.y += PADDING_TOPBOTTOM;
2486 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2487 item_rect
.height
= size
.y
- 1 * PADDING_TOPBOTTOM
;
2489 //Here we add the tree indent
2490 item_rect
.x
+= indent
;
2493 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2494 state
|= wxDATAVIEW_CELL_SELECTED
;
2496 // TODO: it would be much more efficient to create a clipping
2497 // region for the entire column being rendered (in the OnPaint
2498 // of wxDataViewMainWindow) instead of a single clip region for
2499 // each cell. However it would mean that each renderer should
2500 // respect the given wxRect's top & bottom coords, eventually
2501 // violating only the left & right coords - however the user can
2502 // make its own renderer and thus we cannot be sure of that.
2503 dc
.SetClippingRegion( item_rect
);
2504 cell
->Render( item_rect
, &dc
, state
);
2505 dc
.DestroyClippingRegion();
2508 cell_rect
.x
+= cell_rect
.width
;
2512 int wxDataViewMainWindow::GetCountPerPage() const
2514 wxSize size
= GetClientSize();
2515 return size
.y
/ m_lineHeight
;
2518 int wxDataViewMainWindow::GetEndOfLastCol() const
2522 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2524 const wxDataViewColumn
*c
=
2525 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2528 width
+= c
->GetWidth();
2533 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2537 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2539 return y
/ m_lineHeight
;
2542 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2544 wxSize client_size
= GetClientSize();
2545 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2546 &client_size
.x
, &client_size
.y
);
2548 //we should deal with the pixel here
2549 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2550 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2553 return wxMin( GetRowCount()-1, row
);
2556 unsigned int wxDataViewMainWindow::GetRowCount()
2558 if ( m_count
== -1 )
2560 m_count
= RecalculateCount();
2562 GetVirtualSize( &width
, &height
);
2563 height
= m_count
* m_lineHeight
;
2565 SetVirtualSize( width
, height
);
2570 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2577 void wxDataViewMainWindow::SelectAllRows( bool on
)
2584 m_selection
.Clear();
2585 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2586 m_selection
.Add( i
);
2591 unsigned int first_visible
= GetFirstVisibleRow();
2592 unsigned int last_visible
= GetLastVisibleRow();
2594 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2596 unsigned int row
= m_selection
[i
];
2597 if ((row
>= first_visible
) && (row
<= last_visible
))
2600 m_selection
.Clear();
2604 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2606 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2610 m_selection
.Add( row
);
2618 m_selection
.Remove( row
);
2624 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2628 unsigned int tmp
= from
;
2634 for (i
= from
; i
<= to
; i
++)
2636 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2639 m_selection
.Add( i
);
2644 m_selection
.Remove( i
);
2647 RefreshRows( from
, to
);
2650 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2652 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2654 int n
= aSelections
[i
];
2656 m_selection
.Add( n
);
2661 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2663 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2664 m_selection
.Add( row
);
2666 m_selection
.Remove( row
);
2670 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2672 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2675 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2677 wxWindow
*parent
= GetParent();
2678 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2680 le
.SetEventObject(parent
);
2681 le
.SetModel(GetOwner()->GetModel());
2684 parent
->GetEventHandler()->ProcessEvent(le
);
2687 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2689 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2690 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2692 wxSize client_size
= GetClientSize();
2693 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2694 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2695 if (intersect_rect
.width
> 0)
2696 Refresh( true, &intersect_rect
);
2699 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2703 unsigned int tmp
= to
;
2708 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2709 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2711 wxSize client_size
= GetClientSize();
2712 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2713 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2714 if (intersect_rect
.width
> 0)
2715 Refresh( true, &intersect_rect
);
2718 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2720 unsigned int count
= GetRowCount();
2721 if (firstRow
> count
)
2724 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2725 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2727 wxSize client_size
= GetClientSize();
2728 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2729 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2730 if (intersect_rect
.width
> 0)
2731 Refresh( true, &intersect_rect
);
2734 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2736 wxCHECK_RET( newCurrent
< GetRowCount(),
2737 _T("invalid item index in OnArrowChar()") );
2739 // if there is no selection, we cannot move it anywhere
2740 if (!HasCurrentRow())
2743 unsigned int oldCurrent
= m_currentRow
;
2745 // in single selection we just ignore Shift as we can't select several
2747 if ( event
.ShiftDown() && !IsSingleSel() )
2749 RefreshRow( oldCurrent
);
2751 ChangeCurrentRow( newCurrent
);
2753 // select all the items between the old and the new one
2754 if ( oldCurrent
> newCurrent
)
2756 newCurrent
= oldCurrent
;
2757 oldCurrent
= m_currentRow
;
2760 SelectRows( oldCurrent
, newCurrent
, true );
2761 if (oldCurrent
!=newCurrent
)
2762 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2766 RefreshRow( oldCurrent
);
2768 // all previously selected items are unselected unless ctrl is held
2769 if ( !event
.ControlDown() )
2770 SelectAllRows(false);
2772 ChangeCurrentRow( newCurrent
);
2774 if ( !event
.ControlDown() )
2776 SelectRow( m_currentRow
, true );
2777 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2780 RefreshRow( m_currentRow
);
2783 //EnsureVisible( m_currentRow );
2786 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2790 rect
.y
= m_lineHeight
* row
;
2791 rect
.width
= GetEndOfLastCol();
2792 rect
.height
= m_lineHeight
;
2797 class RowToItemJob
: public DoJob
2800 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2801 virtual ~RowToItemJob(){};
2803 virtual int operator() ( wxDataViewTreeNode
* node
)
2806 if( current
== static_cast<int>(row
))
2808 ret
= node
->GetItem() ;
2812 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2814 current
+= node
->GetSubTreeCount();
2819 //If the current has no child node, we can find the desired item of the row number directly.
2820 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2821 if( node
->GetNodes().GetCount() == 0)
2823 int index
= static_cast<int>(row
) - current
- 1;
2824 ret
= node
->GetChildren().Item( index
);
2831 virtual int operator() ( void * n
)
2834 if( current
== static_cast<int>(row
))
2836 ret
= wxDataViewItem( n
) ;
2841 wxDataViewItem
GetResult(){ return ret
; }
2848 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2850 RowToItemJob
job( row
, -2 );
2851 Walker( m_root
, job
);
2852 return job
.GetResult();
2855 class RowToTreeNodeJob
: public DoJob
2858 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2861 this->current
= current
;
2865 virtual ~RowToTreeNodeJob(){};
2867 virtual int operator() ( wxDataViewTreeNode
* node
)
2870 if( current
== static_cast<int>(row
))
2876 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2878 current
+= node
->GetSubTreeCount();
2884 //If the current has no child node, we can find the desired item of the row number directly.
2885 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2886 if( node
->GetNodes().GetCount() == 0)
2888 int index
= static_cast<int>(row
) - current
- 1;
2889 void * n
= node
->GetChildren().Item( index
);
2890 ret
= new wxDataViewTreeNode( parent
) ;
2891 ret
->SetItem( wxDataViewItem( n
));
2892 ret
->SetHasChildren(false);
2901 virtual int operator() ( void * n
)
2904 if( current
== static_cast<int>(row
))
2906 ret
= new wxDataViewTreeNode( parent
) ;
2907 ret
->SetItem( wxDataViewItem( n
));
2908 ret
->SetHasChildren(false);
2914 wxDataViewTreeNode
* GetResult(){ return ret
; }
2918 wxDataViewTreeNode
* ret
;
2919 wxDataViewTreeNode
* parent
;
2923 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2925 RowToTreeNodeJob
job( row
, -2, m_root
);
2926 Walker( m_root
, job
);
2927 return job
.GetResult();
2930 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2932 wxWindow
*parent
= GetParent();
2933 wxDataViewEvent
le(type
, parent
->GetId());
2935 le
.SetEventObject(parent
);
2936 le
.SetModel(GetOwner()->GetModel());
2939 parent
->GetEventHandler()->ProcessEvent(le
);
2943 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2945 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2948 if( node
->HasChildren())
2950 if( !node
->IsOpen())
2952 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2953 //Check if the user prevent expanding
2954 if( e
.GetSkipped() )
2958 //Here I build the children of current node
2959 if( node
->GetChildrenNumber() == 0 )
2962 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2966 //Send the expanded event
2967 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2971 SelectRow( row
, false );
2972 SelectRow( row
+ 1, true );
2973 ChangeCurrentRow( row
+ 1 );
2974 SendSelectionChangedEvent( GetItemByRow(row
+1));
2982 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2984 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2987 wxDataViewTreeNode
* nd
= node
;
2989 if( node
->HasChildren() && node
->IsOpen() )
2991 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2992 if( e
.GetSkipped() )
2997 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3001 node
= node
->GetParent();
3004 int parent
= GetRowByItem( node
->GetItem() ) ;
3007 SelectRow( row
, false);
3008 SelectRow(parent
, true );
3009 ChangeCurrentRow( parent
);
3010 SendSelectionChangedEvent( node
->GetItem() );
3014 if( !nd
->HasChildren())
3019 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3021 wxDataViewModel
* model
= GetOwner()->GetModel();
3025 //Compose the a parent-chain of the finding item
3027 list
.DeleteContents( true );
3028 wxDataViewItem
it( item
);
3031 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3032 list
.Insert( pItem
);
3033 it
= model
->GetParent( it
);
3036 //Find the item along the parent-chain.
3037 //This algorithm is designed to speed up the node-finding method
3038 wxDataViewTreeNode
* node
= m_root
;
3039 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3041 if( node
->HasChildren() )
3043 if( node
->GetChildrenNumber() == 0 )
3046 BuildTreeHelper(model
, node
->GetItem(), node
);
3049 wxDataViewTreeNodes nodes
= node
->GetNodes();
3051 for (; i
< nodes
.GetCount(); i
++)
3053 if (nodes
[i
]->GetItem() == (**iter
))
3059 if (i
== nodes
.GetCount())
3068 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3070 wxDataViewColumn
*col
= NULL
;
3071 unsigned int cols
= GetOwner()->GetColumnCount();
3072 unsigned int colnum
= 0;
3073 unsigned int x_start
= 0;
3075 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3076 for (x_start
= 0; colnum
< cols
; colnum
++)
3078 col
= GetOwner()->GetColumn(colnum
);
3079 if (col
->IsHidden())
3080 continue; // skip it!
3082 unsigned int w
= col
->GetWidth();
3083 if (x_start
+w
>= (unsigned int)x
)
3090 item
= GetItemByRow( y
/m_lineHeight
);
3093 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3095 int row
= GetRowByItem(item
);
3096 int y
= row
*m_lineHeight
;
3097 int h
= m_lineHeight
;
3099 wxDataViewColumn
*col
= NULL
;
3100 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3102 col
= GetOwner()->GetColumn( i
);
3103 x
+= col
->GetWidth();
3104 if( GetOwner()->GetColumn(i
+1) == column
)
3107 int w
= col
->GetWidth();
3108 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3109 return wxRect(x
, y
, w
, h
);
3112 int wxDataViewMainWindow::RecalculateCount()
3114 return m_root
->GetSubTreeCount();
3117 class ItemToRowJob
: public DoJob
3120 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3121 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3122 virtual ~ItemToRowJob(){};
3124 //Maybe binary search will help to speed up this process
3125 virtual int operator() ( wxDataViewTreeNode
* node
)
3128 if( node
->GetItem() == item
)
3133 if( node
->GetItem() == **m_iter
)
3140 ret
+= node
->GetSubTreeCount();
3146 virtual int operator() ( void * n
)
3149 if( n
== item
.GetID() )
3153 //the row number is begin from zero
3154 int GetResult(){ return ret
-1 ; }
3156 ItemList::const_iterator m_iter
;
3157 wxDataViewItem item
;
3162 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3164 wxDataViewModel
* model
= GetOwner()->GetModel();
3171 //Compose the a parent-chain of the finding item
3173 wxDataViewItem
* pItem
= NULL
;
3174 list
.DeleteContents( true );
3175 wxDataViewItem
it( item
);
3178 pItem
= new wxDataViewItem( it
);
3179 list
.Insert( pItem
);
3180 it
= model
->GetParent( it
);
3182 pItem
= new wxDataViewItem( );
3183 list
.Insert( pItem
);
3185 ItemToRowJob
job( item
, list
.begin() );
3186 Walker(m_root
, job
);
3187 return job
.GetResult();
3190 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3192 if( !model
->IsContainer( item
) )
3195 wxDataViewItemArray children
;
3196 unsigned int num
= model
->GetChildren( item
, children
);
3198 while( index
< num
)
3200 if( model
->IsContainer( children
[index
] ) )
3202 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3203 n
->SetItem(children
[index
]);
3204 n
->SetHasChildren( true ) ;
3209 node
->AddLeaf( children
[index
].GetID() );
3213 node
->SetSubTreeCount( num
);
3214 wxDataViewTreeNode
* n
= node
->GetParent();
3216 n
->ChangeSubTreeCount(num
);
3220 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3222 //First we define a invalid item to fetch the top-level elements
3223 wxDataViewItem item
;
3225 BuildTreeHelper( model
, item
, m_root
);
3229 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3231 if( node
->GetNodeNumber() != 0 )
3233 int len
= node
->GetNodeNumber();
3235 wxDataViewTreeNodes nodes
= node
->GetNodes();
3236 for( ; i
< len
; i
++ )
3238 DestroyTreeHelper(nodes
[i
]);
3244 void wxDataViewMainWindow::DestroyTree()
3246 DestroyTreeHelper(m_root
);
3247 m_root
->SetSubTreeCount(0);
3251 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3253 if (event
.GetKeyCode() == WXK_TAB
)
3255 wxNavigationKeyEvent nevent
;
3256 nevent
.SetWindowChange( event
.ControlDown() );
3257 nevent
.SetDirection( !event
.ShiftDown() );
3258 nevent
.SetEventObject( GetParent()->GetParent() );
3259 nevent
.SetCurrentFocus( m_parent
);
3260 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3264 // no item -> nothing to do
3265 if (!HasCurrentRow())
3271 // don't use m_linesPerPage directly as it might not be computed yet
3272 const int pageSize
= GetCountPerPage();
3273 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3275 switch ( event
.GetKeyCode() )
3278 if ( m_currentRow
> 0 )
3279 OnArrowChar( m_currentRow
- 1, event
);
3283 if ( m_currentRow
< GetRowCount() - 1 )
3284 OnArrowChar( m_currentRow
+ 1, event
);
3286 //Add the process for tree expanding/collapsing
3288 OnCollapsing(m_currentRow
);
3291 OnExpanding( m_currentRow
);
3295 OnArrowChar( GetRowCount() - 1, event
);
3300 OnArrowChar( 0, event
);
3305 int steps
= pageSize
- 1;
3306 int index
= m_currentRow
- steps
;
3310 OnArrowChar( index
, event
);
3316 int steps
= pageSize
- 1;
3317 unsigned int index
= m_currentRow
+ steps
;
3318 unsigned int count
= GetRowCount();
3319 if ( index
>= count
)
3322 OnArrowChar( index
, event
);
3331 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3333 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3335 // let the base handle mouse wheel events.
3340 int x
= event
.GetX();
3341 int y
= event
.GetY();
3342 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3343 wxDataViewColumn
*col
= NULL
;
3346 unsigned int cols
= GetOwner()->GetColumnCount();
3348 for (i
= 0; i
< cols
; i
++)
3350 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3352 continue; // skip it!
3354 if (x
< xpos
+ c
->GetWidth())
3359 xpos
+= c
->GetWidth();
3364 wxDataViewRenderer
*cell
= col
->GetRenderer();
3365 unsigned int current
= y
/ m_lineHeight
;
3366 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3368 // Unselect all if below the last row ?
3372 //Test whether the mouse is hovered on the tree item button
3374 if (GetOwner()->GetExpanderColumn() == col
)
3376 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3377 if( node
!=NULL
&& node
->HasChildren() )
3379 int indent
= node
->GetIndentLevel();
3380 indent
= GetOwner()->GetIndent()*indent
;
3381 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3382 if( rect
.Contains( x
, y
) )
3384 //So the mouse is over the expander
3386 if (m_underMouse
&& m_underMouse
!= node
)
3388 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3389 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3391 if (m_underMouse
!= node
)
3393 //wxLogMessage("Do the row: %d", current);
3396 m_underMouse
= node
;
3399 if (node
!=NULL
&& !node
->HasChildren())
3404 if (m_underMouse
!= NULL
)
3406 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3407 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3408 m_underMouse
= NULL
;
3412 wxDataViewModel
*model
= GetOwner()->GetModel();
3414 if (event
.Dragging())
3416 if (m_dragCount
== 0)
3418 // we have to report the raw, physical coords as we want to be
3419 // able to call HitTest(event.m_pointDrag) from the user code to
3420 // get the item being dragged
3421 m_dragStart
= event
.GetPosition();
3426 if (m_dragCount
!= 3)
3429 if (event
.LeftIsDown())
3431 // Notify cell about drag
3440 bool forceClick
= false;
3442 if (event
.ButtonDClick())
3444 m_renameTimer
->Stop();
3445 m_lastOnSame
= false;
3448 if (event
.LeftDClick())
3450 if ( current
== m_lineLastClicked
)
3452 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3454 wxDataViewItem item
= GetItemByRow(current
);
3456 model
->GetValue( value
, item
, col
->GetModelColumn() );
3457 cell
->SetValue( value
);
3458 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3459 col
->GetWidth(), m_lineHeight
);
3460 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3465 wxWindow
*parent
= GetParent();
3466 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3467 le
.SetItem( GetItemByRow(current
) );
3468 le
.SetEventObject(parent
);
3469 le
.SetModel(GetOwner()->GetModel());
3471 parent
->GetEventHandler()->ProcessEvent(le
);
3477 // The first click was on another item, so don't interpret this as
3478 // a double click, but as a simple click instead
3485 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3487 // select single line
3488 SelectAllRows( false );
3489 SelectRow( m_lineSelectSingleOnUp
, true );
3492 //Process the event of user clicking the expander
3493 bool expander
= false;
3494 if (GetOwner()->GetExpanderColumn() == col
)
3496 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3497 if( node
!=NULL
&& node
->HasChildren() )
3499 int indent
= node
->GetIndentLevel();
3500 indent
= GetOwner()->GetIndent()*indent
;
3501 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3502 if( rect
.Contains( x
, y
) )
3505 if( node
->IsOpen() )
3506 OnCollapsing(current
);
3508 OnExpanding( current
);
3512 //If the user click the expander, we do not do editing even if the column with expander are editable
3513 if (m_lastOnSame
&& !expander
)
3515 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3516 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3518 m_renameTimer
->Start( 100, true );
3522 m_lastOnSame
= false;
3523 m_lineSelectSingleOnUp
= (unsigned int)-1;
3527 // This is necessary, because after a DnD operation in
3528 // from and to ourself, the up event is swallowed by the
3529 // DnD code. So on next non-up event (which means here and
3530 // now) m_lineSelectSingleOnUp should be reset.
3531 m_lineSelectSingleOnUp
= (unsigned int)-1;
3534 if (event
.RightDown())
3536 m_lineBeforeLastClicked
= m_lineLastClicked
;
3537 m_lineLastClicked
= current
;
3539 // If the item is already selected, do not update the selection.
3540 // Multi-selections should not be cleared if a selected item is clicked.
3541 if (!IsRowSelected(current
))
3543 SelectAllRows(false);
3544 ChangeCurrentRow(current
);
3545 SelectRow(m_currentRow
,true);
3546 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3549 // notify cell about right click
3552 // Allow generation of context menu event
3555 else if (event
.MiddleDown())
3557 // notify cell about middle click
3560 if (event
.LeftDown() || forceClick
)
3564 m_lineBeforeLastClicked
= m_lineLastClicked
;
3565 m_lineLastClicked
= current
;
3567 unsigned int oldCurrentRow
= m_currentRow
;
3568 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3570 bool cmdModifierDown
= event
.CmdDown();
3571 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3573 if ( IsSingleSel() || !IsRowSelected(current
) )
3575 SelectAllRows( false );
3576 ChangeCurrentRow(current
);
3577 SelectRow(m_currentRow
,true);
3578 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3580 else // multi sel & current is highlighted & no mod keys
3582 m_lineSelectSingleOnUp
= current
;
3583 ChangeCurrentRow(current
); // change focus
3586 else // multi sel & either ctrl or shift is down
3588 if (cmdModifierDown
)
3590 ChangeCurrentRow(current
);
3591 ReverseRowSelection(m_currentRow
);
3592 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3594 else if (event
.ShiftDown())
3596 ChangeCurrentRow(current
);
3598 unsigned int lineFrom
= oldCurrentRow
,
3601 if ( lineTo
< lineFrom
)
3604 lineFrom
= m_currentRow
;
3607 SelectRows(lineFrom
, lineTo
, true);
3608 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3610 else // !ctrl, !shift
3612 // test in the enclosing if should make it impossible
3613 wxFAIL_MSG( _T("how did we get here?") );
3617 if (m_currentRow
!= oldCurrentRow
)
3618 RefreshRow( oldCurrentRow
);
3620 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3622 // Update selection here...
3625 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3626 (current
== oldCurrentRow
)) && oldWasSelected
;
3630 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3634 if (HasCurrentRow())
3640 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3644 if (HasCurrentRow())
3650 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3652 if( m_selection
.GetCount() != 1 )
3653 return wxDataViewItem();
3655 return GetItemByRow( m_selection
.Item(0));
3658 //-----------------------------------------------------------------------------
3660 //-----------------------------------------------------------------------------
3661 WX_DEFINE_LIST(wxDataViewColumnList
);
3663 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3665 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3666 EVT_SIZE(wxDataViewCtrl::OnSize
)
3669 wxDataViewCtrl::~wxDataViewCtrl()
3672 GetModel()->RemoveNotifier( m_notifier
);
3675 void wxDataViewCtrl::Init()
3680 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3681 const wxPoint
& pos
, const wxSize
& size
,
3682 long style
, const wxValidator
& validator
)
3684 if (!wxControl::Create( parent
, id
, pos
, size
,
3685 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3691 MacSetClipChildren( true ) ;
3694 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3696 if (HasFlag(wxDV_NO_HEADER
))
3697 m_headerArea
= NULL
;
3699 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3701 SetTargetWindow( m_clientArea
);
3703 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3705 sizer
->Add( m_headerArea
, 0, wxGROW
);
3706 sizer
->Add( m_clientArea
, 1, wxGROW
);
3713 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3717 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3720 // we need to process arrows ourselves for scrolling
3721 if ( nMsg
== WM_GETDLGCODE
)
3723 rc
|= DLGC_WANTARROWS
;
3731 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3733 // We need to override OnSize so that our scrolled
3734 // window a) does call Layout() to use sizers for
3735 // positioning the controls but b) does not query
3736 // the sizer for their size and use that for setting
3737 // the scrollable area as set that ourselves by
3738 // calling SetScrollbar() further down.
3745 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3747 if (!wxDataViewCtrlBase::AssociateModel( model
))
3750 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3752 model
->AddNotifier( m_notifier
);
3754 m_clientArea
->BuildTree(model
);
3756 m_clientArea
->UpdateDisplay();
3761 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3763 if (!wxDataViewCtrlBase::AppendColumn(col
))
3766 m_cols
.Append( col
);
3771 void wxDataViewCtrl::OnColumnChange()
3774 m_headerArea
->UpdateDisplay();
3776 m_clientArea
->UpdateDisplay();
3779 void wxDataViewCtrl::DoSetExpanderColumn()
3781 m_clientArea
->UpdateDisplay();
3784 void wxDataViewCtrl::DoSetIndent()
3786 m_clientArea
->UpdateDisplay();
3789 unsigned int wxDataViewCtrl::GetColumnCount() const
3791 return m_cols
.GetCount();
3794 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3796 wxDataViewColumnList::const_iterator iter
;
3798 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3803 if ((*iter
)->IsHidden())
3810 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3812 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3823 bool wxDataViewCtrl::ClearColumns()
3830 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3832 int ret
= 0, dead
= 0;
3833 int len
= GetColumnCount();
3834 for (int i
=0; i
<len
; i
++)
3836 wxDataViewColumn
* col
= GetColumn(i
);
3837 if (col
->IsHidden())
3839 ret
+= col
->GetWidth();
3842 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3849 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3854 //Selection code with wxDataViewItem as parameters
3855 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3857 return m_clientArea
->GetSelection();
3860 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3863 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3864 int len
= selection
.GetCount();
3865 for( int i
= 0; i
< len
; i
++)
3867 unsigned int row
= selection
[i
];
3868 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3873 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3875 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3876 int len
= sel
.GetCount();
3877 for( int i
= 0; i
< len
; i
++ )
3879 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3881 selection
.Add( static_cast<unsigned int>(row
) );
3883 m_clientArea
->SetSelections( selection
);
3886 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3888 int row
= m_clientArea
->GetRowByItem( item
);
3891 //Unselect all rows before select another in the single select mode
3892 if (m_clientArea
->IsSingleSel())
3893 m_clientArea
->SelectAllRows(false);
3894 m_clientArea
->SelectRow(row
, true);
3898 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3900 int row
= m_clientArea
->GetRowByItem( item
);
3902 m_clientArea
->SelectRow(row
, false);
3905 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3907 int row
= m_clientArea
->GetRowByItem( item
);
3910 return m_clientArea
->IsRowSelected(row
);
3915 //Selection code with row number as parameter
3916 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3919 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3920 int len
= selection
.GetCount();
3921 for( int i
= 0; i
< len
; i
++)
3923 unsigned int row
= selection
[i
];
3929 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3931 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3932 int len
= sel
.GetCount();
3933 for( int i
= 0; i
< len
; i
++ )
3937 selection
.Add( static_cast<unsigned int>(row
) );
3939 m_clientArea
->SetSelections( selection
);
3942 void wxDataViewCtrl::Select( int row
)
3946 if (m_clientArea
->IsSingleSel())
3947 m_clientArea
->SelectAllRows(false);
3948 m_clientArea
->SelectRow( row
, true );
3952 void wxDataViewCtrl::Unselect( int row
)
3955 m_clientArea
->SelectRow(row
, false);
3958 bool wxDataViewCtrl::IsSelected( int row
) const
3961 return m_clientArea
->IsRowSelected(row
);
3965 void wxDataViewCtrl::SelectRange( int from
, int to
)
3968 for( int i
= from
; i
< to
; i
++ )
3970 m_clientArea
->Select(sel
);
3973 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3975 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3976 for( int i
= from
; i
< to
; i
++ )
3977 if( sel
.Index( i
) != wxNOT_FOUND
)
3979 m_clientArea
->SetSelections(sel
);
3982 void wxDataViewCtrl::SelectAll()
3984 m_clientArea
->SelectAllRows(true);
3987 void wxDataViewCtrl::UnselectAll()
3989 m_clientArea
->SelectAllRows(false);
3992 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3996 if( row
> m_clientArea
->GetRowCount() )
3997 row
= m_clientArea
->GetRowCount();
3999 int first
= m_clientArea
->GetFirstVisibleRow();
4000 int last
= m_clientArea
->GetLastVisibleRow();
4002 m_clientArea
->ScrollTo( row
, column
);
4003 else if( row
> last
)
4004 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4006 m_clientArea
->ScrollTo( first
, column
);
4009 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4011 int row
= m_clientArea
->GetRowByItem(item
);
4014 if( column
== NULL
)
4015 EnsureVisible(row
, -1);
4019 int len
= GetColumnCount();
4020 for( int i
= 0; i
< len
; i
++ )
4021 if( GetColumn(i
) == column
)
4026 EnsureVisible( row
, col
);
4032 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4034 m_clientArea
->HitTest(point
, item
, column
);
4037 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4039 return m_clientArea
->GetItemRect(item
, column
);
4042 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4044 return m_clientArea
->GetItemByRow( row
);
4047 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4049 return m_clientArea
->GetRowByItem( item
);
4052 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4054 int row
= m_clientArea
->GetRowByItem( item
);
4056 m_clientArea
->Expand(row
);
4059 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4061 int row
= m_clientArea
->GetRowByItem( item
);
4063 m_clientArea
->Collapse(row
);
4067 // !wxUSE_GENERICDATAVIEWCTRL
4070 // wxUSE_DATAVIEWCTRL