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
);
235 void AdjustDC(wxDC
& dc
);
238 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
239 DECLARE_EVENT_TABLE()
242 #endif // defined(__WXMSW__)
244 //-----------------------------------------------------------------------------
245 // wxDataViewRenameTimer
246 //-----------------------------------------------------------------------------
248 class wxDataViewRenameTimer
: public wxTimer
251 wxDataViewMainWindow
*m_owner
;
254 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
258 //-----------------------------------------------------------------------------
259 // wxDataViewTreeNode
260 //-----------------------------------------------------------------------------
261 class wxDataViewTreeNode
;
262 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
263 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
265 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
266 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
268 class wxDataViewTreeNode
271 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
272 { this->parent
= parent
;
280 //I don't know what I need to do in the destructure
281 ~wxDataViewTreeNode()
286 wxDataViewTreeNode
* GetParent() { return parent
; }
287 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
288 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
289 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
291 void AddNode( wxDataViewTreeNode
* node
)
293 leaves
.Add( node
->GetItem().GetID() );
295 leaves
.Sort( &wxGenericTreeModelItemCmp
);
298 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
300 void AddLeaf( void * leaf
)
304 leaves
.Sort( &wxGenericTreeModelItemCmp
);
307 wxDataViewItem
& GetItem() { return item
; }
308 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
310 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
311 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
315 wxDataViewTreeNode
* node
= this;
316 while( node
->GetParent()->GetParent() != NULL
)
318 node
= node
->GetParent();
331 int len
= nodes
.GetCount();
333 for ( int i
= 0 ;i
< len
; i
++)
334 sum
+= nodes
[i
]->GetSubTreeCount();
336 sum
+= leaves
.GetCount();
339 ChangeSubTreeCount(-sum
);
345 ChangeSubTreeCount(sum
);
348 bool HasChildren() { return hasChildren
; }
349 void SetHasChildren( bool has
){ hasChildren
= has
; }
351 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
352 int GetSubTreeCount() { return subTreeCount
; }
353 void ChangeSubTreeCount( int num
)
359 parent
->ChangeSubTreeCount(num
);
366 nodes
.Sort( &wxGenericTreeModelNodeCmp
);
367 int len
= nodes
.GetCount();
368 for (int i
= 0; i
< len
; i
++)
372 leaves
.Sort( &wxGenericTreeModelItemCmp
);
377 wxDataViewTreeNode
* parent
;
378 wxDataViewTreeNodes nodes
;
379 wxDataViewTreeLeaves leaves
;
386 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
388 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
391 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
393 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
398 //-----------------------------------------------------------------------------
399 // wxDataViewMainWindow
400 //-----------------------------------------------------------------------------
402 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
404 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
405 WX_DEFINE_LIST(ItemList
);
407 class wxDataViewMainWindow
: public wxWindow
410 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
412 const wxPoint
&pos
= wxDefaultPosition
,
413 const wxSize
&size
= wxDefaultSize
,
414 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
415 virtual ~wxDataViewMainWindow();
417 // notifications from wxDataViewModel
418 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
419 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
420 bool ItemChanged( const wxDataViewItem
&item
);
421 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
432 g_model
= GetOwner()->GetModel();
433 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
436 if (g_model
->HasDefaultCompare())
444 g_column
= col
->GetModelColumn();
445 g_asending
= col
->IsSortOrderAscending();
448 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
449 wxDataViewCtrl
*GetOwner() { return m_owner
; }
450 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
452 void OnPaint( wxPaintEvent
&event
);
453 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
454 void OnChar( wxKeyEvent
&event
);
455 void OnMouse( wxMouseEvent
&event
);
456 void OnSetFocus( wxFocusEvent
&event
);
457 void OnKillFocus( wxFocusEvent
&event
);
459 void UpdateDisplay();
460 void RecalculateDisplay();
461 void OnInternalIdle();
463 void OnRenameTimer();
465 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
466 void ScrollTo( int rows
, int column
);
468 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
469 void ChangeCurrentRow( unsigned int row
);
471 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
472 bool IsEmpty() { return GetRowCount() == 0; }
474 int GetCountPerPage() const;
475 int GetEndOfLastCol() const;
476 unsigned int GetFirstVisibleRow() const;
477 //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
478 unsigned int GetLastVisibleRow();
479 unsigned int GetRowCount() ;
481 wxDataViewItem
GetSelection() const;
482 wxDataViewSelection
GetSelections(){ return m_selection
; }
483 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
484 void Select( const wxArrayInt
& aSelections
);
485 void SelectAllRows( bool on
);
486 void SelectRow( unsigned int row
, bool on
);
487 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
488 void ReverseRowSelection( unsigned int row
);
489 bool IsRowSelected( unsigned int row
);
490 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
492 void RefreshRow( unsigned int row
);
493 void RefreshRows( unsigned int from
, unsigned int to
);
494 void RefreshRowsAfter( unsigned int firstRow
);
496 // returns the colour to be used for drawing the rules
497 wxColour
GetRuleColour() const
499 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
502 wxRect
GetLineRect( unsigned int row
) const;
504 //Some useful functions for row and item mapping
505 wxDataViewItem
GetItemByRow( unsigned int row
) const;
506 int GetRowByItem( const wxDataViewItem
& item
);
508 //Methods for building the mapping tree
509 void BuildTree( wxDataViewModel
* model
);
511 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
512 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
514 void Expand( unsigned int row
) { OnExpanding( row
); }
515 void Collapse( unsigned int row
) { OnCollapsing( row
); }
517 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
518 //We did not need this temporarily
519 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
521 int RecalculateCount() ;
523 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
524 void OnExpanding( unsigned int row
);
525 void OnCollapsing( unsigned int row
);
527 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
530 wxDataViewCtrl
*m_owner
;
534 wxDataViewColumn
*m_currentCol
;
535 unsigned int m_currentRow
;
536 wxDataViewSelection m_selection
;
538 wxDataViewRenameTimer
*m_renameTimer
;
546 // for double click logic
547 unsigned int m_lineLastClicked
,
548 m_lineBeforeLastClicked
,
549 m_lineSelectSingleOnUp
;
551 // the pen used to draw horiz/vertical rules
554 // the pen used to draw the expander and the lines
557 //This is the tree structure of the model
558 wxDataViewTreeNode
* m_root
;
560 //This is the tree node under the cursor
561 wxDataViewTreeNode
* m_underMouse
;
563 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
564 DECLARE_EVENT_TABLE()
567 // ---------------------------------------------------------
568 // wxGenericDataViewModelNotifier
569 // ---------------------------------------------------------
571 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
574 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
575 { m_mainWindow
= mainWindow
; }
577 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
578 { return m_mainWindow
->ItemAdded( parent
, item
); }
579 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
580 { return m_mainWindow
->ItemDeleted( parent
, item
); }
581 virtual bool ItemChanged( const wxDataViewItem
& item
)
582 { return m_mainWindow
->ItemChanged(item
); }
583 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
584 { return m_mainWindow
->ValueChanged( item
, col
); }
585 virtual bool Cleared()
586 { return m_mainWindow
->Cleared(); }
587 virtual void Resort()
588 { m_mainWindow
->Resort(); }
590 wxDataViewMainWindow
*m_mainWindow
;
593 // ---------------------------------------------------------
594 // wxDataViewRenderer
595 // ---------------------------------------------------------
597 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
599 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
600 wxDataViewCellMode mode
,
602 wxDataViewRendererBase( varianttype
, mode
, align
)
609 wxDataViewRenderer::~wxDataViewRenderer()
615 wxDC
*wxDataViewRenderer::GetDC()
619 if (GetOwner() == NULL
)
621 if (GetOwner()->GetOwner() == NULL
)
623 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
629 // ---------------------------------------------------------
630 // wxDataViewCustomRenderer
631 // ---------------------------------------------------------
633 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
635 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
636 wxDataViewCellMode mode
, int align
) :
637 wxDataViewRenderer( varianttype
, mode
, align
)
641 // ---------------------------------------------------------
642 // wxDataViewTextRenderer
643 // ---------------------------------------------------------
645 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
647 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
648 wxDataViewCellMode mode
, int align
) :
649 wxDataViewCustomRenderer( varianttype
, mode
, align
)
653 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
655 m_text
= value
.GetString();
660 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
665 bool wxDataViewTextRenderer::HasEditorCtrl()
670 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
671 wxRect labelRect
, const wxVariant
&value
)
673 return new wxTextCtrl( parent
, wxID_ANY
, value
,
674 wxPoint(labelRect
.x
,labelRect
.y
),
675 wxSize(labelRect
.width
,labelRect
.height
) );
678 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
680 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
681 value
= text
->GetValue();
685 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
687 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
688 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
689 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
690 view
->GetForegroundColour();
692 dc
->SetTextForeground(col
);
693 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
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 WXUNUSED(state
) )
957 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
958 wxString tmp
= m_date
.FormatDate();
959 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
964 wxSize
wxDataViewDateRenderer::GetSize() const
966 const wxDataViewCtrl
* view
= GetView();
967 wxString tmp
= m_date
.FormatDate();
969 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
970 return wxSize(x
,y
+d
);
973 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
974 const wxDataViewItem
& item
, unsigned int col
)
977 model
->GetValue( variant
, item
, col
);
978 wxDateTime value
= variant
.GetDateTime();
980 #if wxUSE_DATE_RENDERER_POPUP
981 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
982 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
983 wxPoint pos
= wxGetMousePosition();
986 popup
->Popup( popup
->m_cal
);
987 #else // !wxUSE_DATE_RENDERER_POPUP
988 wxMessageBox(value
.Format());
989 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
993 // ---------------------------------------------------------
994 // wxDataViewIconTextRenderer
995 // ---------------------------------------------------------
997 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
999 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1000 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1001 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1004 SetAlignment(align
);
1007 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1011 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1017 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1022 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1024 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1026 dc
->SetFont( view
->GetFont() );
1028 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1029 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1030 view
->GetForegroundColour();
1032 dc
->SetTextForeground(col
);
1034 const wxIcon
&icon
= m_value
.GetIcon();
1037 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1038 cell
.x
+= icon
.GetWidth()+4;
1041 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1046 wxSize
wxDataViewIconTextRenderer::GetSize() const
1048 return wxSize(80,16); // TODO
1051 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1056 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1061 // ---------------------------------------------------------
1063 // ---------------------------------------------------------
1065 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1067 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1068 unsigned int model_column
,
1069 int width
, wxAlignment align
, int flags
) :
1070 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1072 SetAlignment(align
);
1076 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1079 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1080 unsigned int model_column
,
1081 int width
, wxAlignment align
, int flags
) :
1082 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1084 SetAlignment(align
);
1087 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1090 wxDataViewColumn::~wxDataViewColumn()
1094 void wxDataViewColumn::Init( int width
)
1097 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1101 void wxDataViewColumn::SetResizeable( bool resizeable
)
1104 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1106 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1109 void wxDataViewColumn::SetHidden( bool hidden
)
1112 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1114 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1116 // tell our owner to e.g. update its scrollbars:
1118 GetOwner()->OnColumnChange();
1121 void wxDataViewColumn::SetSortable( bool sortable
)
1124 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1126 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1128 // Update header button
1130 GetOwner()->OnColumnChange();
1133 void wxDataViewColumn::SetSortOrder( bool ascending
)
1135 m_ascending
= ascending
;
1137 // Update header button
1139 GetOwner()->OnColumnChange();
1142 bool wxDataViewColumn::IsSortOrderAscending() const
1147 void wxDataViewColumn::SetInternalWidth( int width
)
1151 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1152 if (m_owner
&& m_owner
->m_clientArea
)
1153 m_owner
->m_clientArea
->RecalculateDisplay();
1156 void wxDataViewColumn::SetWidth( int width
)
1158 m_owner
->m_headerArea
->UpdateDisplay();
1160 SetInternalWidth(width
);
1164 //-----------------------------------------------------------------------------
1165 // wxDataViewHeaderWindowBase
1166 //-----------------------------------------------------------------------------
1168 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1170 wxWindow
*parent
= GetParent();
1171 wxDataViewEvent
le(type
, parent
->GetId());
1173 le
.SetEventObject(parent
);
1175 le
.SetDataViewColumn(GetColumn(n
));
1176 le
.SetModel(GetOwner()->GetModel());
1178 // for events created by wxDataViewHeaderWindow the
1179 // row / value fields are not valid
1181 parent
->GetEventHandler()->ProcessEvent(le
);
1184 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1186 // implemented in msw/listctrl.cpp:
1187 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1189 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1191 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1192 const wxPoint
&pos
, const wxSize
&size
,
1193 const wxString
&name
)
1197 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1200 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1201 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1202 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1203 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1205 // create the native WC_HEADER window:
1206 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1207 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1208 m_hWnd
= CreateWindowEx(0,
1219 wxLogLastError(_T("CreateWindowEx"));
1223 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1224 // to call wxDataViewHeaderWindow::MSWOnNotify
1225 SubclassWin(m_hWnd
);
1227 // the following is required to get the default win's font for
1228 // header windows and must be done befor sending the HDM_LAYOUT msg
1235 // Retrieve the bounding rectangle of the parent window's
1236 // client area, and then request size and position values
1237 // from the header control.
1238 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1240 hdl
.prc
= &rcParent
;
1242 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1244 wxLogLastError(_T("SendMessage"));
1248 // Set the size, position, and visibility of the header control.
1249 SetWindowPos((HWND
)m_hWnd
,
1253 wp
.flags
| SWP_SHOWWINDOW
);
1255 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1256 // a wxBoxSizer and in order to avoid super-big header windows,
1257 // we need to set our height as fixed
1258 SetMinSize(wxSize(-1, wp
.cy
));
1259 SetMaxSize(wxSize(-1, wp
.cy
));
1264 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1269 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1271 // remove old columns
1272 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1273 Header_DeleteItem((HWND
)m_hWnd
, 0);
1275 // add the updated array of columns to the header control
1276 unsigned int cols
= GetOwner()->GetColumnCount();
1277 unsigned int added
= 0;
1278 wxDataViewModel
* model
= GetOwner()->GetModel();
1279 for (unsigned int i
= 0; i
< cols
; i
++)
1281 wxDataViewColumn
*col
= GetColumn( i
);
1282 if (col
->IsHidden())
1283 continue; // don't add it!
1286 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1287 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1288 hdi
.cxy
= col
->GetWidth();
1289 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1290 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1291 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1294 if(model
&& m_owner
->GetSortingColumn() == col
)
1296 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1297 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1298 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1302 // lParam is reserved for application's use:
1303 // we store there the column index to use it later in MSWOnNotify
1304 // (since columns may have been hidden)
1305 hdi
.lParam
= (LPARAM
)i
;
1307 // the native wxMSW implementation of the header window
1308 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1309 // on the right: to correct this effect we make the column
1310 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1312 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1314 switch (col
->GetAlignment())
1317 hdi
.fmt
|= HDF_LEFT
;
1319 case wxALIGN_CENTER
:
1320 case wxALIGN_CENTER_HORIZONTAL
:
1321 hdi
.fmt
|= HDF_CENTER
;
1324 hdi
.fmt
|= HDF_RIGHT
;
1328 // such alignment is not allowed for the column header!
1332 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1333 (WPARAM
)added
, (LPARAM
)&hdi
);
1338 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1342 // NOTE: we don't just return nmHDR->iItem because when there are
1343 // hidden columns, nmHDR->iItem may be different from
1344 // nmHDR->pitem->lParam
1346 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1348 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1353 item
.mask
= HDI_LPARAM
;
1354 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1356 return (unsigned int)item
.lParam
;
1359 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1361 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1363 // is it a message from the header?
1364 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1365 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1367 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1368 switch ( nmhdr
->code
)
1370 case HDN_BEGINTRACK
:
1371 // user has started to resize a column:
1372 // do we need to veto it?
1373 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1381 // user has started to reorder a column
1384 case HDN_ITEMCHANGING
:
1385 if (nmHDR
->pitem
!= NULL
&&
1386 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1388 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1389 if (nmHDR
->pitem
->cxy
< minWidth
)
1391 // do not allow the user to resize this column under
1392 // its minimal width:
1398 case HDN_ITEMCHANGED
: // user is resizing a column
1399 case HDN_ENDTRACK
: // user has finished resizing a column
1400 case HDN_ENDDRAG
: // user has finished reordering a column
1402 // update the width of the modified column:
1403 if (nmHDR
->pitem
!= NULL
&&
1404 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1406 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1407 unsigned int w
= nmHDR
->pitem
->cxy
;
1408 wxDataViewColumn
*col
= GetColumn(idx
);
1410 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1411 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1412 w
-= COLUMN_WIDTH_OFFSET
;
1414 if (w
>= (unsigned)col
->GetMinWidth())
1415 col
->SetInternalWidth(w
);
1421 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1422 wxDataViewModel
* model
= GetOwner()->GetModel();
1424 if(nmHDR
->iButton
== 0)
1426 wxDataViewColumn
*col
= GetColumn(idx
);
1427 if(col
->IsSortable())
1429 if(model
&& m_owner
->GetSortingColumn() == col
)
1431 bool order
= col
->IsSortOrderAscending();
1432 col
->SetSortOrder(!order
);
1436 m_owner
->SetSortingColumn(col
);
1444 wxEventType evt
= nmHDR
->iButton
== 0 ?
1445 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1446 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1447 SendEvent(evt
, idx
);
1453 // NOTE: for some reason (i.e. for a bug in Windows)
1454 // the HDN_ITEMCLICK notification is not sent on
1455 // right clicks, so we need to handle NM_RCLICK
1458 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1459 if (column
!= wxNOT_FOUND
)
1462 item
.mask
= HDI_LPARAM
;
1463 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1465 // 'idx' may be different from 'column' if there are
1466 // hidden columns...
1467 unsigned int idx
= (unsigned int)item
.lParam
;
1468 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1474 case HDN_GETDISPINFOW
:
1475 // see wxListCtrl::MSWOnNotify for more info!
1478 case HDN_ITEMDBLCLICK
:
1480 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1481 int w
= GetOwner()->GetBestColumnWidth(idx
);
1483 // update the native control:
1485 ZeroMemory(&hd
, sizeof(hd
));
1486 hd
.mask
= HDI_WIDTH
;
1488 Header_SetItem(GetHwnd(),
1489 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1492 // update the wxDataViewColumn class:
1493 GetColumn(idx
)->SetInternalWidth(w
);
1498 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1504 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1505 const wxRect
*WXUNUSED(rect
))
1507 wxSize ourSz
= GetClientSize();
1508 wxSize ownerSz
= m_owner
->GetClientSize();
1510 // where should the (logical) origin of this window be placed?
1512 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1514 // put this window on top of our parent and
1515 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1516 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1520 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1521 int WXUNUSED(w
), int WXUNUSED(h
),
1524 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1525 // the wxDataViewCtrl window gets resized: the following dummy call
1526 // to ScrollWindow() is required in order to get this header window
1527 // correctly repainted when it's (horizontally) scrolled:
1532 #else // !defined(__WXMSW__)
1534 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1535 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1536 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1537 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1538 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1541 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1542 const wxPoint
&pos
, const wxSize
&size
,
1543 const wxString
&name
)
1547 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1550 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1551 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1552 SetOwnForegroundColour( attr
.colFg
);
1553 SetOwnBackgroundColour( attr
.colBg
);
1555 SetOwnFont( attr
.font
);
1557 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1558 // a wxBoxSizer and in order to avoid super-big header windows,
1559 // we need to set our height as fixed
1560 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1561 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1566 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1569 GetClientSize( &w
, &h
);
1571 wxAutoBufferedPaintDC
dc( this );
1573 dc
.SetBackground(GetBackgroundColour());
1577 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1580 m_owner
->GetViewStart( &x
, NULL
);
1582 // account for the horz scrollbar offset
1583 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1585 dc
.SetFont( GetFont() );
1587 unsigned int cols
= GetOwner()->GetColumnCount();
1590 for (i
= 0; i
< cols
; i
++)
1592 wxDataViewColumn
*col
= GetColumn( i
);
1593 if (col
->IsHidden())
1594 continue; // skip it!
1596 int cw
= col
->GetWidth();
1599 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1600 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1602 if (col
->IsSortOrderAscending())
1603 sortArrow
= wxHDR_SORT_ICON_UP
;
1605 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1609 if (m_parent
->IsEnabled())
1611 if ((int) i
== m_hover
)
1612 state
= wxCONTROL_CURRENT
;
1616 state
= (int) wxCONTROL_DISABLED
;
1619 wxRendererNative::Get().DrawHeaderButton
1623 wxRect(xpos
, 0, cw
, ch
-1),
1628 // align as required the column title:
1630 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1631 switch (col
->GetAlignment())
1634 x
+= HEADER_HORIZ_BORDER
;
1637 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1640 case wxALIGN_CENTER
:
1641 case wxALIGN_CENTER_HORIZONTAL
:
1642 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1646 // always center the title vertically:
1647 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1649 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1651 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1652 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1653 dc
.DrawText( col
->GetTitle(), x
, y
);
1654 dc
.DestroyClippingRegion();
1660 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1662 GetParent()->SetFocus();
1666 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1668 // we want to work with logical coords
1670 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1671 int y
= event
.GetY();
1675 // we don't draw the line beyond our window,
1676 // but we allow dragging it there
1678 GetClientSize( &w
, NULL
);
1679 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1682 if (event
.ButtonUp())
1684 m_isDragging
= false;
1690 m_currentX
= wxMax(m_minX
+ 7, x
);
1694 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1696 GetOwner()->Refresh();
1700 else // not dragging
1703 m_column
= wxNOT_FOUND
;
1705 bool hit_border
= false;
1707 // end of the current column
1710 // find the column where this event occured
1711 int countCol
= m_owner
->GetColumnCount();
1712 for (int column
= 0; column
< countCol
; column
++)
1714 wxDataViewColumn
*p
= GetColumn(column
);
1717 continue; // skip if not shown
1719 xpos
+= p
->GetWidth();
1721 if ((abs(x
-xpos
) < 3) && (y
< 22))
1729 // inside the column
1736 int old_hover
= m_hover
;
1738 if (event
.Leaving())
1739 m_hover
= wxNOT_FOUND
;
1740 if (old_hover
!= m_hover
)
1743 if (m_column
== wxNOT_FOUND
)
1746 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1747 if (event
.LeftDClick() && resizeable
)
1749 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1752 else if (event
.LeftDown() || event
.RightUp())
1754 if (hit_border
&& event
.LeftDown() && resizeable
)
1756 m_isDragging
= true;
1760 else // click on a column
1762 wxDataViewModel
* model
= GetOwner()->GetModel();
1763 wxEventType evt
= event
.LeftDown() ?
1764 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1765 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1766 SendEvent(evt
, m_column
);
1768 //Left click the header
1769 if(event
.LeftDown())
1771 wxDataViewColumn
*col
= GetColumn(m_column
);
1772 if(col
->IsSortable())
1774 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1775 if(model
&& sortCol
== col
)
1777 bool order
= col
->IsSortOrderAscending();
1778 col
->SetSortOrder(!order
);
1782 m_owner
->SetSortingColumn(col
);
1788 //Send the column sorted event
1789 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1793 else if (event
.Moving())
1795 if (hit_border
&& resizeable
)
1796 m_currentCursor
= m_resizeCursor
;
1798 m_currentCursor
= wxSTANDARD_CURSOR
;
1800 SetCursor(*m_currentCursor
);
1805 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1806 void wxGenericDataViewHeaderWindow::DrawCurrent()
1809 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1811 int x1
= m_currentX
;
1813 ClientToScreen (&x1
, &y1
);
1815 int x2
= m_currentX
-1;
1817 ++x2
; // but why ????
1820 m_owner
->GetClientSize( NULL
, &y2
);
1821 m_owner
->ClientToScreen( &x2
, &y2
);
1824 dc
.SetLogicalFunction(wxINVERT
);
1825 dc
.SetPen(m_penCurrent
);
1826 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1828 dc
.DrawLine(x1
, y1
, x2
, y2
);
1832 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1836 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1837 m_owner
->GetViewStart( &x
, NULL
);
1839 // shift the DC origin to match the position of the main window horizontal
1840 // scrollbar: this allows us to always use logical coords
1841 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1844 #endif // defined(__WXMSW__)
1846 //-----------------------------------------------------------------------------
1847 // wxDataViewRenameTimer
1848 //-----------------------------------------------------------------------------
1850 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1855 void wxDataViewRenameTimer::Notify()
1857 m_owner
->OnRenameTimer();
1860 //-----------------------------------------------------------------------------
1861 // wxDataViewMainWindow
1862 //-----------------------------------------------------------------------------
1864 //The tree building helper, declared firstly
1865 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1867 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1869 if (row1
> row2
) return 1;
1870 if (row1
== row2
) return 0;
1875 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1877 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1878 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1879 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1880 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1881 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1882 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1885 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1886 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1887 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1888 m_selection( wxDataViewSelectionCmp
)
1893 m_lastOnSame
= false;
1894 m_renameTimer
= new wxDataViewRenameTimer( this );
1896 // TODO: user better initial values/nothing selected
1897 m_currentCol
= NULL
;
1900 // TODO: we need to calculate this smartly
1909 m_dragStart
= wxPoint(0,0);
1910 m_lineLastClicked
= (unsigned int) -1;
1911 m_lineBeforeLastClicked
= (unsigned int) -1;
1912 m_lineSelectSingleOnUp
= (unsigned int) -1;
1916 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1917 SetBackgroundColour( *wxWHITE
);
1919 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1921 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1922 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1923 //Some new added code to deal with the tree structure
1924 m_root
= new wxDataViewTreeNode( NULL
);
1925 m_root
->SetHasChildren(true);
1927 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1929 m_underMouse
= NULL
;
1933 wxDataViewMainWindow::~wxDataViewMainWindow()
1936 delete m_renameTimer
;
1939 void wxDataViewMainWindow::OnRenameTimer()
1941 // We have to call this here because changes may just have
1942 // been made and no screen update taken place.
1947 unsigned int cols
= GetOwner()->GetColumnCount();
1949 for (i
= 0; i
< cols
; i
++)
1951 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1953 continue; // skip it!
1955 if (c
== m_currentCol
)
1957 xpos
+= c
->GetWidth();
1959 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1960 m_currentCol
->GetWidth(), m_lineHeight
);
1962 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1963 &labelRect
.x
, &labelRect
.y
);
1965 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1966 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1970 //------------------------------------------------------------------
1971 // Helper class for do operation on the tree node
1972 //------------------------------------------------------------------
1979 //The return value control how the tree-walker tranverse the tree
1980 // 0: Job done, stop tranverse and return
1981 // 1: Ignore the current node's subtree and continue
1982 // 2: Job not done, continue
1983 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1984 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1985 virtual int operator() ( void * n
) = 0;
1988 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1993 switch( func( node
) )
2004 wxDataViewTreeNodes nodes
= node
->GetNodes();
2005 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2007 int len_nodes
= nodes
.GetCount();
2008 int len
= leaves
.GetCount();
2009 int i
= 0, nodes_i
= 0;
2011 for( ; i
< len
; i
++ )
2013 void * n
= leaves
[i
];
2014 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2016 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2019 if( Walker( nd
, func
) )
2038 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2042 wxDataViewTreeNode
* node
;
2043 node
= FindNode(parent
);
2048 node
->SetHasChildren( true );
2050 if( g_model
->IsContainer( item
) )
2052 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2053 newnode
->SetItem(item
);
2054 newnode
->SetHasChildren( true );
2055 node
->AddNode( newnode
);
2058 node
->AddLeaf( item
.GetID() );
2060 node
->ChangeSubTreeCount(1);
2068 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2070 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2071 const wxDataViewItem
& item
)
2073 wxDataViewTreeNode
* node
= FindNode(parent
);
2075 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2076 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2079 node
->GetChildren().Remove( item
.GetID() );
2080 //Manuplate selection
2081 if( m_selection
.GetCount() > 1 )
2083 m_selection
.Empty();
2085 bool isContainer
= false;
2086 wxDataViewTreeNodes nds
= node
->GetNodes();
2087 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2089 if (nds
[i
]->GetItem() == item
)
2097 wxDataViewTreeNode
* n
= NULL
;
2098 wxDataViewTreeNodes nodes
= node
->GetNodes();
2099 int len
= nodes
.GetCount();
2100 for( int i
= 0 ; i
< len
; i
++)
2102 if( nodes
[i
]->GetItem() == item
)
2109 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2111 node
->GetNodes().Remove( n
);
2112 sub
-= n
->GetSubTreeCount();
2113 DestroyTreeHelper(n
);
2115 //Make the row number invalid and get a new valid one when user call GetRowCount
2117 node
->ChangeSubTreeCount(sub
);
2118 if( node
->GetChildrenNumber() == 0)
2120 node
->GetParent()->GetNodes().Remove( node
);
2124 //Change the current row to the last row if the current exceed the max row number
2125 if( m_currentRow
> GetRowCount() )
2126 m_currentRow
= m_count
- 1;
2133 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2139 wxWindow
*parent
= GetParent();
2140 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2141 le
.SetEventObject(parent
);
2142 le
.SetModel(GetOwner()->GetModel());
2144 parent
->GetEventHandler()->ProcessEvent(le
);
2149 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2151 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2152 /*#define MAX_VIRTUAL_WIDTH 100000
2154 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2155 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2156 Refresh( true, &rect );
2164 wxWindow
*parent
= GetParent();
2165 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2166 le
.SetEventObject(parent
);
2167 le
.SetModel(GetOwner()->GetModel());
2170 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2171 parent
->GetEventHandler()->ProcessEvent(le
);
2176 bool wxDataViewMainWindow::Cleared()
2186 void wxDataViewMainWindow::UpdateDisplay()
2191 void wxDataViewMainWindow::OnInternalIdle()
2193 wxWindow::OnInternalIdle();
2197 RecalculateDisplay();
2202 void wxDataViewMainWindow::RecalculateDisplay()
2204 wxDataViewModel
*model
= GetOwner()->GetModel();
2211 int width
= GetEndOfLastCol();
2212 int height
= GetRowCount() * m_lineHeight
;
2214 SetVirtualSize( width
, height
);
2215 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2220 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2222 wxWindow::ScrollWindow( dx
, dy
, rect
);
2224 if (GetOwner()->m_headerArea
)
2225 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2228 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2231 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2232 int sy
= rows
*m_lineHeight
/y
;
2236 wxRect rect
= GetClientRect();
2238 int x_start
= 0, x_end
= 0, w
= 0;
2240 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2241 for (x_start
= 0; colnum
< column
; colnum
++)
2243 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2244 if (col
->IsHidden())
2245 continue; // skip it!
2247 w
= col
->GetWidth();
2251 x_end
= x_start
+ w
;
2252 xe
= xx
+ rect
.width
;
2255 sx
= ( xx
+ x_end
- xe
)/x
;
2262 m_owner
->Scroll( sx
, sy
);
2265 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2267 wxDataViewModel
*model
= GetOwner()->GetModel();
2268 wxAutoBufferedPaintDC
dc( this );
2271 dc
.SetBackground(GetBackgroundColour());
2273 GetOwner()->PrepareDC( dc
);
2274 dc
.SetFont( GetFont() );
2276 wxRect update
= GetUpdateRegion().GetBox();
2277 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2279 // compute which items needs to be redrawn
2280 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2281 unsigned int item_count
=
2282 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2283 (int)(GetRowCount( )- item_start
) );
2284 unsigned int item_last
= item_start
+ item_count
;
2286 // compute which columns needs to be redrawn
2287 unsigned int cols
= GetOwner()->GetColumnCount();
2288 unsigned int col_start
= 0;
2289 unsigned int x_start
= 0;
2290 for (x_start
= 0; col_start
< cols
; col_start
++)
2292 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2293 if (col
->IsHidden())
2294 continue; // skip it!
2296 unsigned int w
= col
->GetWidth();
2297 if (x_start
+w
>= (unsigned int)update
.x
)
2303 unsigned int col_last
= col_start
;
2304 unsigned int x_last
= x_start
;
2305 for (; col_last
< cols
; col_last
++)
2307 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2308 if (col
->IsHidden())
2309 continue; // skip it!
2311 if (x_last
> (unsigned int)update
.GetRight())
2314 x_last
+= col
->GetWidth();
2317 // Draw horizontal rules if required
2318 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2320 dc
.SetPen(m_penRule
);
2321 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2323 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2325 int y
= i
* m_lineHeight
;
2326 dc
.DrawLine(x_start
, y
, x_last
, y
);
2330 // Draw vertical rules if required
2331 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2333 dc
.SetPen(m_penRule
);
2334 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2337 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2339 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2340 if (col
->IsHidden())
2341 continue; // skip it
2343 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2344 x
, item_last
* m_lineHeight
);
2346 x
+= col
->GetWidth();
2349 // Draw last vertical rule
2350 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2351 x
, item_last
* m_lineHeight
);
2354 // redraw the background for the items which are selected/current
2355 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2357 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2358 if (selected
|| item
== m_currentRow
)
2360 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2361 if (item
== m_currentRow
)
2362 flags
|= wxCONTROL_CURRENT
;
2364 flags
|= wxCONTROL_FOCUSED
;
2366 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2367 wxRendererNative::Get().DrawItemSelectionRect
2377 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2380 // TODO: last column for RTL support
2381 expander
= GetOwner()->GetColumn( 0 );
2382 GetOwner()->SetExpanderColumn(expander
);
2385 // redraw all cells for all rows which must be repainted and for all columns
2387 cell_rect
.x
= x_start
;
2388 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2389 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2391 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2392 wxDataViewRenderer
*cell
= col
->GetRenderer();
2393 cell_rect
.width
= col
->GetWidth();
2395 if (col
->IsHidden())
2396 continue; // skipt it!
2399 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2401 // get the cell value and set it into the renderer
2403 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2409 wxDataViewItem dataitem
= node
->GetItem();
2410 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2411 cell
->SetValue( value
);
2413 // update the y offset
2414 cell_rect
.y
= item
* m_lineHeight
;
2416 //Draw the expander here.
2417 int indent
= node
->GetIndentLevel();
2418 if( col
== expander
)
2420 //Calculate the indent first
2421 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2423 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2424 // change the cell_rect.x to the appropriate pos
2425 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2426 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2427 dc
.SetPen( m_penExpander
);
2428 dc
.SetBrush( wxNullBrush
);
2429 if( node
->HasChildren() )
2431 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2433 if (m_underMouse
== node
)
2435 flag
|= wxCONTROL_CURRENT
;
2437 if( node
->IsOpen() )
2438 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2440 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2444 // I am wandering whether we should draw dot lines between tree nodes
2446 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2449 //force the expander column to left-center align
2450 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2454 // cannot be bigger than allocated space
2455 wxSize size
= cell
->GetSize();
2456 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2457 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2458 size
.y
= wxMin( size
.y
, cell_rect
.height
);
2460 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2461 int align
= cell
->GetAlignment();
2463 // horizontal alignment:
2464 item_rect
.x
= cell_rect
.x
;
2465 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2466 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2467 else if (align
& wxALIGN_RIGHT
)
2468 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2469 //else: wxALIGN_LEFT is the default
2471 // vertical alignment:
2472 item_rect
.y
= cell_rect
.y
;
2473 if (align
& wxALIGN_CENTER_VERTICAL
)
2474 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2475 else if (align
& wxALIGN_BOTTOM
)
2476 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2477 //else: wxALIGN_TOP is the default
2480 item_rect
.x
+= PADDING_RIGHTLEFT
;
2481 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2483 //Here we add the tree indent
2484 item_rect
.x
+= indent
;
2487 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2488 state
|= wxDATAVIEW_CELL_SELECTED
;
2490 // TODO: it would be much more efficient to create a clipping
2491 // region for the entire column being rendered (in the OnPaint
2492 // of wxDataViewMainWindow) instead of a single clip region for
2493 // each cell. However it would mean that each renderer should
2494 // respect the given wxRect's top & bottom coords, eventually
2495 // violating only the left & right coords - however the user can
2496 // make its own renderer and thus we cannot be sure of that.
2497 dc
.SetClippingRegion( item_rect
);
2498 cell
->Render( item_rect
, &dc
, state
);
2499 dc
.DestroyClippingRegion();
2502 cell_rect
.x
+= cell_rect
.width
;
2506 int wxDataViewMainWindow::GetCountPerPage() const
2508 wxSize size
= GetClientSize();
2509 return size
.y
/ m_lineHeight
;
2512 int wxDataViewMainWindow::GetEndOfLastCol() const
2516 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2518 const wxDataViewColumn
*c
=
2519 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2522 width
+= c
->GetWidth();
2527 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2531 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2533 return y
/ m_lineHeight
;
2536 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2538 wxSize client_size
= GetClientSize();
2539 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2540 &client_size
.x
, &client_size
.y
);
2542 //we should deal with the pixel here
2543 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2545 return wxMin( GetRowCount()-1, row
);
2548 unsigned int wxDataViewMainWindow::GetRowCount()
2550 if ( m_count
== -1 )
2552 m_count
= RecalculateCount();
2554 GetVirtualSize( &width
, &height
);
2555 height
= m_count
* m_lineHeight
;
2557 SetVirtualSize( width
, height
);
2562 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2569 void wxDataViewMainWindow::SelectAllRows( bool on
)
2576 m_selection
.Clear();
2577 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2578 m_selection
.Add( i
);
2583 unsigned int first_visible
= GetFirstVisibleRow();
2584 unsigned int last_visible
= GetLastVisibleRow();
2586 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2588 unsigned int row
= m_selection
[i
];
2589 if ((row
>= first_visible
) && (row
<= last_visible
))
2592 m_selection
.Clear();
2596 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2598 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2602 m_selection
.Add( row
);
2610 m_selection
.Remove( row
);
2616 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2620 unsigned int tmp
= from
;
2626 for (i
= from
; i
<= to
; i
++)
2628 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2631 m_selection
.Add( i
);
2636 m_selection
.Remove( i
);
2639 RefreshRows( from
, to
);
2642 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2644 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2646 int n
= aSelections
[i
];
2648 m_selection
.Add( n
);
2653 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2655 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2656 m_selection
.Add( row
);
2658 m_selection
.Remove( row
);
2662 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2664 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2667 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2669 wxWindow
*parent
= GetParent();
2670 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2672 le
.SetEventObject(parent
);
2673 le
.SetModel(GetOwner()->GetModel());
2676 parent
->GetEventHandler()->ProcessEvent(le
);
2679 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2681 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2682 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2684 wxSize client_size
= GetClientSize();
2685 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2686 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2687 if (intersect_rect
.width
> 0)
2688 Refresh( true, &intersect_rect
);
2691 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2695 unsigned int tmp
= to
;
2700 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2701 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2703 wxSize client_size
= GetClientSize();
2704 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2705 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2706 if (intersect_rect
.width
> 0)
2707 Refresh( true, &intersect_rect
);
2710 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2712 unsigned int count
= GetRowCount();
2713 if (firstRow
> count
)
2716 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2717 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2719 wxSize client_size
= GetClientSize();
2720 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2721 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2722 if (intersect_rect
.width
> 0)
2723 Refresh( true, &intersect_rect
);
2726 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2728 wxCHECK_RET( newCurrent
< GetRowCount(),
2729 _T("invalid item index in OnArrowChar()") );
2731 // if there is no selection, we cannot move it anywhere
2732 if (!HasCurrentRow())
2735 unsigned int oldCurrent
= m_currentRow
;
2737 // in single selection we just ignore Shift as we can't select several
2739 if ( event
.ShiftDown() && !IsSingleSel() )
2741 RefreshRow( oldCurrent
);
2743 ChangeCurrentRow( newCurrent
);
2745 // select all the items between the old and the new one
2746 if ( oldCurrent
> newCurrent
)
2748 newCurrent
= oldCurrent
;
2749 oldCurrent
= m_currentRow
;
2752 SelectRows( oldCurrent
, newCurrent
, true );
2753 if (oldCurrent
!=newCurrent
)
2754 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2758 RefreshRow( oldCurrent
);
2760 // all previously selected items are unselected unless ctrl is held
2761 if ( !event
.ControlDown() )
2762 SelectAllRows(false);
2764 ChangeCurrentRow( newCurrent
);
2766 if ( !event
.ControlDown() )
2768 SelectRow( m_currentRow
, true );
2769 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2772 RefreshRow( m_currentRow
);
2775 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2778 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2782 rect
.y
= m_lineHeight
* row
;
2783 rect
.width
= GetEndOfLastCol();
2784 rect
.height
= m_lineHeight
;
2789 class RowToItemJob
: public DoJob
2792 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2793 virtual ~RowToItemJob(){};
2795 virtual int operator() ( wxDataViewTreeNode
* node
)
2798 if( current
== static_cast<int>(row
))
2800 ret
= node
->GetItem() ;
2804 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2806 current
+= node
->GetSubTreeCount();
2811 //If the current has no child node, we can find the desired item of the row number directly.
2812 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2813 if( node
->GetNodes().GetCount() == 0)
2815 int index
= static_cast<int>(row
) - current
- 1;
2816 ret
= node
->GetChildren().Item( index
);
2823 virtual int operator() ( void * n
)
2826 if( current
== static_cast<int>(row
))
2828 ret
= wxDataViewItem( n
) ;
2833 wxDataViewItem
GetResult(){ return ret
; }
2840 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2842 RowToItemJob
job( row
, -2 );
2843 Walker( m_root
, job
);
2844 return job
.GetResult();
2847 class RowToTreeNodeJob
: public DoJob
2850 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2853 this->current
= current
;
2857 virtual ~RowToTreeNodeJob(){};
2859 virtual int operator() ( wxDataViewTreeNode
* node
)
2862 if( current
== static_cast<int>(row
))
2868 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2870 current
+= node
->GetSubTreeCount();
2876 //If the current has no child node, we can find the desired item of the row number directly.
2877 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2878 if( node
->GetNodes().GetCount() == 0)
2880 int index
= static_cast<int>(row
) - current
- 1;
2881 void * n
= node
->GetChildren().Item( index
);
2882 ret
= new wxDataViewTreeNode( parent
) ;
2883 ret
->SetItem( wxDataViewItem( n
));
2884 ret
->SetHasChildren(false);
2893 virtual int operator() ( void * n
)
2896 if( current
== static_cast<int>(row
))
2898 ret
= new wxDataViewTreeNode( parent
) ;
2899 ret
->SetItem( wxDataViewItem( n
));
2900 ret
->SetHasChildren(false);
2906 wxDataViewTreeNode
* GetResult(){ return ret
; }
2910 wxDataViewTreeNode
* ret
;
2911 wxDataViewTreeNode
* parent
;
2915 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2917 RowToTreeNodeJob
job( row
, -2, m_root
);
2918 Walker( m_root
, job
);
2919 return job
.GetResult();
2922 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2924 wxWindow
*parent
= GetParent();
2925 wxDataViewEvent
le(type
, parent
->GetId());
2927 le
.SetEventObject(parent
);
2928 le
.SetModel(GetOwner()->GetModel());
2931 parent
->GetEventHandler()->ProcessEvent(le
);
2935 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2937 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2940 if( node
->HasChildren())
2942 if( !node
->IsOpen())
2944 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2945 //Check if the user prevent expanding
2946 if( e
.GetSkipped() )
2950 //Here I build the children of current node
2951 if( node
->GetChildrenNumber() == 0 )
2954 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2958 //Send the expanded event
2959 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2963 SelectRow( row
, false );
2964 SelectRow( row
+ 1, true );
2965 ChangeCurrentRow( row
+ 1 );
2966 SendSelectionChangedEvent( GetItemByRow(row
+1));
2974 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2976 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2979 wxDataViewTreeNode
* nd
= node
;
2981 if( node
->HasChildren() && node
->IsOpen() )
2983 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2984 if( e
.GetSkipped() )
2989 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2993 node
= node
->GetParent();
2996 int parent
= GetRowByItem( node
->GetItem() ) ;
2999 SelectRow( row
, false);
3000 SelectRow(parent
, true );
3001 ChangeCurrentRow( parent
);
3002 SendSelectionChangedEvent( node
->GetItem() );
3006 if( !nd
->HasChildren())
3011 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3013 wxDataViewModel
* model
= GetOwner()->GetModel();
3017 //Compose the a parent-chain of the finding item
3019 list
.DeleteContents( true );
3020 wxDataViewItem
it( item
);
3023 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3024 list
.Insert( pItem
);
3025 it
= model
->GetParent( it
);
3028 //Find the item along the parent-chain.
3029 //This algorithm is designed to speed up the node-finding method
3030 wxDataViewTreeNode
* node
= m_root
;
3031 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
3033 if( node
->HasChildren() )
3035 if( node
->GetChildrenNumber() == 0 )
3038 BuildTreeHelper(model
, node
->GetItem(), node
);
3041 wxDataViewTreeNodes nodes
= node
->GetNodes();
3043 for (; i
< nodes
.GetCount(); i
++)
3045 if (nodes
[i
]->GetItem() == (**iter
))
3051 if (i
== nodes
.GetCount())
3060 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3062 wxDataViewColumn
*col
= NULL
;
3063 unsigned int cols
= GetOwner()->GetColumnCount();
3064 unsigned int colnum
= 0;
3065 unsigned int x_start
= 0;
3067 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3068 for (x_start
= 0; colnum
< cols
; colnum
++)
3070 col
= GetOwner()->GetColumn(colnum
);
3071 if (col
->IsHidden())
3072 continue; // skip it!
3074 unsigned int w
= col
->GetWidth();
3075 if (x_start
+w
>= (unsigned int)x
)
3082 item
= GetItemByRow( y
/m_lineHeight
);
3085 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3087 int row
= GetRowByItem(item
);
3088 int y
= row
*m_lineHeight
;
3089 int h
= m_lineHeight
;
3091 wxDataViewColumn
*col
= NULL
;
3092 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3094 col
= GetOwner()->GetColumn( i
);
3095 x
+= col
->GetWidth();
3096 if( GetOwner()->GetColumn(i
+1) == column
)
3099 int w
= col
->GetWidth();
3100 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3101 return wxRect(x
, y
, w
, h
);
3104 int wxDataViewMainWindow::RecalculateCount()
3106 return m_root
->GetSubTreeCount();
3109 class ItemToRowJob
: public DoJob
3112 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3113 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3114 virtual ~ItemToRowJob(){};
3116 //Maybe binary search will help to speed up this process
3117 virtual int operator() ( wxDataViewTreeNode
* node
)
3120 if( node
->GetItem() == item
)
3125 if( node
->GetItem() == **m_iter
)
3132 ret
+= node
->GetSubTreeCount();
3138 virtual int operator() ( void * n
)
3141 if( n
== item
.GetID() )
3145 //the row number is begin from zero
3146 int GetResult(){ return ret
-1 ; }
3148 ItemList::const_iterator m_iter
;
3149 wxDataViewItem item
;
3154 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3156 wxDataViewModel
* model
= GetOwner()->GetModel();
3163 //Compose the a parent-chain of the finding item
3165 wxDataViewItem
* pItem
= NULL
;
3166 list
.DeleteContents( true );
3167 wxDataViewItem
it( item
);
3170 pItem
= new wxDataViewItem( it
);
3171 list
.Insert( pItem
);
3172 it
= model
->GetParent( it
);
3174 pItem
= new wxDataViewItem( );
3175 list
.Insert( pItem
);
3177 ItemToRowJob
job( item
, list
.begin() );
3178 Walker(m_root
, job
);
3179 return job
.GetResult();
3182 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3184 if( !model
->IsContainer( item
) )
3187 wxDataViewItemArray children
;
3188 unsigned int num
= model
->GetChildren( item
, children
);
3189 unsigned int index
= 0;
3190 while( index
< num
)
3192 if( model
->IsContainer( children
[index
] ) )
3194 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3195 n
->SetItem(children
[index
]);
3196 n
->SetHasChildren( true ) ;
3201 node
->AddLeaf( children
[index
].GetID() );
3205 node
->SetSubTreeCount( num
);
3206 wxDataViewTreeNode
* n
= node
->GetParent();
3208 n
->ChangeSubTreeCount(num
);
3212 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3214 //First we define a invalid item to fetch the top-level elements
3215 wxDataViewItem item
;
3217 BuildTreeHelper( model
, item
, m_root
);
3221 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3223 if( node
->GetNodeNumber() != 0 )
3225 int len
= node
->GetNodeNumber();
3227 wxDataViewTreeNodes nodes
= node
->GetNodes();
3228 for( ; i
< len
; i
++ )
3230 DestroyTreeHelper(nodes
[i
]);
3236 void wxDataViewMainWindow::DestroyTree()
3238 DestroyTreeHelper(m_root
);
3239 m_root
->SetSubTreeCount(0);
3243 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3245 if (event
.GetKeyCode() == WXK_TAB
)
3247 wxNavigationKeyEvent nevent
;
3248 nevent
.SetWindowChange( event
.ControlDown() );
3249 nevent
.SetDirection( !event
.ShiftDown() );
3250 nevent
.SetEventObject( GetParent()->GetParent() );
3251 nevent
.SetCurrentFocus( m_parent
);
3252 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3256 // no item -> nothing to do
3257 if (!HasCurrentRow())
3263 // don't use m_linesPerPage directly as it might not be computed yet
3264 const int pageSize
= GetCountPerPage();
3265 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3267 switch ( event
.GetKeyCode() )
3271 if (m_currentRow
> 0)
3273 wxWindow
*parent
= GetParent();
3274 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3275 le
.SetItem( GetItemByRow(m_currentRow
) );
3276 le
.SetEventObject(parent
);
3277 le
.SetModel(GetOwner()->GetModel());
3279 parent
->GetEventHandler()->ProcessEvent(le
);
3284 if ( m_currentRow
> 0 )
3285 OnArrowChar( m_currentRow
- 1, event
);
3289 if ( m_currentRow
< GetRowCount() - 1 )
3290 OnArrowChar( m_currentRow
+ 1, event
);
3292 //Add the process for tree expanding/collapsing
3294 OnCollapsing(m_currentRow
);
3297 OnExpanding( m_currentRow
);
3301 OnArrowChar( GetRowCount() - 1, event
);
3306 OnArrowChar( 0, event
);
3311 int steps
= pageSize
- 1;
3312 int index
= m_currentRow
- steps
;
3316 OnArrowChar( index
, event
);
3322 int steps
= pageSize
- 1;
3323 unsigned int index
= m_currentRow
+ steps
;
3324 unsigned int count
= GetRowCount();
3325 if ( index
>= count
)
3328 OnArrowChar( index
, event
);
3337 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3339 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3341 // let the base handle mouse wheel events.
3346 int x
= event
.GetX();
3347 int y
= event
.GetY();
3348 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3349 wxDataViewColumn
*col
= NULL
;
3352 unsigned int cols
= GetOwner()->GetColumnCount();
3354 for (i
= 0; i
< cols
; i
++)
3356 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3358 continue; // skip it!
3360 if (x
< xpos
+ c
->GetWidth())
3365 xpos
+= c
->GetWidth();
3370 wxDataViewRenderer
*cell
= col
->GetRenderer();
3371 unsigned int current
= y
/ m_lineHeight
;
3372 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3374 // Unselect all if below the last row ?
3378 //Test whether the mouse is hovered on the tree item button
3380 if (GetOwner()->GetExpanderColumn() == col
)
3382 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3383 if( node
!=NULL
&& node
->HasChildren() )
3385 int indent
= node
->GetIndentLevel();
3386 indent
= GetOwner()->GetIndent()*indent
;
3387 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3388 if( rect
.Contains( x
, y
) )
3390 //So the mouse is over the expander
3392 if (m_underMouse
&& m_underMouse
!= node
)
3394 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3395 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3397 if (m_underMouse
!= node
)
3399 //wxLogMessage("Do the row: %d", current);
3402 m_underMouse
= node
;
3405 if (node
!=NULL
&& !node
->HasChildren())
3410 if (m_underMouse
!= NULL
)
3412 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3413 Refresh(GetRowByItem(m_underMouse
->GetItem()));
3414 m_underMouse
= NULL
;
3418 wxDataViewModel
*model
= GetOwner()->GetModel();
3420 if (event
.Dragging())
3422 if (m_dragCount
== 0)
3424 // we have to report the raw, physical coords as we want to be
3425 // able to call HitTest(event.m_pointDrag) from the user code to
3426 // get the item being dragged
3427 m_dragStart
= event
.GetPosition();
3432 if (m_dragCount
!= 3)
3435 if (event
.LeftIsDown())
3437 // Notify cell about drag
3446 bool forceClick
= false;
3448 if (event
.ButtonDClick())
3450 m_renameTimer
->Stop();
3451 m_lastOnSame
= false;
3454 if (event
.LeftDClick())
3456 if ( current
== m_lineLastClicked
)
3458 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3460 wxDataViewItem item
= GetItemByRow(current
);
3462 model
->GetValue( value
, item
, col
->GetModelColumn() );
3463 cell
->SetValue( value
);
3464 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3465 col
->GetWidth(), m_lineHeight
);
3466 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3471 wxWindow
*parent
= GetParent();
3472 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3473 le
.SetItem( GetItemByRow(current
) );
3474 le
.SetEventObject(parent
);
3475 le
.SetModel(GetOwner()->GetModel());
3477 parent
->GetEventHandler()->ProcessEvent(le
);
3483 // The first click was on another item, so don't interpret this as
3484 // a double click, but as a simple click instead
3491 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3493 // select single line
3494 SelectAllRows( false );
3495 SelectRow( m_lineSelectSingleOnUp
, true );
3498 //Process the event of user clicking the expander
3499 bool expander
= false;
3500 if (GetOwner()->GetExpanderColumn() == col
)
3502 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3503 if( node
!=NULL
&& node
->HasChildren() )
3505 int indent
= node
->GetIndentLevel();
3506 indent
= GetOwner()->GetIndent()*indent
;
3507 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3508 if( rect
.Contains( x
, y
) )
3511 if( node
->IsOpen() )
3512 OnCollapsing(current
);
3514 OnExpanding( current
);
3518 //If the user click the expander, we do not do editing even if the column with expander are editable
3519 if (m_lastOnSame
&& !expander
)
3521 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3522 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3524 m_renameTimer
->Start( 100, true );
3528 m_lastOnSame
= false;
3529 m_lineSelectSingleOnUp
= (unsigned int)-1;
3533 // This is necessary, because after a DnD operation in
3534 // from and to ourself, the up event is swallowed by the
3535 // DnD code. So on next non-up event (which means here and
3536 // now) m_lineSelectSingleOnUp should be reset.
3537 m_lineSelectSingleOnUp
= (unsigned int)-1;
3540 if (event
.RightDown())
3542 m_lineBeforeLastClicked
= m_lineLastClicked
;
3543 m_lineLastClicked
= current
;
3545 // If the item is already selected, do not update the selection.
3546 // Multi-selections should not be cleared if a selected item is clicked.
3547 if (!IsRowSelected(current
))
3549 SelectAllRows(false);
3550 ChangeCurrentRow(current
);
3551 SelectRow(m_currentRow
,true);
3552 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3555 // notify cell about right click
3558 // Allow generation of context menu event
3561 else if (event
.MiddleDown())
3563 // notify cell about middle click
3566 if (event
.LeftDown() || forceClick
)
3570 m_lineBeforeLastClicked
= m_lineLastClicked
;
3571 m_lineLastClicked
= current
;
3573 unsigned int oldCurrentRow
= m_currentRow
;
3574 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3576 bool cmdModifierDown
= event
.CmdDown();
3577 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3579 if ( IsSingleSel() || !IsRowSelected(current
) )
3581 SelectAllRows( false );
3582 ChangeCurrentRow(current
);
3583 SelectRow(m_currentRow
,true);
3584 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3586 else // multi sel & current is highlighted & no mod keys
3588 m_lineSelectSingleOnUp
= current
;
3589 ChangeCurrentRow(current
); // change focus
3592 else // multi sel & either ctrl or shift is down
3594 if (cmdModifierDown
)
3596 ChangeCurrentRow(current
);
3597 ReverseRowSelection(m_currentRow
);
3598 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3600 else if (event
.ShiftDown())
3602 ChangeCurrentRow(current
);
3604 unsigned int lineFrom
= oldCurrentRow
,
3607 if ( lineTo
< lineFrom
)
3610 lineFrom
= m_currentRow
;
3613 SelectRows(lineFrom
, lineTo
, true);
3614 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3616 else // !ctrl, !shift
3618 // test in the enclosing if should make it impossible
3619 wxFAIL_MSG( _T("how did we get here?") );
3623 if (m_currentRow
!= oldCurrentRow
)
3624 RefreshRow( oldCurrentRow
);
3626 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3628 // Update selection here...
3631 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3632 (current
== oldCurrentRow
)) && oldWasSelected
;
3636 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3640 if (HasCurrentRow())
3646 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3650 if (HasCurrentRow())
3656 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3658 if( m_selection
.GetCount() != 1 )
3659 return wxDataViewItem();
3661 return GetItemByRow( m_selection
.Item(0));
3664 //-----------------------------------------------------------------------------
3666 //-----------------------------------------------------------------------------
3667 WX_DEFINE_LIST(wxDataViewColumnList
);
3669 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3671 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3672 EVT_SIZE(wxDataViewCtrl::OnSize
)
3675 wxDataViewCtrl::~wxDataViewCtrl()
3678 GetModel()->RemoveNotifier( m_notifier
);
3681 void wxDataViewCtrl::Init()
3686 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3687 const wxPoint
& pos
, const wxSize
& size
,
3688 long style
, const wxValidator
& validator
)
3690 if (!wxControl::Create( parent
, id
, pos
, size
,
3691 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3697 MacSetClipChildren( true ) ;
3700 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3702 if (HasFlag(wxDV_NO_HEADER
))
3703 m_headerArea
= NULL
;
3705 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3707 SetTargetWindow( m_clientArea
);
3709 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3711 sizer
->Add( m_headerArea
, 0, wxGROW
);
3712 sizer
->Add( m_clientArea
, 1, wxGROW
);
3719 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3723 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3726 // we need to process arrows ourselves for scrolling
3727 if ( nMsg
== WM_GETDLGCODE
)
3729 rc
|= DLGC_WANTARROWS
;
3737 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3739 // We need to override OnSize so that our scrolled
3740 // window a) does call Layout() to use sizers for
3741 // positioning the controls but b) does not query
3742 // the sizer for their size and use that for setting
3743 // the scrollable area as set that ourselves by
3744 // calling SetScrollbar() further down.
3751 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3753 if (!wxDataViewCtrlBase::AssociateModel( model
))
3756 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3758 model
->AddNotifier( m_notifier
);
3760 m_clientArea
->BuildTree(model
);
3762 m_clientArea
->UpdateDisplay();
3767 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3769 if (!wxDataViewCtrlBase::AppendColumn(col
))
3772 m_cols
.Append( col
);
3777 void wxDataViewCtrl::OnColumnChange()
3780 m_headerArea
->UpdateDisplay();
3782 m_clientArea
->UpdateDisplay();
3785 void wxDataViewCtrl::DoSetExpanderColumn()
3787 m_clientArea
->UpdateDisplay();
3790 void wxDataViewCtrl::DoSetIndent()
3792 m_clientArea
->UpdateDisplay();
3795 unsigned int wxDataViewCtrl::GetColumnCount() const
3797 return m_cols
.GetCount();
3800 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3802 wxDataViewColumnList::const_iterator iter
;
3804 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3809 if ((*iter
)->IsHidden())
3816 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3818 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3829 bool wxDataViewCtrl::ClearColumns()
3836 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3838 int ret
= 0, dead
= 0;
3839 int len
= GetColumnCount();
3840 for (int i
=0; i
<len
; i
++)
3842 wxDataViewColumn
* col
= GetColumn(i
);
3843 if (col
->IsHidden())
3845 ret
+= col
->GetWidth();
3848 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3855 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3860 //Selection code with wxDataViewItem as parameters
3861 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3863 return m_clientArea
->GetSelection();
3866 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3869 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3870 int len
= selection
.GetCount();
3871 for( int i
= 0; i
< len
; i
++)
3873 unsigned int row
= selection
[i
];
3874 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3879 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3881 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3882 int len
= sel
.GetCount();
3883 for( int i
= 0; i
< len
; i
++ )
3885 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3887 selection
.Add( static_cast<unsigned int>(row
) );
3889 m_clientArea
->SetSelections( selection
);
3892 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3894 int row
= m_clientArea
->GetRowByItem( item
);
3897 //Unselect all rows before select another in the single select mode
3898 if (m_clientArea
->IsSingleSel())
3899 m_clientArea
->SelectAllRows(false);
3900 m_clientArea
->SelectRow(row
, true);
3904 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3906 int row
= m_clientArea
->GetRowByItem( item
);
3908 m_clientArea
->SelectRow(row
, false);
3911 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3913 int row
= m_clientArea
->GetRowByItem( item
);
3916 return m_clientArea
->IsRowSelected(row
);
3921 //Selection code with row number as parameter
3922 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3925 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3926 int len
= selection
.GetCount();
3927 for( int i
= 0; i
< len
; i
++)
3929 unsigned int row
= selection
[i
];
3935 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3937 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3938 int len
= sel
.GetCount();
3939 for( int i
= 0; i
< len
; i
++ )
3943 selection
.Add( static_cast<unsigned int>(row
) );
3945 m_clientArea
->SetSelections( selection
);
3948 void wxDataViewCtrl::Select( int row
)
3952 if (m_clientArea
->IsSingleSel())
3953 m_clientArea
->SelectAllRows(false);
3954 m_clientArea
->SelectRow( row
, true );
3958 void wxDataViewCtrl::Unselect( int row
)
3961 m_clientArea
->SelectRow(row
, false);
3964 bool wxDataViewCtrl::IsSelected( int row
) const
3967 return m_clientArea
->IsRowSelected(row
);
3971 void wxDataViewCtrl::SelectRange( int from
, int to
)
3974 for( int i
= from
; i
< to
; i
++ )
3976 m_clientArea
->Select(sel
);
3979 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3981 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3982 for( int i
= from
; i
< to
; i
++ )
3983 if( sel
.Index( i
) != wxNOT_FOUND
)
3985 m_clientArea
->SetSelections(sel
);
3988 void wxDataViewCtrl::SelectAll()
3990 m_clientArea
->SelectAllRows(true);
3993 void wxDataViewCtrl::UnselectAll()
3995 m_clientArea
->SelectAllRows(false);
3998 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4002 if( row
> (int) m_clientArea
->GetRowCount() )
4003 row
= m_clientArea
->GetRowCount();
4005 int first
= m_clientArea
->GetFirstVisibleRow();
4006 int last
= m_clientArea
->GetLastVisibleRow();
4008 m_clientArea
->ScrollTo( row
, column
);
4009 else if( row
> last
)
4010 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4012 m_clientArea
->ScrollTo( first
, column
);
4015 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4017 int row
= m_clientArea
->GetRowByItem(item
);
4020 if( column
== NULL
)
4021 EnsureVisible(row
, -1);
4025 int len
= GetColumnCount();
4026 for( int i
= 0; i
< len
; i
++ )
4027 if( GetColumn(i
) == column
)
4032 EnsureVisible( row
, col
);
4038 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4040 m_clientArea
->HitTest(point
, item
, column
);
4043 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4045 return m_clientArea
->GetItemRect(item
, column
);
4048 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4050 return m_clientArea
->GetItemByRow( row
);
4053 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4055 return m_clientArea
->GetRowByItem( item
);
4058 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4060 int row
= m_clientArea
->GetRowByItem( item
);
4062 m_clientArea
->Expand(row
);
4065 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4067 int row
= m_clientArea
->GetRowByItem( item
);
4069 m_clientArea
->Collapse(row
);
4073 // !wxUSE_GENERICDATAVIEWCTRL
4076 // wxUSE_DATAVIEWCTRL