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 // ---------------------------------------------------------
639 // wxDataViewTextRenderer
640 // ---------------------------------------------------------
642 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
644 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
645 wxDataViewCellMode mode
, int align
) :
646 wxDataViewCustomRenderer( varianttype
, mode
, align
)
650 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
652 m_text
= value
.GetString();
657 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
662 bool wxDataViewTextRenderer::HasEditorCtrl()
667 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
668 wxRect labelRect
, const wxVariant
&value
)
670 return new wxTextCtrl( parent
, wxID_ANY
, value
,
671 wxPoint(labelRect
.x
,labelRect
.y
),
672 wxSize(labelRect
.width
,labelRect
.height
) );
675 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
677 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
678 value
= text
->GetValue();
682 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
684 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
685 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
686 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
687 view
->GetForegroundColour();
689 dc
->SetTextForeground(col
);
690 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
691 // dc->DrawText( m_text, cell.x, cell.y );
696 wxSize
wxDataViewTextRenderer::GetSize() const
698 const wxDataViewCtrl
*view
= GetView();
702 view
->GetTextExtent( m_text
, &x
, &y
);
703 return wxSize( x
, y
);
705 return wxSize(80,20);
708 // ---------------------------------------------------------
709 // wxDataViewBitmapRenderer
710 // ---------------------------------------------------------
712 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
714 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
715 wxDataViewCellMode mode
, int align
) :
716 wxDataViewCustomRenderer( varianttype
, mode
, align
)
720 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
722 if (value
.GetType() == wxT("wxBitmap"))
724 if (value
.GetType() == wxT("wxIcon"))
730 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
735 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
738 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
739 else if (m_icon
.Ok())
740 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
745 wxSize
wxDataViewBitmapRenderer::GetSize() const
748 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
749 else if (m_icon
.Ok())
750 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
752 return wxSize(16,16);
755 // ---------------------------------------------------------
756 // wxDataViewToggleRenderer
757 // ---------------------------------------------------------
759 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
761 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
762 wxDataViewCellMode mode
, int align
) :
763 wxDataViewCustomRenderer( varianttype
, mode
, align
)
768 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
770 m_toggle
= value
.GetBool();
775 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
780 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
782 // User wxRenderer here
785 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
787 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
792 flags
|= wxCONTROL_CHECKED
;
793 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
794 flags
|= wxCONTROL_DISABLED
;
796 wxRendererNative::Get().DrawCheckBox(
797 GetOwner()->GetOwner(),
805 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
806 wxDataViewModel
*model
,
807 const wxDataViewItem
& item
, unsigned int col
)
809 bool value
= !m_toggle
;
810 wxVariant variant
= value
;
811 model
->SetValue( variant
, item
, col
);
812 model
->ValueChanged( item
, col
);
816 wxSize
wxDataViewToggleRenderer::GetSize() const
818 return wxSize(20,20);
821 // ---------------------------------------------------------
822 // wxDataViewProgressRenderer
823 // ---------------------------------------------------------
825 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
827 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
828 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
829 wxDataViewCustomRenderer( varianttype
, mode
, align
)
835 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
839 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
841 m_value
= (long) value
;
843 if (m_value
< 0) m_value
= 0;
844 if (m_value
> 100) m_value
= 100;
849 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
851 value
= (long) m_value
;
855 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
857 double pct
= (double)m_value
/ 100.0;
859 bar
.width
= (int)(cell
.width
* pct
);
860 dc
->SetPen( *wxTRANSPARENT_PEN
);
861 dc
->SetBrush( *wxBLUE_BRUSH
);
862 dc
->DrawRectangle( bar
);
864 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
865 dc
->SetPen( *wxBLACK_PEN
);
866 dc
->DrawRectangle( cell
);
871 wxSize
wxDataViewProgressRenderer::GetSize() const
873 return wxSize(40,12);
876 // ---------------------------------------------------------
877 // wxDataViewDateRenderer
878 // ---------------------------------------------------------
880 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
882 #if wxUSE_DATE_RENDERER_POPUP
884 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
887 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
888 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
889 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
894 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
895 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
896 sizer
->Add( m_cal
, 1, wxGROW
);
901 void OnCalendar( wxCalendarEvent
&event
);
903 wxCalendarCtrl
*m_cal
;
904 wxDataViewModel
*m_model
;
906 const wxDataViewItem
& m_item
;
909 virtual void OnDismiss()
914 DECLARE_EVENT_TABLE()
917 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
918 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
921 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
923 wxDateTime date
= event
.GetDate();
924 wxVariant value
= date
;
925 m_model
->SetValue( value
, m_item
, m_col
);
926 m_model
->ValueChanged( m_item
, m_col
);
930 #endif // wxUSE_DATE_RENDERER_POPUP
932 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
934 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
935 wxDataViewCellMode mode
, int align
) :
936 wxDataViewCustomRenderer( varianttype
, mode
, align
)
940 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
942 m_date
= value
.GetDateTime();
947 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
953 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
955 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
956 wxString tmp
= m_date
.FormatDate();
957 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
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
&value
) const
1020 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1022 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1024 dc
->SetFont( view
->GetFont() );
1026 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1027 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1028 view
->GetForegroundColour();
1030 dc
->SetTextForeground(col
);
1032 const wxIcon
&icon
= m_value
.GetIcon();
1035 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1036 cell
.x
+= icon
.GetWidth()+4;
1039 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
1044 wxSize
wxDataViewIconTextRenderer::GetSize() const
1046 const wxDataViewCtrl
*view
= GetView();
1047 if (!m_value
.GetText().empty())
1050 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1052 if (m_value
.GetIcon().IsOk())
1053 x
+= m_value
.GetIcon().GetWidth() + 4;
1054 return wxSize( x
, y
);
1056 return wxSize(80,20);
1059 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1064 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1069 // ---------------------------------------------------------
1071 // ---------------------------------------------------------
1073 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1075 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1076 unsigned int model_column
,
1077 int width
, wxAlignment align
, int flags
) :
1078 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1080 SetAlignment(align
);
1084 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1087 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1088 unsigned int model_column
,
1089 int width
, wxAlignment align
, int flags
) :
1090 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1092 SetAlignment(align
);
1095 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1098 wxDataViewColumn::~wxDataViewColumn()
1102 void wxDataViewColumn::Init( int width
)
1105 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1109 void wxDataViewColumn::SetResizeable( bool resizeable
)
1112 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1114 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1117 void wxDataViewColumn::SetHidden( bool hidden
)
1120 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1122 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1124 // tell our owner to e.g. update its scrollbars:
1126 GetOwner()->OnColumnChange();
1129 void wxDataViewColumn::SetSortable( bool sortable
)
1132 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1134 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1136 // Update header button
1138 GetOwner()->OnColumnChange();
1141 void wxDataViewColumn::SetSortOrder( bool ascending
)
1143 m_ascending
= ascending
;
1145 // Update header button
1147 GetOwner()->OnColumnChange();
1150 bool wxDataViewColumn::IsSortOrderAscending() const
1155 void wxDataViewColumn::SetInternalWidth( int width
)
1159 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1160 if (m_owner
&& m_owner
->m_clientArea
)
1161 m_owner
->m_clientArea
->RecalculateDisplay();
1164 void wxDataViewColumn::SetWidth( int width
)
1166 m_owner
->m_headerArea
->UpdateDisplay();
1168 SetInternalWidth(width
);
1172 //-----------------------------------------------------------------------------
1173 // wxDataViewHeaderWindowBase
1174 //-----------------------------------------------------------------------------
1176 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1178 wxWindow
*parent
= GetParent();
1179 wxDataViewEvent
le(type
, parent
->GetId());
1181 le
.SetEventObject(parent
);
1183 le
.SetDataViewColumn(GetColumn(n
));
1184 le
.SetModel(GetOwner()->GetModel());
1186 // for events created by wxDataViewHeaderWindow the
1187 // row / value fields are not valid
1189 parent
->GetEventHandler()->ProcessEvent(le
);
1192 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1194 // implemented in msw/listctrl.cpp:
1195 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1197 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1199 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1200 const wxPoint
&pos
, const wxSize
&size
,
1201 const wxString
&name
)
1205 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1208 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1209 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1210 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1211 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1213 // create the native WC_HEADER window:
1214 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1215 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1216 m_hWnd
= CreateWindowEx(0,
1227 wxLogLastError(_T("CreateWindowEx"));
1231 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1232 // to call wxDataViewHeaderWindow::MSWOnNotify
1233 SubclassWin(m_hWnd
);
1235 // the following is required to get the default win's font for
1236 // header windows and must be done befor sending the HDM_LAYOUT msg
1243 // Retrieve the bounding rectangle of the parent window's
1244 // client area, and then request size and position values
1245 // from the header control.
1246 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1248 hdl
.prc
= &rcParent
;
1250 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1252 wxLogLastError(_T("SendMessage"));
1256 // Set the size, position, and visibility of the header control.
1257 SetWindowPos((HWND
)m_hWnd
,
1261 wp
.flags
| SWP_SHOWWINDOW
);
1263 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1264 // a wxBoxSizer and in order to avoid super-big header windows,
1265 // we need to set our height as fixed
1266 SetMinSize(wxSize(-1, wp
.cy
));
1267 SetMaxSize(wxSize(-1, wp
.cy
));
1272 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1277 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1279 // remove old columns
1280 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1281 Header_DeleteItem((HWND
)m_hWnd
, 0);
1283 // add the updated array of columns to the header control
1284 unsigned int cols
= GetOwner()->GetColumnCount();
1285 unsigned int added
= 0;
1286 wxDataViewModel
* model
= GetOwner()->GetModel();
1287 for (unsigned int i
= 0; i
< cols
; i
++)
1289 wxDataViewColumn
*col
= GetColumn( i
);
1290 if (col
->IsHidden())
1291 continue; // don't add it!
1294 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1295 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1296 hdi
.cxy
= col
->GetWidth();
1297 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1298 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1299 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1302 if(model
&& m_owner
->GetSortingColumn() == col
)
1304 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1305 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1306 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1310 // lParam is reserved for application's use:
1311 // we store there the column index to use it later in MSWOnNotify
1312 // (since columns may have been hidden)
1313 hdi
.lParam
= (LPARAM
)i
;
1315 // the native wxMSW implementation of the header window
1316 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1317 // on the right: to correct this effect we make the column
1318 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1320 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1322 switch (col
->GetAlignment())
1325 hdi
.fmt
|= HDF_LEFT
;
1327 case wxALIGN_CENTER
:
1328 case wxALIGN_CENTER_HORIZONTAL
:
1329 hdi
.fmt
|= HDF_CENTER
;
1332 hdi
.fmt
|= HDF_RIGHT
;
1336 // such alignment is not allowed for the column header!
1340 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1341 (WPARAM
)added
, (LPARAM
)&hdi
);
1346 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1350 // NOTE: we don't just return nmHDR->iItem because when there are
1351 // hidden columns, nmHDR->iItem may be different from
1352 // nmHDR->pitem->lParam
1354 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1356 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1361 item
.mask
= HDI_LPARAM
;
1362 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1364 return (unsigned int)item
.lParam
;
1367 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1369 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1371 // is it a message from the header?
1372 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1373 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1375 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1376 switch ( nmhdr
->code
)
1378 case HDN_BEGINTRACK
:
1379 // user has started to resize a column:
1380 // do we need to veto it?
1381 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1389 // user has started to reorder a column
1392 case HDN_ITEMCHANGING
:
1393 if (nmHDR
->pitem
!= NULL
&&
1394 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1396 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1397 if (nmHDR
->pitem
->cxy
< minWidth
)
1399 // do not allow the user to resize this column under
1400 // its minimal width:
1406 case HDN_ITEMCHANGED
: // user is resizing a column
1407 case HDN_ENDTRACK
: // user has finished resizing a column
1408 case HDN_ENDDRAG
: // user has finished reordering a column
1410 // update the width of the modified column:
1411 if (nmHDR
->pitem
!= NULL
&&
1412 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1414 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1415 unsigned int w
= nmHDR
->pitem
->cxy
;
1416 wxDataViewColumn
*col
= GetColumn(idx
);
1418 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1419 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1420 w
-= COLUMN_WIDTH_OFFSET
;
1422 if (w
>= (unsigned)col
->GetMinWidth())
1423 col
->SetInternalWidth(w
);
1429 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1430 wxDataViewModel
* model
= GetOwner()->GetModel();
1432 if(nmHDR
->iButton
== 0)
1434 wxDataViewColumn
*col
= GetColumn(idx
);
1435 if(col
->IsSortable())
1437 if(model
&& m_owner
->GetSortingColumn() == col
)
1439 bool order
= col
->IsSortOrderAscending();
1440 col
->SetSortOrder(!order
);
1444 m_owner
->SetSortingColumn(col
);
1452 wxEventType evt
= nmHDR
->iButton
== 0 ?
1453 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1454 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1455 SendEvent(evt
, idx
);
1461 // NOTE: for some reason (i.e. for a bug in Windows)
1462 // the HDN_ITEMCLICK notification is not sent on
1463 // right clicks, so we need to handle NM_RCLICK
1466 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1467 if (column
!= wxNOT_FOUND
)
1470 item
.mask
= HDI_LPARAM
;
1471 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1473 // 'idx' may be different from 'column' if there are
1474 // hidden columns...
1475 unsigned int idx
= (unsigned int)item
.lParam
;
1476 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1482 case HDN_GETDISPINFOW
:
1483 // see wxListCtrl::MSWOnNotify for more info!
1486 case HDN_ITEMDBLCLICK
:
1488 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1489 int w
= GetOwner()->GetBestColumnWidth(idx
);
1491 // update the native control:
1493 ZeroMemory(&hd
, sizeof(hd
));
1494 hd
.mask
= HDI_WIDTH
;
1496 Header_SetItem(GetHwnd(),
1497 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1500 // update the wxDataViewColumn class:
1501 GetColumn(idx
)->SetInternalWidth(w
);
1506 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1512 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1513 const wxRect
*WXUNUSED(rect
))
1515 wxSize ourSz
= GetClientSize();
1516 wxSize ownerSz
= m_owner
->GetClientSize();
1518 // where should the (logical) origin of this window be placed?
1520 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1522 // put this window on top of our parent and
1523 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1524 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1528 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1529 int WXUNUSED(w
), int WXUNUSED(h
),
1532 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1533 // the wxDataViewCtrl window gets resized: the following dummy call
1534 // to ScrollWindow() is required in order to get this header window
1535 // correctly repainted when it's (horizontally) scrolled:
1540 #else // !defined(__WXMSW__)
1542 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1543 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1544 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1545 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1546 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1549 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1550 const wxPoint
&pos
, const wxSize
&size
,
1551 const wxString
&name
)
1555 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1558 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1559 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1560 SetOwnForegroundColour( attr
.colFg
);
1561 SetOwnBackgroundColour( attr
.colBg
);
1563 SetOwnFont( attr
.font
);
1565 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1566 // a wxBoxSizer and in order to avoid super-big header windows,
1567 // we need to set our height as fixed
1568 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1569 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1574 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1577 GetClientSize( &w
, &h
);
1579 wxAutoBufferedPaintDC
dc( this );
1581 dc
.SetBackground(GetBackgroundColour());
1585 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1588 m_owner
->GetViewStart( &x
, NULL
);
1590 // account for the horz scrollbar offset
1591 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1593 dc
.SetFont( GetFont() );
1595 unsigned int cols
= GetOwner()->GetColumnCount();
1598 for (i
= 0; i
< cols
; i
++)
1600 wxDataViewColumn
*col
= GetColumn( i
);
1601 if (col
->IsHidden())
1602 continue; // skip it!
1604 int cw
= col
->GetWidth();
1607 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1608 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1610 if (col
->IsSortOrderAscending())
1611 sortArrow
= wxHDR_SORT_ICON_UP
;
1613 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1617 if (m_parent
->IsEnabled())
1619 if ((int) i
== m_hover
)
1620 state
= wxCONTROL_CURRENT
;
1624 state
= (int) wxCONTROL_DISABLED
;
1627 wxRendererNative::Get().DrawHeaderButton
1631 wxRect(xpos
, 0, cw
, ch
-1),
1636 // align as required the column title:
1638 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1639 switch (col
->GetAlignment())
1642 x
+= HEADER_HORIZ_BORDER
;
1645 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1648 case wxALIGN_CENTER
:
1649 case wxALIGN_CENTER_HORIZONTAL
:
1650 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1654 // always center the title vertically:
1655 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1657 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1659 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1660 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1661 dc
.DrawText( col
->GetTitle(), x
, y
);
1662 dc
.DestroyClippingRegion();
1668 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1670 GetParent()->SetFocus();
1674 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1676 // we want to work with logical coords
1678 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1679 int y
= event
.GetY();
1683 // we don't draw the line beyond our window,
1684 // but we allow dragging it there
1686 GetClientSize( &w
, NULL
);
1687 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1690 if (event
.ButtonUp())
1692 m_isDragging
= false;
1698 m_currentX
= wxMax(m_minX
+ 7, x
);
1702 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1704 GetOwner()->Refresh();
1708 else // not dragging
1711 m_column
= wxNOT_FOUND
;
1713 bool hit_border
= false;
1715 // end of the current column
1718 // find the column where this event occured
1719 int countCol
= m_owner
->GetColumnCount();
1720 for (int column
= 0; column
< countCol
; column
++)
1722 wxDataViewColumn
*p
= GetColumn(column
);
1725 continue; // skip if not shown
1727 xpos
+= p
->GetWidth();
1729 if ((abs(x
-xpos
) < 3) && (y
< 22))
1737 // inside the column
1744 int old_hover
= m_hover
;
1746 if (event
.Leaving())
1747 m_hover
= wxNOT_FOUND
;
1748 if (old_hover
!= m_hover
)
1751 if (m_column
== wxNOT_FOUND
)
1754 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1755 if (event
.LeftDClick() && resizeable
)
1757 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1760 else if (event
.LeftDown() || event
.RightUp())
1762 if (hit_border
&& event
.LeftDown() && resizeable
)
1764 m_isDragging
= true;
1768 else // click on a column
1770 wxDataViewModel
* model
= GetOwner()->GetModel();
1771 wxEventType evt
= event
.LeftDown() ?
1772 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1773 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1774 SendEvent(evt
, m_column
);
1776 //Left click the header
1777 if(event
.LeftDown())
1779 wxDataViewColumn
*col
= GetColumn(m_column
);
1780 if(col
->IsSortable())
1782 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1783 if(model
&& sortCol
== col
)
1785 bool order
= col
->IsSortOrderAscending();
1786 col
->SetSortOrder(!order
);
1790 m_owner
->SetSortingColumn(col
);
1796 //Send the column sorted event
1797 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1801 else if (event
.Moving())
1803 if (hit_border
&& resizeable
)
1804 m_currentCursor
= m_resizeCursor
;
1806 m_currentCursor
= wxSTANDARD_CURSOR
;
1808 SetCursor(*m_currentCursor
);
1813 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1817 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1818 m_owner
->GetViewStart( &x
, NULL
);
1820 // shift the DC origin to match the position of the main window horizontal
1821 // scrollbar: this allows us to always use logical coords
1822 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1825 #endif // defined(__WXMSW__)
1827 //-----------------------------------------------------------------------------
1828 // wxDataViewRenameTimer
1829 //-----------------------------------------------------------------------------
1831 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1836 void wxDataViewRenameTimer::Notify()
1838 m_owner
->OnRenameTimer();
1841 //-----------------------------------------------------------------------------
1842 // wxDataViewMainWindow
1843 //-----------------------------------------------------------------------------
1845 //The tree building helper, declared firstly
1846 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1848 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1850 if (row1
> row2
) return 1;
1851 if (row1
== row2
) return 0;
1856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1858 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1859 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1860 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1861 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1862 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1863 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1866 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1867 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1868 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1869 m_selection( wxDataViewSelectionCmp
)
1874 m_lastOnSame
= false;
1875 m_renameTimer
= new wxDataViewRenameTimer( this );
1877 // TODO: user better initial values/nothing selected
1878 m_currentCol
= NULL
;
1881 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1884 m_dragStart
= wxPoint(0,0);
1885 m_lineLastClicked
= (unsigned int) -1;
1886 m_lineBeforeLastClicked
= (unsigned int) -1;
1887 m_lineSelectSingleOnUp
= (unsigned int) -1;
1891 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1892 SetBackgroundColour( *wxWHITE
);
1894 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1896 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1897 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1898 //Some new added code to deal with the tree structure
1899 m_root
= new wxDataViewTreeNode( NULL
);
1900 m_root
->SetHasChildren(true);
1902 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1904 m_underMouse
= NULL
;
1908 wxDataViewMainWindow::~wxDataViewMainWindow()
1911 delete m_renameTimer
;
1914 void wxDataViewMainWindow::OnRenameTimer()
1916 // We have to call this here because changes may just have
1917 // been made and no screen update taken place.
1922 unsigned int cols
= GetOwner()->GetColumnCount();
1924 for (i
= 0; i
< cols
; i
++)
1926 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1928 continue; // skip it!
1930 if (c
== m_currentCol
)
1932 xpos
+= c
->GetWidth();
1934 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1935 m_currentCol
->GetWidth(), m_lineHeight
);
1937 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1938 &labelRect
.x
, &labelRect
.y
);
1940 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1941 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1945 //------------------------------------------------------------------
1946 // Helper class for do operation on the tree node
1947 //------------------------------------------------------------------
1954 //The return value control how the tree-walker tranverse the tree
1955 // 0: Job done, stop tranverse and return
1956 // 1: Ignore the current node's subtree and continue
1957 // 2: Job not done, continue
1958 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1959 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1960 virtual int operator() ( void * n
) = 0;
1963 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1968 switch( func( node
) )
1979 wxDataViewTreeNodes nodes
= node
->GetNodes();
1980 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1982 int len_nodes
= nodes
.GetCount();
1983 int len
= leaves
.GetCount();
1984 int i
= 0, nodes_i
= 0;
1986 for( ; i
< len
; i
++ )
1988 void * n
= leaves
[i
];
1989 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1991 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1994 if( Walker( nd
, func
) )
2013 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2017 wxDataViewTreeNode
* node
;
2018 node
= FindNode(parent
);
2023 node
->SetHasChildren( true );
2025 if( g_model
->IsContainer( item
) )
2027 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2028 newnode
->SetItem(item
);
2029 newnode
->SetHasChildren( true );
2030 node
->AddNode( newnode
);
2033 node
->AddLeaf( item
.GetID() );
2035 node
->ChangeSubTreeCount(1);
2043 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2045 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2046 const wxDataViewItem
& item
)
2048 wxDataViewTreeNode
* node
= FindNode(parent
);
2050 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2051 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2054 node
->GetChildren().Remove( item
.GetID() );
2055 //Manuplate selection
2056 if( m_selection
.GetCount() > 1 )
2058 m_selection
.Empty();
2060 bool isContainer
= false;
2061 wxDataViewTreeNodes nds
= node
->GetNodes();
2062 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2064 if (nds
[i
]->GetItem() == item
)
2072 wxDataViewTreeNode
* n
= NULL
;
2073 wxDataViewTreeNodes nodes
= node
->GetNodes();
2074 int len
= nodes
.GetCount();
2075 for( int i
= 0 ; i
< len
; i
++)
2077 if( nodes
[i
]->GetItem() == item
)
2084 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2086 node
->GetNodes().Remove( n
);
2087 sub
-= n
->GetSubTreeCount();
2088 DestroyTreeHelper(n
);
2090 //Make the row number invalid and get a new valid one when user call GetRowCount
2092 node
->ChangeSubTreeCount(sub
);
2093 if( node
->GetChildrenNumber() == 0)
2095 node
->GetParent()->GetNodes().Remove( node
);
2099 //Change the current row to the last row if the current exceed the max row number
2100 if( m_currentRow
> GetRowCount() )
2101 m_currentRow
= m_count
- 1;
2108 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2114 wxWindow
*parent
= GetParent();
2115 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2116 le
.SetEventObject(parent
);
2117 le
.SetModel(GetOwner()->GetModel());
2119 parent
->GetEventHandler()->ProcessEvent(le
);
2124 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2126 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2127 /*#define MAX_VIRTUAL_WIDTH 100000
2129 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2130 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2131 Refresh( true, &rect );
2139 wxWindow
*parent
= GetParent();
2140 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2141 le
.SetEventObject(parent
);
2142 le
.SetModel(GetOwner()->GetModel());
2145 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2146 parent
->GetEventHandler()->ProcessEvent(le
);
2151 bool wxDataViewMainWindow::Cleared()
2161 void wxDataViewMainWindow::UpdateDisplay()
2166 void wxDataViewMainWindow::OnInternalIdle()
2168 wxWindow::OnInternalIdle();
2172 RecalculateDisplay();
2177 void wxDataViewMainWindow::RecalculateDisplay()
2179 wxDataViewModel
*model
= GetOwner()->GetModel();
2186 int width
= GetEndOfLastCol();
2187 int height
= GetRowCount() * m_lineHeight
;
2189 SetVirtualSize( width
, height
);
2190 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2195 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2197 wxWindow::ScrollWindow( dx
, dy
, rect
);
2199 if (GetOwner()->m_headerArea
)
2200 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2203 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2206 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2207 int sy
= rows
*m_lineHeight
/y
;
2211 wxRect rect
= GetClientRect();
2213 int x_start
= 0, x_end
= 0, w
= 0;
2215 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2216 for (x_start
= 0; colnum
< column
; colnum
++)
2218 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2219 if (col
->IsHidden())
2220 continue; // skip it!
2222 w
= col
->GetWidth();
2226 x_end
= x_start
+ w
;
2227 xe
= xx
+ rect
.width
;
2230 sx
= ( xx
+ x_end
- xe
)/x
;
2237 m_owner
->Scroll( sx
, sy
);
2240 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2242 wxDataViewModel
*model
= GetOwner()->GetModel();
2243 wxAutoBufferedPaintDC
dc( this );
2246 dc
.SetBackground(GetBackgroundColour());
2248 GetOwner()->PrepareDC( dc
);
2249 dc
.SetFont( GetFont() );
2251 wxRect update
= GetUpdateRegion().GetBox();
2252 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2254 // compute which items needs to be redrawn
2255 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2256 unsigned int item_count
=
2257 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2258 (int)(GetRowCount( )- item_start
) );
2259 unsigned int item_last
= item_start
+ item_count
;
2261 // compute which columns needs to be redrawn
2262 unsigned int cols
= GetOwner()->GetColumnCount();
2263 unsigned int col_start
= 0;
2264 unsigned int x_start
= 0;
2265 for (x_start
= 0; col_start
< cols
; col_start
++)
2267 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2268 if (col
->IsHidden())
2269 continue; // skip it!
2271 unsigned int w
= col
->GetWidth();
2272 if (x_start
+w
>= (unsigned int)update
.x
)
2278 unsigned int col_last
= col_start
;
2279 unsigned int x_last
= x_start
;
2280 for (; col_last
< cols
; col_last
++)
2282 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2283 if (col
->IsHidden())
2284 continue; // skip it!
2286 if (x_last
> (unsigned int)update
.GetRight())
2289 x_last
+= col
->GetWidth();
2292 // Draw horizontal rules if required
2293 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2295 dc
.SetPen(m_penRule
);
2296 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2298 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2300 int y
= i
* m_lineHeight
;
2301 dc
.DrawLine(x_start
, y
, x_last
, y
);
2305 // Draw vertical rules if required
2306 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2308 dc
.SetPen(m_penRule
);
2309 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2312 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2314 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2315 if (col
->IsHidden())
2316 continue; // skip it
2318 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2319 x
, item_last
* m_lineHeight
);
2321 x
+= col
->GetWidth();
2324 // Draw last vertical rule
2325 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2326 x
, item_last
* m_lineHeight
);
2329 // redraw the background for the items which are selected/current
2330 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2332 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2333 if (selected
|| item
== m_currentRow
)
2335 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2336 if (item
== m_currentRow
)
2337 flags
|= wxCONTROL_CURRENT
;
2339 flags
|= wxCONTROL_FOCUSED
;
2341 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2342 wxRendererNative::Get().DrawItemSelectionRect
2352 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2355 // TODO: last column for RTL support
2356 expander
= GetOwner()->GetColumn( 0 );
2357 GetOwner()->SetExpanderColumn(expander
);
2360 // redraw all cells for all rows which must be repainted and for all columns
2362 cell_rect
.x
= x_start
;
2363 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2364 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2366 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2367 wxDataViewRenderer
*cell
= col
->GetRenderer();
2368 cell_rect
.width
= col
->GetWidth();
2370 if (col
->IsHidden())
2371 continue; // skipt it!
2374 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2376 // get the cell value and set it into the renderer
2378 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2382 wxDataViewItem dataitem
= node
->GetItem();
2384 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2387 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2388 cell
->SetValue( value
);
2390 // update the y offset
2391 cell_rect
.y
= item
* m_lineHeight
;
2393 //Draw the expander here.
2394 int indent
= node
->GetIndentLevel();
2395 if( col
== expander
)
2397 //Calculate the indent first
2398 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2400 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2401 // change the cell_rect.x to the appropriate pos
2402 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2403 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2404 dc
.SetPen( m_penExpander
);
2405 dc
.SetBrush( wxNullBrush
);
2406 if( node
->HasChildren() )
2408 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2410 if (m_underMouse
== node
)
2412 flag
|= wxCONTROL_CURRENT
;
2414 if( node
->IsOpen() )
2415 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2417 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2421 // I am wandering whether we should draw dot lines between tree nodes
2423 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2426 //force the expander column to left-center align
2427 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2431 // cannot be bigger than allocated space
2432 wxSize size
= cell
->GetSize();
2433 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2434 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2435 // size.y = wxMin( size.y, cell_rect.height );
2436 size
.y
= cell_rect
.height
;
2438 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2439 int align
= cell
->GetAlignment();
2441 // horizontal alignment:
2442 item_rect
.x
= cell_rect
.x
;
2443 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2444 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2445 else if (align
& wxALIGN_RIGHT
)
2446 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2447 //else: wxALIGN_LEFT is the default
2449 // vertical alignment:
2450 item_rect
.y
= cell_rect
.y
;
2451 if (align
& wxALIGN_CENTER_VERTICAL
)
2452 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2453 else if (align
& wxALIGN_BOTTOM
)
2454 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2455 //else: wxALIGN_TOP is the default
2458 item_rect
.x
+= PADDING_RIGHTLEFT
;
2459 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2461 //Here we add the tree indent
2462 item_rect
.x
+= indent
;
2465 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2466 state
|= wxDATAVIEW_CELL_SELECTED
;
2468 // TODO: it would be much more efficient to create a clipping
2469 // region for the entire column being rendered (in the OnPaint
2470 // of wxDataViewMainWindow) instead of a single clip region for
2471 // each cell. However it would mean that each renderer should
2472 // respect the given wxRect's top & bottom coords, eventually
2473 // violating only the left & right coords - however the user can
2474 // make its own renderer and thus we cannot be sure of that.
2475 dc
.SetClippingRegion( item_rect
);
2476 cell
->Render( item_rect
, &dc
, state
);
2477 dc
.DestroyClippingRegion();
2480 cell_rect
.x
+= cell_rect
.width
;
2484 int wxDataViewMainWindow::GetCountPerPage() const
2486 wxSize size
= GetClientSize();
2487 return size
.y
/ m_lineHeight
;
2490 int wxDataViewMainWindow::GetEndOfLastCol() const
2494 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2496 const wxDataViewColumn
*c
=
2497 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2500 width
+= c
->GetWidth();
2505 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2509 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2511 return y
/ m_lineHeight
;
2514 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2516 wxSize client_size
= GetClientSize();
2517 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2518 &client_size
.x
, &client_size
.y
);
2520 //we should deal with the pixel here
2521 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2523 return wxMin( GetRowCount()-1, row
);
2526 unsigned int wxDataViewMainWindow::GetRowCount()
2528 if ( m_count
== -1 )
2530 m_count
= RecalculateCount();
2532 GetVirtualSize( &width
, &height
);
2533 height
= m_count
* m_lineHeight
;
2535 SetVirtualSize( width
, height
);
2540 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2547 void wxDataViewMainWindow::SelectAllRows( bool on
)
2554 m_selection
.Clear();
2555 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2556 m_selection
.Add( i
);
2561 unsigned int first_visible
= GetFirstVisibleRow();
2562 unsigned int last_visible
= GetLastVisibleRow();
2564 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2566 unsigned int row
= m_selection
[i
];
2567 if ((row
>= first_visible
) && (row
<= last_visible
))
2570 m_selection
.Clear();
2574 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2576 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2580 m_selection
.Add( row
);
2588 m_selection
.Remove( row
);
2594 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2598 unsigned int tmp
= from
;
2604 for (i
= from
; i
<= to
; i
++)
2606 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2609 m_selection
.Add( i
);
2614 m_selection
.Remove( i
);
2617 RefreshRows( from
, to
);
2620 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2622 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2624 int n
= aSelections
[i
];
2626 m_selection
.Add( n
);
2631 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2633 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2634 m_selection
.Add( row
);
2636 m_selection
.Remove( row
);
2640 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2642 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2645 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2647 wxWindow
*parent
= GetParent();
2648 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2650 le
.SetEventObject(parent
);
2651 le
.SetModel(GetOwner()->GetModel());
2654 parent
->GetEventHandler()->ProcessEvent(le
);
2657 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2659 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2660 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2662 wxSize client_size
= GetClientSize();
2663 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2664 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2665 if (intersect_rect
.width
> 0)
2666 Refresh( true, &intersect_rect
);
2669 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2673 unsigned int tmp
= to
;
2678 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2679 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2681 wxSize client_size
= GetClientSize();
2682 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2683 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2684 if (intersect_rect
.width
> 0)
2685 Refresh( true, &intersect_rect
);
2688 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2690 unsigned int count
= GetRowCount();
2691 if (firstRow
> count
)
2694 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2695 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2697 wxSize client_size
= GetClientSize();
2698 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2699 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2700 if (intersect_rect
.width
> 0)
2701 Refresh( true, &intersect_rect
);
2704 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2706 wxCHECK_RET( newCurrent
< GetRowCount(),
2707 _T("invalid item index in OnArrowChar()") );
2709 // if there is no selection, we cannot move it anywhere
2710 if (!HasCurrentRow())
2713 unsigned int oldCurrent
= m_currentRow
;
2715 // in single selection we just ignore Shift as we can't select several
2717 if ( event
.ShiftDown() && !IsSingleSel() )
2719 RefreshRow( oldCurrent
);
2721 ChangeCurrentRow( newCurrent
);
2723 // select all the items between the old and the new one
2724 if ( oldCurrent
> newCurrent
)
2726 newCurrent
= oldCurrent
;
2727 oldCurrent
= m_currentRow
;
2730 SelectRows( oldCurrent
, newCurrent
, true );
2731 if (oldCurrent
!=newCurrent
)
2732 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2736 RefreshRow( oldCurrent
);
2738 // all previously selected items are unselected unless ctrl is held
2739 if ( !event
.ControlDown() )
2740 SelectAllRows(false);
2742 ChangeCurrentRow( newCurrent
);
2744 if ( !event
.ControlDown() )
2746 SelectRow( m_currentRow
, true );
2747 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2750 RefreshRow( m_currentRow
);
2753 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2756 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2760 rect
.y
= m_lineHeight
* row
;
2761 rect
.width
= GetEndOfLastCol();
2762 rect
.height
= m_lineHeight
;
2767 class RowToItemJob
: public DoJob
2770 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2771 virtual ~RowToItemJob(){};
2773 virtual int operator() ( wxDataViewTreeNode
* node
)
2776 if( current
== static_cast<int>(row
))
2778 ret
= node
->GetItem() ;
2782 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2784 current
+= node
->GetSubTreeCount();
2789 //If the current has no child node, we can find the desired item of the row number directly.
2790 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2791 if( node
->GetNodes().GetCount() == 0)
2793 int index
= static_cast<int>(row
) - current
- 1;
2794 ret
= node
->GetChildren().Item( index
);
2801 virtual int operator() ( void * n
)
2804 if( current
== static_cast<int>(row
))
2806 ret
= wxDataViewItem( n
) ;
2811 wxDataViewItem
GetResult(){ return ret
; }
2818 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2820 RowToItemJob
job( row
, -2 );
2821 Walker( m_root
, job
);
2822 return job
.GetResult();
2825 class RowToTreeNodeJob
: public DoJob
2828 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2831 this->current
= current
;
2835 virtual ~RowToTreeNodeJob(){};
2837 virtual int operator() ( wxDataViewTreeNode
* node
)
2840 if( current
== static_cast<int>(row
))
2846 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2848 current
+= node
->GetSubTreeCount();
2854 //If the current has no child node, we can find the desired item of the row number directly.
2855 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2856 if( node
->GetNodes().GetCount() == 0)
2858 int index
= static_cast<int>(row
) - current
- 1;
2859 void * n
= node
->GetChildren().Item( index
);
2860 ret
= new wxDataViewTreeNode( parent
) ;
2861 ret
->SetItem( wxDataViewItem( n
));
2862 ret
->SetHasChildren(false);
2871 virtual int operator() ( void * n
)
2874 if( current
== static_cast<int>(row
))
2876 ret
= new wxDataViewTreeNode( parent
) ;
2877 ret
->SetItem( wxDataViewItem( n
));
2878 ret
->SetHasChildren(false);
2884 wxDataViewTreeNode
* GetResult(){ return ret
; }
2888 wxDataViewTreeNode
* ret
;
2889 wxDataViewTreeNode
* parent
;
2893 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2895 RowToTreeNodeJob
job( row
, -2, m_root
);
2896 Walker( m_root
, job
);
2897 return job
.GetResult();
2900 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2902 wxWindow
*parent
= GetParent();
2903 wxDataViewEvent
le(type
, parent
->GetId());
2905 le
.SetEventObject(parent
);
2906 le
.SetModel(GetOwner()->GetModel());
2909 parent
->GetEventHandler()->ProcessEvent(le
);
2913 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2915 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2918 if( node
->HasChildren())
2920 if( !node
->IsOpen())
2922 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2923 //Check if the user prevent expanding
2924 if( e
.GetSkipped() )
2928 //Here I build the children of current node
2929 if( node
->GetChildrenNumber() == 0 )
2932 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2936 //Send the expanded event
2937 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2941 SelectRow( row
, false );
2942 SelectRow( row
+ 1, true );
2943 ChangeCurrentRow( row
+ 1 );
2944 SendSelectionChangedEvent( GetItemByRow(row
+1));
2952 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2954 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2957 wxDataViewTreeNode
* nd
= node
;
2959 if( node
->HasChildren() && node
->IsOpen() )
2961 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2962 if( e
.GetSkipped() )
2967 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2971 node
= node
->GetParent();
2974 int parent
= GetRowByItem( node
->GetItem() ) ;
2977 SelectRow( row
, false);
2978 SelectRow(parent
, true );
2979 ChangeCurrentRow( parent
);
2980 SendSelectionChangedEvent( node
->GetItem() );
2984 if( !nd
->HasChildren())
2989 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2991 wxDataViewModel
* model
= GetOwner()->GetModel();
2995 //Compose the a parent-chain of the finding item
2997 list
.DeleteContents( true );
2998 wxDataViewItem
it( item
);
3001 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3002 list
.Insert( pItem
);
3003 it
= model
->GetParent( it
);
3006 //Find the item along the parent-chain.
3007 //This algorithm is designed to speed up the node-finding method
3008 wxDataViewTreeNode
* node
= m_root
;
3009 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3011 if( node
->HasChildren() )
3013 if( node
->GetChildrenNumber() == 0 )
3016 BuildTreeHelper(model
, node
->GetItem(), node
);
3019 wxDataViewTreeNodes nodes
= node
->GetNodes();
3021 for (; i
< nodes
.GetCount(); i
++)
3023 if (nodes
[i
]->GetItem() == (**iter
))
3029 if (i
== nodes
.GetCount())
3038 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3040 wxDataViewColumn
*col
= NULL
;
3041 unsigned int cols
= GetOwner()->GetColumnCount();
3042 unsigned int colnum
= 0;
3043 unsigned int x_start
= 0;
3045 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3046 for (x_start
= 0; colnum
< cols
; colnum
++)
3048 col
= GetOwner()->GetColumn(colnum
);
3049 if (col
->IsHidden())
3050 continue; // skip it!
3052 unsigned int w
= col
->GetWidth();
3053 if (x_start
+w
>= (unsigned int)x
)
3060 item
= GetItemByRow( y
/m_lineHeight
);
3063 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3065 int row
= GetRowByItem(item
);
3066 int y
= row
*m_lineHeight
;
3067 int h
= m_lineHeight
;
3069 wxDataViewColumn
*col
= NULL
;
3070 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3072 col
= GetOwner()->GetColumn( i
);
3073 x
+= col
->GetWidth();
3074 if( GetOwner()->GetColumn(i
+1) == column
)
3077 int w
= col
->GetWidth();
3078 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3079 return wxRect(x
, y
, w
, h
);
3082 int wxDataViewMainWindow::RecalculateCount()
3084 return m_root
->GetSubTreeCount();
3087 class ItemToRowJob
: public DoJob
3090 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3091 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3092 virtual ~ItemToRowJob(){};
3094 //Maybe binary search will help to speed up this process
3095 virtual int operator() ( wxDataViewTreeNode
* node
)
3098 if( node
->GetItem() == item
)
3103 if( node
->GetItem() == **m_iter
)
3110 ret
+= node
->GetSubTreeCount();
3116 virtual int operator() ( void * n
)
3119 if( n
== item
.GetID() )
3123 //the row number is begin from zero
3124 int GetResult(){ return ret
-1 ; }
3126 ItemList::const_iterator m_iter
;
3127 wxDataViewItem item
;
3132 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3134 wxDataViewModel
* model
= GetOwner()->GetModel();
3141 //Compose the a parent-chain of the finding item
3143 wxDataViewItem
* pItem
= NULL
;
3144 list
.DeleteContents( true );
3145 wxDataViewItem
it( item
);
3148 pItem
= new wxDataViewItem( it
);
3149 list
.Insert( pItem
);
3150 it
= model
->GetParent( it
);
3152 pItem
= new wxDataViewItem( );
3153 list
.Insert( pItem
);
3155 ItemToRowJob
job( item
, list
.begin() );
3156 Walker(m_root
, job
);
3157 return job
.GetResult();
3160 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3162 if( !model
->IsContainer( item
) )
3165 wxDataViewItemArray children
;
3166 unsigned int num
= model
->GetChildren( item
, children
);
3167 unsigned int index
= 0;
3168 while( index
< num
)
3170 if( model
->IsContainer( children
[index
] ) )
3172 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3173 n
->SetItem(children
[index
]);
3174 n
->SetHasChildren( true ) ;
3179 node
->AddLeaf( children
[index
].GetID() );
3183 node
->SetSubTreeCount( num
);
3184 wxDataViewTreeNode
* n
= node
->GetParent();
3186 n
->ChangeSubTreeCount(num
);
3190 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3192 //First we define a invalid item to fetch the top-level elements
3193 wxDataViewItem item
;
3195 BuildTreeHelper( model
, item
, m_root
);
3199 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3201 if( node
->GetNodeNumber() != 0 )
3203 int len
= node
->GetNodeNumber();
3205 wxDataViewTreeNodes nodes
= node
->GetNodes();
3206 for( ; i
< len
; i
++ )
3208 DestroyTreeHelper(nodes
[i
]);
3214 void wxDataViewMainWindow::DestroyTree()
3216 DestroyTreeHelper(m_root
);
3217 m_root
->SetSubTreeCount(0);
3221 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3223 if (event
.GetKeyCode() == WXK_TAB
)
3225 wxNavigationKeyEvent nevent
;
3226 nevent
.SetWindowChange( event
.ControlDown() );
3227 nevent
.SetDirection( !event
.ShiftDown() );
3228 nevent
.SetEventObject( GetParent()->GetParent() );
3229 nevent
.SetCurrentFocus( m_parent
);
3230 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3234 // no item -> nothing to do
3235 if (!HasCurrentRow())
3241 // don't use m_linesPerPage directly as it might not be computed yet
3242 const int pageSize
= GetCountPerPage();
3243 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3245 switch ( event
.GetKeyCode() )
3249 if (m_currentRow
> 0)
3251 wxWindow
*parent
= GetParent();
3252 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3253 le
.SetItem( GetItemByRow(m_currentRow
) );
3254 le
.SetEventObject(parent
);
3255 le
.SetModel(GetOwner()->GetModel());
3257 parent
->GetEventHandler()->ProcessEvent(le
);
3262 if ( m_currentRow
> 0 )
3263 OnArrowChar( m_currentRow
- 1, event
);
3267 if ( m_currentRow
< GetRowCount() - 1 )
3268 OnArrowChar( m_currentRow
+ 1, event
);
3270 //Add the process for tree expanding/collapsing
3272 OnCollapsing(m_currentRow
);
3275 OnExpanding( m_currentRow
);
3279 OnArrowChar( GetRowCount() - 1, event
);
3284 OnArrowChar( 0, event
);
3289 int steps
= pageSize
- 1;
3290 int index
= m_currentRow
- steps
;
3294 OnArrowChar( index
, event
);
3300 int steps
= pageSize
- 1;
3301 unsigned int index
= m_currentRow
+ steps
;
3302 unsigned int count
= GetRowCount();
3303 if ( index
>= count
)
3306 OnArrowChar( index
, event
);
3315 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3317 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3319 // let the base handle mouse wheel events.
3324 int x
= event
.GetX();
3325 int y
= event
.GetY();
3326 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3327 wxDataViewColumn
*col
= NULL
;
3330 unsigned int cols
= GetOwner()->GetColumnCount();
3332 for (i
= 0; i
< cols
; i
++)
3334 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3336 continue; // skip it!
3338 if (x
< xpos
+ c
->GetWidth())
3343 xpos
+= c
->GetWidth();
3348 wxDataViewRenderer
*cell
= col
->GetRenderer();
3349 unsigned int current
= y
/ m_lineHeight
;
3350 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3352 // Unselect all if below the last row ?
3356 //Test whether the mouse is hovered on the tree item button
3358 if (GetOwner()->GetExpanderColumn() == col
)
3360 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3361 if( node
!=NULL
&& node
->HasChildren() )
3363 int indent
= node
->GetIndentLevel();
3364 indent
= GetOwner()->GetIndent()*indent
;
3365 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3366 if( rect
.Contains( x
, y
) )
3368 //So the mouse is over the expander
3370 if (m_underMouse
&& m_underMouse
!= node
)
3372 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3373 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3375 if (m_underMouse
!= node
)
3377 //wxLogMessage("Do the row: %d", current);
3380 m_underMouse
= node
;
3383 if (node
!=NULL
&& !node
->HasChildren())
3388 if (m_underMouse
!= NULL
)
3390 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3391 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3392 m_underMouse
= NULL
;
3396 wxDataViewModel
*model
= GetOwner()->GetModel();
3398 if (event
.Dragging())
3400 if (m_dragCount
== 0)
3402 // we have to report the raw, physical coords as we want to be
3403 // able to call HitTest(event.m_pointDrag) from the user code to
3404 // get the item being dragged
3405 m_dragStart
= event
.GetPosition();
3410 if (m_dragCount
!= 3)
3413 if (event
.LeftIsDown())
3415 // Notify cell about drag
3424 bool forceClick
= false;
3426 if (event
.ButtonDClick())
3428 m_renameTimer
->Stop();
3429 m_lastOnSame
= false;
3432 wxDataViewItem item
= GetItemByRow(current
);
3433 bool ignore_other_columns
=
3434 ((GetOwner()->GetExpanderColumn() != col
) &&
3435 (model
->IsContainer(item
)) &&
3436 (!model
->HasContainerColumns(item
)));
3438 if (event
.LeftDClick())
3440 if ( current
== m_lineLastClicked
)
3442 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3445 model
->GetValue( value
, item
, col
->GetModelColumn() );
3446 cell
->SetValue( value
);
3447 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3448 col
->GetWidth(), m_lineHeight
);
3449 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3454 wxWindow
*parent
= GetParent();
3455 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3456 le
.SetItem( GetItemByRow(current
) );
3457 le
.SetEventObject(parent
);
3458 le
.SetModel(GetOwner()->GetModel());
3460 parent
->GetEventHandler()->ProcessEvent(le
);
3466 // The first click was on another item, so don't interpret this as
3467 // a double click, but as a simple click instead
3474 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3476 // select single line
3477 SelectAllRows( false );
3478 SelectRow( m_lineSelectSingleOnUp
, true );
3481 //Process the event of user clicking the expander
3482 bool expander
= false;
3483 if (GetOwner()->GetExpanderColumn() == col
)
3485 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3486 if( node
!=NULL
&& node
->HasChildren() )
3488 int indent
= node
->GetIndentLevel();
3489 indent
= GetOwner()->GetIndent()*indent
;
3490 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3491 if( rect
.Contains( x
, y
) )
3494 if( node
->IsOpen() )
3495 OnCollapsing(current
);
3497 OnExpanding( current
);
3501 //If the user click the expander, we do not do editing even if the column with expander are editable
3502 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3504 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3505 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3507 m_renameTimer
->Start( 100, true );
3511 m_lastOnSame
= false;
3512 m_lineSelectSingleOnUp
= (unsigned int)-1;
3516 // This is necessary, because after a DnD operation in
3517 // from and to ourself, the up event is swallowed by the
3518 // DnD code. So on next non-up event (which means here and
3519 // now) m_lineSelectSingleOnUp should be reset.
3520 m_lineSelectSingleOnUp
= (unsigned int)-1;
3523 if (event
.RightDown())
3525 m_lineBeforeLastClicked
= m_lineLastClicked
;
3526 m_lineLastClicked
= current
;
3528 // If the item is already selected, do not update the selection.
3529 // Multi-selections should not be cleared if a selected item is clicked.
3530 if (!IsRowSelected(current
))
3532 SelectAllRows(false);
3533 ChangeCurrentRow(current
);
3534 SelectRow(m_currentRow
,true);
3535 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3538 // notify cell about right click
3541 // Allow generation of context menu event
3544 else if (event
.MiddleDown())
3546 // notify cell about middle click
3549 if (event
.LeftDown() || forceClick
)
3553 m_lineBeforeLastClicked
= m_lineLastClicked
;
3554 m_lineLastClicked
= current
;
3556 unsigned int oldCurrentRow
= m_currentRow
;
3557 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3559 bool cmdModifierDown
= event
.CmdDown();
3560 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3562 if ( IsSingleSel() || !IsRowSelected(current
) )
3564 SelectAllRows( false );
3565 ChangeCurrentRow(current
);
3566 SelectRow(m_currentRow
,true);
3567 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3569 else // multi sel & current is highlighted & no mod keys
3571 m_lineSelectSingleOnUp
= current
;
3572 ChangeCurrentRow(current
); // change focus
3575 else // multi sel & either ctrl or shift is down
3577 if (cmdModifierDown
)
3579 ChangeCurrentRow(current
);
3580 ReverseRowSelection(m_currentRow
);
3581 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3583 else if (event
.ShiftDown())
3585 ChangeCurrentRow(current
);
3587 unsigned int lineFrom
= oldCurrentRow
,
3590 if ( lineTo
< lineFrom
)
3593 lineFrom
= m_currentRow
;
3596 SelectRows(lineFrom
, lineTo
, true);
3597 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3599 else // !ctrl, !shift
3601 // test in the enclosing if should make it impossible
3602 wxFAIL_MSG( _T("how did we get here?") );
3606 if (m_currentRow
!= oldCurrentRow
)
3607 RefreshRow( oldCurrentRow
);
3609 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3611 // Update selection here...
3614 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3615 (current
== oldCurrentRow
)) && oldWasSelected
;
3619 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3623 if (HasCurrentRow())
3629 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3633 if (HasCurrentRow())
3639 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3641 if( m_selection
.GetCount() != 1 )
3642 return wxDataViewItem();
3644 return GetItemByRow( m_selection
.Item(0));
3647 //-----------------------------------------------------------------------------
3649 //-----------------------------------------------------------------------------
3650 WX_DEFINE_LIST(wxDataViewColumnList
);
3652 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3654 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3655 EVT_SIZE(wxDataViewCtrl::OnSize
)
3658 wxDataViewCtrl::~wxDataViewCtrl()
3661 GetModel()->RemoveNotifier( m_notifier
);
3664 void wxDataViewCtrl::Init()
3669 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3670 const wxPoint
& pos
, const wxSize
& size
,
3671 long style
, const wxValidator
& validator
)
3673 if (!wxControl::Create( parent
, id
, pos
, size
,
3674 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3677 SetInitialSize(size
);
3682 MacSetClipChildren( true ) ;
3685 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3687 if (HasFlag(wxDV_NO_HEADER
))
3688 m_headerArea
= NULL
;
3690 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3692 SetTargetWindow( m_clientArea
);
3694 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3696 sizer
->Add( m_headerArea
, 0, wxGROW
);
3697 sizer
->Add( m_clientArea
, 1, wxGROW
);
3704 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3708 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3711 // we need to process arrows ourselves for scrolling
3712 if ( nMsg
== WM_GETDLGCODE
)
3714 rc
|= DLGC_WANTARROWS
;
3722 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3724 // We need to override OnSize so that our scrolled
3725 // window a) does call Layout() to use sizers for
3726 // positioning the controls but b) does not query
3727 // the sizer for their size and use that for setting
3728 // the scrollable area as set that ourselves by
3729 // calling SetScrollbar() further down.
3736 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3738 if (!wxDataViewCtrlBase::AssociateModel( model
))
3741 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3743 model
->AddNotifier( m_notifier
);
3745 m_clientArea
->BuildTree(model
);
3747 m_clientArea
->UpdateDisplay();
3752 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3754 if (!wxDataViewCtrlBase::AppendColumn(col
))
3757 m_cols
.Append( col
);
3762 void wxDataViewCtrl::OnColumnChange()
3765 m_headerArea
->UpdateDisplay();
3767 m_clientArea
->UpdateDisplay();
3770 void wxDataViewCtrl::DoSetExpanderColumn()
3772 m_clientArea
->UpdateDisplay();
3775 void wxDataViewCtrl::DoSetIndent()
3777 m_clientArea
->UpdateDisplay();
3780 unsigned int wxDataViewCtrl::GetColumnCount() const
3782 return m_cols
.GetCount();
3785 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3787 wxDataViewColumnList::const_iterator iter
;
3789 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3794 if ((*iter
)->IsHidden())
3801 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3803 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3814 bool wxDataViewCtrl::ClearColumns()
3821 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3823 int ret
= 0, dead
= 0;
3824 int len
= GetColumnCount();
3825 for (int i
=0; i
<len
; i
++)
3827 wxDataViewColumn
* col
= GetColumn(i
);
3828 if (col
->IsHidden())
3830 ret
+= col
->GetWidth();
3833 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3840 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3845 //Selection code with wxDataViewItem as parameters
3846 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3848 return m_clientArea
->GetSelection();
3851 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3854 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3855 int len
= selection
.GetCount();
3856 for( int i
= 0; i
< len
; i
++)
3858 unsigned int row
= selection
[i
];
3859 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3864 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3866 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3867 int len
= sel
.GetCount();
3868 for( int i
= 0; i
< len
; i
++ )
3870 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3872 selection
.Add( static_cast<unsigned int>(row
) );
3874 m_clientArea
->SetSelections( selection
);
3877 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3879 int row
= m_clientArea
->GetRowByItem( item
);
3882 //Unselect all rows before select another in the single select mode
3883 if (m_clientArea
->IsSingleSel())
3884 m_clientArea
->SelectAllRows(false);
3885 m_clientArea
->SelectRow(row
, true);
3889 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3891 int row
= m_clientArea
->GetRowByItem( item
);
3893 m_clientArea
->SelectRow(row
, false);
3896 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3898 int row
= m_clientArea
->GetRowByItem( item
);
3901 return m_clientArea
->IsRowSelected(row
);
3906 //Selection code with row number as parameter
3907 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3910 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3911 int len
= selection
.GetCount();
3912 for( int i
= 0; i
< len
; i
++)
3914 unsigned int row
= selection
[i
];
3920 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3922 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3923 int len
= sel
.GetCount();
3924 for( int i
= 0; i
< len
; i
++ )
3928 selection
.Add( static_cast<unsigned int>(row
) );
3930 m_clientArea
->SetSelections( selection
);
3933 void wxDataViewCtrl::Select( int row
)
3937 if (m_clientArea
->IsSingleSel())
3938 m_clientArea
->SelectAllRows(false);
3939 m_clientArea
->SelectRow( row
, true );
3943 void wxDataViewCtrl::Unselect( int row
)
3946 m_clientArea
->SelectRow(row
, false);
3949 bool wxDataViewCtrl::IsSelected( int row
) const
3952 return m_clientArea
->IsRowSelected(row
);
3956 void wxDataViewCtrl::SelectRange( int from
, int to
)
3959 for( int i
= from
; i
< to
; i
++ )
3961 m_clientArea
->Select(sel
);
3964 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3966 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3967 for( int i
= from
; i
< to
; i
++ )
3968 if( sel
.Index( i
) != wxNOT_FOUND
)
3970 m_clientArea
->SetSelections(sel
);
3973 void wxDataViewCtrl::SelectAll()
3975 m_clientArea
->SelectAllRows(true);
3978 void wxDataViewCtrl::UnselectAll()
3980 m_clientArea
->SelectAllRows(false);
3983 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3987 if( row
> (int) m_clientArea
->GetRowCount() )
3988 row
= m_clientArea
->GetRowCount();
3990 int first
= m_clientArea
->GetFirstVisibleRow();
3991 int last
= m_clientArea
->GetLastVisibleRow();
3993 m_clientArea
->ScrollTo( row
, column
);
3994 else if( row
> last
)
3995 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3997 m_clientArea
->ScrollTo( first
, column
);
4000 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4002 int row
= m_clientArea
->GetRowByItem(item
);
4005 if( column
== NULL
)
4006 EnsureVisible(row
, -1);
4010 int len
= GetColumnCount();
4011 for( int i
= 0; i
< len
; i
++ )
4012 if( GetColumn(i
) == column
)
4017 EnsureVisible( row
, col
);
4023 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4025 m_clientArea
->HitTest(point
, item
, column
);
4028 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4030 return m_clientArea
->GetItemRect(item
, column
);
4033 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4035 return m_clientArea
->GetItemByRow( row
);
4038 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4040 return m_clientArea
->GetRowByItem( item
);
4043 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4045 int row
= m_clientArea
->GetRowByItem( item
);
4047 m_clientArea
->Expand(row
);
4050 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4052 int row
= m_clientArea
->GetRowByItem( item
);
4054 m_clientArea
->Collapse(row
);
4058 // !wxUSE_GENERICDATAVIEWCTRL
4061 // wxUSE_DATAVIEWCTRL