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 0
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called when the main window gets scrolled
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
146 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
148 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
150 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
151 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
154 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
157 #else // !defined(__WXMSW__)
159 #define HEADER_WINDOW_HEIGHT 25
160 #define HEADER_HORIZ_BORDER 5
161 #define HEADER_VERT_BORDER 3
162 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
164 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
167 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
169 const wxPoint
&pos
= wxDefaultPosition
,
170 const wxSize
&size
= wxDefaultSize
,
171 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
174 Create(parent
, id
, pos
, size
, name
);
177 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
,
179 const wxString
&name
);
181 ~wxGenericDataViewHeaderWindow()
183 delete m_resizeCursor
;
186 virtual void UpdateDisplay() { Refresh(); }
190 void OnPaint( wxPaintEvent
&event
);
191 void OnMouse( wxMouseEvent
&event
);
192 void OnSetFocus( wxFocusEvent
&event
);
197 // vars used for column resizing:
199 wxCursor
*m_resizeCursor
;
200 const wxCursor
*m_currentCursor
;
203 bool m_dirty
; // needs refresh?
204 int m_hover
; // index of the column under the mouse
205 int m_column
; // index of the column being resized
206 int m_currentX
; // divider line position in logical (unscrolled) coords
207 int m_minX
; // minimal position beyond which the divider line
208 // can't be dragged in logical coords
210 // the pen used to draw the current column width drag line
211 // when resizing the columsn
215 // internal utilities:
219 m_currentCursor
= (wxCursor
*) NULL
;
220 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
222 m_isDragging
= false;
225 m_hover
= wxNOT_FOUND
;
226 m_column
= wxNOT_FOUND
;
230 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
231 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
234 void AdjustDC(wxDC
& dc
);
237 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
238 DECLARE_EVENT_TABLE()
241 #endif // defined(__WXMSW__)
243 //-----------------------------------------------------------------------------
244 // wxDataViewRenameTimer
245 //-----------------------------------------------------------------------------
247 class wxDataViewRenameTimer
: public wxTimer
250 wxDataViewMainWindow
*m_owner
;
253 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
257 //-----------------------------------------------------------------------------
258 // wxDataViewTreeNode
259 //-----------------------------------------------------------------------------
260 class wxDataViewTreeNode
;
261 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
262 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
264 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
265 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
267 class wxDataViewTreeNode
270 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
277 m_hasChildren
= false;
281 ~wxDataViewTreeNode()
285 wxDataViewTreeNode
* GetParent() { return m_parent
; }
286 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
287 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
288 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
290 void AddNode( wxDataViewTreeNode
* node
)
292 m_leaves
.Add( node
->GetItem().GetID() );
294 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
297 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
299 void AddLeaf( void * leaf
)
301 m_leaves
.Add( leaf
);
303 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
306 wxDataViewItem
& GetItem() { return m_item
; }
307 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
309 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
310 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
314 wxDataViewTreeNode
* node
= this;
315 while( node
->GetParent()->GetParent() != NULL
)
317 node
= node
->GetParent();
330 int len
= m_nodes
.GetCount();
332 for ( int i
= 0 ;i
< len
; i
++)
333 sum
+= m_nodes
[i
]->GetSubTreeCount();
335 sum
+= m_leaves
.GetCount();
338 ChangeSubTreeCount(-sum
);
344 ChangeSubTreeCount(sum
);
347 bool HasChildren() { return m_hasChildren
; }
348 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
350 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
351 int GetSubTreeCount() { return m_subTreeCount
; }
352 void ChangeSubTreeCount( int num
)
356 m_subTreeCount
+= num
;
358 m_parent
->ChangeSubTreeCount(num
);
365 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
366 int len
= m_nodes
.GetCount();
367 for (int i
= 0; i
< len
; i
++)
368 m_nodes
[i
]->Resort();
369 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
374 wxDataViewTreeNode
*m_parent
;
375 wxDataViewTreeNodes m_nodes
;
376 wxDataViewTreeLeaves m_leaves
;
377 wxDataViewItem m_item
;
383 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
385 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
388 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
390 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
395 //-----------------------------------------------------------------------------
396 // wxDataViewMainWindow
397 //-----------------------------------------------------------------------------
399 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
401 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
402 WX_DEFINE_LIST(ItemList
);
404 class wxDataViewMainWindow
: public wxWindow
407 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
409 const wxPoint
&pos
= wxDefaultPosition
,
410 const wxSize
&size
= wxDefaultSize
,
411 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
412 virtual ~wxDataViewMainWindow();
414 // notifications from wxDataViewModel
415 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
416 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
417 bool ItemChanged( const wxDataViewItem
&item
);
418 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
429 g_model
= GetOwner()->GetModel();
430 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
433 if (g_model
->HasDefaultCompare())
441 g_column
= col
->GetModelColumn();
442 g_asending
= col
->IsSortOrderAscending();
445 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
446 wxDataViewCtrl
*GetOwner() { return m_owner
; }
447 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
449 void OnPaint( wxPaintEvent
&event
);
450 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
451 void OnChar( wxKeyEvent
&event
);
452 void OnMouse( wxMouseEvent
&event
);
453 void OnSetFocus( wxFocusEvent
&event
);
454 void OnKillFocus( wxFocusEvent
&event
);
456 void UpdateDisplay();
457 void RecalculateDisplay();
458 void OnInternalIdle();
460 void OnRenameTimer();
462 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
463 void ScrollTo( int rows
, int column
);
465 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
466 void ChangeCurrentRow( unsigned int row
);
468 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
469 bool IsEmpty() { return GetRowCount() == 0; }
471 int GetCountPerPage() const;
472 int GetEndOfLastCol() const;
473 unsigned int GetFirstVisibleRow() const;
474 //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
475 unsigned int GetLastVisibleRow();
476 unsigned int GetRowCount() ;
478 wxDataViewItem
GetSelection() const;
479 wxDataViewSelection
GetSelections(){ return m_selection
; }
480 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
481 void Select( const wxArrayInt
& aSelections
);
482 void SelectAllRows( bool on
);
483 void SelectRow( unsigned int row
, bool on
);
484 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
485 void ReverseRowSelection( unsigned int row
);
486 bool IsRowSelected( unsigned int row
);
487 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
489 void RefreshRow( unsigned int row
);
490 void RefreshRows( unsigned int from
, unsigned int to
);
491 void RefreshRowsAfter( unsigned int firstRow
);
493 // returns the colour to be used for drawing the rules
494 wxColour
GetRuleColour() const
496 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
499 wxRect
GetLineRect( unsigned int row
) const;
501 //Some useful functions for row and item mapping
502 wxDataViewItem
GetItemByRow( unsigned int row
) const;
503 int GetRowByItem( const wxDataViewItem
& item
);
505 //Methods for building the mapping tree
506 void BuildTree( wxDataViewModel
* model
);
508 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
509 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
511 void Expand( unsigned int row
) { OnExpanding( row
); }
512 void Collapse( unsigned int row
) { OnCollapsing( row
); }
514 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
515 //We did not need this temporarily
516 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
518 int RecalculateCount() ;
520 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
521 void OnExpanding( unsigned int row
);
522 void OnCollapsing( unsigned int row
);
524 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
527 wxDataViewCtrl
*m_owner
;
531 wxDataViewColumn
*m_currentCol
;
532 unsigned int m_currentRow
;
533 wxDataViewSelection m_selection
;
535 wxDataViewRenameTimer
*m_renameTimer
;
543 // for double click logic
544 unsigned int m_lineLastClicked
,
545 m_lineBeforeLastClicked
,
546 m_lineSelectSingleOnUp
;
548 // the pen used to draw horiz/vertical rules
551 // the pen used to draw the expander and the lines
554 //This is the tree structure of the model
555 wxDataViewTreeNode
* m_root
;
557 //This is the tree node under the cursor
558 wxDataViewTreeNode
* m_underMouse
;
560 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
561 DECLARE_EVENT_TABLE()
564 // ---------------------------------------------------------
565 // wxGenericDataViewModelNotifier
566 // ---------------------------------------------------------
568 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
571 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
572 { m_mainWindow
= mainWindow
; }
574 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
575 { return m_mainWindow
->ItemAdded( parent
, item
); }
576 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
577 { return m_mainWindow
->ItemDeleted( parent
, item
); }
578 virtual bool ItemChanged( const wxDataViewItem
& item
)
579 { return m_mainWindow
->ItemChanged(item
); }
580 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
581 { return m_mainWindow
->ValueChanged( item
, col
); }
582 virtual bool Cleared()
583 { return m_mainWindow
->Cleared(); }
584 virtual void Resort()
585 { m_mainWindow
->Resort(); }
587 wxDataViewMainWindow
*m_mainWindow
;
590 // ---------------------------------------------------------
591 // wxDataViewRenderer
592 // ---------------------------------------------------------
594 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
596 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
597 wxDataViewCellMode mode
,
599 wxDataViewRendererBase( varianttype
, mode
, align
)
606 wxDataViewRenderer::~wxDataViewRenderer()
612 wxDC
*wxDataViewRenderer::GetDC()
616 if (GetOwner() == NULL
)
618 if (GetOwner()->GetOwner() == NULL
)
620 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
626 // ---------------------------------------------------------
627 // wxDataViewCustomRenderer
628 // ---------------------------------------------------------
630 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
632 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
633 wxDataViewCellMode mode
, int align
) :
634 wxDataViewRenderer( varianttype
, mode
, align
)
638 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
640 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
641 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
642 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
643 view
->GetForegroundColour();
644 dc
->SetTextForeground(col
);
645 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
648 // ---------------------------------------------------------
649 // wxDataViewTextRenderer
650 // ---------------------------------------------------------
652 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
654 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
655 wxDataViewCellMode mode
, int align
) :
656 wxDataViewCustomRenderer( varianttype
, mode
, align
)
660 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
662 m_text
= value
.GetString();
667 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
672 bool wxDataViewTextRenderer::HasEditorCtrl()
677 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
678 wxRect labelRect
, const wxVariant
&value
)
680 return new wxTextCtrl( parent
, wxID_ANY
, value
,
681 wxPoint(labelRect
.x
,labelRect
.y
),
682 wxSize(labelRect
.width
,labelRect
.height
) );
685 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
687 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
688 value
= text
->GetValue();
692 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
694 RenderText( m_text
, 0, cell
, dc
, state
);
698 wxSize
wxDataViewTextRenderer::GetSize() const
700 const wxDataViewCtrl
*view
= GetView();
704 view
->GetTextExtent( m_text
, &x
, &y
);
705 return wxSize( x
, y
);
707 return wxSize(80,20);
710 // ---------------------------------------------------------
711 // wxDataViewBitmapRenderer
712 // ---------------------------------------------------------
714 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
716 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
717 wxDataViewCellMode mode
, int align
) :
718 wxDataViewCustomRenderer( varianttype
, mode
, align
)
722 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
724 if (value
.GetType() == wxT("wxBitmap"))
726 if (value
.GetType() == wxT("wxIcon"))
732 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
737 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
740 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
741 else if (m_icon
.Ok())
742 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
747 wxSize
wxDataViewBitmapRenderer::GetSize() const
750 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
751 else if (m_icon
.Ok())
752 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
754 return wxSize(16,16);
757 // ---------------------------------------------------------
758 // wxDataViewToggleRenderer
759 // ---------------------------------------------------------
761 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
763 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
764 wxDataViewCellMode mode
, int align
) :
765 wxDataViewCustomRenderer( varianttype
, mode
, align
)
770 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
772 m_toggle
= value
.GetBool();
777 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
782 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
784 // User wxRenderer here
787 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
789 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
794 flags
|= wxCONTROL_CHECKED
;
795 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
796 flags
|= wxCONTROL_DISABLED
;
798 wxRendererNative::Get().DrawCheckBox(
799 GetOwner()->GetOwner(),
807 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
808 wxDataViewModel
*model
,
809 const wxDataViewItem
& item
, unsigned int col
)
811 bool value
= !m_toggle
;
812 wxVariant variant
= value
;
813 model
->SetValue( variant
, item
, col
);
814 model
->ValueChanged( item
, col
);
818 wxSize
wxDataViewToggleRenderer::GetSize() const
820 return wxSize(20,20);
823 // ---------------------------------------------------------
824 // wxDataViewProgressRenderer
825 // ---------------------------------------------------------
827 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
829 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
830 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
831 wxDataViewCustomRenderer( varianttype
, mode
, align
)
837 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
841 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
843 m_value
= (long) value
;
845 if (m_value
< 0) m_value
= 0;
846 if (m_value
> 100) m_value
= 100;
851 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
853 value
= (long) m_value
;
857 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
859 double pct
= (double)m_value
/ 100.0;
861 bar
.width
= (int)(cell
.width
* pct
);
862 dc
->SetPen( *wxTRANSPARENT_PEN
);
863 dc
->SetBrush( *wxBLUE_BRUSH
);
864 dc
->DrawRectangle( bar
);
866 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
867 dc
->SetPen( *wxBLACK_PEN
);
868 dc
->DrawRectangle( cell
);
873 wxSize
wxDataViewProgressRenderer::GetSize() const
875 return wxSize(40,12);
878 // ---------------------------------------------------------
879 // wxDataViewDateRenderer
880 // ---------------------------------------------------------
882 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
884 #if wxUSE_DATE_RENDERER_POPUP
886 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
889 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
890 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
891 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
896 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
897 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
898 sizer
->Add( m_cal
, 1, wxGROW
);
903 void OnCalendar( wxCalendarEvent
&event
);
905 wxCalendarCtrl
*m_cal
;
906 wxDataViewModel
*m_model
;
908 const wxDataViewItem
& m_item
;
911 virtual void OnDismiss()
916 DECLARE_EVENT_TABLE()
919 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
920 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
923 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
925 wxDateTime date
= event
.GetDate();
926 wxVariant value
= date
;
927 m_model
->SetValue( value
, m_item
, m_col
);
928 m_model
->ValueChanged( m_item
, m_col
);
932 #endif // wxUSE_DATE_RENDERER_POPUP
934 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
936 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
937 wxDataViewCellMode mode
, int align
) :
938 wxDataViewCustomRenderer( varianttype
, mode
, align
)
942 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
944 m_date
= value
.GetDateTime();
949 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
955 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
957 wxString tmp
= m_date
.FormatDate();
958 RenderText( tmp
, 0, cell
, dc
, state
);
962 wxSize
wxDataViewDateRenderer::GetSize() const
964 const wxDataViewCtrl
* view
= GetView();
965 wxString tmp
= m_date
.FormatDate();
967 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
968 return wxSize(x
,y
+d
);
971 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
972 const wxDataViewItem
& item
, unsigned int col
)
975 model
->GetValue( variant
, item
, col
);
976 wxDateTime value
= variant
.GetDateTime();
978 #if wxUSE_DATE_RENDERER_POPUP
979 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
980 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
981 wxPoint pos
= wxGetMousePosition();
984 popup
->Popup( popup
->m_cal
);
985 #else // !wxUSE_DATE_RENDERER_POPUP
986 wxMessageBox(value
.Format());
987 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
991 // ---------------------------------------------------------
992 // wxDataViewIconTextRenderer
993 // ---------------------------------------------------------
995 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
997 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
998 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
999 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1002 SetAlignment(align
);
1005 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1009 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1015 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1020 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1023 const wxIcon
&icon
= m_value
.GetIcon();
1026 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1027 xoffset
= icon
.GetWidth()+4;
1030 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1035 wxSize
wxDataViewIconTextRenderer::GetSize() const
1037 const wxDataViewCtrl
*view
= GetView();
1038 if (!m_value
.GetText().empty())
1041 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1043 if (m_value
.GetIcon().IsOk())
1044 x
+= m_value
.GetIcon().GetWidth() + 4;
1045 return wxSize( x
, y
);
1047 return wxSize(80,20);
1051 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1052 wxRect
WXUNUSED(labelRect
),
1053 const wxVariant
& WXUNUSED(value
))
1059 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1060 wxVariant
& WXUNUSED(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())
1615 if ((int) i
== m_hover
)
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
;
1641 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1644 case wxALIGN_CENTER
:
1645 case wxALIGN_CENTER_HORIZONTAL
:
1646 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1650 // always center the title vertically:
1651 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1653 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1655 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1656 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1657 dc
.DrawText( col
->GetTitle(), x
, y
);
1658 dc
.DestroyClippingRegion();
1664 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1666 GetParent()->SetFocus();
1670 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1672 // we want to work with logical coords
1674 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1675 int y
= event
.GetY();
1679 // we don't draw the line beyond our window,
1680 // but we allow dragging it there
1682 GetClientSize( &w
, NULL
);
1683 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1686 if (event
.ButtonUp())
1688 m_isDragging
= false;
1694 m_currentX
= wxMax(m_minX
+ 7, x
);
1698 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1700 GetOwner()->Refresh();
1704 else // not dragging
1707 m_column
= wxNOT_FOUND
;
1709 bool hit_border
= false;
1711 // end of the current column
1714 // find the column where this event occured
1715 int countCol
= m_owner
->GetColumnCount();
1716 for (int column
= 0; column
< countCol
; column
++)
1718 wxDataViewColumn
*p
= GetColumn(column
);
1721 continue; // skip if not shown
1723 xpos
+= p
->GetWidth();
1725 if ((abs(x
-xpos
) < 3) && (y
< 22))
1733 // inside the column
1740 int old_hover
= m_hover
;
1742 if (event
.Leaving())
1743 m_hover
= wxNOT_FOUND
;
1744 if (old_hover
!= m_hover
)
1747 if (m_column
== wxNOT_FOUND
)
1750 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1751 if (event
.LeftDClick() && resizeable
)
1753 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1756 else if (event
.LeftDown() || event
.RightUp())
1758 if (hit_border
&& event
.LeftDown() && resizeable
)
1760 m_isDragging
= true;
1764 else // click on a column
1766 wxDataViewModel
* model
= GetOwner()->GetModel();
1767 wxEventType evt
= event
.LeftDown() ?
1768 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1769 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1770 SendEvent(evt
, m_column
);
1772 //Left click the header
1773 if(event
.LeftDown())
1775 wxDataViewColumn
*col
= GetColumn(m_column
);
1776 if(col
->IsSortable())
1778 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1779 if(model
&& sortCol
== col
)
1781 bool order
= col
->IsSortOrderAscending();
1782 col
->SetSortOrder(!order
);
1786 m_owner
->SetSortingColumn(col
);
1792 //Send the column sorted event
1793 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1797 else if (event
.Moving())
1799 if (hit_border
&& resizeable
)
1800 m_currentCursor
= m_resizeCursor
;
1802 m_currentCursor
= wxSTANDARD_CURSOR
;
1804 SetCursor(*m_currentCursor
);
1809 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1813 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1814 m_owner
->GetViewStart( &x
, NULL
);
1816 // shift the DC origin to match the position of the main window horizontal
1817 // scrollbar: this allows us to always use logical coords
1818 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1821 #endif // defined(__WXMSW__)
1823 //-----------------------------------------------------------------------------
1824 // wxDataViewRenameTimer
1825 //-----------------------------------------------------------------------------
1827 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1832 void wxDataViewRenameTimer::Notify()
1834 m_owner
->OnRenameTimer();
1837 //-----------------------------------------------------------------------------
1838 // wxDataViewMainWindow
1839 //-----------------------------------------------------------------------------
1841 //The tree building helper, declared firstly
1842 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1844 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1846 if (row1
> row2
) return 1;
1847 if (row1
== row2
) return 0;
1852 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1854 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1855 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1856 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1857 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1858 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1859 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1862 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1863 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1864 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1865 m_selection( wxDataViewSelectionCmp
)
1870 m_lastOnSame
= false;
1871 m_renameTimer
= new wxDataViewRenameTimer( this );
1873 // TODO: user better initial values/nothing selected
1874 m_currentCol
= NULL
;
1877 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1880 m_dragStart
= wxPoint(0,0);
1881 m_lineLastClicked
= (unsigned int) -1;
1882 m_lineBeforeLastClicked
= (unsigned int) -1;
1883 m_lineSelectSingleOnUp
= (unsigned int) -1;
1887 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1888 SetBackgroundColour( *wxWHITE
);
1890 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1892 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1893 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1894 //Some new added code to deal with the tree structure
1895 m_root
= new wxDataViewTreeNode( NULL
);
1896 m_root
->SetHasChildren(true);
1898 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1900 m_underMouse
= NULL
;
1904 wxDataViewMainWindow::~wxDataViewMainWindow()
1907 delete m_renameTimer
;
1910 void wxDataViewMainWindow::OnRenameTimer()
1912 // We have to call this here because changes may just have
1913 // been made and no screen update taken place.
1918 unsigned int cols
= GetOwner()->GetColumnCount();
1920 for (i
= 0; i
< cols
; i
++)
1922 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1924 continue; // skip it!
1926 if (c
== m_currentCol
)
1928 xpos
+= c
->GetWidth();
1930 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1931 m_currentCol
->GetWidth(), m_lineHeight
);
1933 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1934 &labelRect
.x
, &labelRect
.y
);
1936 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1937 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1941 //------------------------------------------------------------------
1942 // Helper class for do operation on the tree node
1943 //------------------------------------------------------------------
1950 //The return value control how the tree-walker tranverse the tree
1951 // 0: Job done, stop tranverse and return
1952 // 1: Ignore the current node's subtree and continue
1953 // 2: Job not done, continue
1954 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1955 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1956 virtual int operator() ( void * n
) = 0;
1959 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1964 switch( func( node
) )
1975 wxDataViewTreeNodes nodes
= node
->GetNodes();
1976 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1978 int len_nodes
= nodes
.GetCount();
1979 int len
= leaves
.GetCount();
1980 int i
= 0, nodes_i
= 0;
1982 for( ; i
< len
; i
++ )
1984 void * n
= leaves
[i
];
1985 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1987 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1990 if( Walker( nd
, func
) )
2009 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2013 wxDataViewTreeNode
* node
;
2014 node
= FindNode(parent
);
2019 node
->SetHasChildren( true );
2021 if( g_model
->IsContainer( item
) )
2023 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2024 newnode
->SetItem(item
);
2025 newnode
->SetHasChildren( true );
2026 node
->AddNode( newnode
);
2029 node
->AddLeaf( item
.GetID() );
2031 node
->ChangeSubTreeCount(1);
2039 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2041 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2042 const wxDataViewItem
& item
)
2044 wxDataViewTreeNode
* node
= FindNode(parent
);
2046 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2047 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2050 node
->GetChildren().Remove( item
.GetID() );
2051 //Manuplate selection
2052 if( m_selection
.GetCount() > 1 )
2054 m_selection
.Empty();
2056 bool isContainer
= false;
2057 wxDataViewTreeNodes nds
= node
->GetNodes();
2058 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2060 if (nds
[i
]->GetItem() == item
)
2068 wxDataViewTreeNode
* n
= NULL
;
2069 wxDataViewTreeNodes nodes
= node
->GetNodes();
2070 int len
= nodes
.GetCount();
2071 for( int i
= 0 ; i
< len
; i
++)
2073 if( nodes
[i
]->GetItem() == item
)
2080 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2082 node
->GetNodes().Remove( n
);
2083 sub
-= n
->GetSubTreeCount();
2084 DestroyTreeHelper(n
);
2086 //Make the row number invalid and get a new valid one when user call GetRowCount
2088 node
->ChangeSubTreeCount(sub
);
2089 if( node
->GetChildrenNumber() == 0)
2091 node
->GetParent()->GetNodes().Remove( node
);
2095 //Change the current row to the last row if the current exceed the max row number
2096 if( m_currentRow
> GetRowCount() )
2097 m_currentRow
= m_count
- 1;
2104 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2110 wxWindow
*parent
= GetParent();
2111 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2112 le
.SetEventObject(parent
);
2113 le
.SetModel(GetOwner()->GetModel());
2115 parent
->GetEventHandler()->ProcessEvent(le
);
2120 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2122 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2123 /*#define MAX_VIRTUAL_WIDTH 100000
2125 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2126 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2127 Refresh( true, &rect );
2135 wxWindow
*parent
= GetParent();
2136 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2137 le
.SetEventObject(parent
);
2138 le
.SetModel(GetOwner()->GetModel());
2141 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2142 parent
->GetEventHandler()->ProcessEvent(le
);
2147 bool wxDataViewMainWindow::Cleared()
2157 void wxDataViewMainWindow::UpdateDisplay()
2162 void wxDataViewMainWindow::OnInternalIdle()
2164 wxWindow::OnInternalIdle();
2168 RecalculateDisplay();
2173 void wxDataViewMainWindow::RecalculateDisplay()
2175 wxDataViewModel
*model
= GetOwner()->GetModel();
2182 int width
= GetEndOfLastCol();
2183 int height
= GetRowCount() * m_lineHeight
;
2185 SetVirtualSize( width
, height
);
2186 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2191 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2193 wxWindow::ScrollWindow( dx
, dy
, rect
);
2195 if (GetOwner()->m_headerArea
)
2196 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2199 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2202 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2203 int sy
= rows
*m_lineHeight
/y
;
2207 wxRect rect
= GetClientRect();
2209 int x_start
= 0, x_end
= 0, w
= 0;
2211 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2212 for (x_start
= 0; colnum
< column
; colnum
++)
2214 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2215 if (col
->IsHidden())
2216 continue; // skip it!
2218 w
= col
->GetWidth();
2222 x_end
= x_start
+ w
;
2223 xe
= xx
+ rect
.width
;
2226 sx
= ( xx
+ x_end
- xe
)/x
;
2233 m_owner
->Scroll( sx
, sy
);
2236 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2238 wxDataViewModel
*model
= GetOwner()->GetModel();
2239 wxAutoBufferedPaintDC
dc( this );
2242 dc
.SetBackground(GetBackgroundColour());
2244 GetOwner()->PrepareDC( dc
);
2245 dc
.SetFont( GetFont() );
2247 wxRect update
= GetUpdateRegion().GetBox();
2248 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2250 // compute which items needs to be redrawn
2251 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2252 unsigned int item_count
=
2253 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2254 (int)(GetRowCount( )- item_start
) );
2255 unsigned int item_last
= item_start
+ item_count
;
2257 // compute which columns needs to be redrawn
2258 unsigned int cols
= GetOwner()->GetColumnCount();
2259 unsigned int col_start
= 0;
2260 unsigned int x_start
= 0;
2261 for (x_start
= 0; col_start
< cols
; col_start
++)
2263 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2264 if (col
->IsHidden())
2265 continue; // skip it!
2267 unsigned int w
= col
->GetWidth();
2268 if (x_start
+w
>= (unsigned int)update
.x
)
2274 unsigned int col_last
= col_start
;
2275 unsigned int x_last
= x_start
;
2276 for (; col_last
< cols
; col_last
++)
2278 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2279 if (col
->IsHidden())
2280 continue; // skip it!
2282 if (x_last
> (unsigned int)update
.GetRight())
2285 x_last
+= col
->GetWidth();
2288 // Draw horizontal rules if required
2289 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2291 dc
.SetPen(m_penRule
);
2292 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2294 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2296 int y
= i
* m_lineHeight
;
2297 dc
.DrawLine(x_start
, y
, x_last
, y
);
2301 // Draw vertical rules if required
2302 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2304 dc
.SetPen(m_penRule
);
2305 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2308 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2310 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2311 if (col
->IsHidden())
2312 continue; // skip it
2314 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2315 x
, item_last
* m_lineHeight
);
2317 x
+= col
->GetWidth();
2320 // Draw last vertical rule
2321 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2322 x
, item_last
* m_lineHeight
);
2325 // redraw the background for the items which are selected/current
2326 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2328 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2329 if (selected
|| item
== m_currentRow
)
2331 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2332 if (item
== m_currentRow
)
2333 flags
|= wxCONTROL_CURRENT
;
2335 flags
|= wxCONTROL_FOCUSED
;
2337 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2338 wxRendererNative::Get().DrawItemSelectionRect
2348 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2351 // TODO: last column for RTL support
2352 expander
= GetOwner()->GetColumn( 0 );
2353 GetOwner()->SetExpanderColumn(expander
);
2356 // redraw all cells for all rows which must be repainted and for all columns
2358 cell_rect
.x
= x_start
;
2359 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2360 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2362 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2363 wxDataViewRenderer
*cell
= col
->GetRenderer();
2364 cell_rect
.width
= col
->GetWidth();
2366 if (col
->IsHidden())
2367 continue; // skipt it!
2370 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2372 // get the cell value and set it into the renderer
2374 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2378 wxDataViewItem dataitem
= node
->GetItem();
2380 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2383 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2384 cell
->SetValue( value
);
2386 // update the y offset
2387 cell_rect
.y
= item
* m_lineHeight
;
2389 //Draw the expander here.
2390 int indent
= node
->GetIndentLevel();
2391 if( col
== expander
)
2393 //Calculate the indent first
2394 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2396 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2397 // change the cell_rect.x to the appropriate pos
2398 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2399 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2400 dc
.SetPen( m_penExpander
);
2401 dc
.SetBrush( wxNullBrush
);
2402 if( node
->HasChildren() )
2404 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2406 if (m_underMouse
== node
)
2408 flag
|= wxCONTROL_CURRENT
;
2410 if( node
->IsOpen() )
2411 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2413 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2417 // I am wandering whether we should draw dot lines between tree nodes
2419 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2422 //force the expander column to left-center align
2423 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2427 // cannot be bigger than allocated space
2428 wxSize size
= cell
->GetSize();
2429 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2430 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2431 // size.y = wxMin( size.y, cell_rect.height );
2432 size
.y
= cell_rect
.height
;
2434 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2435 int align
= cell
->GetAlignment();
2437 // horizontal alignment:
2438 item_rect
.x
= cell_rect
.x
;
2439 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2440 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2441 else if (align
& wxALIGN_RIGHT
)
2442 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2443 //else: wxALIGN_LEFT is the default
2445 // vertical alignment:
2446 item_rect
.y
= cell_rect
.y
;
2447 if (align
& wxALIGN_CENTER_VERTICAL
)
2448 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2449 else if (align
& wxALIGN_BOTTOM
)
2450 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2451 //else: wxALIGN_TOP is the default
2454 item_rect
.x
+= PADDING_RIGHTLEFT
;
2455 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2457 //Here we add the tree indent
2458 item_rect
.x
+= indent
;
2461 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2462 state
|= wxDATAVIEW_CELL_SELECTED
;
2464 // TODO: it would be much more efficient to create a clipping
2465 // region for the entire column being rendered (in the OnPaint
2466 // of wxDataViewMainWindow) instead of a single clip region for
2467 // each cell. However it would mean that each renderer should
2468 // respect the given wxRect's top & bottom coords, eventually
2469 // violating only the left & right coords - however the user can
2470 // make its own renderer and thus we cannot be sure of that.
2471 dc
.SetClippingRegion( item_rect
);
2472 cell
->Render( item_rect
, &dc
, state
);
2473 dc
.DestroyClippingRegion();
2476 cell_rect
.x
+= cell_rect
.width
;
2480 int wxDataViewMainWindow::GetCountPerPage() const
2482 wxSize size
= GetClientSize();
2483 return size
.y
/ m_lineHeight
;
2486 int wxDataViewMainWindow::GetEndOfLastCol() const
2490 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2492 const wxDataViewColumn
*c
=
2493 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2496 width
+= c
->GetWidth();
2501 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2505 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2507 return y
/ m_lineHeight
;
2510 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2512 wxSize client_size
= GetClientSize();
2513 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2514 &client_size
.x
, &client_size
.y
);
2516 //we should deal with the pixel here
2517 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2519 return wxMin( GetRowCount()-1, row
);
2522 unsigned int wxDataViewMainWindow::GetRowCount()
2524 if ( m_count
== -1 )
2526 m_count
= RecalculateCount();
2528 GetVirtualSize( &width
, &height
);
2529 height
= m_count
* m_lineHeight
;
2531 SetVirtualSize( width
, height
);
2536 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2543 void wxDataViewMainWindow::SelectAllRows( bool on
)
2550 m_selection
.Clear();
2551 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2552 m_selection
.Add( i
);
2557 unsigned int first_visible
= GetFirstVisibleRow();
2558 unsigned int last_visible
= GetLastVisibleRow();
2560 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2562 unsigned int row
= m_selection
[i
];
2563 if ((row
>= first_visible
) && (row
<= last_visible
))
2566 m_selection
.Clear();
2570 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2572 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2576 m_selection
.Add( row
);
2584 m_selection
.Remove( row
);
2590 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2594 unsigned int tmp
= from
;
2600 for (i
= from
; i
<= to
; i
++)
2602 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2605 m_selection
.Add( i
);
2610 m_selection
.Remove( i
);
2613 RefreshRows( from
, to
);
2616 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2618 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2620 int n
= aSelections
[i
];
2622 m_selection
.Add( n
);
2627 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2629 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2630 m_selection
.Add( row
);
2632 m_selection
.Remove( row
);
2636 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2638 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2641 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2643 wxWindow
*parent
= GetParent();
2644 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2646 le
.SetEventObject(parent
);
2647 le
.SetModel(GetOwner()->GetModel());
2650 parent
->GetEventHandler()->ProcessEvent(le
);
2653 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2655 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2656 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2658 wxSize client_size
= GetClientSize();
2659 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2660 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2661 if (intersect_rect
.width
> 0)
2662 Refresh( true, &intersect_rect
);
2665 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2669 unsigned int tmp
= to
;
2674 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2675 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2677 wxSize client_size
= GetClientSize();
2678 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2679 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2680 if (intersect_rect
.width
> 0)
2681 Refresh( true, &intersect_rect
);
2684 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2686 unsigned int count
= GetRowCount();
2687 if (firstRow
> count
)
2690 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2691 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2693 wxSize client_size
= GetClientSize();
2694 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2695 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2696 if (intersect_rect
.width
> 0)
2697 Refresh( true, &intersect_rect
);
2700 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2702 wxCHECK_RET( newCurrent
< GetRowCount(),
2703 _T("invalid item index in OnArrowChar()") );
2705 // if there is no selection, we cannot move it anywhere
2706 if (!HasCurrentRow())
2709 unsigned int oldCurrent
= m_currentRow
;
2711 // in single selection we just ignore Shift as we can't select several
2713 if ( event
.ShiftDown() && !IsSingleSel() )
2715 RefreshRow( oldCurrent
);
2717 ChangeCurrentRow( newCurrent
);
2719 // select all the items between the old and the new one
2720 if ( oldCurrent
> newCurrent
)
2722 newCurrent
= oldCurrent
;
2723 oldCurrent
= m_currentRow
;
2726 SelectRows( oldCurrent
, newCurrent
, true );
2727 if (oldCurrent
!=newCurrent
)
2728 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2732 RefreshRow( oldCurrent
);
2734 // all previously selected items are unselected unless ctrl is held
2735 if ( !event
.ControlDown() )
2736 SelectAllRows(false);
2738 ChangeCurrentRow( newCurrent
);
2740 if ( !event
.ControlDown() )
2742 SelectRow( m_currentRow
, true );
2743 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2746 RefreshRow( m_currentRow
);
2749 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2752 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2756 rect
.y
= m_lineHeight
* row
;
2757 rect
.width
= GetEndOfLastCol();
2758 rect
.height
= m_lineHeight
;
2763 class RowToItemJob
: public DoJob
2766 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2767 virtual ~RowToItemJob(){};
2769 virtual int operator() ( wxDataViewTreeNode
* node
)
2772 if( current
== static_cast<int>(row
))
2774 ret
= node
->GetItem() ;
2778 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2780 current
+= node
->GetSubTreeCount();
2785 //If the current has no child node, we can find the desired item of the row number directly.
2786 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2787 if( node
->GetNodes().GetCount() == 0)
2789 int index
= static_cast<int>(row
) - current
- 1;
2790 ret
= node
->GetChildren().Item( index
);
2797 virtual int operator() ( void * n
)
2800 if( current
== static_cast<int>(row
))
2802 ret
= wxDataViewItem( n
) ;
2807 wxDataViewItem
GetResult(){ return ret
; }
2814 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2816 RowToItemJob
job( row
, -2 );
2817 Walker( m_root
, job
);
2818 return job
.GetResult();
2821 class RowToTreeNodeJob
: public DoJob
2824 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2827 this->current
= current
;
2831 virtual ~RowToTreeNodeJob(){};
2833 virtual int operator() ( wxDataViewTreeNode
* node
)
2836 if( current
== static_cast<int>(row
))
2842 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2844 current
+= node
->GetSubTreeCount();
2850 //If the current has no child node, we can find the desired item of the row number directly.
2851 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2852 if( node
->GetNodes().GetCount() == 0)
2854 int index
= static_cast<int>(row
) - current
- 1;
2855 void * n
= node
->GetChildren().Item( index
);
2856 ret
= new wxDataViewTreeNode( parent
) ;
2857 ret
->SetItem( wxDataViewItem( n
));
2858 ret
->SetHasChildren(false);
2867 virtual int operator() ( void * n
)
2870 if( current
== static_cast<int>(row
))
2872 ret
= new wxDataViewTreeNode( parent
) ;
2873 ret
->SetItem( wxDataViewItem( n
));
2874 ret
->SetHasChildren(false);
2880 wxDataViewTreeNode
* GetResult(){ return ret
; }
2884 wxDataViewTreeNode
* ret
;
2885 wxDataViewTreeNode
* parent
;
2889 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2891 RowToTreeNodeJob
job( row
, -2, m_root
);
2892 Walker( m_root
, job
);
2893 return job
.GetResult();
2896 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2898 wxWindow
*parent
= GetParent();
2899 wxDataViewEvent
le(type
, parent
->GetId());
2901 le
.SetEventObject(parent
);
2902 le
.SetModel(GetOwner()->GetModel());
2905 parent
->GetEventHandler()->ProcessEvent(le
);
2909 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2911 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2914 if( node
->HasChildren())
2916 if( !node
->IsOpen())
2918 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2919 //Check if the user prevent expanding
2920 if( e
.GetSkipped() )
2924 //Here I build the children of current node
2925 if( node
->GetChildrenNumber() == 0 )
2928 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2932 //Send the expanded event
2933 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2937 SelectRow( row
, false );
2938 SelectRow( row
+ 1, true );
2939 ChangeCurrentRow( row
+ 1 );
2940 SendSelectionChangedEvent( GetItemByRow(row
+1));
2948 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2950 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2953 wxDataViewTreeNode
* nd
= node
;
2955 if( node
->HasChildren() && node
->IsOpen() )
2957 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2958 if( e
.GetSkipped() )
2963 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2967 node
= node
->GetParent();
2970 int parent
= GetRowByItem( node
->GetItem() ) ;
2973 SelectRow( row
, false);
2974 SelectRow(parent
, true );
2975 ChangeCurrentRow( parent
);
2976 SendSelectionChangedEvent( node
->GetItem() );
2980 if( !nd
->HasChildren())
2985 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2987 wxDataViewModel
* model
= GetOwner()->GetModel();
2991 //Compose the a parent-chain of the finding item
2993 list
.DeleteContents( true );
2994 wxDataViewItem
it( item
);
2997 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2998 list
.Insert( pItem
);
2999 it
= model
->GetParent( it
);
3002 //Find the item along the parent-chain.
3003 //This algorithm is designed to speed up the node-finding method
3004 wxDataViewTreeNode
* node
= m_root
;
3005 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3007 if( node
->HasChildren() )
3009 if( node
->GetChildrenNumber() == 0 )
3012 BuildTreeHelper(model
, node
->GetItem(), node
);
3015 wxDataViewTreeNodes nodes
= node
->GetNodes();
3017 for (; i
< nodes
.GetCount(); i
++)
3019 if (nodes
[i
]->GetItem() == (**iter
))
3025 if (i
== nodes
.GetCount())
3034 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3036 wxDataViewColumn
*col
= NULL
;
3037 unsigned int cols
= GetOwner()->GetColumnCount();
3038 unsigned int colnum
= 0;
3039 unsigned int x_start
= 0;
3041 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3042 for (x_start
= 0; colnum
< cols
; colnum
++)
3044 col
= GetOwner()->GetColumn(colnum
);
3045 if (col
->IsHidden())
3046 continue; // skip it!
3048 unsigned int w
= col
->GetWidth();
3049 if (x_start
+w
>= (unsigned int)x
)
3056 item
= GetItemByRow( y
/m_lineHeight
);
3059 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3061 int row
= GetRowByItem(item
);
3062 int y
= row
*m_lineHeight
;
3063 int h
= m_lineHeight
;
3065 wxDataViewColumn
*col
= NULL
;
3066 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3068 col
= GetOwner()->GetColumn( i
);
3069 x
+= col
->GetWidth();
3070 if( GetOwner()->GetColumn(i
+1) == column
)
3073 int w
= col
->GetWidth();
3074 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3075 return wxRect(x
, y
, w
, h
);
3078 int wxDataViewMainWindow::RecalculateCount()
3080 return m_root
->GetSubTreeCount();
3083 class ItemToRowJob
: public DoJob
3086 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3087 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3088 virtual ~ItemToRowJob(){};
3090 //Maybe binary search will help to speed up this process
3091 virtual int operator() ( wxDataViewTreeNode
* node
)
3094 if( node
->GetItem() == item
)
3099 if( node
->GetItem() == **m_iter
)
3106 ret
+= node
->GetSubTreeCount();
3112 virtual int operator() ( void * n
)
3115 if( n
== item
.GetID() )
3119 //the row number is begin from zero
3120 int GetResult(){ return ret
-1 ; }
3122 ItemList::const_iterator m_iter
;
3123 wxDataViewItem item
;
3128 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3130 wxDataViewModel
* model
= GetOwner()->GetModel();
3137 //Compose the a parent-chain of the finding item
3139 wxDataViewItem
* pItem
= NULL
;
3140 list
.DeleteContents( true );
3141 wxDataViewItem
it( item
);
3144 pItem
= new wxDataViewItem( it
);
3145 list
.Insert( pItem
);
3146 it
= model
->GetParent( it
);
3148 pItem
= new wxDataViewItem( );
3149 list
.Insert( pItem
);
3151 ItemToRowJob
job( item
, list
.begin() );
3152 Walker(m_root
, job
);
3153 return job
.GetResult();
3156 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3158 if( !model
->IsContainer( item
) )
3161 wxDataViewItemArray children
;
3162 unsigned int num
= model
->GetChildren( item
, children
);
3163 unsigned int index
= 0;
3164 while( index
< num
)
3166 if( model
->IsContainer( children
[index
] ) )
3168 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3169 n
->SetItem(children
[index
]);
3170 n
->SetHasChildren( true ) ;
3175 node
->AddLeaf( children
[index
].GetID() );
3179 node
->SetSubTreeCount( num
);
3180 wxDataViewTreeNode
* n
= node
->GetParent();
3182 n
->ChangeSubTreeCount(num
);
3186 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3188 //First we define a invalid item to fetch the top-level elements
3189 wxDataViewItem item
;
3191 BuildTreeHelper( model
, item
, m_root
);
3195 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3197 if( node
->GetNodeNumber() != 0 )
3199 int len
= node
->GetNodeNumber();
3201 wxDataViewTreeNodes nodes
= node
->GetNodes();
3202 for( ; i
< len
; i
++ )
3204 DestroyTreeHelper(nodes
[i
]);
3210 void wxDataViewMainWindow::DestroyTree()
3212 DestroyTreeHelper(m_root
);
3213 m_root
->SetSubTreeCount(0);
3217 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3219 if (event
.GetKeyCode() == WXK_TAB
)
3221 wxNavigationKeyEvent nevent
;
3222 nevent
.SetWindowChange( event
.ControlDown() );
3223 nevent
.SetDirection( !event
.ShiftDown() );
3224 nevent
.SetEventObject( GetParent()->GetParent() );
3225 nevent
.SetCurrentFocus( m_parent
);
3226 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3230 // no item -> nothing to do
3231 if (!HasCurrentRow())
3237 // don't use m_linesPerPage directly as it might not be computed yet
3238 const int pageSize
= GetCountPerPage();
3239 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3241 switch ( event
.GetKeyCode() )
3245 if (m_currentRow
> 0)
3247 wxWindow
*parent
= GetParent();
3248 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3249 le
.SetItem( GetItemByRow(m_currentRow
) );
3250 le
.SetEventObject(parent
);
3251 le
.SetModel(GetOwner()->GetModel());
3253 parent
->GetEventHandler()->ProcessEvent(le
);
3258 if ( m_currentRow
> 0 )
3259 OnArrowChar( m_currentRow
- 1, event
);
3263 if ( m_currentRow
< GetRowCount() - 1 )
3264 OnArrowChar( m_currentRow
+ 1, event
);
3266 //Add the process for tree expanding/collapsing
3268 OnCollapsing(m_currentRow
);
3271 OnExpanding( m_currentRow
);
3275 OnArrowChar( GetRowCount() - 1, event
);
3280 OnArrowChar( 0, event
);
3285 int steps
= pageSize
- 1;
3286 int index
= m_currentRow
- steps
;
3290 OnArrowChar( index
, event
);
3296 int steps
= pageSize
- 1;
3297 unsigned int index
= m_currentRow
+ steps
;
3298 unsigned int count
= GetRowCount();
3299 if ( index
>= count
)
3302 OnArrowChar( index
, event
);
3311 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3313 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3315 // let the base handle mouse wheel events.
3320 int x
= event
.GetX();
3321 int y
= event
.GetY();
3322 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3323 wxDataViewColumn
*col
= NULL
;
3326 unsigned int cols
= GetOwner()->GetColumnCount();
3328 for (i
= 0; i
< cols
; i
++)
3330 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3332 continue; // skip it!
3334 if (x
< xpos
+ c
->GetWidth())
3339 xpos
+= c
->GetWidth();
3344 wxDataViewRenderer
*cell
= col
->GetRenderer();
3345 unsigned int current
= y
/ m_lineHeight
;
3346 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3348 // Unselect all if below the last row ?
3352 //Test whether the mouse is hovered on the tree item button
3354 if (GetOwner()->GetExpanderColumn() == col
)
3356 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3357 if( node
!=NULL
&& node
->HasChildren() )
3359 int indent
= node
->GetIndentLevel();
3360 indent
= GetOwner()->GetIndent()*indent
;
3361 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3362 if( rect
.Contains( x
, y
) )
3364 //So the mouse is over the expander
3366 if (m_underMouse
&& m_underMouse
!= node
)
3368 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3369 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3371 if (m_underMouse
!= node
)
3373 //wxLogMessage("Do the row: %d", current);
3374 RefreshRow(current
);
3376 m_underMouse
= node
;
3379 if (node
!=NULL
&& !node
->HasChildren())
3384 if (m_underMouse
!= NULL
)
3386 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3387 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3388 m_underMouse
= NULL
;
3392 wxDataViewModel
*model
= GetOwner()->GetModel();
3394 if (event
.Dragging())
3396 if (m_dragCount
== 0)
3398 // we have to report the raw, physical coords as we want to be
3399 // able to call HitTest(event.m_pointDrag) from the user code to
3400 // get the item being dragged
3401 m_dragStart
= event
.GetPosition();
3406 if (m_dragCount
!= 3)
3409 if (event
.LeftIsDown())
3411 // Notify cell about drag
3420 bool forceClick
= false;
3422 if (event
.ButtonDClick())
3424 m_renameTimer
->Stop();
3425 m_lastOnSame
= false;
3428 wxDataViewItem item
= GetItemByRow(current
);
3429 bool ignore_other_columns
=
3430 ((GetOwner()->GetExpanderColumn() != col
) &&
3431 (model
->IsContainer(item
)) &&
3432 (!model
->HasContainerColumns(item
)));
3434 if (event
.LeftDClick())
3436 if ( current
== m_lineLastClicked
)
3438 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3441 model
->GetValue( value
, item
, col
->GetModelColumn() );
3442 cell
->SetValue( value
);
3443 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3444 col
->GetWidth(), m_lineHeight
);
3445 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3450 wxWindow
*parent
= GetParent();
3451 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3453 le
.SetEventObject(parent
);
3454 le
.SetModel(GetOwner()->GetModel());
3456 parent
->GetEventHandler()->ProcessEvent(le
);
3462 // The first click was on another item, so don't interpret this as
3463 // a double click, but as a simple click instead
3470 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3472 // select single line
3473 SelectAllRows( false );
3474 SelectRow( m_lineSelectSingleOnUp
, true );
3477 //Process the event of user clicking the expander
3478 bool expander
= false;
3479 if (GetOwner()->GetExpanderColumn() == col
)
3481 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3482 if( node
!=NULL
&& node
->HasChildren() )
3484 int indent
= node
->GetIndentLevel();
3485 indent
= GetOwner()->GetIndent()*indent
;
3486 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3487 if( rect
.Contains( x
, y
) )
3490 if( node
->IsOpen() )
3491 OnCollapsing(current
);
3493 OnExpanding( current
);
3497 //If the user click the expander, we do not do editing even if the column with expander are editable
3498 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3500 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3501 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3503 m_renameTimer
->Start( 100, true );
3507 m_lastOnSame
= false;
3508 m_lineSelectSingleOnUp
= (unsigned int)-1;
3512 // This is necessary, because after a DnD operation in
3513 // from and to ourself, the up event is swallowed by the
3514 // DnD code. So on next non-up event (which means here and
3515 // now) m_lineSelectSingleOnUp should be reset.
3516 m_lineSelectSingleOnUp
= (unsigned int)-1;
3519 if (event
.RightDown())
3521 m_lineBeforeLastClicked
= m_lineLastClicked
;
3522 m_lineLastClicked
= current
;
3524 // If the item is already selected, do not update the selection.
3525 // Multi-selections should not be cleared if a selected item is clicked.
3526 if (!IsRowSelected(current
))
3528 SelectAllRows(false);
3529 ChangeCurrentRow(current
);
3530 SelectRow(m_currentRow
,true);
3531 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3534 // notify cell about right click
3537 // Allow generation of context menu event
3540 else if (event
.MiddleDown())
3542 // notify cell about middle click
3545 if (event
.LeftDown() || forceClick
)
3549 m_lineBeforeLastClicked
= m_lineLastClicked
;
3550 m_lineLastClicked
= current
;
3552 unsigned int oldCurrentRow
= m_currentRow
;
3553 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3555 bool cmdModifierDown
= event
.CmdDown();
3556 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3558 if ( IsSingleSel() || !IsRowSelected(current
) )
3560 SelectAllRows( false );
3561 ChangeCurrentRow(current
);
3562 SelectRow(m_currentRow
,true);
3563 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3565 else // multi sel & current is highlighted & no mod keys
3567 m_lineSelectSingleOnUp
= current
;
3568 ChangeCurrentRow(current
); // change focus
3571 else // multi sel & either ctrl or shift is down
3573 if (cmdModifierDown
)
3575 ChangeCurrentRow(current
);
3576 ReverseRowSelection(m_currentRow
);
3577 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3579 else if (event
.ShiftDown())
3581 ChangeCurrentRow(current
);
3583 unsigned int lineFrom
= oldCurrentRow
,
3586 if ( lineTo
< lineFrom
)
3589 lineFrom
= m_currentRow
;
3592 SelectRows(lineFrom
, lineTo
, true);
3593 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3595 else // !ctrl, !shift
3597 // test in the enclosing if should make it impossible
3598 wxFAIL_MSG( _T("how did we get here?") );
3602 if (m_currentRow
!= oldCurrentRow
)
3603 RefreshRow( oldCurrentRow
);
3605 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3607 // Update selection here...
3610 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3611 (current
== oldCurrentRow
)) && oldWasSelected
;
3615 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3619 if (HasCurrentRow())
3625 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3629 if (HasCurrentRow())
3635 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3637 if( m_selection
.GetCount() != 1 )
3638 return wxDataViewItem();
3640 return GetItemByRow( m_selection
.Item(0));
3643 //-----------------------------------------------------------------------------
3645 //-----------------------------------------------------------------------------
3646 WX_DEFINE_LIST(wxDataViewColumnList
);
3648 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3650 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3651 EVT_SIZE(wxDataViewCtrl::OnSize
)
3654 wxDataViewCtrl::~wxDataViewCtrl()
3657 GetModel()->RemoveNotifier( m_notifier
);
3660 void wxDataViewCtrl::Init()
3665 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3666 const wxPoint
& pos
, const wxSize
& size
,
3667 long style
, const wxValidator
& validator
)
3669 if (!wxControl::Create( parent
, id
, pos
, size
,
3670 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3673 SetInitialSize(size
);
3678 MacSetClipChildren( true ) ;
3681 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3683 if (HasFlag(wxDV_NO_HEADER
))
3684 m_headerArea
= NULL
;
3686 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3688 SetTargetWindow( m_clientArea
);
3690 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3692 sizer
->Add( m_headerArea
, 0, wxGROW
);
3693 sizer
->Add( m_clientArea
, 1, wxGROW
);
3700 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3704 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3707 // we need to process arrows ourselves for scrolling
3708 if ( nMsg
== WM_GETDLGCODE
)
3710 rc
|= DLGC_WANTARROWS
;
3718 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3720 // We need to override OnSize so that our scrolled
3721 // window a) does call Layout() to use sizers for
3722 // positioning the controls but b) does not query
3723 // the sizer for their size and use that for setting
3724 // the scrollable area as set that ourselves by
3725 // calling SetScrollbar() further down.
3732 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3734 if (!wxDataViewCtrlBase::AssociateModel( model
))
3737 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3739 model
->AddNotifier( m_notifier
);
3741 m_clientArea
->BuildTree(model
);
3743 m_clientArea
->UpdateDisplay();
3748 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3750 if (!wxDataViewCtrlBase::AppendColumn(col
))
3753 m_cols
.Append( col
);
3758 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3760 if (!wxDataViewCtrlBase::PrependColumn(col
))
3763 m_cols
.Insert( col
);
3768 void wxDataViewCtrl::OnColumnChange()
3771 m_headerArea
->UpdateDisplay();
3773 m_clientArea
->UpdateDisplay();
3776 void wxDataViewCtrl::DoSetExpanderColumn()
3778 m_clientArea
->UpdateDisplay();
3781 void wxDataViewCtrl::DoSetIndent()
3783 m_clientArea
->UpdateDisplay();
3786 unsigned int wxDataViewCtrl::GetColumnCount() const
3788 return m_cols
.GetCount();
3791 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3793 wxDataViewColumnList::const_iterator iter
;
3795 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3800 if ((*iter
)->IsHidden())
3807 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3809 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3820 bool wxDataViewCtrl::ClearColumns()
3827 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3829 int ret
= 0, dead
= 0;
3830 int len
= GetColumnCount();
3831 for (int i
=0; i
<len
; i
++)
3833 wxDataViewColumn
* col
= GetColumn(i
);
3834 if (col
->IsHidden())
3836 ret
+= col
->GetWidth();
3839 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3846 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3851 //Selection code with wxDataViewItem as parameters
3852 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3854 return m_clientArea
->GetSelection();
3857 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3860 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3861 int len
= selection
.GetCount();
3862 for( int i
= 0; i
< len
; i
++)
3864 unsigned int row
= selection
[i
];
3865 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3870 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3872 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3873 int len
= sel
.GetCount();
3874 for( int i
= 0; i
< len
; i
++ )
3876 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3878 selection
.Add( static_cast<unsigned int>(row
) );
3880 m_clientArea
->SetSelections( selection
);
3883 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3885 int row
= m_clientArea
->GetRowByItem( item
);
3888 //Unselect all rows before select another in the single select mode
3889 if (m_clientArea
->IsSingleSel())
3890 m_clientArea
->SelectAllRows(false);
3891 m_clientArea
->SelectRow(row
, true);
3895 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3897 int row
= m_clientArea
->GetRowByItem( item
);
3899 m_clientArea
->SelectRow(row
, false);
3902 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3904 int row
= m_clientArea
->GetRowByItem( item
);
3907 return m_clientArea
->IsRowSelected(row
);
3912 //Selection code with row number as parameter
3913 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3916 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3917 int len
= selection
.GetCount();
3918 for( int i
= 0; i
< len
; i
++)
3920 unsigned int row
= selection
[i
];
3926 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3928 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3929 int len
= sel
.GetCount();
3930 for( int i
= 0; i
< len
; i
++ )
3934 selection
.Add( static_cast<unsigned int>(row
) );
3936 m_clientArea
->SetSelections( selection
);
3939 void wxDataViewCtrl::Select( int row
)
3943 if (m_clientArea
->IsSingleSel())
3944 m_clientArea
->SelectAllRows(false);
3945 m_clientArea
->SelectRow( row
, true );
3949 void wxDataViewCtrl::Unselect( int row
)
3952 m_clientArea
->SelectRow(row
, false);
3955 bool wxDataViewCtrl::IsSelected( int row
) const
3958 return m_clientArea
->IsRowSelected(row
);
3962 void wxDataViewCtrl::SelectRange( int from
, int to
)
3965 for( int i
= from
; i
< to
; i
++ )
3967 m_clientArea
->Select(sel
);
3970 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3972 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3973 for( int i
= from
; i
< to
; i
++ )
3974 if( sel
.Index( i
) != wxNOT_FOUND
)
3976 m_clientArea
->SetSelections(sel
);
3979 void wxDataViewCtrl::SelectAll()
3981 m_clientArea
->SelectAllRows(true);
3984 void wxDataViewCtrl::UnselectAll()
3986 m_clientArea
->SelectAllRows(false);
3989 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3993 if( row
> (int) m_clientArea
->GetRowCount() )
3994 row
= m_clientArea
->GetRowCount();
3996 int first
= m_clientArea
->GetFirstVisibleRow();
3997 int last
= m_clientArea
->GetLastVisibleRow();
3999 m_clientArea
->ScrollTo( row
, column
);
4000 else if( row
> last
)
4001 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4003 m_clientArea
->ScrollTo( first
, column
);
4006 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4008 int row
= m_clientArea
->GetRowByItem(item
);
4011 if( column
== NULL
)
4012 EnsureVisible(row
, -1);
4016 int len
= GetColumnCount();
4017 for( int i
= 0; i
< len
; i
++ )
4018 if( GetColumn(i
) == column
)
4023 EnsureVisible( row
, col
);
4029 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4031 m_clientArea
->HitTest(point
, item
, column
);
4034 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4036 return m_clientArea
->GetItemRect(item
, column
);
4039 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4041 return m_clientArea
->GetItemByRow( row
);
4044 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4046 return m_clientArea
->GetRowByItem( item
);
4049 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4051 int row
= m_clientArea
->GetRowByItem( item
);
4053 m_clientArea
->Expand(row
);
4056 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4058 int row
= m_clientArea
->GetRowByItem( item
);
4060 m_clientArea
->Collapse(row
);
4064 // !wxUSE_GENERICDATAVIEWCTRL
4067 // wxUSE_DATAVIEWCTRL