1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 #define USE_NATIVE_HEADER_WINDOW 0
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called when the main window gets scrolled
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
146 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
148 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
150 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
151 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
154 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
157 #else // !defined(__WXMSW__)
159 #define HEADER_WINDOW_HEIGHT 25
160 #define HEADER_HORIZ_BORDER 5
161 #define HEADER_VERT_BORDER 3
162 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
164 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
167 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
169 const wxPoint
&pos
= wxDefaultPosition
,
170 const wxSize
&size
= wxDefaultSize
,
171 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
174 Create(parent
, id
, pos
, size
, name
);
177 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
,
179 const wxString
&name
);
181 ~wxGenericDataViewHeaderWindow()
183 delete m_resizeCursor
;
186 virtual void UpdateDisplay() { Refresh(); }
190 void OnPaint( wxPaintEvent
&event
);
191 void OnMouse( wxMouseEvent
&event
);
192 void OnSetFocus( wxFocusEvent
&event
);
197 // vars used for column resizing:
199 wxCursor
*m_resizeCursor
;
200 const wxCursor
*m_currentCursor
;
203 bool m_dirty
; // needs refresh?
204 int m_hover
; // index of the column under the mouse
205 int m_column
; // index of the column being resized
206 int m_currentX
; // divider line position in logical (unscrolled) coords
207 int m_minX
; // minimal position beyond which the divider line
208 // can't be dragged in logical coords
210 // the pen used to draw the current column width drag line
211 // when resizing the columsn
215 // internal utilities:
219 m_currentCursor
= (wxCursor
*) NULL
;
220 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
222 m_isDragging
= false;
225 m_hover
= wxNOT_FOUND
;
226 m_column
= wxNOT_FOUND
;
230 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
231 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
234 void AdjustDC(wxDC
& dc
);
237 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
238 DECLARE_EVENT_TABLE()
241 #endif // defined(__WXMSW__)
243 //-----------------------------------------------------------------------------
244 // wxDataViewRenameTimer
245 //-----------------------------------------------------------------------------
247 class wxDataViewRenameTimer
: public wxTimer
250 wxDataViewMainWindow
*m_owner
;
253 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
257 //-----------------------------------------------------------------------------
258 // wxDataViewTreeNode
259 //-----------------------------------------------------------------------------
260 class wxDataViewTreeNode
;
261 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
262 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
264 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
265 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
267 class wxDataViewTreeNode
270 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
277 m_hasChildren
= false;
281 ~wxDataViewTreeNode()
285 wxDataViewTreeNode
* GetParent() { return m_parent
; }
286 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
287 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
288 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
290 void AddNode( wxDataViewTreeNode
* node
)
292 m_leaves
.Add( node
->GetItem().GetID() );
294 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
297 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
299 void AddLeaf( void * leaf
)
301 m_leaves
.Add( leaf
);
303 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
306 wxDataViewItem
& GetItem() { return m_item
; }
307 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
309 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
310 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
314 wxDataViewTreeNode
* node
= this;
315 while( node
->GetParent()->GetParent() != NULL
)
317 node
= node
->GetParent();
330 int len
= m_nodes
.GetCount();
332 for ( int i
= 0 ;i
< len
; i
++)
333 sum
+= m_nodes
[i
]->GetSubTreeCount();
335 sum
+= m_leaves
.GetCount();
338 ChangeSubTreeCount(-sum
);
344 ChangeSubTreeCount(sum
);
347 bool HasChildren() { return m_hasChildren
; }
348 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
350 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
351 int GetSubTreeCount() { return m_subTreeCount
; }
352 void ChangeSubTreeCount( int num
)
356 m_subTreeCount
+= num
;
358 m_parent
->ChangeSubTreeCount(num
);
365 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
366 int len
= m_nodes
.GetCount();
367 for (int i
= 0; i
< len
; i
++)
368 m_nodes
[i
]->Resort();
369 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
374 wxDataViewTreeNode
*m_parent
;
375 wxDataViewTreeNodes m_nodes
;
376 wxDataViewTreeLeaves m_leaves
;
377 wxDataViewItem m_item
;
383 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
385 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
388 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
390 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
395 //-----------------------------------------------------------------------------
396 // wxDataViewMainWindow
397 //-----------------------------------------------------------------------------
399 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
401 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
402 WX_DEFINE_LIST(ItemList
);
404 class wxDataViewMainWindow
: public wxWindow
407 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
409 const wxPoint
&pos
= wxDefaultPosition
,
410 const wxSize
&size
= wxDefaultSize
,
411 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
412 virtual ~wxDataViewMainWindow();
414 // notifications from wxDataViewModel
415 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
416 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
417 bool ItemChanged( const wxDataViewItem
&item
);
418 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
429 g_model
= GetOwner()->GetModel();
430 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
433 if (g_model
->HasDefaultCompare())
441 g_column
= col
->GetModelColumn();
442 g_asending
= col
->IsSortOrderAscending();
445 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
446 wxDataViewCtrl
*GetOwner() { return m_owner
; }
447 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
449 void OnPaint( wxPaintEvent
&event
);
450 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
451 void OnChar( wxKeyEvent
&event
);
452 void OnMouse( wxMouseEvent
&event
);
453 void OnSetFocus( wxFocusEvent
&event
);
454 void OnKillFocus( wxFocusEvent
&event
);
456 void UpdateDisplay();
457 void RecalculateDisplay();
458 void OnInternalIdle();
460 void OnRenameTimer();
462 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
463 void ScrollTo( int rows
, int column
);
465 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
466 void ChangeCurrentRow( unsigned int row
);
468 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
469 bool IsEmpty() { return GetRowCount() == 0; }
471 int GetCountPerPage() const;
472 int GetEndOfLastCol() const;
473 unsigned int GetFirstVisibleRow() const;
474 //I change this method to un const because in the tree view, the displaying number of the tree are changing along with the expanding/collapsing of the tree nodes
475 unsigned int GetLastVisibleRow();
476 unsigned int GetRowCount() ;
478 wxDataViewItem
GetSelection() const;
479 wxDataViewSelection
GetSelections(){ return m_selection
; }
480 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
481 void Select( const wxArrayInt
& aSelections
);
482 void SelectAllRows( bool on
);
483 void SelectRow( unsigned int row
, bool on
);
484 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
485 void ReverseRowSelection( unsigned int row
);
486 bool IsRowSelected( unsigned int row
);
487 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
489 void RefreshRow( unsigned int row
);
490 void RefreshRows( unsigned int from
, unsigned int to
);
491 void RefreshRowsAfter( unsigned int firstRow
);
493 // returns the colour to be used for drawing the rules
494 wxColour
GetRuleColour() const
496 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
499 wxRect
GetLineRect( unsigned int row
) const;
501 //Some useful functions for row and item mapping
502 wxDataViewItem
GetItemByRow( unsigned int row
) const;
503 int GetRowByItem( const wxDataViewItem
& item
);
505 //Methods for building the mapping tree
506 void BuildTree( wxDataViewModel
* model
);
508 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
509 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
511 void Expand( unsigned int row
) { OnExpanding( row
); }
512 void Collapse( unsigned int row
) { OnCollapsing( row
); }
514 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
515 //We did not need this temporarily
516 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
518 int RecalculateCount() ;
520 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
521 void OnExpanding( unsigned int row
);
522 void OnCollapsing( unsigned int row
);
524 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
527 wxDataViewCtrl
*m_owner
;
531 wxDataViewColumn
*m_currentCol
;
532 unsigned int m_currentRow
;
533 wxDataViewSelection m_selection
;
535 wxDataViewRenameTimer
*m_renameTimer
;
543 // for double click logic
544 unsigned int m_lineLastClicked
,
545 m_lineBeforeLastClicked
,
546 m_lineSelectSingleOnUp
;
548 // the pen used to draw horiz/vertical rules
551 // the pen used to draw the expander and the lines
554 //This is the tree structure of the model
555 wxDataViewTreeNode
* m_root
;
557 //This is the tree node under the cursor
558 wxDataViewTreeNode
* m_underMouse
;
560 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
561 DECLARE_EVENT_TABLE()
564 // ---------------------------------------------------------
565 // wxGenericDataViewModelNotifier
566 // ---------------------------------------------------------
568 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
571 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
572 { m_mainWindow
= mainWindow
; }
574 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
575 { return m_mainWindow
->ItemAdded( parent
, item
); }
576 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
577 { return m_mainWindow
->ItemDeleted( parent
, item
); }
578 virtual bool ItemChanged( const wxDataViewItem
& item
)
579 { return m_mainWindow
->ItemChanged(item
); }
580 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
581 { return m_mainWindow
->ValueChanged( item
, col
); }
582 virtual bool Cleared()
583 { return m_mainWindow
->Cleared(); }
584 virtual void Resort()
585 { m_mainWindow
->Resort(); }
587 wxDataViewMainWindow
*m_mainWindow
;
590 // ---------------------------------------------------------
591 // wxDataViewRenderer
592 // ---------------------------------------------------------
594 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
596 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
597 wxDataViewCellMode mode
,
599 wxDataViewRendererBase( varianttype
, mode
, align
)
606 wxDataViewRenderer::~wxDataViewRenderer()
612 wxDC
*wxDataViewRenderer::GetDC()
616 if (GetOwner() == NULL
)
618 if (GetOwner()->GetOwner() == NULL
)
620 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
626 // ---------------------------------------------------------
627 // wxDataViewCustomRenderer
628 // ---------------------------------------------------------
630 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
632 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
633 wxDataViewCellMode mode
, int align
) :
634 wxDataViewRenderer( varianttype
, mode
, align
)
638 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
640 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
641 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
642 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
643 view
->GetForegroundColour();
644 dc
->SetTextForeground(col
);
645 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
648 // ---------------------------------------------------------
649 // wxDataViewTextRenderer
650 // ---------------------------------------------------------
652 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
654 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
655 wxDataViewCellMode mode
, int align
) :
656 wxDataViewCustomRenderer( varianttype
, mode
, align
)
660 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
662 m_text
= value
.GetString();
667 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
672 bool wxDataViewTextRenderer::HasEditorCtrl()
677 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
678 wxRect labelRect
, const wxVariant
&value
)
680 return new wxTextCtrl( parent
, wxID_ANY
, value
,
681 wxPoint(labelRect
.x
,labelRect
.y
),
682 wxSize(labelRect
.width
,labelRect
.height
) );
685 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
687 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
688 value
= text
->GetValue();
692 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
694 RenderText( m_text
, 0, cell
, dc
, state
);
698 wxSize
wxDataViewTextRenderer::GetSize() const
700 const wxDataViewCtrl
*view
= GetView();
704 view
->GetTextExtent( m_text
, &x
, &y
);
705 return wxSize( x
, y
);
707 return wxSize(80,20);
710 // ---------------------------------------------------------
711 // wxDataViewBitmapRenderer
712 // ---------------------------------------------------------
714 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
716 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
717 wxDataViewCellMode mode
, int align
) :
718 wxDataViewCustomRenderer( varianttype
, mode
, align
)
722 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
724 if (value
.GetType() == wxT("wxBitmap"))
726 if (value
.GetType() == wxT("wxIcon"))
732 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
737 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
740 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
741 else if (m_icon
.Ok())
742 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
747 wxSize
wxDataViewBitmapRenderer::GetSize() const
750 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
751 else if (m_icon
.Ok())
752 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
754 return wxSize(16,16);
757 // ---------------------------------------------------------
758 // wxDataViewToggleRenderer
759 // ---------------------------------------------------------
761 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
763 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
764 wxDataViewCellMode mode
, int align
) :
765 wxDataViewCustomRenderer( varianttype
, mode
, align
)
770 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
772 m_toggle
= value
.GetBool();
777 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
782 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
784 // User wxRenderer here
787 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
789 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
794 flags
|= wxCONTROL_CHECKED
;
795 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
796 flags
|= wxCONTROL_DISABLED
;
798 wxRendererNative::Get().DrawCheckBox(
799 GetOwner()->GetOwner(),
807 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
808 wxDataViewModel
*model
,
809 const wxDataViewItem
& item
, unsigned int col
)
811 bool value
= !m_toggle
;
812 wxVariant variant
= value
;
813 model
->SetValue( variant
, item
, col
);
814 model
->ValueChanged( item
, col
);
818 wxSize
wxDataViewToggleRenderer::GetSize() const
820 return wxSize(20,20);
823 // ---------------------------------------------------------
824 // wxDataViewProgressRenderer
825 // ---------------------------------------------------------
827 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
829 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
830 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
831 wxDataViewCustomRenderer( varianttype
, mode
, align
)
837 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
841 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
843 m_value
= (long) value
;
845 if (m_value
< 0) m_value
= 0;
846 if (m_value
> 100) m_value
= 100;
851 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
853 value
= (long) m_value
;
857 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
859 double pct
= (double)m_value
/ 100.0;
861 bar
.width
= (int)(cell
.width
* pct
);
862 dc
->SetPen( *wxTRANSPARENT_PEN
);
863 dc
->SetBrush( *wxBLUE_BRUSH
);
864 dc
->DrawRectangle( bar
);
866 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
867 dc
->SetPen( *wxBLACK_PEN
);
868 dc
->DrawRectangle( cell
);
873 wxSize
wxDataViewProgressRenderer::GetSize() const
875 return wxSize(40,12);
878 // ---------------------------------------------------------
879 // wxDataViewDateRenderer
880 // ---------------------------------------------------------
882 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
884 #if wxUSE_DATE_RENDERER_POPUP
886 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
889 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
890 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
891 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
896 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
897 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
898 sizer
->Add( m_cal
, 1, wxGROW
);
903 void OnCalendar( wxCalendarEvent
&event
);
905 wxCalendarCtrl
*m_cal
;
906 wxDataViewModel
*m_model
;
908 const wxDataViewItem
& m_item
;
911 virtual void OnDismiss()
916 DECLARE_EVENT_TABLE()
919 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
920 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
923 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
925 wxDateTime date
= event
.GetDate();
926 wxVariant value
= date
;
927 m_model
->SetValue( value
, m_item
, m_col
);
928 m_model
->ValueChanged( m_item
, m_col
);
932 #endif // wxUSE_DATE_RENDERER_POPUP
934 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
936 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
937 wxDataViewCellMode mode
, int align
) :
938 wxDataViewCustomRenderer( varianttype
, mode
, align
)
942 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
944 m_date
= value
.GetDateTime();
949 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
955 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
957 wxString tmp
= m_date
.FormatDate();
958 RenderText( tmp
, 0, cell
, dc
, state
);
962 wxSize
wxDataViewDateRenderer::GetSize() const
964 const wxDataViewCtrl
* view
= GetView();
965 wxString tmp
= m_date
.FormatDate();
967 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
968 return wxSize(x
,y
+d
);
971 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
972 const wxDataViewItem
& item
, unsigned int col
)
975 model
->GetValue( variant
, item
, col
);
976 wxDateTime value
= variant
.GetDateTime();
978 #if wxUSE_DATE_RENDERER_POPUP
979 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
980 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
981 wxPoint pos
= wxGetMousePosition();
984 popup
->Popup( popup
->m_cal
);
985 #else // !wxUSE_DATE_RENDERER_POPUP
986 wxMessageBox(value
.Format());
987 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
991 // ---------------------------------------------------------
992 // wxDataViewIconTextRenderer
993 // ---------------------------------------------------------
995 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
997 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
998 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
999 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1002 SetAlignment(align
);
1005 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1009 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1015 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1020 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1023 const wxIcon
&icon
= m_value
.GetIcon();
1026 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1027 xoffset
= icon
.GetWidth()+4;
1030 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1035 wxSize
wxDataViewIconTextRenderer::GetSize() const
1037 const wxDataViewCtrl
*view
= GetView();
1038 if (!m_value
.GetText().empty())
1041 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1043 if (m_value
.GetIcon().IsOk())
1044 x
+= m_value
.GetIcon().GetWidth() + 4;
1045 return wxSize( x
, y
);
1047 return wxSize(80,20);
1050 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1055 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1060 // ---------------------------------------------------------
1062 // ---------------------------------------------------------
1064 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1066 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1067 unsigned int model_column
,
1068 int width
, wxAlignment align
, int flags
) :
1069 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1071 SetAlignment(align
);
1075 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1078 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1079 unsigned int model_column
,
1080 int width
, wxAlignment align
, int flags
) :
1081 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1083 SetAlignment(align
);
1086 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1089 wxDataViewColumn::~wxDataViewColumn()
1093 void wxDataViewColumn::Init( int width
)
1096 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1100 void wxDataViewColumn::SetResizeable( bool resizeable
)
1103 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1105 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1108 void wxDataViewColumn::SetHidden( bool hidden
)
1111 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1113 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1115 // tell our owner to e.g. update its scrollbars:
1117 GetOwner()->OnColumnChange();
1120 void wxDataViewColumn::SetSortable( bool sortable
)
1123 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1125 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1127 // Update header button
1129 GetOwner()->OnColumnChange();
1132 void wxDataViewColumn::SetSortOrder( bool ascending
)
1134 m_ascending
= ascending
;
1136 // Update header button
1138 GetOwner()->OnColumnChange();
1141 bool wxDataViewColumn::IsSortOrderAscending() const
1146 void wxDataViewColumn::SetInternalWidth( int width
)
1150 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1151 if (m_owner
&& m_owner
->m_clientArea
)
1152 m_owner
->m_clientArea
->RecalculateDisplay();
1155 void wxDataViewColumn::SetWidth( int width
)
1157 m_owner
->m_headerArea
->UpdateDisplay();
1159 SetInternalWidth(width
);
1163 //-----------------------------------------------------------------------------
1164 // wxDataViewHeaderWindowBase
1165 //-----------------------------------------------------------------------------
1167 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1169 wxWindow
*parent
= GetParent();
1170 wxDataViewEvent
le(type
, parent
->GetId());
1172 le
.SetEventObject(parent
);
1174 le
.SetDataViewColumn(GetColumn(n
));
1175 le
.SetModel(GetOwner()->GetModel());
1177 // for events created by wxDataViewHeaderWindow the
1178 // row / value fields are not valid
1180 parent
->GetEventHandler()->ProcessEvent(le
);
1183 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1185 // implemented in msw/listctrl.cpp:
1186 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1188 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1190 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1191 const wxPoint
&pos
, const wxSize
&size
,
1192 const wxString
&name
)
1196 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1199 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1200 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1201 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1202 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1204 // create the native WC_HEADER window:
1205 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1206 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1207 m_hWnd
= CreateWindowEx(0,
1218 wxLogLastError(_T("CreateWindowEx"));
1222 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1223 // to call wxDataViewHeaderWindow::MSWOnNotify
1224 SubclassWin(m_hWnd
);
1226 // the following is required to get the default win's font for
1227 // header windows and must be done befor sending the HDM_LAYOUT msg
1234 // Retrieve the bounding rectangle of the parent window's
1235 // client area, and then request size and position values
1236 // from the header control.
1237 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1239 hdl
.prc
= &rcParent
;
1241 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1243 wxLogLastError(_T("SendMessage"));
1247 // Set the size, position, and visibility of the header control.
1248 SetWindowPos((HWND
)m_hWnd
,
1252 wp
.flags
| SWP_SHOWWINDOW
);
1254 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1255 // a wxBoxSizer and in order to avoid super-big header windows,
1256 // we need to set our height as fixed
1257 SetMinSize(wxSize(-1, wp
.cy
));
1258 SetMaxSize(wxSize(-1, wp
.cy
));
1263 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1268 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1270 // remove old columns
1271 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1272 Header_DeleteItem((HWND
)m_hWnd
, 0);
1274 // add the updated array of columns to the header control
1275 unsigned int cols
= GetOwner()->GetColumnCount();
1276 unsigned int added
= 0;
1277 wxDataViewModel
* model
= GetOwner()->GetModel();
1278 for (unsigned int i
= 0; i
< cols
; i
++)
1280 wxDataViewColumn
*col
= GetColumn( i
);
1281 if (col
->IsHidden())
1282 continue; // don't add it!
1285 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1286 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1287 hdi
.cxy
= col
->GetWidth();
1288 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1289 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1290 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1293 if(model
&& m_owner
->GetSortingColumn() == col
)
1295 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1296 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1297 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1301 // lParam is reserved for application's use:
1302 // we store there the column index to use it later in MSWOnNotify
1303 // (since columns may have been hidden)
1304 hdi
.lParam
= (LPARAM
)i
;
1306 // the native wxMSW implementation of the header window
1307 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1308 // on the right: to correct this effect we make the column
1309 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1311 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1313 switch (col
->GetAlignment())
1316 hdi
.fmt
|= HDF_LEFT
;
1318 case wxALIGN_CENTER
:
1319 case wxALIGN_CENTER_HORIZONTAL
:
1320 hdi
.fmt
|= HDF_CENTER
;
1323 hdi
.fmt
|= HDF_RIGHT
;
1327 // such alignment is not allowed for the column header!
1331 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1332 (WPARAM
)added
, (LPARAM
)&hdi
);
1337 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1341 // NOTE: we don't just return nmHDR->iItem because when there are
1342 // hidden columns, nmHDR->iItem may be different from
1343 // nmHDR->pitem->lParam
1345 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1347 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1352 item
.mask
= HDI_LPARAM
;
1353 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1355 return (unsigned int)item
.lParam
;
1358 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1360 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1362 // is it a message from the header?
1363 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1364 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1366 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1367 switch ( nmhdr
->code
)
1369 case HDN_BEGINTRACK
:
1370 // user has started to resize a column:
1371 // do we need to veto it?
1372 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1380 // user has started to reorder a column
1383 case HDN_ITEMCHANGING
:
1384 if (nmHDR
->pitem
!= NULL
&&
1385 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1387 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1388 if (nmHDR
->pitem
->cxy
< minWidth
)
1390 // do not allow the user to resize this column under
1391 // its minimal width:
1397 case HDN_ITEMCHANGED
: // user is resizing a column
1398 case HDN_ENDTRACK
: // user has finished resizing a column
1399 case HDN_ENDDRAG
: // user has finished reordering a column
1401 // update the width of the modified column:
1402 if (nmHDR
->pitem
!= NULL
&&
1403 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1405 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1406 unsigned int w
= nmHDR
->pitem
->cxy
;
1407 wxDataViewColumn
*col
= GetColumn(idx
);
1409 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1410 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1411 w
-= COLUMN_WIDTH_OFFSET
;
1413 if (w
>= (unsigned)col
->GetMinWidth())
1414 col
->SetInternalWidth(w
);
1420 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1421 wxDataViewModel
* model
= GetOwner()->GetModel();
1423 if(nmHDR
->iButton
== 0)
1425 wxDataViewColumn
*col
= GetColumn(idx
);
1426 if(col
->IsSortable())
1428 if(model
&& m_owner
->GetSortingColumn() == col
)
1430 bool order
= col
->IsSortOrderAscending();
1431 col
->SetSortOrder(!order
);
1435 m_owner
->SetSortingColumn(col
);
1443 wxEventType evt
= nmHDR
->iButton
== 0 ?
1444 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1445 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1446 SendEvent(evt
, idx
);
1452 // NOTE: for some reason (i.e. for a bug in Windows)
1453 // the HDN_ITEMCLICK notification is not sent on
1454 // right clicks, so we need to handle NM_RCLICK
1457 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1458 if (column
!= wxNOT_FOUND
)
1461 item
.mask
= HDI_LPARAM
;
1462 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1464 // 'idx' may be different from 'column' if there are
1465 // hidden columns...
1466 unsigned int idx
= (unsigned int)item
.lParam
;
1467 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1473 case HDN_GETDISPINFOW
:
1474 // see wxListCtrl::MSWOnNotify for more info!
1477 case HDN_ITEMDBLCLICK
:
1479 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1480 int w
= GetOwner()->GetBestColumnWidth(idx
);
1482 // update the native control:
1484 ZeroMemory(&hd
, sizeof(hd
));
1485 hd
.mask
= HDI_WIDTH
;
1487 Header_SetItem(GetHwnd(),
1488 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1491 // update the wxDataViewColumn class:
1492 GetColumn(idx
)->SetInternalWidth(w
);
1497 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1503 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1504 const wxRect
*WXUNUSED(rect
))
1506 wxSize ourSz
= GetClientSize();
1507 wxSize ownerSz
= m_owner
->GetClientSize();
1509 // where should the (logical) origin of this window be placed?
1511 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1513 // put this window on top of our parent and
1514 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1515 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1519 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1520 int WXUNUSED(w
), int WXUNUSED(h
),
1523 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1524 // the wxDataViewCtrl window gets resized: the following dummy call
1525 // to ScrollWindow() is required in order to get this header window
1526 // correctly repainted when it's (horizontally) scrolled:
1531 #else // !defined(__WXMSW__)
1533 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1534 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1535 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1536 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1537 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1540 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1541 const wxPoint
&pos
, const wxSize
&size
,
1542 const wxString
&name
)
1546 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1549 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1550 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1551 SetOwnForegroundColour( attr
.colFg
);
1552 SetOwnBackgroundColour( attr
.colBg
);
1554 SetOwnFont( attr
.font
);
1556 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1557 // a wxBoxSizer and in order to avoid super-big header windows,
1558 // we need to set our height as fixed
1559 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1560 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1565 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1568 GetClientSize( &w
, &h
);
1570 wxAutoBufferedPaintDC
dc( this );
1572 dc
.SetBackground(GetBackgroundColour());
1576 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1579 m_owner
->GetViewStart( &x
, NULL
);
1581 // account for the horz scrollbar offset
1582 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1584 dc
.SetFont( GetFont() );
1586 unsigned int cols
= GetOwner()->GetColumnCount();
1589 for (i
= 0; i
< cols
; i
++)
1591 wxDataViewColumn
*col
= GetColumn( i
);
1592 if (col
->IsHidden())
1593 continue; // skip it!
1595 int cw
= col
->GetWidth();
1598 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1599 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1601 if (col
->IsSortOrderAscending())
1602 sortArrow
= wxHDR_SORT_ICON_UP
;
1604 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1608 if (m_parent
->IsEnabled())
1610 if ((int) i
== m_hover
)
1611 state
= wxCONTROL_CURRENT
;
1615 state
= (int) wxCONTROL_DISABLED
;
1618 wxRendererNative::Get().DrawHeaderButton
1622 wxRect(xpos
, 0, cw
, ch
-1),
1627 // align as required the column title:
1629 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1630 switch (col
->GetAlignment())
1633 x
+= HEADER_HORIZ_BORDER
;
1636 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1639 case wxALIGN_CENTER
:
1640 case wxALIGN_CENTER_HORIZONTAL
:
1641 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1645 // always center the title vertically:
1646 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1648 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1650 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1651 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1652 dc
.DrawText( col
->GetTitle(), x
, y
);
1653 dc
.DestroyClippingRegion();
1659 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1661 GetParent()->SetFocus();
1665 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1667 // we want to work with logical coords
1669 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1670 int y
= event
.GetY();
1674 // we don't draw the line beyond our window,
1675 // but we allow dragging it there
1677 GetClientSize( &w
, NULL
);
1678 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1681 if (event
.ButtonUp())
1683 m_isDragging
= false;
1689 m_currentX
= wxMax(m_minX
+ 7, x
);
1693 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1695 GetOwner()->Refresh();
1699 else // not dragging
1702 m_column
= wxNOT_FOUND
;
1704 bool hit_border
= false;
1706 // end of the current column
1709 // find the column where this event occured
1710 int countCol
= m_owner
->GetColumnCount();
1711 for (int column
= 0; column
< countCol
; column
++)
1713 wxDataViewColumn
*p
= GetColumn(column
);
1716 continue; // skip if not shown
1718 xpos
+= p
->GetWidth();
1720 if ((abs(x
-xpos
) < 3) && (y
< 22))
1728 // inside the column
1735 int old_hover
= m_hover
;
1737 if (event
.Leaving())
1738 m_hover
= wxNOT_FOUND
;
1739 if (old_hover
!= m_hover
)
1742 if (m_column
== wxNOT_FOUND
)
1745 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1746 if (event
.LeftDClick() && resizeable
)
1748 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1751 else if (event
.LeftDown() || event
.RightUp())
1753 if (hit_border
&& event
.LeftDown() && resizeable
)
1755 m_isDragging
= true;
1759 else // click on a column
1761 wxDataViewModel
* model
= GetOwner()->GetModel();
1762 wxEventType evt
= event
.LeftDown() ?
1763 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1764 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1765 SendEvent(evt
, m_column
);
1767 //Left click the header
1768 if(event
.LeftDown())
1770 wxDataViewColumn
*col
= GetColumn(m_column
);
1771 if(col
->IsSortable())
1773 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1774 if(model
&& sortCol
== col
)
1776 bool order
= col
->IsSortOrderAscending();
1777 col
->SetSortOrder(!order
);
1781 m_owner
->SetSortingColumn(col
);
1787 //Send the column sorted event
1788 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1792 else if (event
.Moving())
1794 if (hit_border
&& resizeable
)
1795 m_currentCursor
= m_resizeCursor
;
1797 m_currentCursor
= wxSTANDARD_CURSOR
;
1799 SetCursor(*m_currentCursor
);
1804 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1808 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1809 m_owner
->GetViewStart( &x
, NULL
);
1811 // shift the DC origin to match the position of the main window horizontal
1812 // scrollbar: this allows us to always use logical coords
1813 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1816 #endif // defined(__WXMSW__)
1818 //-----------------------------------------------------------------------------
1819 // wxDataViewRenameTimer
1820 //-----------------------------------------------------------------------------
1822 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1827 void wxDataViewRenameTimer::Notify()
1829 m_owner
->OnRenameTimer();
1832 //-----------------------------------------------------------------------------
1833 // wxDataViewMainWindow
1834 //-----------------------------------------------------------------------------
1836 //The tree building helper, declared firstly
1837 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1839 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1841 if (row1
> row2
) return 1;
1842 if (row1
== row2
) return 0;
1847 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1849 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1850 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1851 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1852 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1853 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1854 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1857 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1858 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1859 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1860 m_selection( wxDataViewSelectionCmp
)
1865 m_lastOnSame
= false;
1866 m_renameTimer
= new wxDataViewRenameTimer( this );
1868 // TODO: user better initial values/nothing selected
1869 m_currentCol
= NULL
;
1872 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1875 m_dragStart
= wxPoint(0,0);
1876 m_lineLastClicked
= (unsigned int) -1;
1877 m_lineBeforeLastClicked
= (unsigned int) -1;
1878 m_lineSelectSingleOnUp
= (unsigned int) -1;
1882 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1883 SetBackgroundColour( *wxWHITE
);
1885 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1887 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1888 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1889 //Some new added code to deal with the tree structure
1890 m_root
= new wxDataViewTreeNode( NULL
);
1891 m_root
->SetHasChildren(true);
1893 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1895 m_underMouse
= NULL
;
1899 wxDataViewMainWindow::~wxDataViewMainWindow()
1902 delete m_renameTimer
;
1905 void wxDataViewMainWindow::OnRenameTimer()
1907 // We have to call this here because changes may just have
1908 // been made and no screen update taken place.
1913 unsigned int cols
= GetOwner()->GetColumnCount();
1915 for (i
= 0; i
< cols
; i
++)
1917 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1919 continue; // skip it!
1921 if (c
== m_currentCol
)
1923 xpos
+= c
->GetWidth();
1925 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1926 m_currentCol
->GetWidth(), m_lineHeight
);
1928 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1929 &labelRect
.x
, &labelRect
.y
);
1931 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1932 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1936 //------------------------------------------------------------------
1937 // Helper class for do operation on the tree node
1938 //------------------------------------------------------------------
1945 //The return value control how the tree-walker tranverse the tree
1946 // 0: Job done, stop tranverse and return
1947 // 1: Ignore the current node's subtree and continue
1948 // 2: Job not done, continue
1949 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1950 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1951 virtual int operator() ( void * n
) = 0;
1954 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1959 switch( func( node
) )
1970 wxDataViewTreeNodes nodes
= node
->GetNodes();
1971 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1973 int len_nodes
= nodes
.GetCount();
1974 int len
= leaves
.GetCount();
1975 int i
= 0, nodes_i
= 0;
1977 for( ; i
< len
; i
++ )
1979 void * n
= leaves
[i
];
1980 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1982 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1985 if( Walker( nd
, func
) )
2004 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2008 wxDataViewTreeNode
* node
;
2009 node
= FindNode(parent
);
2014 node
->SetHasChildren( true );
2016 if( g_model
->IsContainer( item
) )
2018 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2019 newnode
->SetItem(item
);
2020 newnode
->SetHasChildren( true );
2021 node
->AddNode( newnode
);
2024 node
->AddLeaf( item
.GetID() );
2026 node
->ChangeSubTreeCount(1);
2034 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2036 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2037 const wxDataViewItem
& item
)
2039 wxDataViewTreeNode
* node
= FindNode(parent
);
2041 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2042 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2045 node
->GetChildren().Remove( item
.GetID() );
2046 //Manuplate selection
2047 if( m_selection
.GetCount() > 1 )
2049 m_selection
.Empty();
2051 bool isContainer
= false;
2052 wxDataViewTreeNodes nds
= node
->GetNodes();
2053 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2055 if (nds
[i
]->GetItem() == item
)
2063 wxDataViewTreeNode
* n
= NULL
;
2064 wxDataViewTreeNodes nodes
= node
->GetNodes();
2065 int len
= nodes
.GetCount();
2066 for( int i
= 0 ; i
< len
; i
++)
2068 if( nodes
[i
]->GetItem() == item
)
2075 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2077 node
->GetNodes().Remove( n
);
2078 sub
-= n
->GetSubTreeCount();
2079 DestroyTreeHelper(n
);
2081 //Make the row number invalid and get a new valid one when user call GetRowCount
2083 node
->ChangeSubTreeCount(sub
);
2084 if( node
->GetChildrenNumber() == 0)
2086 node
->GetParent()->GetNodes().Remove( node
);
2090 //Change the current row to the last row if the current exceed the max row number
2091 if( m_currentRow
> GetRowCount() )
2092 m_currentRow
= m_count
- 1;
2099 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2105 wxWindow
*parent
= GetParent();
2106 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2107 le
.SetEventObject(parent
);
2108 le
.SetModel(GetOwner()->GetModel());
2110 parent
->GetEventHandler()->ProcessEvent(le
);
2115 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2117 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2118 /*#define MAX_VIRTUAL_WIDTH 100000
2120 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2121 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2122 Refresh( true, &rect );
2130 wxWindow
*parent
= GetParent();
2131 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2132 le
.SetEventObject(parent
);
2133 le
.SetModel(GetOwner()->GetModel());
2136 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2137 parent
->GetEventHandler()->ProcessEvent(le
);
2142 bool wxDataViewMainWindow::Cleared()
2152 void wxDataViewMainWindow::UpdateDisplay()
2157 void wxDataViewMainWindow::OnInternalIdle()
2159 wxWindow::OnInternalIdle();
2163 RecalculateDisplay();
2168 void wxDataViewMainWindow::RecalculateDisplay()
2170 wxDataViewModel
*model
= GetOwner()->GetModel();
2177 int width
= GetEndOfLastCol();
2178 int height
= GetRowCount() * m_lineHeight
;
2180 SetVirtualSize( width
, height
);
2181 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2186 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2188 wxWindow::ScrollWindow( dx
, dy
, rect
);
2190 if (GetOwner()->m_headerArea
)
2191 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2194 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2197 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2198 int sy
= rows
*m_lineHeight
/y
;
2202 wxRect rect
= GetClientRect();
2204 int x_start
= 0, x_end
= 0, w
= 0;
2206 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2207 for (x_start
= 0; colnum
< column
; colnum
++)
2209 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2210 if (col
->IsHidden())
2211 continue; // skip it!
2213 w
= col
->GetWidth();
2217 x_end
= x_start
+ w
;
2218 xe
= xx
+ rect
.width
;
2221 sx
= ( xx
+ x_end
- xe
)/x
;
2228 m_owner
->Scroll( sx
, sy
);
2231 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2233 wxDataViewModel
*model
= GetOwner()->GetModel();
2234 wxAutoBufferedPaintDC
dc( this );
2237 dc
.SetBackground(GetBackgroundColour());
2239 GetOwner()->PrepareDC( dc
);
2240 dc
.SetFont( GetFont() );
2242 wxRect update
= GetUpdateRegion().GetBox();
2243 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2245 // compute which items needs to be redrawn
2246 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2247 unsigned int item_count
=
2248 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2249 (int)(GetRowCount( )- item_start
) );
2250 unsigned int item_last
= item_start
+ item_count
;
2252 // compute which columns needs to be redrawn
2253 unsigned int cols
= GetOwner()->GetColumnCount();
2254 unsigned int col_start
= 0;
2255 unsigned int x_start
= 0;
2256 for (x_start
= 0; col_start
< cols
; col_start
++)
2258 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2259 if (col
->IsHidden())
2260 continue; // skip it!
2262 unsigned int w
= col
->GetWidth();
2263 if (x_start
+w
>= (unsigned int)update
.x
)
2269 unsigned int col_last
= col_start
;
2270 unsigned int x_last
= x_start
;
2271 for (; col_last
< cols
; col_last
++)
2273 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2274 if (col
->IsHidden())
2275 continue; // skip it!
2277 if (x_last
> (unsigned int)update
.GetRight())
2280 x_last
+= col
->GetWidth();
2283 // Draw horizontal rules if required
2284 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2286 dc
.SetPen(m_penRule
);
2287 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2289 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2291 int y
= i
* m_lineHeight
;
2292 dc
.DrawLine(x_start
, y
, x_last
, y
);
2296 // Draw vertical rules if required
2297 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2299 dc
.SetPen(m_penRule
);
2300 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2303 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2305 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2306 if (col
->IsHidden())
2307 continue; // skip it
2309 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2310 x
, item_last
* m_lineHeight
);
2312 x
+= col
->GetWidth();
2315 // Draw last vertical rule
2316 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2317 x
, item_last
* m_lineHeight
);
2320 // redraw the background for the items which are selected/current
2321 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2323 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2324 if (selected
|| item
== m_currentRow
)
2326 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2327 if (item
== m_currentRow
)
2328 flags
|= wxCONTROL_CURRENT
;
2330 flags
|= wxCONTROL_FOCUSED
;
2332 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2333 wxRendererNative::Get().DrawItemSelectionRect
2343 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2346 // TODO: last column for RTL support
2347 expander
= GetOwner()->GetColumn( 0 );
2348 GetOwner()->SetExpanderColumn(expander
);
2351 // redraw all cells for all rows which must be repainted and for all columns
2353 cell_rect
.x
= x_start
;
2354 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2355 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2357 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2358 wxDataViewRenderer
*cell
= col
->GetRenderer();
2359 cell_rect
.width
= col
->GetWidth();
2361 if (col
->IsHidden())
2362 continue; // skipt it!
2365 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2367 // get the cell value and set it into the renderer
2369 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2373 wxDataViewItem dataitem
= node
->GetItem();
2375 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2378 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2379 cell
->SetValue( value
);
2381 // update the y offset
2382 cell_rect
.y
= item
* m_lineHeight
;
2384 //Draw the expander here.
2385 int indent
= node
->GetIndentLevel();
2386 if( col
== expander
)
2388 //Calculate the indent first
2389 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2391 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2392 // change the cell_rect.x to the appropriate pos
2393 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2394 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2395 dc
.SetPen( m_penExpander
);
2396 dc
.SetBrush( wxNullBrush
);
2397 if( node
->HasChildren() )
2399 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2401 if (m_underMouse
== node
)
2403 flag
|= wxCONTROL_CURRENT
;
2405 if( node
->IsOpen() )
2406 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2408 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2412 // I am wandering whether we should draw dot lines between tree nodes
2414 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2417 //force the expander column to left-center align
2418 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2422 // cannot be bigger than allocated space
2423 wxSize size
= cell
->GetSize();
2424 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2425 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2426 // size.y = wxMin( size.y, cell_rect.height );
2427 size
.y
= cell_rect
.height
;
2429 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2430 int align
= cell
->GetAlignment();
2432 // horizontal alignment:
2433 item_rect
.x
= cell_rect
.x
;
2434 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2435 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2436 else if (align
& wxALIGN_RIGHT
)
2437 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2438 //else: wxALIGN_LEFT is the default
2440 // vertical alignment:
2441 item_rect
.y
= cell_rect
.y
;
2442 if (align
& wxALIGN_CENTER_VERTICAL
)
2443 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2444 else if (align
& wxALIGN_BOTTOM
)
2445 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2446 //else: wxALIGN_TOP is the default
2449 item_rect
.x
+= PADDING_RIGHTLEFT
;
2450 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2452 //Here we add the tree indent
2453 item_rect
.x
+= indent
;
2456 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2457 state
|= wxDATAVIEW_CELL_SELECTED
;
2459 // TODO: it would be much more efficient to create a clipping
2460 // region for the entire column being rendered (in the OnPaint
2461 // of wxDataViewMainWindow) instead of a single clip region for
2462 // each cell. However it would mean that each renderer should
2463 // respect the given wxRect's top & bottom coords, eventually
2464 // violating only the left & right coords - however the user can
2465 // make its own renderer and thus we cannot be sure of that.
2466 dc
.SetClippingRegion( item_rect
);
2467 cell
->Render( item_rect
, &dc
, state
);
2468 dc
.DestroyClippingRegion();
2471 cell_rect
.x
+= cell_rect
.width
;
2475 int wxDataViewMainWindow::GetCountPerPage() const
2477 wxSize size
= GetClientSize();
2478 return size
.y
/ m_lineHeight
;
2481 int wxDataViewMainWindow::GetEndOfLastCol() const
2485 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2487 const wxDataViewColumn
*c
=
2488 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2491 width
+= c
->GetWidth();
2496 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2500 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2502 return y
/ m_lineHeight
;
2505 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2507 wxSize client_size
= GetClientSize();
2508 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2509 &client_size
.x
, &client_size
.y
);
2511 //we should deal with the pixel here
2512 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2514 return wxMin( GetRowCount()-1, row
);
2517 unsigned int wxDataViewMainWindow::GetRowCount()
2519 if ( m_count
== -1 )
2521 m_count
= RecalculateCount();
2523 GetVirtualSize( &width
, &height
);
2524 height
= m_count
* m_lineHeight
;
2526 SetVirtualSize( width
, height
);
2531 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2538 void wxDataViewMainWindow::SelectAllRows( bool on
)
2545 m_selection
.Clear();
2546 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2547 m_selection
.Add( i
);
2552 unsigned int first_visible
= GetFirstVisibleRow();
2553 unsigned int last_visible
= GetLastVisibleRow();
2555 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2557 unsigned int row
= m_selection
[i
];
2558 if ((row
>= first_visible
) && (row
<= last_visible
))
2561 m_selection
.Clear();
2565 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2567 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2571 m_selection
.Add( row
);
2579 m_selection
.Remove( row
);
2585 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2589 unsigned int tmp
= from
;
2595 for (i
= from
; i
<= to
; i
++)
2597 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2600 m_selection
.Add( i
);
2605 m_selection
.Remove( i
);
2608 RefreshRows( from
, to
);
2611 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2613 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2615 int n
= aSelections
[i
];
2617 m_selection
.Add( n
);
2622 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2624 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2625 m_selection
.Add( row
);
2627 m_selection
.Remove( row
);
2631 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2633 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2636 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2638 wxWindow
*parent
= GetParent();
2639 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2641 le
.SetEventObject(parent
);
2642 le
.SetModel(GetOwner()->GetModel());
2645 parent
->GetEventHandler()->ProcessEvent(le
);
2648 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2650 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2651 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2653 wxSize client_size
= GetClientSize();
2654 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2655 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2656 if (intersect_rect
.width
> 0)
2657 Refresh( true, &intersect_rect
);
2660 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2664 unsigned int tmp
= to
;
2669 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2670 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2672 wxSize client_size
= GetClientSize();
2673 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2674 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2675 if (intersect_rect
.width
> 0)
2676 Refresh( true, &intersect_rect
);
2679 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2681 unsigned int count
= GetRowCount();
2682 if (firstRow
> count
)
2685 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2686 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2688 wxSize client_size
= GetClientSize();
2689 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2690 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2691 if (intersect_rect
.width
> 0)
2692 Refresh( true, &intersect_rect
);
2695 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2697 wxCHECK_RET( newCurrent
< GetRowCount(),
2698 _T("invalid item index in OnArrowChar()") );
2700 // if there is no selection, we cannot move it anywhere
2701 if (!HasCurrentRow())
2704 unsigned int oldCurrent
= m_currentRow
;
2706 // in single selection we just ignore Shift as we can't select several
2708 if ( event
.ShiftDown() && !IsSingleSel() )
2710 RefreshRow( oldCurrent
);
2712 ChangeCurrentRow( newCurrent
);
2714 // select all the items between the old and the new one
2715 if ( oldCurrent
> newCurrent
)
2717 newCurrent
= oldCurrent
;
2718 oldCurrent
= m_currentRow
;
2721 SelectRows( oldCurrent
, newCurrent
, true );
2722 if (oldCurrent
!=newCurrent
)
2723 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2727 RefreshRow( oldCurrent
);
2729 // all previously selected items are unselected unless ctrl is held
2730 if ( !event
.ControlDown() )
2731 SelectAllRows(false);
2733 ChangeCurrentRow( newCurrent
);
2735 if ( !event
.ControlDown() )
2737 SelectRow( m_currentRow
, true );
2738 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2741 RefreshRow( m_currentRow
);
2744 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2747 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2751 rect
.y
= m_lineHeight
* row
;
2752 rect
.width
= GetEndOfLastCol();
2753 rect
.height
= m_lineHeight
;
2758 class RowToItemJob
: public DoJob
2761 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2762 virtual ~RowToItemJob(){};
2764 virtual int operator() ( wxDataViewTreeNode
* node
)
2767 if( current
== static_cast<int>(row
))
2769 ret
= node
->GetItem() ;
2773 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2775 current
+= node
->GetSubTreeCount();
2780 //If the current has no child node, we can find the desired item of the row number directly.
2781 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2782 if( node
->GetNodes().GetCount() == 0)
2784 int index
= static_cast<int>(row
) - current
- 1;
2785 ret
= node
->GetChildren().Item( index
);
2792 virtual int operator() ( void * n
)
2795 if( current
== static_cast<int>(row
))
2797 ret
= wxDataViewItem( n
) ;
2802 wxDataViewItem
GetResult(){ return ret
; }
2809 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2811 RowToItemJob
job( row
, -2 );
2812 Walker( m_root
, job
);
2813 return job
.GetResult();
2816 class RowToTreeNodeJob
: public DoJob
2819 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2822 this->current
= current
;
2826 virtual ~RowToTreeNodeJob(){};
2828 virtual int operator() ( wxDataViewTreeNode
* node
)
2831 if( current
== static_cast<int>(row
))
2837 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2839 current
+= node
->GetSubTreeCount();
2845 //If the current has no child node, we can find the desired item of the row number directly.
2846 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2847 if( node
->GetNodes().GetCount() == 0)
2849 int index
= static_cast<int>(row
) - current
- 1;
2850 void * n
= node
->GetChildren().Item( index
);
2851 ret
= new wxDataViewTreeNode( parent
) ;
2852 ret
->SetItem( wxDataViewItem( n
));
2853 ret
->SetHasChildren(false);
2862 virtual int operator() ( void * n
)
2865 if( current
== static_cast<int>(row
))
2867 ret
= new wxDataViewTreeNode( parent
) ;
2868 ret
->SetItem( wxDataViewItem( n
));
2869 ret
->SetHasChildren(false);
2875 wxDataViewTreeNode
* GetResult(){ return ret
; }
2879 wxDataViewTreeNode
* ret
;
2880 wxDataViewTreeNode
* parent
;
2884 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2886 RowToTreeNodeJob
job( row
, -2, m_root
);
2887 Walker( m_root
, job
);
2888 return job
.GetResult();
2891 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2893 wxWindow
*parent
= GetParent();
2894 wxDataViewEvent
le(type
, parent
->GetId());
2896 le
.SetEventObject(parent
);
2897 le
.SetModel(GetOwner()->GetModel());
2900 parent
->GetEventHandler()->ProcessEvent(le
);
2904 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2906 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2909 if( node
->HasChildren())
2911 if( !node
->IsOpen())
2913 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2914 //Check if the user prevent expanding
2915 if( e
.GetSkipped() )
2919 //Here I build the children of current node
2920 if( node
->GetChildrenNumber() == 0 )
2923 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2927 //Send the expanded event
2928 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2932 SelectRow( row
, false );
2933 SelectRow( row
+ 1, true );
2934 ChangeCurrentRow( row
+ 1 );
2935 SendSelectionChangedEvent( GetItemByRow(row
+1));
2943 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2945 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2948 wxDataViewTreeNode
* nd
= node
;
2950 if( node
->HasChildren() && node
->IsOpen() )
2952 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2953 if( e
.GetSkipped() )
2958 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2962 node
= node
->GetParent();
2965 int parent
= GetRowByItem( node
->GetItem() ) ;
2968 SelectRow( row
, false);
2969 SelectRow(parent
, true );
2970 ChangeCurrentRow( parent
);
2971 SendSelectionChangedEvent( node
->GetItem() );
2975 if( !nd
->HasChildren())
2980 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2982 wxDataViewModel
* model
= GetOwner()->GetModel();
2986 //Compose the a parent-chain of the finding item
2988 list
.DeleteContents( true );
2989 wxDataViewItem
it( item
);
2992 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2993 list
.Insert( pItem
);
2994 it
= model
->GetParent( it
);
2997 //Find the item along the parent-chain.
2998 //This algorithm is designed to speed up the node-finding method
2999 wxDataViewTreeNode
* node
= m_root
;
3000 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3002 if( node
->HasChildren() )
3004 if( node
->GetChildrenNumber() == 0 )
3007 BuildTreeHelper(model
, node
->GetItem(), node
);
3010 wxDataViewTreeNodes nodes
= node
->GetNodes();
3012 for (; i
< nodes
.GetCount(); i
++)
3014 if (nodes
[i
]->GetItem() == (**iter
))
3020 if (i
== nodes
.GetCount())
3029 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3031 wxDataViewColumn
*col
= NULL
;
3032 unsigned int cols
= GetOwner()->GetColumnCount();
3033 unsigned int colnum
= 0;
3034 unsigned int x_start
= 0;
3036 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3037 for (x_start
= 0; colnum
< cols
; colnum
++)
3039 col
= GetOwner()->GetColumn(colnum
);
3040 if (col
->IsHidden())
3041 continue; // skip it!
3043 unsigned int w
= col
->GetWidth();
3044 if (x_start
+w
>= (unsigned int)x
)
3051 item
= GetItemByRow( y
/m_lineHeight
);
3054 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3056 int row
= GetRowByItem(item
);
3057 int y
= row
*m_lineHeight
;
3058 int h
= m_lineHeight
;
3060 wxDataViewColumn
*col
= NULL
;
3061 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3063 col
= GetOwner()->GetColumn( i
);
3064 x
+= col
->GetWidth();
3065 if( GetOwner()->GetColumn(i
+1) == column
)
3068 int w
= col
->GetWidth();
3069 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3070 return wxRect(x
, y
, w
, h
);
3073 int wxDataViewMainWindow::RecalculateCount()
3075 return m_root
->GetSubTreeCount();
3078 class ItemToRowJob
: public DoJob
3081 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3082 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3083 virtual ~ItemToRowJob(){};
3085 //Maybe binary search will help to speed up this process
3086 virtual int operator() ( wxDataViewTreeNode
* node
)
3089 if( node
->GetItem() == item
)
3094 if( node
->GetItem() == **m_iter
)
3101 ret
+= node
->GetSubTreeCount();
3107 virtual int operator() ( void * n
)
3110 if( n
== item
.GetID() )
3114 //the row number is begin from zero
3115 int GetResult(){ return ret
-1 ; }
3117 ItemList::const_iterator m_iter
;
3118 wxDataViewItem item
;
3123 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3125 wxDataViewModel
* model
= GetOwner()->GetModel();
3132 //Compose the a parent-chain of the finding item
3134 wxDataViewItem
* pItem
= NULL
;
3135 list
.DeleteContents( true );
3136 wxDataViewItem
it( item
);
3139 pItem
= new wxDataViewItem( it
);
3140 list
.Insert( pItem
);
3141 it
= model
->GetParent( it
);
3143 pItem
= new wxDataViewItem( );
3144 list
.Insert( pItem
);
3146 ItemToRowJob
job( item
, list
.begin() );
3147 Walker(m_root
, job
);
3148 return job
.GetResult();
3151 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3153 if( !model
->IsContainer( item
) )
3156 wxDataViewItemArray children
;
3157 unsigned int num
= model
->GetChildren( item
, children
);
3158 unsigned int index
= 0;
3159 while( index
< num
)
3161 if( model
->IsContainer( children
[index
] ) )
3163 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3164 n
->SetItem(children
[index
]);
3165 n
->SetHasChildren( true ) ;
3170 node
->AddLeaf( children
[index
].GetID() );
3174 node
->SetSubTreeCount( num
);
3175 wxDataViewTreeNode
* n
= node
->GetParent();
3177 n
->ChangeSubTreeCount(num
);
3181 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3183 //First we define a invalid item to fetch the top-level elements
3184 wxDataViewItem item
;
3186 BuildTreeHelper( model
, item
, m_root
);
3190 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3192 if( node
->GetNodeNumber() != 0 )
3194 int len
= node
->GetNodeNumber();
3196 wxDataViewTreeNodes nodes
= node
->GetNodes();
3197 for( ; i
< len
; i
++ )
3199 DestroyTreeHelper(nodes
[i
]);
3205 void wxDataViewMainWindow::DestroyTree()
3207 DestroyTreeHelper(m_root
);
3208 m_root
->SetSubTreeCount(0);
3212 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3214 if (event
.GetKeyCode() == WXK_TAB
)
3216 wxNavigationKeyEvent nevent
;
3217 nevent
.SetWindowChange( event
.ControlDown() );
3218 nevent
.SetDirection( !event
.ShiftDown() );
3219 nevent
.SetEventObject( GetParent()->GetParent() );
3220 nevent
.SetCurrentFocus( m_parent
);
3221 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3225 // no item -> nothing to do
3226 if (!HasCurrentRow())
3232 // don't use m_linesPerPage directly as it might not be computed yet
3233 const int pageSize
= GetCountPerPage();
3234 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3236 switch ( event
.GetKeyCode() )
3240 if (m_currentRow
> 0)
3242 wxWindow
*parent
= GetParent();
3243 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3244 le
.SetItem( GetItemByRow(m_currentRow
) );
3245 le
.SetEventObject(parent
);
3246 le
.SetModel(GetOwner()->GetModel());
3248 parent
->GetEventHandler()->ProcessEvent(le
);
3253 if ( m_currentRow
> 0 )
3254 OnArrowChar( m_currentRow
- 1, event
);
3258 if ( m_currentRow
< GetRowCount() - 1 )
3259 OnArrowChar( m_currentRow
+ 1, event
);
3261 //Add the process for tree expanding/collapsing
3263 OnCollapsing(m_currentRow
);
3266 OnExpanding( m_currentRow
);
3270 OnArrowChar( GetRowCount() - 1, event
);
3275 OnArrowChar( 0, event
);
3280 int steps
= pageSize
- 1;
3281 int index
= m_currentRow
- steps
;
3285 OnArrowChar( index
, event
);
3291 int steps
= pageSize
- 1;
3292 unsigned int index
= m_currentRow
+ steps
;
3293 unsigned int count
= GetRowCount();
3294 if ( index
>= count
)
3297 OnArrowChar( index
, event
);
3306 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3308 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3310 // let the base handle mouse wheel events.
3315 int x
= event
.GetX();
3316 int y
= event
.GetY();
3317 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3318 wxDataViewColumn
*col
= NULL
;
3321 unsigned int cols
= GetOwner()->GetColumnCount();
3323 for (i
= 0; i
< cols
; i
++)
3325 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3327 continue; // skip it!
3329 if (x
< xpos
+ c
->GetWidth())
3334 xpos
+= c
->GetWidth();
3339 wxDataViewRenderer
*cell
= col
->GetRenderer();
3340 unsigned int current
= y
/ m_lineHeight
;
3341 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3343 // Unselect all if below the last row ?
3347 //Test whether the mouse is hovered on the tree item button
3349 if (GetOwner()->GetExpanderColumn() == col
)
3351 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3352 if( node
!=NULL
&& node
->HasChildren() )
3354 int indent
= node
->GetIndentLevel();
3355 indent
= GetOwner()->GetIndent()*indent
;
3356 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3357 if( rect
.Contains( x
, y
) )
3359 //So the mouse is over the expander
3361 if (m_underMouse
&& m_underMouse
!= node
)
3363 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3364 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3366 if (m_underMouse
!= node
)
3368 //wxLogMessage("Do the row: %d", current);
3371 m_underMouse
= node
;
3374 if (node
!=NULL
&& !node
->HasChildren())
3379 if (m_underMouse
!= NULL
)
3381 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3382 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3383 m_underMouse
= NULL
;
3387 wxDataViewModel
*model
= GetOwner()->GetModel();
3389 if (event
.Dragging())
3391 if (m_dragCount
== 0)
3393 // we have to report the raw, physical coords as we want to be
3394 // able to call HitTest(event.m_pointDrag) from the user code to
3395 // get the item being dragged
3396 m_dragStart
= event
.GetPosition();
3401 if (m_dragCount
!= 3)
3404 if (event
.LeftIsDown())
3406 // Notify cell about drag
3415 bool forceClick
= false;
3417 if (event
.ButtonDClick())
3419 m_renameTimer
->Stop();
3420 m_lastOnSame
= false;
3423 wxDataViewItem item
= GetItemByRow(current
);
3424 bool ignore_other_columns
=
3425 ((GetOwner()->GetExpanderColumn() != col
) &&
3426 (model
->IsContainer(item
)) &&
3427 (!model
->HasContainerColumns(item
)));
3429 if (event
.LeftDClick())
3431 if ( current
== m_lineLastClicked
)
3433 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3436 model
->GetValue( value
, item
, col
->GetModelColumn() );
3437 cell
->SetValue( value
);
3438 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3439 col
->GetWidth(), m_lineHeight
);
3440 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3445 wxWindow
*parent
= GetParent();
3446 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3448 le
.SetEventObject(parent
);
3449 le
.SetModel(GetOwner()->GetModel());
3451 parent
->GetEventHandler()->ProcessEvent(le
);
3457 // The first click was on another item, so don't interpret this as
3458 // a double click, but as a simple click instead
3465 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3467 // select single line
3468 SelectAllRows( false );
3469 SelectRow( m_lineSelectSingleOnUp
, true );
3472 //Process the event of user clicking the expander
3473 bool expander
= false;
3474 if (GetOwner()->GetExpanderColumn() == col
)
3476 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3477 if( node
!=NULL
&& node
->HasChildren() )
3479 int indent
= node
->GetIndentLevel();
3480 indent
= GetOwner()->GetIndent()*indent
;
3481 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3482 if( rect
.Contains( x
, y
) )
3485 if( node
->IsOpen() )
3486 OnCollapsing(current
);
3488 OnExpanding( current
);
3492 //If the user click the expander, we do not do editing even if the column with expander are editable
3493 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3495 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3496 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3498 m_renameTimer
->Start( 100, true );
3502 m_lastOnSame
= false;
3503 m_lineSelectSingleOnUp
= (unsigned int)-1;
3507 // This is necessary, because after a DnD operation in
3508 // from and to ourself, the up event is swallowed by the
3509 // DnD code. So on next non-up event (which means here and
3510 // now) m_lineSelectSingleOnUp should be reset.
3511 m_lineSelectSingleOnUp
= (unsigned int)-1;
3514 if (event
.RightDown())
3516 m_lineBeforeLastClicked
= m_lineLastClicked
;
3517 m_lineLastClicked
= current
;
3519 // If the item is already selected, do not update the selection.
3520 // Multi-selections should not be cleared if a selected item is clicked.
3521 if (!IsRowSelected(current
))
3523 SelectAllRows(false);
3524 ChangeCurrentRow(current
);
3525 SelectRow(m_currentRow
,true);
3526 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3529 // notify cell about right click
3532 // Allow generation of context menu event
3535 else if (event
.MiddleDown())
3537 // notify cell about middle click
3540 if (event
.LeftDown() || forceClick
)
3544 m_lineBeforeLastClicked
= m_lineLastClicked
;
3545 m_lineLastClicked
= current
;
3547 unsigned int oldCurrentRow
= m_currentRow
;
3548 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3550 bool cmdModifierDown
= event
.CmdDown();
3551 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3553 if ( IsSingleSel() || !IsRowSelected(current
) )
3555 SelectAllRows( false );
3556 ChangeCurrentRow(current
);
3557 SelectRow(m_currentRow
,true);
3558 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3560 else // multi sel & current is highlighted & no mod keys
3562 m_lineSelectSingleOnUp
= current
;
3563 ChangeCurrentRow(current
); // change focus
3566 else // multi sel & either ctrl or shift is down
3568 if (cmdModifierDown
)
3570 ChangeCurrentRow(current
);
3571 ReverseRowSelection(m_currentRow
);
3572 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3574 else if (event
.ShiftDown())
3576 ChangeCurrentRow(current
);
3578 unsigned int lineFrom
= oldCurrentRow
,
3581 if ( lineTo
< lineFrom
)
3584 lineFrom
= m_currentRow
;
3587 SelectRows(lineFrom
, lineTo
, true);
3588 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3590 else // !ctrl, !shift
3592 // test in the enclosing if should make it impossible
3593 wxFAIL_MSG( _T("how did we get here?") );
3597 if (m_currentRow
!= oldCurrentRow
)
3598 RefreshRow( oldCurrentRow
);
3600 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3602 // Update selection here...
3605 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3606 (current
== oldCurrentRow
)) && oldWasSelected
;
3610 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3614 if (HasCurrentRow())
3620 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3624 if (HasCurrentRow())
3630 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3632 if( m_selection
.GetCount() != 1 )
3633 return wxDataViewItem();
3635 return GetItemByRow( m_selection
.Item(0));
3638 //-----------------------------------------------------------------------------
3640 //-----------------------------------------------------------------------------
3641 WX_DEFINE_LIST(wxDataViewColumnList
);
3643 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3645 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3646 EVT_SIZE(wxDataViewCtrl::OnSize
)
3649 wxDataViewCtrl::~wxDataViewCtrl()
3652 GetModel()->RemoveNotifier( m_notifier
);
3655 void wxDataViewCtrl::Init()
3660 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3661 const wxPoint
& pos
, const wxSize
& size
,
3662 long style
, const wxValidator
& validator
)
3664 if (!wxControl::Create( parent
, id
, pos
, size
,
3665 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3668 SetInitialSize(size
);
3673 MacSetClipChildren( true ) ;
3676 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3678 if (HasFlag(wxDV_NO_HEADER
))
3679 m_headerArea
= NULL
;
3681 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3683 SetTargetWindow( m_clientArea
);
3685 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3687 sizer
->Add( m_headerArea
, 0, wxGROW
);
3688 sizer
->Add( m_clientArea
, 1, wxGROW
);
3695 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3699 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3702 // we need to process arrows ourselves for scrolling
3703 if ( nMsg
== WM_GETDLGCODE
)
3705 rc
|= DLGC_WANTARROWS
;
3713 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3715 // We need to override OnSize so that our scrolled
3716 // window a) does call Layout() to use sizers for
3717 // positioning the controls but b) does not query
3718 // the sizer for their size and use that for setting
3719 // the scrollable area as set that ourselves by
3720 // calling SetScrollbar() further down.
3727 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3729 if (!wxDataViewCtrlBase::AssociateModel( model
))
3732 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3734 model
->AddNotifier( m_notifier
);
3736 m_clientArea
->BuildTree(model
);
3738 m_clientArea
->UpdateDisplay();
3743 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3745 if (!wxDataViewCtrlBase::AppendColumn(col
))
3748 m_cols
.Append( col
);
3753 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3755 if (!wxDataViewCtrlBase::PrependColumn(col
))
3758 m_cols
.Insert( col
);
3763 void wxDataViewCtrl::OnColumnChange()
3766 m_headerArea
->UpdateDisplay();
3768 m_clientArea
->UpdateDisplay();
3771 void wxDataViewCtrl::DoSetExpanderColumn()
3773 m_clientArea
->UpdateDisplay();
3776 void wxDataViewCtrl::DoSetIndent()
3778 m_clientArea
->UpdateDisplay();
3781 unsigned int wxDataViewCtrl::GetColumnCount() const
3783 return m_cols
.GetCount();
3786 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3788 wxDataViewColumnList::const_iterator iter
;
3790 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3795 if ((*iter
)->IsHidden())
3802 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3804 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3815 bool wxDataViewCtrl::ClearColumns()
3822 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3824 int ret
= 0, dead
= 0;
3825 int len
= GetColumnCount();
3826 for (int i
=0; i
<len
; i
++)
3828 wxDataViewColumn
* col
= GetColumn(i
);
3829 if (col
->IsHidden())
3831 ret
+= col
->GetWidth();
3834 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3841 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3846 //Selection code with wxDataViewItem as parameters
3847 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3849 return m_clientArea
->GetSelection();
3852 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3855 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3856 int len
= selection
.GetCount();
3857 for( int i
= 0; i
< len
; i
++)
3859 unsigned int row
= selection
[i
];
3860 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3865 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3867 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3868 int len
= sel
.GetCount();
3869 for( int i
= 0; i
< len
; i
++ )
3871 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3873 selection
.Add( static_cast<unsigned int>(row
) );
3875 m_clientArea
->SetSelections( selection
);
3878 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3880 int row
= m_clientArea
->GetRowByItem( item
);
3883 //Unselect all rows before select another in the single select mode
3884 if (m_clientArea
->IsSingleSel())
3885 m_clientArea
->SelectAllRows(false);
3886 m_clientArea
->SelectRow(row
, true);
3890 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3892 int row
= m_clientArea
->GetRowByItem( item
);
3894 m_clientArea
->SelectRow(row
, false);
3897 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3899 int row
= m_clientArea
->GetRowByItem( item
);
3902 return m_clientArea
->IsRowSelected(row
);
3907 //Selection code with row number as parameter
3908 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3911 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3912 int len
= selection
.GetCount();
3913 for( int i
= 0; i
< len
; i
++)
3915 unsigned int row
= selection
[i
];
3921 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3923 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3924 int len
= sel
.GetCount();
3925 for( int i
= 0; i
< len
; i
++ )
3929 selection
.Add( static_cast<unsigned int>(row
) );
3931 m_clientArea
->SetSelections( selection
);
3934 void wxDataViewCtrl::Select( int row
)
3938 if (m_clientArea
->IsSingleSel())
3939 m_clientArea
->SelectAllRows(false);
3940 m_clientArea
->SelectRow( row
, true );
3944 void wxDataViewCtrl::Unselect( int row
)
3947 m_clientArea
->SelectRow(row
, false);
3950 bool wxDataViewCtrl::IsSelected( int row
) const
3953 return m_clientArea
->IsRowSelected(row
);
3957 void wxDataViewCtrl::SelectRange( int from
, int to
)
3960 for( int i
= from
; i
< to
; i
++ )
3962 m_clientArea
->Select(sel
);
3965 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3967 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3968 for( int i
= from
; i
< to
; i
++ )
3969 if( sel
.Index( i
) != wxNOT_FOUND
)
3971 m_clientArea
->SetSelections(sel
);
3974 void wxDataViewCtrl::SelectAll()
3976 m_clientArea
->SelectAllRows(true);
3979 void wxDataViewCtrl::UnselectAll()
3981 m_clientArea
->SelectAllRows(false);
3984 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3988 if( row
> (int) m_clientArea
->GetRowCount() )
3989 row
= m_clientArea
->GetRowCount();
3991 int first
= m_clientArea
->GetFirstVisibleRow();
3992 int last
= m_clientArea
->GetLastVisibleRow();
3994 m_clientArea
->ScrollTo( row
, column
);
3995 else if( row
> last
)
3996 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3998 m_clientArea
->ScrollTo( first
, column
);
4001 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4003 int row
= m_clientArea
->GetRowByItem(item
);
4006 if( column
== NULL
)
4007 EnsureVisible(row
, -1);
4011 int len
= GetColumnCount();
4012 for( int i
= 0; i
< len
; i
++ )
4013 if( GetColumn(i
) == column
)
4018 EnsureVisible( row
, col
);
4024 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4026 m_clientArea
->HitTest(point
, item
, column
);
4029 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4031 return m_clientArea
->GetItemRect(item
, column
);
4034 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4036 return m_clientArea
->GetItemByRow( row
);
4039 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4041 return m_clientArea
->GetRowByItem( item
);
4044 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4046 int row
= m_clientArea
->GetRowByItem( item
);
4048 m_clientArea
->Expand(row
);
4051 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4053 int row
= m_clientArea
->GetRowByItem( item
);
4055 m_clientArea
->Collapse(row
);
4059 // !wxUSE_GENERICDATAVIEWCTRL
4062 // wxUSE_DATAVIEWCTRL