1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the cell padding on the top/bottom
60 static const int PADDING_TOPBOTTOM
= 1;
62 // the expander space margin
63 static const int EXPANDER_MARGIN
= 4;
65 //-----------------------------------------------------------------------------
66 // wxDataViewHeaderWindow
67 //-----------------------------------------------------------------------------
69 #define USE_NATIVE_HEADER_WINDOW 0
71 // NB: for some reason, this class must be dllexport'ed or we get warnings from
73 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
76 wxDataViewHeaderWindowBase()
79 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
80 const wxPoint
&pos
, const wxSize
&size
,
83 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
86 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
87 wxDataViewCtrl
*GetOwner() { return m_owner
; }
89 // called on column addition/removal
90 virtual void UpdateDisplay() { /* by default, do nothing */ }
92 // returns the n-th column
93 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
96 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
103 wxDataViewCtrl
*m_owner
;
105 // sends an event generated from the n-th wxDataViewColumn
106 void SendEvent(wxEventType type
, unsigned int n
);
109 // on wxMSW the header window (only that part however) can be made native!
110 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
112 #define COLUMN_WIDTH_OFFSET 2
113 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
115 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
119 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
121 const wxPoint
&pos
= wxDefaultPosition
,
122 const wxSize
&size
= wxDefaultSize
,
123 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
125 Create(parent
, id
, pos
, size
, name
);
128 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
,
130 const wxString
&name
);
132 ~wxDataViewHeaderWindowMSW();
134 // called when any column setting is changed and/or changed
136 virtual void UpdateDisplay();
138 // called when the main window gets scrolled
139 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
142 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
143 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
145 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
147 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
148 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
151 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
154 #else // !defined(__WXMSW__)
156 #define HEADER_WINDOW_HEIGHT 25
157 #define HEADER_HORIZ_BORDER 5
158 #define HEADER_VERT_BORDER 3
159 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
161 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
164 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
166 const wxPoint
&pos
= wxDefaultPosition
,
167 const wxSize
&size
= wxDefaultSize
,
168 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
171 Create(parent
, id
, pos
, size
, name
);
174 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
175 const wxPoint
&pos
, const wxSize
&size
,
176 const wxString
&name
);
178 ~wxGenericDataViewHeaderWindow()
180 delete m_resizeCursor
;
183 virtual void UpdateDisplay() { Refresh(); }
187 void OnPaint( wxPaintEvent
&event
);
188 void OnMouse( wxMouseEvent
&event
);
189 void OnSetFocus( wxFocusEvent
&event
);
194 // vars used for column resizing:
196 wxCursor
*m_resizeCursor
;
197 const wxCursor
*m_currentCursor
;
200 bool m_dirty
; // needs refresh?
201 int m_column
; // index of the column being resized
202 int m_currentX
; // divider line position in logical (unscrolled) coords
203 int m_minX
; // minimal position beyond which the divider line
204 // can't be dragged in logical coords
206 // the pen used to draw the current column width drag line
207 // when resizing the columsn
211 // internal utilities:
215 m_currentCursor
= (wxCursor
*) NULL
;
216 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
218 m_isDragging
= false;
221 m_column
= wxNOT_FOUND
;
225 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
226 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
230 void AdjustDC(wxDC
& dc
);
233 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
234 DECLARE_EVENT_TABLE()
237 #endif // defined(__WXMSW__)
239 //-----------------------------------------------------------------------------
240 // wxDataViewRenameTimer
241 //-----------------------------------------------------------------------------
243 class wxDataViewRenameTimer
: public wxTimer
246 wxDataViewMainWindow
*m_owner
;
249 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
253 //-----------------------------------------------------------------------------
254 // wxDataViewTreeNode
255 //-----------------------------------------------------------------------------
256 class wxDataViewTreeNode
;
257 WX_DEFINE_SORTED_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
258 WX_DEFINE_SORTED_ARRAY( void* , wxDataViewTreeLeaves
);
260 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
);
261 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
);
263 class wxDataViewTreeNode
266 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
267 :leaves( wxGenericTreeModelItemCmp
),
268 nodes(wxGenericTreeModelNodeCmp
)
269 { this->parent
= parent
;
277 //I don't know what I need to do in the destructure
278 ~wxDataViewTreeNode()
283 wxDataViewTreeNode
* GetParent() { return parent
; }
284 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
285 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
286 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
288 void AddNode( wxDataViewTreeNode
* node
)
291 leaves
.Add( node
->GetItem().GetID() );
293 void AddLeaf( void * leaf
) { leaves
.Add( leaf
); }
295 wxDataViewItem
& GetItem() { return item
; }
296 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
298 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
299 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
303 wxDataViewTreeNode
* node
= this;
304 while( node
->GetParent()->GetParent() != NULL
)
306 node
= node
->GetParent();
319 int len
= nodes
.GetCount();
321 for ( int i
= 0 ;i
< len
; i
++)
322 sum
+= nodes
[i
]->GetSubTreeCount();
324 sum
+= leaves
.GetCount();
327 ChangeSubTreeCount(-sum
);
333 ChangeSubTreeCount(sum
);
336 bool HasChildren() { return hasChildren
; }
337 void SetHasChildren( bool has
){ hasChildren
= has
; }
339 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
340 int GetSubTreeCount() { return subTreeCount
; }
341 void ChangeSubTreeCount( int num
)
347 parent
->ChangeSubTreeCount(num
);
352 wxDataViewTreeNodes nds
= nodes
;
353 wxDataViewTreeLeaves lvs
= leaves
;
357 int len
= nds
.GetCount();
361 for(i
= 0; i
< len
; i
++)
363 for(i
= 0; i
< len
; i
++)
367 len
= lvs
.GetCount();
370 for(int i
= 0; i
< len
; i
++)
376 wxDataViewTreeNode
* parent
;
377 wxDataViewTreeNodes nodes
;
378 wxDataViewTreeLeaves leaves
;
385 //Below is the compare stuff
386 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
387 static wxDataViewModel
* g_model
;
388 static unsigned int g_column
;
389 static bool g_asending
;
391 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
)
393 return g_model
->Compare( node1
->GetItem(), node2
->GetItem(), g_column
, g_asending
);
396 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
)
398 return g_model
->Compare( id1
, id2
, g_column
, g_asending
);
403 //-----------------------------------------------------------------------------
404 // wxDataViewMainWindow
405 //-----------------------------------------------------------------------------
407 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
409 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
410 WX_DEFINE_LIST(ItemList
);
412 class wxDataViewMainWindow
: public wxWindow
415 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
417 const wxPoint
&pos
= wxDefaultPosition
,
418 const wxSize
&size
= wxDefaultSize
,
419 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
420 virtual ~wxDataViewMainWindow();
422 // notifications from wxDataViewModel
423 void SendModelEvent( wxEventType type
, const wxDataViewItem
& item
);
424 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
425 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
426 bool ItemChanged( const wxDataViewItem
&item
);
427 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
438 g_model
= GetOwner()->GetModel();
439 g_column
= GetOwner()->GetSortingColumn();
440 wxDataViewColumn
* col
= GetOwner()->GetColumn(g_column
);
446 g_asending
= col
->IsSortOrderAscending();
449 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
450 wxDataViewCtrl
*GetOwner() { return m_owner
; }
451 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
453 void OnPaint( wxPaintEvent
&event
);
454 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
455 void OnChar( wxKeyEvent
&event
);
456 void OnMouse( wxMouseEvent
&event
);
457 void OnSetFocus( wxFocusEvent
&event
);
458 void OnKillFocus( wxFocusEvent
&event
);
460 void UpdateDisplay();
461 void RecalculateDisplay();
462 void OnInternalIdle();
464 void OnRenameTimer();
466 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
467 void ScrollTo( int rows
, int column
);
469 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
470 void ChangeCurrentRow( unsigned int row
);
472 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
473 bool IsEmpty() { return GetRowCount() == 0; }
475 int GetCountPerPage() const;
476 int GetEndOfLastCol() const;
477 unsigned int GetFirstVisibleRow() const;
478 //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
479 unsigned int GetLastVisibleRow();
480 unsigned int GetRowCount() ;
482 wxDataViewItem
GetSelection() const;
483 wxDataViewSelection
GetSelections(){ return m_selection
; }
484 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
485 void Select( const wxArrayInt
& aSelections
);
486 void SelectAllRows( bool on
);
487 void SelectRow( unsigned int row
, bool on
);
488 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
489 void ReverseRowSelection( unsigned int row
);
490 bool IsRowSelected( unsigned int row
);
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 //void EnsureVisible( unsigned int row );
503 wxRect
GetLineRect( unsigned int row
) const;
505 //Some useful functions for row and item mapping
506 wxDataViewItem
GetItemByRow( unsigned int row
) const;
507 int GetRowByItem( const wxDataViewItem
& item
);
509 //Methods for building the mapping tree
510 void BuildTree( wxDataViewModel
* model
);
512 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
513 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
515 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
516 //We did not need this temporarily
517 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
519 int RecalculateCount() ;
521 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
522 void OnExpanding( unsigned int row
);
523 void OnCollapsing( unsigned int row
);
525 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
528 wxDataViewCtrl
*m_owner
;
532 wxDataViewColumn
*m_currentCol
;
533 unsigned int m_currentRow
;
534 wxDataViewSelection m_selection
;
536 wxDataViewRenameTimer
*m_renameTimer
;
544 // for double click logic
545 unsigned int m_lineLastClicked
,
546 m_lineBeforeLastClicked
,
547 m_lineSelectSingleOnUp
;
549 // the pen used to draw horiz/vertical rules
552 // the pen used to draw the expander and the lines
555 //This is the tree structure of the model
556 wxDataViewTreeNode
* m_root
;
559 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
560 DECLARE_EVENT_TABLE()
563 // ---------------------------------------------------------
564 // wxGenericDataViewModelNotifier
565 // ---------------------------------------------------------
567 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
570 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
571 { m_mainWindow
= mainWindow
; }
573 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
574 { return m_mainWindow
->ItemAdded( parent
, item
); }
575 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
576 { return m_mainWindow
->ItemDeleted( parent
, item
); }
577 virtual bool ItemChanged( const wxDataViewItem
& item
)
578 { return m_mainWindow
->ItemChanged(item
); }
579 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
580 { return m_mainWindow
->ValueChanged( item
, col
); }
581 virtual bool Cleared()
582 { return m_mainWindow
->Cleared(); }
583 virtual void Resort()
584 { m_mainWindow
->Resort(); }
586 wxDataViewMainWindow
*m_mainWindow
;
589 // ---------------------------------------------------------
590 // wxDataViewRenderer
591 // ---------------------------------------------------------
593 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
595 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
596 wxDataViewCellMode mode
,
598 wxDataViewRendererBase( varianttype
, mode
, align
)
605 wxDataViewRenderer::~wxDataViewRenderer()
611 wxDC
*wxDataViewRenderer::GetDC()
615 if (GetOwner() == NULL
)
617 if (GetOwner()->GetOwner() == NULL
)
619 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
625 // ---------------------------------------------------------
626 // wxDataViewCustomRenderer
627 // ---------------------------------------------------------
629 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
631 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
632 wxDataViewCellMode mode
, int align
) :
633 wxDataViewRenderer( varianttype
, mode
, align
)
637 // ---------------------------------------------------------
638 // wxDataViewTextRenderer
639 // ---------------------------------------------------------
641 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
643 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
644 wxDataViewCellMode mode
, int align
) :
645 wxDataViewCustomRenderer( varianttype
, mode
, align
)
649 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
651 m_text
= value
.GetString();
656 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
661 bool wxDataViewTextRenderer::HasEditorCtrl()
666 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
667 wxRect labelRect
, const wxVariant
&value
)
669 return new wxTextCtrl( parent
, wxID_ANY
, value
,
670 wxPoint(labelRect
.x
,labelRect
.y
),
671 wxSize(labelRect
.width
,labelRect
.height
) );
674 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
676 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
677 value
= text
->GetValue();
681 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
683 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
684 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
685 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
686 view
->GetForegroundColour();
688 dc
->SetTextForeground(col
);
689 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
694 wxSize
wxDataViewTextRenderer::GetSize() const
696 const wxDataViewCtrl
*view
= GetView();
700 view
->GetTextExtent( m_text
, &x
, &y
);
701 return wxSize( x
, y
);
703 return wxSize(80,20);
706 // ---------------------------------------------------------
707 // wxDataViewBitmapRenderer
708 // ---------------------------------------------------------
710 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
712 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
713 wxDataViewCellMode mode
, int align
) :
714 wxDataViewCustomRenderer( varianttype
, mode
, align
)
718 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
720 if (value
.GetType() == wxT("wxBitmap"))
722 if (value
.GetType() == wxT("wxIcon"))
728 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
733 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
736 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
737 else if (m_icon
.Ok())
738 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
743 wxSize
wxDataViewBitmapRenderer::GetSize() const
746 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
747 else if (m_icon
.Ok())
748 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
750 return wxSize(16,16);
753 // ---------------------------------------------------------
754 // wxDataViewToggleRenderer
755 // ---------------------------------------------------------
757 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
759 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
760 wxDataViewCellMode mode
, int align
) :
761 wxDataViewCustomRenderer( varianttype
, mode
, align
)
766 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
768 m_toggle
= value
.GetBool();
773 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
778 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
780 // User wxRenderer here
783 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
785 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
790 flags
|= wxCONTROL_CHECKED
;
791 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
792 flags
|= wxCONTROL_DISABLED
;
794 wxRendererNative::Get().DrawCheckBox(
795 GetOwner()->GetOwner(),
803 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
804 wxDataViewModel
*model
,
805 const wxDataViewItem
& item
, unsigned int col
)
807 bool value
= !m_toggle
;
808 wxVariant variant
= value
;
809 model
->SetValue( variant
, item
, col
);
810 model
->ValueChanged( item
, col
);
814 wxSize
wxDataViewToggleRenderer::GetSize() const
816 return wxSize(20,20);
819 // ---------------------------------------------------------
820 // wxDataViewProgressRenderer
821 // ---------------------------------------------------------
823 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
825 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
826 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
827 wxDataViewCustomRenderer( varianttype
, mode
, align
)
833 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
837 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
839 m_value
= (long) value
;
841 if (m_value
< 0) m_value
= 0;
842 if (m_value
> 100) m_value
= 100;
847 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
849 value
= (long) m_value
;
853 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
855 double pct
= (double)m_value
/ 100.0;
857 bar
.width
= (int)(cell
.width
* pct
);
858 dc
->SetPen( *wxTRANSPARENT_PEN
);
859 dc
->SetBrush( *wxBLUE_BRUSH
);
860 dc
->DrawRectangle( bar
);
862 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
863 dc
->SetPen( *wxBLACK_PEN
);
864 dc
->DrawRectangle( cell
);
869 wxSize
wxDataViewProgressRenderer::GetSize() const
871 return wxSize(40,12);
874 // ---------------------------------------------------------
875 // wxDataViewDateRenderer
876 // ---------------------------------------------------------
878 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
880 #if wxUSE_DATE_RENDERER_POPUP
882 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
885 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
886 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
887 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
892 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
893 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
894 sizer
->Add( m_cal
, 1, wxGROW
);
899 void OnCalendar( wxCalendarEvent
&event
);
901 wxCalendarCtrl
*m_cal
;
902 wxDataViewModel
*m_model
;
904 const wxDataViewItem
& m_item
;
907 virtual void OnDismiss()
912 DECLARE_EVENT_TABLE()
915 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
916 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
919 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
921 wxDateTime date
= event
.GetDate();
922 wxVariant value
= date
;
923 m_model
->SetValue( value
, m_item
, m_col
);
924 m_model
->ValueChanged( m_item
, m_col
);
928 #endif // wxUSE_DATE_RENDERER_POPUP
930 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
932 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
933 wxDataViewCellMode mode
, int align
) :
934 wxDataViewCustomRenderer( varianttype
, mode
, align
)
938 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
940 m_date
= value
.GetDateTime();
945 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
951 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
953 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
954 wxString tmp
= m_date
.FormatDate();
955 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
960 wxSize
wxDataViewDateRenderer::GetSize() const
962 const wxDataViewCtrl
* view
= GetView();
963 wxString tmp
= m_date
.FormatDate();
965 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
966 return wxSize(x
,y
+d
);
969 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
970 const wxDataViewItem
& item
, unsigned int col
)
973 model
->GetValue( variant
, item
, col
);
974 wxDateTime value
= variant
.GetDateTime();
976 #if wxUSE_DATE_RENDERER_POPUP
977 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
978 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
979 wxPoint pos
= wxGetMousePosition();
982 popup
->Popup( popup
->m_cal
);
983 #else // !wxUSE_DATE_RENDERER_POPUP
984 wxMessageBox(value
.Format());
985 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
989 // ---------------------------------------------------------
991 // ---------------------------------------------------------
993 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
995 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
996 unsigned int model_column
,
997 int width
, wxAlignment align
, int flags
) :
998 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1000 SetAlignment(align
);
1004 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1007 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1008 unsigned int model_column
,
1009 int width
, wxAlignment align
, int flags
) :
1010 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1012 SetAlignment(align
);
1015 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1018 wxDataViewColumn::~wxDataViewColumn()
1022 void wxDataViewColumn::Init( int width
)
1025 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1029 void wxDataViewColumn::SetResizeable( bool resizeable
)
1032 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1034 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1037 void wxDataViewColumn::SetHidden( bool hidden
)
1040 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1042 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1044 // tell our owner to e.g. update its scrollbars:
1046 GetOwner()->OnColumnChange();
1049 void wxDataViewColumn::SetSortable( bool sortable
)
1052 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1054 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1056 // Update header button
1058 GetOwner()->OnColumnChange();
1061 void wxDataViewColumn::SetSortOrder( bool ascending
)
1063 m_ascending
= ascending
;
1065 // Update header button
1067 GetOwner()->OnColumnChange();
1070 bool wxDataViewColumn::IsSortOrderAscending() const
1075 void wxDataViewColumn::SetInternalWidth( int width
)
1079 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1080 if (m_owner
&& m_owner
->m_clientArea
)
1081 m_owner
->m_clientArea
->RecalculateDisplay();
1084 void wxDataViewColumn::SetWidth( int width
)
1086 m_owner
->m_headerArea
->UpdateDisplay();
1088 SetInternalWidth(width
);
1092 //-----------------------------------------------------------------------------
1093 // wxDataViewHeaderWindowBase
1094 //-----------------------------------------------------------------------------
1096 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1098 wxWindow
*parent
= GetParent();
1099 wxDataViewEvent
le(type
, parent
->GetId());
1101 le
.SetEventObject(parent
);
1103 le
.SetDataViewColumn(GetColumn(n
));
1104 le
.SetModel(GetOwner()->GetModel());
1106 // for events created by wxDataViewHeaderWindow the
1107 // row / value fields are not valid
1109 parent
->GetEventHandler()->ProcessEvent(le
);
1112 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1114 // implemented in msw/listctrl.cpp:
1115 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1117 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1119 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1120 const wxPoint
&pos
, const wxSize
&size
,
1121 const wxString
&name
)
1125 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1128 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1129 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1130 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1131 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1133 // create the native WC_HEADER window:
1134 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1135 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1136 m_hWnd
= CreateWindowEx(0,
1147 wxLogLastError(_T("CreateWindowEx"));
1151 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1152 // to call wxDataViewHeaderWindow::MSWOnNotify
1153 SubclassWin(m_hWnd
);
1155 // the following is required to get the default win's font for
1156 // header windows and must be done befor sending the HDM_LAYOUT msg
1163 // Retrieve the bounding rectangle of the parent window's
1164 // client area, and then request size and position values
1165 // from the header control.
1166 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1168 hdl
.prc
= &rcParent
;
1170 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1172 wxLogLastError(_T("SendMessage"));
1176 // Set the size, position, and visibility of the header control.
1177 SetWindowPos((HWND
)m_hWnd
,
1181 wp
.flags
| SWP_SHOWWINDOW
);
1183 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1184 // a wxBoxSizer and in order to avoid super-big header windows,
1185 // we need to set our height as fixed
1186 SetMinSize(wxSize(-1, wp
.cy
));
1187 SetMaxSize(wxSize(-1, wp
.cy
));
1192 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1197 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1199 // remove old columns
1200 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1201 Header_DeleteItem((HWND
)m_hWnd
, 0);
1203 // add the updated array of columns to the header control
1204 unsigned int cols
= GetOwner()->GetColumnCount();
1205 unsigned int added
= 0;
1206 wxDataViewModel
* model
= GetOwner()->GetModel();
1207 for (unsigned int i
= 0; i
< cols
; i
++)
1209 wxDataViewColumn
*col
= GetColumn( i
);
1210 if (col
->IsHidden())
1211 continue; // don't add it!
1214 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1215 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1216 hdi
.cxy
= col
->GetWidth();
1217 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1218 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1219 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1222 if(model
&& m_owner
->GetSortingColumn() == i
)
1224 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1225 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1226 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1230 // lParam is reserved for application's use:
1231 // we store there the column index to use it later in MSWOnNotify
1232 // (since columns may have been hidden)
1233 hdi
.lParam
= (LPARAM
)i
;
1235 // the native wxMSW implementation of the header window
1236 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1237 // on the right: to correct this effect we make the column
1238 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1240 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1242 switch (col
->GetAlignment())
1245 hdi
.fmt
|= HDF_LEFT
;
1247 case wxALIGN_CENTER
:
1248 case wxALIGN_CENTER_HORIZONTAL
:
1249 hdi
.fmt
|= HDF_CENTER
;
1252 hdi
.fmt
|= HDF_RIGHT
;
1256 // such alignment is not allowed for the column header!
1260 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1261 (WPARAM
)added
, (LPARAM
)&hdi
);
1266 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1270 // NOTE: we don't just return nmHDR->iItem because when there are
1271 // hidden columns, nmHDR->iItem may be different from
1272 // nmHDR->pitem->lParam
1274 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1276 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1281 item
.mask
= HDI_LPARAM
;
1282 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1284 return (unsigned int)item
.lParam
;
1287 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1289 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1291 // is it a message from the header?
1292 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1293 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1295 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1296 switch ( nmhdr
->code
)
1298 case HDN_BEGINTRACK
:
1299 // user has started to resize a column:
1300 // do we need to veto it?
1301 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1309 // user has started to reorder a column
1312 case HDN_ITEMCHANGING
:
1313 if (nmHDR
->pitem
!= NULL
&&
1314 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1316 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1317 if (nmHDR
->pitem
->cxy
< minWidth
)
1319 // do not allow the user to resize this column under
1320 // its minimal width:
1326 case HDN_ITEMCHANGED
: // user is resizing a column
1327 case HDN_ENDTRACK
: // user has finished resizing a column
1328 case HDN_ENDDRAG
: // user has finished reordering a column
1330 // update the width of the modified column:
1331 if (nmHDR
->pitem
!= NULL
&&
1332 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1334 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1335 unsigned int w
= nmHDR
->pitem
->cxy
;
1336 wxDataViewColumn
*col
= GetColumn(idx
);
1338 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1339 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1340 w
-= COLUMN_WIDTH_OFFSET
;
1342 if (w
>= (unsigned)col
->GetMinWidth())
1343 col
->SetInternalWidth(w
);
1349 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1350 wxDataViewModel
* model
= GetOwner()->GetModel();
1352 if(nmHDR
->iButton
== 0)
1354 wxDataViewColumn
*col
= GetColumn(idx
);
1355 if(col
->IsSortable())
1357 if(model
&& m_owner
->GetSortingColumn() == idx
)
1359 bool order
= col
->IsSortOrderAscending();
1360 col
->SetSortOrder(!order
);
1364 m_owner
->SetSortingColumn(idx
);
1372 wxEventType evt
= nmHDR
->iButton
== 0 ?
1373 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1374 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1375 SendEvent(evt
, idx
);
1381 // NOTE: for some reason (i.e. for a bug in Windows)
1382 // the HDN_ITEMCLICK notification is not sent on
1383 // right clicks, so we need to handle NM_RCLICK
1386 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1387 if (column
!= wxNOT_FOUND
)
1390 item
.mask
= HDI_LPARAM
;
1391 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1393 // 'idx' may be different from 'column' if there are
1394 // hidden columns...
1395 unsigned int idx
= (unsigned int)item
.lParam
;
1396 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1402 case HDN_GETDISPINFOW
:
1403 // see wxListCtrl::MSWOnNotify for more info!
1406 case HDN_ITEMDBLCLICK
:
1408 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1409 int w
= GetOwner()->GetBestColumnWidth(idx
);
1411 // update the native control:
1413 ZeroMemory(&hd
, sizeof(hd
));
1414 hd
.mask
= HDI_WIDTH
;
1416 Header_SetItem(GetHwnd(),
1417 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1420 // update the wxDataViewColumn class:
1421 GetColumn(idx
)->SetInternalWidth(w
);
1426 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1432 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1433 const wxRect
*WXUNUSED(rect
))
1435 wxSize ourSz
= GetClientSize();
1436 wxSize ownerSz
= m_owner
->GetClientSize();
1438 // where should the (logical) origin of this window be placed?
1440 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1442 // put this window on top of our parent and
1443 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1444 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1448 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1449 int WXUNUSED(w
), int WXUNUSED(h
),
1452 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1453 // the wxDataViewCtrl window gets resized: the following dummy call
1454 // to ScrollWindow() is required in order to get this header window
1455 // correctly repainted when it's (horizontally) scrolled:
1460 #else // !defined(__WXMSW__)
1462 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1463 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1464 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1465 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1466 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1469 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1470 const wxPoint
&pos
, const wxSize
&size
,
1471 const wxString
&name
)
1475 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1478 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1479 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1480 SetOwnForegroundColour( attr
.colFg
);
1481 SetOwnBackgroundColour( attr
.colBg
);
1483 SetOwnFont( attr
.font
);
1485 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1486 // a wxBoxSizer and in order to avoid super-big header windows,
1487 // we need to set our height as fixed
1488 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1489 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1494 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1497 GetClientSize( &w
, &h
);
1499 wxAutoBufferedPaintDC
dc( this );
1501 dc
.SetBackground(GetBackgroundColour());
1505 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1508 m_owner
->GetViewStart( &x
, NULL
);
1510 // account for the horz scrollbar offset
1511 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1513 dc
.SetFont( GetFont() );
1515 unsigned int cols
= GetOwner()->GetColumnCount();
1518 for (i
= 0; i
< cols
; i
++)
1520 wxDataViewColumn
*col
= GetColumn( i
);
1521 if (col
->IsHidden())
1522 continue; // skip it!
1524 int cw
= col
->GetWidth();
1527 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1528 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == i
)
1530 if (col
->IsSortOrderAscending())
1531 sortArrow
= wxHDR_SORT_ICON_UP
;
1533 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1536 wxRendererNative::Get().DrawHeaderButton
1540 wxRect(xpos
, 0, cw
, ch
-1),
1541 m_parent
->IsEnabled() ? 0
1542 : (int)wxCONTROL_DISABLED
,
1546 // align as required the column title:
1548 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1549 switch (col
->GetAlignment())
1552 x
+= HEADER_HORIZ_BORDER
;
1554 case wxALIGN_CENTER
:
1555 case wxALIGN_CENTER_HORIZONTAL
:
1556 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1559 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1563 // always center the title vertically:
1564 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1566 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1568 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1569 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1570 dc
.DrawText( col
->GetTitle(), x
, y
);
1571 dc
.DestroyClippingRegion();
1577 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1579 GetParent()->SetFocus();
1583 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1585 // we want to work with logical coords
1587 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1588 int y
= event
.GetY();
1592 // we don't draw the line beyond our window,
1593 // but we allow dragging it there
1595 GetClientSize( &w
, NULL
);
1596 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1599 // erase the line if it was drawn
1603 if (event
.ButtonUp())
1605 m_isDragging
= false;
1611 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1614 GetOwner()->Refresh();
1618 m_currentX
= wxMax(m_minX
+ 7, x
);
1620 // draw in the new location
1621 if (m_currentX
< w
) DrawCurrent();
1625 else // not dragging
1628 m_column
= wxNOT_FOUND
;
1630 bool hit_border
= false;
1632 // end of the current column
1635 // find the column where this event occured
1636 int countCol
= m_owner
->GetColumnCount();
1637 for (int column
= 0; column
< countCol
; column
++)
1639 wxDataViewColumn
*p
= GetColumn(column
);
1642 continue; // skip if not shown
1644 xpos
+= p
->GetWidth();
1646 if ((abs(x
-xpos
) < 3) && (y
< 22))
1654 // inside the column
1661 if (m_column
== wxNOT_FOUND
)
1664 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1665 if (event
.LeftDClick() && resizeable
)
1667 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1670 else if (event
.LeftDown() || event
.RightUp())
1672 if (hit_border
&& event
.LeftDown() && resizeable
)
1674 m_isDragging
= true;
1679 else // click on a column
1681 wxDataViewModel
* model
= GetOwner()->GetModel();
1682 wxEventType evt
= event
.LeftDown() ?
1683 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1684 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1685 SendEvent(evt
, m_column
);
1687 //Left click the header
1688 if(event
.LeftDown())
1690 wxDataViewColumn
*col
= GetColumn(m_column
);
1691 if(col
->IsSortable())
1693 unsigned int colnum
= m_owner
->GetSortingColumn();
1694 if(model
&& static_cast<int>(colnum
) == m_column
)
1696 bool order
= col
->IsSortOrderAscending();
1697 col
->SetSortOrder(!order
);
1701 m_owner
->SetSortingColumn(m_column
);
1707 //Send the column sorted event
1708 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1712 else if (event
.Moving())
1714 if (hit_border
&& resizeable
)
1715 m_currentCursor
= m_resizeCursor
;
1717 m_currentCursor
= wxSTANDARD_CURSOR
;
1719 SetCursor(*m_currentCursor
);
1724 void wxGenericDataViewHeaderWindow::DrawCurrent()
1726 int x1
= m_currentX
;
1728 ClientToScreen (&x1
, &y1
);
1730 int x2
= m_currentX
-1;
1732 ++x2
; // but why ????
1735 m_owner
->GetClientSize( NULL
, &y2
);
1736 m_owner
->ClientToScreen( &x2
, &y2
);
1739 dc
.SetLogicalFunction(wxINVERT
);
1740 dc
.SetPen(m_penCurrent
);
1741 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1743 dc
.DrawLine(x1
, y1
, x2
, y2
);
1746 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1750 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1751 m_owner
->GetViewStart( &x
, NULL
);
1753 // shift the DC origin to match the position of the main window horizontal
1754 // scrollbar: this allows us to always use logical coords
1755 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1758 #endif // defined(__WXMSW__)
1760 //-----------------------------------------------------------------------------
1761 // wxDataViewRenameTimer
1762 //-----------------------------------------------------------------------------
1764 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1769 void wxDataViewRenameTimer::Notify()
1771 m_owner
->OnRenameTimer();
1774 //-----------------------------------------------------------------------------
1775 // wxDataViewMainWindow
1776 //-----------------------------------------------------------------------------
1778 //The tree building helper, declared firstly
1779 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1781 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1783 if (row1
> row2
) return 1;
1784 if (row1
== row2
) return 0;
1789 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1791 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1792 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1793 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1794 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1795 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1796 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1799 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1800 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1801 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1802 m_selection( wxDataViewSelectionCmp
)
1807 m_lastOnSame
= false;
1808 m_renameTimer
= new wxDataViewRenameTimer( this );
1810 // TODO: user better initial values/nothing selected
1811 m_currentCol
= NULL
;
1814 // TODO: we need to calculate this smartly
1821 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1824 m_dragStart
= wxPoint(0,0);
1825 m_lineLastClicked
= (unsigned int) -1;
1826 m_lineBeforeLastClicked
= (unsigned int) -1;
1827 m_lineSelectSingleOnUp
= (unsigned int) -1;
1831 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1832 SetBackgroundColour( *wxWHITE
);
1834 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1836 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1837 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1838 //Some new added code to deal with the tree structure
1839 m_root
= new wxDataViewTreeNode( NULL
);
1840 m_root
->SetHasChildren(true);
1842 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1847 wxDataViewMainWindow::~wxDataViewMainWindow()
1850 delete m_renameTimer
;
1853 void wxDataViewMainWindow::OnRenameTimer()
1855 // We have to call this here because changes may just have
1856 // been made and no screen update taken place.
1861 unsigned int cols
= GetOwner()->GetColumnCount();
1863 for (i
= 0; i
< cols
; i
++)
1865 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1867 continue; // skip it!
1869 if (c
== m_currentCol
)
1871 xpos
+= c
->GetWidth();
1873 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1874 m_currentCol
->GetWidth(), m_lineHeight
);
1876 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1877 &labelRect
.x
, &labelRect
.y
);
1879 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1880 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1883 //------------------------------------------------------------------
1884 // Helper class for do operation on the tree node
1885 //------------------------------------------------------------------
1892 //The return value control how the tree-walker tranverse the tree
1893 // 0: Job done, stop tranverse and return
1894 // 1: Ignore the current node's subtree and continue
1895 // 2: Job not done, continue
1896 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1897 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1898 virtual int operator() ( void * n
) = 0;
1901 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1903 if( node
==NULL
|| !node
->HasChildren())
1906 switch( func( node
) )
1917 wxDataViewTreeNodes nodes
= node
->GetNodes();
1918 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1920 int len_nodes
= nodes
.GetCount();
1921 int len
= leaves
.GetCount();
1922 int i
= 0, nodes_i
= 0;
1924 for( ; i
< len
; i
++ )
1926 void * n
= leaves
[i
];
1927 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1929 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1932 if( Walker( nd
, func
) )
1951 void wxDataViewMainWindow::SendModelEvent( wxEventType type
, const wxDataViewItem
& item
)
1953 wxWindow
*parent
= GetParent();
1954 wxDataViewEvent
le(type
, parent
->GetId());
1956 le
.SetEventObject(parent
);
1957 le
.SetModel(GetOwner()->GetModel());
1960 parent
->GetEventHandler()->ProcessEvent(le
);
1963 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1967 wxDataViewTreeNode
* node
;
1968 node
= FindNode(parent
);
1969 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
, item
);
1974 node
->SetHasChildren( true );
1976 if( g_model
->IsContainer( item
) )
1978 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1979 newnode
->SetItem(item
);
1980 newnode
->SetHasChildren( true );
1981 node
->AddNode( newnode
);
1984 node
->AddLeaf( item
.GetID() );
1986 node
->ChangeSubTreeCount(1);
1994 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1996 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1997 const wxDataViewItem
& item
)
2001 wxDataViewTreeNode
* node
;
2002 node
= FindNode(parent
);
2004 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
, item
);
2005 if( node
== NULL
|| node
->GetChildren().Index( item
.GetID() ) == wxNOT_FOUND
)
2011 node
->GetChildren().Remove( item
.GetID() );
2012 //Manuplate selection
2013 if( m_selection
.GetCount() > 1 )
2015 int row
= m_selection
[0];
2016 m_selection
.Empty();
2017 m_selection
.Add(row
);
2020 if( GetOwner()->GetModel()->IsContainer( item
) )
2022 wxDataViewTreeNode
* n
= NULL
;
2023 wxDataViewTreeNodes nodes
= node
->GetNodes();
2024 int len
= nodes
.GetCount();
2025 for( int i
= 0 ; i
< len
; i
++)
2027 if( nodes
[i
]->GetItem() == item
)
2037 node
->GetNodes().Remove( n
);
2038 sub
-= n
->GetSubTreeCount();
2039 DestroyTreeHelper(n
);
2042 if( node
->GetChildrenNumber() == 0)
2043 node
->SetHasChildren( false );
2045 //Make the row number invalid and get a new valid one when user call GetRowCount
2047 node
->ChangeSubTreeCount(sub
);
2048 //Change the current row to the last row if the current exceed the max row number
2049 if( m_currentRow
> GetRowCount() )
2050 m_currentRow
= m_count
- 1;
2056 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2061 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
,item
);
2066 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2068 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2069 /*#define MAX_VIRTUAL_WIDTH 100000
2071 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2072 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2073 Refresh( true, &rect );
2081 wxWindow
*parent
= GetParent();
2082 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
, parent
->GetId());
2083 le
.SetEventObject(parent
);
2084 le
.SetModel(GetOwner()->GetModel());
2087 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2088 parent
->GetEventHandler()->ProcessEvent(le
);
2093 bool wxDataViewMainWindow::Cleared()
2100 wxWindow
*parent
= GetParent();
2101 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
, parent
->GetId());
2102 le
.SetEventObject(parent
);
2103 le
.SetModel(GetOwner()->GetModel());
2104 parent
->GetEventHandler()->ProcessEvent(le
);
2109 void wxDataViewMainWindow::UpdateDisplay()
2114 void wxDataViewMainWindow::OnInternalIdle()
2116 wxWindow::OnInternalIdle();
2120 RecalculateDisplay();
2125 void wxDataViewMainWindow::RecalculateDisplay()
2127 wxDataViewModel
*model
= GetOwner()->GetModel();
2134 int width
= GetEndOfLastCol();
2135 int height
= GetRowCount() * m_lineHeight
;
2137 SetVirtualSize( width
, height
);
2138 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2143 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2145 wxWindow::ScrollWindow( dx
, dy
, rect
);
2147 if (GetOwner()->m_headerArea
)
2148 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2151 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2154 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2155 int sy
= rows
*m_lineHeight
/y
;
2159 wxRect rect
= GetClientRect();
2160 unsigned int colnum
= 0;
2161 unsigned int x_start
= 0, x_end
= 0, w
= 0;
2163 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2164 for (x_start
= 0; colnum
< column
; colnum
++)
2166 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2167 if (col
->IsHidden())
2168 continue; // skip it!
2170 w
= col
->GetWidth();
2174 x_end
= x_start
+ w
;
2175 xe
= xx
+ rect
.width
;
2178 sx
= ( xx
+ x_end
- xe
)/x
;
2185 m_owner
->Scroll( sx
, sy
);
2188 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2190 wxDataViewModel
*model
= GetOwner()->GetModel();
2191 wxAutoBufferedPaintDC
dc( this );
2194 dc
.SetBackground(GetBackgroundColour());
2196 GetOwner()->PrepareDC( dc
);
2197 dc
.SetFont( GetFont() );
2199 wxRect update
= GetUpdateRegion().GetBox();
2200 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2202 // compute which items needs to be redrawn
2203 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2204 unsigned int item_count
=
2205 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2206 (int)(GetRowCount( )- item_start
) );
2207 unsigned int item_last
= item_start
+ item_count
;
2209 // compute which columns needs to be redrawn
2210 unsigned int cols
= GetOwner()->GetColumnCount();
2211 unsigned int col_start
= 0;
2212 unsigned int x_start
= 0;
2213 for (x_start
= 0; col_start
< cols
; col_start
++)
2215 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2216 if (col
->IsHidden())
2217 continue; // skip it!
2219 unsigned int w
= col
->GetWidth();
2220 if (x_start
+w
>= (unsigned int)update
.x
)
2226 unsigned int col_last
= col_start
;
2227 unsigned int x_last
= x_start
;
2228 for (; col_last
< cols
; col_last
++)
2230 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2231 if (col
->IsHidden())
2232 continue; // skip it!
2234 if (x_last
> (unsigned int)update
.GetRight())
2237 x_last
+= col
->GetWidth();
2240 // Draw horizontal rules if required
2241 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2243 dc
.SetPen(m_penRule
);
2244 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2246 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2248 int y
= i
* m_lineHeight
;
2249 dc
.DrawLine(x_start
, y
, x_last
, y
);
2253 // Draw vertical rules if required
2254 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2256 dc
.SetPen(m_penRule
);
2257 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2260 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2262 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2263 if (col
->IsHidden())
2264 continue; // skip it
2266 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2267 x
, item_last
* m_lineHeight
);
2269 x
+= col
->GetWidth();
2272 // Draw last vertical rule
2273 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2274 x
, item_last
* m_lineHeight
);
2277 // redraw the background for the items which are selected/current
2278 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2280 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2281 if (selected
|| item
== m_currentRow
)
2283 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2284 if (item
== m_currentRow
)
2285 flags
|= wxCONTROL_CURRENT
;
2287 flags
|= wxCONTROL_FOCUSED
;
2289 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2290 wxRendererNative::Get().DrawItemSelectionRect
2300 // redraw all cells for all rows which must be repainted and for all columns
2302 cell_rect
.x
= x_start
;
2303 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2304 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2306 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2307 wxDataViewRenderer
*cell
= col
->GetRenderer();
2308 cell_rect
.width
= col
->GetWidth();
2310 if (col
->IsHidden())
2311 continue; // skipt it!
2313 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2315 // get the cell value and set it into the renderer
2317 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2323 wxDataViewItem dataitem
= node
->GetItem();
2324 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2325 cell
->SetValue( value
);
2327 // update the y offset
2328 cell_rect
.y
= item
* m_lineHeight
;
2330 //Draw the expander here.
2331 int indent
= node
->GetIndentLevel();
2332 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
2334 //Calculate the indent first
2335 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2337 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2338 // change the cell_rect.x to the appropriate pos
2339 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2340 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2341 dc
.SetPen( m_penExpander
);
2342 dc
.SetBrush( wxNullBrush
);
2343 if( node
->HasChildren() )
2345 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2346 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2347 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2348 if( node
->IsOpen() )
2349 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2351 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2355 // I am wandering whether we should draw dot lines between tree nodes
2357 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2360 //force the expander column to left-center align
2361 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2365 // cannot be bigger than allocated space
2366 wxSize size
= cell
->GetSize();
2367 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2368 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2369 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2371 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2372 int align
= cell
->GetAlignment();
2374 // horizontal alignment:
2375 item_rect
.x
= cell_rect
.x
;
2376 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2377 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2378 else if (align
& wxALIGN_RIGHT
)
2379 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2380 //else: wxALIGN_LEFT is the default
2382 // vertical alignment:
2383 item_rect
.y
= cell_rect
.y
;
2384 if (align
& wxALIGN_CENTER_VERTICAL
)
2385 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2386 else if (align
& wxALIGN_BOTTOM
)
2387 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2388 //else: wxALIGN_TOP is the default
2391 item_rect
.x
+= PADDING_RIGHTLEFT
;
2392 item_rect
.y
+= PADDING_TOPBOTTOM
;
2393 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2394 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2396 //Here we add the tree indent
2397 item_rect
.x
+= indent
;
2400 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2401 state
|= wxDATAVIEW_CELL_SELECTED
;
2403 // TODO: it would be much more efficient to create a clipping
2404 // region for the entire column being rendered (in the OnPaint
2405 // of wxDataViewMainWindow) instead of a single clip region for
2406 // each cell. However it would mean that each renderer should
2407 // respect the given wxRect's top & bottom coords, eventually
2408 // violating only the left & right coords - however the user can
2409 // make its own renderer and thus we cannot be sure of that.
2410 dc
.SetClippingRegion( item_rect
);
2411 cell
->Render( item_rect
, &dc
, state
);
2412 dc
.DestroyClippingRegion();
2415 cell_rect
.x
+= cell_rect
.width
;
2419 int wxDataViewMainWindow::GetCountPerPage() const
2421 wxSize size
= GetClientSize();
2422 return size
.y
/ m_lineHeight
;
2425 int wxDataViewMainWindow::GetEndOfLastCol() const
2429 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2431 const wxDataViewColumn
*c
=
2432 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2435 width
+= c
->GetWidth();
2440 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2444 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2446 return y
/ m_lineHeight
;
2449 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2451 wxSize client_size
= GetClientSize();
2452 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2453 &client_size
.x
, &client_size
.y
);
2455 //we should deal with the pixel here
2456 unsigned int row
= (client_size
.y
)/m_lineHeight
;
2457 if( client_size
.y
% m_lineHeight
< m_lineHeight
/2 )
2460 return wxMin( GetRowCount()-1, row
);
2463 unsigned int wxDataViewMainWindow::GetRowCount()
2465 if ( m_count
== -1 )
2467 m_count
= RecalculateCount();
2469 GetVirtualSize( &width
, &height
);
2470 height
= m_count
* m_lineHeight
;
2472 SetVirtualSize( width
, height
);
2477 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2484 void wxDataViewMainWindow::SelectAllRows( bool on
)
2491 m_selection
.Clear();
2492 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2493 m_selection
.Add( i
);
2498 unsigned int first_visible
= GetFirstVisibleRow();
2499 unsigned int last_visible
= GetLastVisibleRow();
2501 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2503 unsigned int row
= m_selection
[i
];
2504 if ((row
>= first_visible
) && (row
<= last_visible
))
2507 m_selection
.Clear();
2511 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2513 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2517 m_selection
.Add( row
);
2525 m_selection
.Remove( row
);
2531 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2535 unsigned int tmp
= from
;
2541 for (i
= from
; i
<= to
; i
++)
2543 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2546 m_selection
.Add( i
);
2551 m_selection
.Remove( i
);
2554 RefreshRows( from
, to
);
2557 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2559 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2561 int n
= aSelections
[i
];
2563 m_selection
.Add( n
);
2568 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2570 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2571 m_selection
.Add( row
);
2573 m_selection
.Remove( row
);
2577 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2579 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2582 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2584 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2585 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2587 wxSize client_size
= GetClientSize();
2588 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2589 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2590 if (intersect_rect
.width
> 0)
2591 Refresh( true, &intersect_rect
);
2594 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2598 unsigned int tmp
= to
;
2603 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2604 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2606 wxSize client_size
= GetClientSize();
2607 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2608 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2609 if (intersect_rect
.width
> 0)
2610 Refresh( true, &intersect_rect
);
2613 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2615 unsigned int count
= GetRowCount();
2616 if (firstRow
> count
)
2619 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2620 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2622 wxSize client_size
= GetClientSize();
2623 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2624 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2625 if (intersect_rect
.width
> 0)
2626 Refresh( true, &intersect_rect
);
2629 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2631 wxCHECK_RET( newCurrent
< GetRowCount(),
2632 _T("invalid item index in OnArrowChar()") );
2634 // if there is no selection, we cannot move it anywhere
2635 if (!HasCurrentRow())
2638 unsigned int oldCurrent
= m_currentRow
;
2640 // in single selection we just ignore Shift as we can't select several
2642 if ( event
.ShiftDown() && !IsSingleSel() )
2644 RefreshRow( oldCurrent
);
2646 ChangeCurrentRow( newCurrent
);
2648 // select all the items between the old and the new one
2649 if ( oldCurrent
> newCurrent
)
2651 newCurrent
= oldCurrent
;
2652 oldCurrent
= m_currentRow
;
2655 SelectRows( oldCurrent
, newCurrent
, true );
2659 RefreshRow( oldCurrent
);
2661 // all previously selected items are unselected unless ctrl is held
2662 if ( !event
.ControlDown() )
2663 SelectAllRows(false);
2665 ChangeCurrentRow( newCurrent
);
2667 if ( !event
.ControlDown() )
2668 SelectRow( m_currentRow
, true );
2670 RefreshRow( m_currentRow
);
2673 //EnsureVisible( m_currentRow );
2676 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2680 rect
.y
= m_lineHeight
* row
;
2681 rect
.width
= GetEndOfLastCol();
2682 rect
.height
= m_lineHeight
;
2687 class RowToItemJob
: public DoJob
2690 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2691 virtual ~RowToItemJob(){};
2693 virtual int operator() ( wxDataViewTreeNode
* node
)
2696 if( current
== static_cast<int>(row
))
2698 ret
= node
->GetItem() ;
2702 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2704 current
+= node
->GetSubTreeCount();
2709 //If the current has no child node, we can find the desired item of the row number directly.
2710 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2711 if( node
->GetNodes().GetCount() == 0)
2713 int index
= static_cast<int>(row
) - current
- 1;
2714 ret
= node
->GetChildren().Item( index
);
2721 virtual int operator() ( void * n
)
2724 if( current
== static_cast<int>(row
))
2726 ret
= wxDataViewItem( n
) ;
2731 wxDataViewItem
GetResult(){ return ret
; }
2738 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2740 RowToItemJob
job( row
, -2 );
2741 Walker( m_root
, job
);
2742 return job
.GetResult();
2745 class RowToTreeNodeJob
: public DoJob
2748 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2751 this->current
= current
;
2755 virtual ~RowToTreeNodeJob(){};
2757 virtual int operator() ( wxDataViewTreeNode
* node
)
2760 if( current
== static_cast<int>(row
))
2766 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2768 current
+= node
->GetSubTreeCount();
2774 //If the current has no child node, we can find the desired item of the row number directly.
2775 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2776 if( node
->GetNodes().GetCount() == 0)
2778 int index
= static_cast<int>(row
) - current
- 1;
2779 void * n
= node
->GetChildren().Item( index
);
2780 ret
= new wxDataViewTreeNode( parent
) ;
2781 ret
->SetItem( wxDataViewItem( n
));
2782 ret
->SetHasChildren(false);
2791 virtual int operator() ( void * n
)
2794 if( current
== static_cast<int>(row
))
2796 ret
= new wxDataViewTreeNode( parent
) ;
2797 ret
->SetItem( wxDataViewItem( n
));
2798 ret
->SetHasChildren(false);
2804 wxDataViewTreeNode
* GetResult(){ return ret
; }
2808 wxDataViewTreeNode
* ret
;
2809 wxDataViewTreeNode
* parent
;
2813 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2815 RowToTreeNodeJob
job( row
, -2, m_root
);
2816 Walker( m_root
, job
);
2817 return job
.GetResult();
2820 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2822 wxWindow
*parent
= GetParent();
2823 wxDataViewEvent
le(type
, parent
->GetId());
2825 le
.SetEventObject(parent
);
2826 le
.SetModel(GetOwner()->GetModel());
2829 parent
->GetEventHandler()->ProcessEvent(le
);
2833 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2835 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2838 if( node
->HasChildren())
2840 if( !node
->IsOpen())
2842 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2843 //Check if the user prevent expanding
2844 if( e
.GetSkipped() )
2848 //Here I build the children of current node
2849 if( node
->GetChildrenNumber() == 0 )
2850 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2853 //Send the expanded event
2854 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2858 SelectRow( row
, false );
2859 SelectRow( row
+ 1, true );
2860 ChangeCurrentRow( row
+ 1 );
2868 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2870 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2873 wxDataViewTreeNode
* nd
= node
;
2875 if( node
->HasChildren() && node
->IsOpen() )
2877 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2878 if( e
.GetSkipped() )
2883 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2887 node
= node
->GetParent();
2890 int parent
= GetRowByItem( node
->GetItem() ) ;
2893 SelectRow( row
, false);
2894 SelectRow(parent
, true );
2895 ChangeCurrentRow( parent
);
2899 if( !nd
->HasChildren())
2904 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2906 wxDataViewModel
* model
= GetOwner()->GetModel();
2910 //Compose the a parent-chain of the finding item
2912 list
.DeleteContents( true );
2913 wxDataViewItem
it( item
);
2916 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2917 list
.Insert( pItem
);
2918 it
= model
->GetParent( it
);
2921 //Find the item along the parent-chain.
2922 //This algorithm is designed to speed up the node-finding method
2923 wxDataViewTreeNode
* node
= m_root
;
2924 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2926 if( node
->HasChildren() )
2928 if( node
->GetChildrenNumber() == 0 )
2929 BuildTreeHelper(model
, node
->GetItem(), node
);
2931 wxDataViewTreeNodes nodes
= node
->GetNodes();
2932 //The wxSortedArray search a node in binary search, so using Item() is more efficient
2933 wxDataViewTreeNode temp
;
2934 temp
.SetItem(*(n
->GetData()));
2935 int index
= nodes
.Index( &temp
);
2936 if( index
== wxNOT_FOUND
)
2938 node
= nodes
[index
];
2946 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2948 wxDataViewColumn
*col
= NULL
;
2949 unsigned int cols
= GetOwner()->GetColumnCount();
2950 unsigned int colnum
= 0;
2951 unsigned int x_start
= 0;
2953 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2954 for (x_start
= 0; colnum
< cols
; colnum
++)
2956 col
= GetOwner()->GetColumn(colnum
);
2957 if (col
->IsHidden())
2958 continue; // skip it!
2960 unsigned int w
= col
->GetWidth();
2961 if (x_start
+w
>= (unsigned int)x
)
2968 item
= GetItemByRow( y
/m_lineHeight
);
2971 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
2973 int row
= GetRowByItem(item
);
2974 int y
= row
*m_lineHeight
;
2975 int h
= m_lineHeight
;
2977 wxDataViewColumn
*col
= NULL
;
2978 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
2980 col
= GetOwner()->GetColumn( i
);
2981 x
+= col
->GetWidth();
2982 if( GetOwner()->GetColumn(i
+1) == column
)
2985 int w
= col
->GetWidth();
2986 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
2987 return wxRect(x
, y
, w
, h
);
2990 int wxDataViewMainWindow::RecalculateCount()
2992 return m_root
->GetSubTreeCount();
2995 class ItemToRowJob
: public DoJob
2998 ItemToRowJob(const wxDataViewItem
& item
, ItemList::Node
* node
)
2999 { this->item
= item
; ret
= -1 ; nd
= node
; }
3000 virtual ~ItemToRowJob(){};
3002 //Maybe binary search will help to speed up this process
3003 virtual int operator() ( wxDataViewTreeNode
* node
)
3006 if( node
->GetItem() == item
)
3011 if( nd
&& node
->GetItem() == *(nd
->GetData()))
3018 ret
+= node
->GetSubTreeCount();
3024 virtual int operator() ( void * n
)
3027 if( n
== item
.GetID() )
3031 //the row number is begin from zero
3032 int GetResult(){ return ret
-1 ; }
3034 ItemList::Node
* nd
;
3035 wxDataViewItem item
;
3040 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3042 wxDataViewModel
* model
= GetOwner()->GetModel();
3049 //Compose the a parent-chain of the finding item
3051 wxDataViewItem
* pItem
= NULL
;
3052 list
.DeleteContents( true );
3053 wxDataViewItem
it( item
);
3056 pItem
= new wxDataViewItem( it
);
3057 list
.Insert( pItem
);
3058 it
= model
->GetParent( it
);
3060 pItem
= new wxDataViewItem( );
3061 list
.Insert( pItem
);
3063 ItemToRowJob
job( item
, list
.GetFirst() );
3064 Walker(m_root
, job
);
3065 return job
.GetResult();
3068 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3070 if( !model
->IsContainer( item
) )
3073 wxDataViewItem i
= model
->GetFirstChild( item
);
3078 if( model
->IsContainer( i
) )
3080 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3082 n
->SetHasChildren( true ) ;
3087 node
->AddLeaf( i
.GetID() );
3089 i
= model
->GetNextSibling( i
);
3091 node
->SetSubTreeCount( num
);
3092 wxDataViewTreeNode
* n
= node
->GetParent();
3094 n
->ChangeSubTreeCount(num
);
3098 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3100 //First we define a invalid item to fetch the top-level elements
3101 wxDataViewItem item
;
3103 BuildTreeHelper( model
, item
, m_root
);
3107 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3109 if( node
->GetNodeNumber() != 0 )
3111 int len
= node
->GetNodeNumber();
3113 wxDataViewTreeNodes nodes
= node
->GetNodes();
3114 for( ; i
< len
; i
++ )
3116 DestroyTreeHelper(nodes
[i
]);
3122 void wxDataViewMainWindow::DestroyTree()
3124 DestroyTreeHelper(m_root
);
3125 m_root
->SetSubTreeCount(0);
3129 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3131 if (event
.GetKeyCode() == WXK_TAB
)
3133 wxNavigationKeyEvent nevent
;
3134 nevent
.SetWindowChange( event
.ControlDown() );
3135 nevent
.SetDirection( !event
.ShiftDown() );
3136 nevent
.SetEventObject( GetParent()->GetParent() );
3137 nevent
.SetCurrentFocus( m_parent
);
3138 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3142 // no item -> nothing to do
3143 if (!HasCurrentRow())
3149 // don't use m_linesPerPage directly as it might not be computed yet
3150 const int pageSize
= GetCountPerPage();
3151 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3153 switch ( event
.GetKeyCode() )
3156 if ( m_currentRow
> 0 )
3157 OnArrowChar( m_currentRow
- 1, event
);
3161 if ( m_currentRow
< GetRowCount() - 1 )
3162 OnArrowChar( m_currentRow
+ 1, event
);
3164 //Add the process for tree expanding/collapsing
3166 OnCollapsing(m_currentRow
);
3169 OnExpanding( m_currentRow
);
3173 OnArrowChar( GetRowCount() - 1, event
);
3178 OnArrowChar( 0, event
);
3183 int steps
= pageSize
- 1;
3184 int index
= m_currentRow
- steps
;
3188 OnArrowChar( index
, event
);
3194 int steps
= pageSize
- 1;
3195 unsigned int index
= m_currentRow
+ steps
;
3196 unsigned int count
= GetRowCount();
3197 if ( index
>= count
)
3200 OnArrowChar( index
, event
);
3209 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3211 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3213 // let the base handle mouse wheel events.
3218 int x
= event
.GetX();
3219 int y
= event
.GetY();
3220 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3222 wxDataViewColumn
*col
= NULL
;
3225 unsigned int cols
= GetOwner()->GetColumnCount();
3227 for (i
= 0; i
< cols
; i
++)
3229 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3231 continue; // skip it!
3233 if (x
< xpos
+ c
->GetWidth())
3238 xpos
+= c
->GetWidth();
3242 wxDataViewRenderer
*cell
= col
->GetRenderer();
3244 unsigned int current
= y
/ m_lineHeight
;
3246 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3248 // Unselect all if below the last row ?
3252 wxDataViewModel
*model
= GetOwner()->GetModel();
3254 if (event
.Dragging())
3256 if (m_dragCount
== 0)
3258 // we have to report the raw, physical coords as we want to be
3259 // able to call HitTest(event.m_pointDrag) from the user code to
3260 // get the item being dragged
3261 m_dragStart
= event
.GetPosition();
3266 if (m_dragCount
!= 3)
3269 if (event
.LeftIsDown())
3271 // Notify cell about drag
3280 bool forceClick
= false;
3282 if (event
.ButtonDClick())
3284 m_renameTimer
->Stop();
3285 m_lastOnSame
= false;
3288 if (event
.LeftDClick())
3290 if ( current
== m_lineLastClicked
)
3292 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3294 wxDataViewItem item
= GetItemByRow(current
);
3296 model
->GetValue( value
, item
, col
->GetModelColumn() );
3297 cell
->SetValue( value
);
3298 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3299 col
->GetWidth(), m_lineHeight
);
3300 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3302 wxWindow
*parent
= GetParent();
3303 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3305 le
.SetEventObject(parent
);
3306 le
.SetColumn(col
->GetModelColumn());
3307 le
.SetDataViewColumn(col
);
3308 le
.SetModel(GetOwner()->GetModel());
3310 parent
->GetEventHandler()->ProcessEvent(le
);
3316 // The first click was on another item, so don't interpret this as
3317 // a double click, but as a simple click instead
3324 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3326 // select single line
3327 SelectAllRows( false );
3328 SelectRow( m_lineSelectSingleOnUp
, true );
3331 //Process the event of user clicking the expander
3332 bool expander
= false;
3333 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3334 if( node
!=NULL
&& node
->HasChildren() )
3336 int indent
= node
->GetIndentLevel();
3337 indent
= GetOwner()->GetIndent()*indent
;
3338 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3339 if( rect
.Contains( x
, y
) )
3342 if( node
->IsOpen() )
3343 OnCollapsing(current
);
3345 OnExpanding( current
);
3349 //If the user click the expander, we do not do editing even if the column with expander are editable
3350 if (m_lastOnSame
&& !expander
)
3352 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3353 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3355 m_renameTimer
->Start( 100, true );
3359 m_lastOnSame
= false;
3360 m_lineSelectSingleOnUp
= (unsigned int)-1;
3364 // This is necessary, because after a DnD operation in
3365 // from and to ourself, the up event is swallowed by the
3366 // DnD code. So on next non-up event (which means here and
3367 // now) m_lineSelectSingleOnUp should be reset.
3368 m_lineSelectSingleOnUp
= (unsigned int)-1;
3371 if (event
.RightDown())
3373 m_lineBeforeLastClicked
= m_lineLastClicked
;
3374 m_lineLastClicked
= current
;
3376 // If the item is already selected, do not update the selection.
3377 // Multi-selections should not be cleared if a selected item is clicked.
3378 if (!IsRowSelected(current
))
3380 SelectAllRows(false);
3381 ChangeCurrentRow(current
);
3382 SelectRow(m_currentRow
,true);
3385 // notify cell about right click
3388 // Allow generation of context menu event
3391 else if (event
.MiddleDown())
3393 // notify cell about middle click
3396 if (event
.LeftDown() || forceClick
)
3402 m_lineBeforeLastClicked
= m_lineLastClicked
;
3403 m_lineLastClicked
= current
;
3405 unsigned int oldCurrentRow
= m_currentRow
;
3406 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3408 bool cmdModifierDown
= event
.CmdDown();
3409 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3411 if ( IsSingleSel() || !IsRowSelected(current
) )
3413 SelectAllRows( false );
3415 ChangeCurrentRow(current
);
3417 SelectRow(m_currentRow
,true);
3419 else // multi sel & current is highlighted & no mod keys
3421 m_lineSelectSingleOnUp
= current
;
3422 ChangeCurrentRow(current
); // change focus
3425 else // multi sel & either ctrl or shift is down
3427 if (cmdModifierDown
)
3429 ChangeCurrentRow(current
);
3431 ReverseRowSelection(m_currentRow
);
3433 else if (event
.ShiftDown())
3435 ChangeCurrentRow(current
);
3437 unsigned int lineFrom
= oldCurrentRow
,
3440 if ( lineTo
< lineFrom
)
3443 lineFrom
= m_currentRow
;
3446 SelectRows(lineFrom
, lineTo
, true);
3448 else // !ctrl, !shift
3450 // test in the enclosing if should make it impossible
3451 wxFAIL_MSG( _T("how did we get here?") );
3455 if (m_currentRow
!= oldCurrentRow
)
3456 RefreshRow( oldCurrentRow
);
3458 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3460 // Update selection here...
3463 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3464 (current
== oldCurrentRow
)) && oldWasSelected
;
3468 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3472 if (HasCurrentRow())
3478 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3482 if (HasCurrentRow())
3488 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3490 if( m_selection
.GetCount() != 1 )
3491 return wxDataViewItem();
3493 return GetItemByRow( m_selection
.Item(0));
3496 //-----------------------------------------------------------------------------
3498 //-----------------------------------------------------------------------------
3500 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3502 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3503 EVT_SIZE(wxDataViewCtrl::OnSize
)
3506 wxDataViewCtrl::~wxDataViewCtrl()
3509 GetModel()->RemoveNotifier( m_notifier
);
3512 void wxDataViewCtrl::Init()
3517 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3518 const wxPoint
& pos
, const wxSize
& size
,
3519 long style
, const wxValidator
& validator
)
3521 if (!wxControl::Create( parent
, id
, pos
, size
,
3522 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3528 MacSetClipChildren( true ) ;
3531 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3533 if (HasFlag(wxDV_NO_HEADER
))
3534 m_headerArea
= NULL
;
3536 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3538 SetTargetWindow( m_clientArea
);
3540 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3542 sizer
->Add( m_headerArea
, 0, wxGROW
);
3543 sizer
->Add( m_clientArea
, 1, wxGROW
);
3550 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3554 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3557 // we need to process arrows ourselves for scrolling
3558 if ( nMsg
== WM_GETDLGCODE
)
3560 rc
|= DLGC_WANTARROWS
;
3568 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3570 // We need to override OnSize so that our scrolled
3571 // window a) does call Layout() to use sizers for
3572 // positioning the controls but b) does not query
3573 // the sizer for their size and use that for setting
3574 // the scrollable area as set that ourselves by
3575 // calling SetScrollbar() further down.
3582 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3584 if (!wxDataViewCtrlBase::AssociateModel( model
))
3587 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3589 model
->AddNotifier( m_notifier
);
3591 m_clientArea
->BuildTree(model
);
3593 m_clientArea
->UpdateDisplay();
3598 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3600 if (!wxDataViewCtrlBase::AppendColumn(col
))
3607 void wxDataViewCtrl::OnColumnChange()
3610 m_headerArea
->UpdateDisplay();
3612 m_clientArea
->UpdateDisplay();
3615 void wxDataViewCtrl::DoSetExpanderColumn()
3617 m_clientArea
->UpdateDisplay();
3620 void wxDataViewCtrl::DoSetIndent()
3622 m_clientArea
->UpdateDisplay();
3625 //Selection code with wxDataViewItem as parameters
3626 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3628 return m_clientArea
->GetSelection();
3631 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3634 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3635 int len
= selection
.GetCount();
3636 for( int i
= 0; i
< len
; i
++)
3638 unsigned int row
= selection
[i
];
3639 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3644 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3646 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3647 int len
= sel
.GetCount();
3648 for( int i
= 0; i
< len
; i
++ )
3650 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3652 selection
.Add( static_cast<unsigned int>(row
) );
3654 m_clientArea
->SetSelections( selection
);
3657 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3659 int row
= m_clientArea
->GetRowByItem( item
);
3661 m_clientArea
->SelectRow(row
, true);
3664 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3666 int row
= m_clientArea
->GetRowByItem( item
);
3668 m_clientArea
->SelectRow(row
, false);
3671 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3673 int row
= m_clientArea
->GetRowByItem( item
);
3676 return m_clientArea
->IsRowSelected(row
);
3681 //Selection code with row number as parameter
3682 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3685 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3686 int len
= selection
.GetCount();
3687 for( int i
= 0; i
< len
; i
++)
3689 unsigned int row
= selection
[i
];
3695 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3697 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3698 int len
= sel
.GetCount();
3699 for( int i
= 0; i
< len
; i
++ )
3703 selection
.Add( static_cast<unsigned int>(row
) );
3705 m_clientArea
->SetSelections( selection
);
3708 void wxDataViewCtrl::Select( int row
)
3711 m_clientArea
->SelectRow( row
, true );
3714 void wxDataViewCtrl::Unselect( int row
)
3717 m_clientArea
->SelectRow(row
, false);
3720 bool wxDataViewCtrl::IsSelected( int row
) const
3723 return m_clientArea
->IsRowSelected(row
);
3727 void wxDataViewCtrl::SelectRange( int from
, int to
)
3730 for( int i
= from
; i
< to
; i
++ )
3732 m_clientArea
->Select(sel
);
3735 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3737 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3738 for( int i
= from
; i
< to
; i
++ )
3739 if( sel
.Index( i
) != wxNOT_FOUND
)
3741 m_clientArea
->SetSelections(sel
);
3744 void wxDataViewCtrl::SelectAll()
3746 m_clientArea
->SelectAllRows(true);
3749 void wxDataViewCtrl::UnselectAll()
3751 m_clientArea
->SelectAllRows(false);
3754 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3758 if( row
> m_clientArea
->GetRowCount() )
3759 row
= m_clientArea
->GetRowCount();
3761 int first
= m_clientArea
->GetFirstVisibleRow();
3762 int last
= m_clientArea
->GetLastVisibleRow();
3764 m_clientArea
->ScrollTo( row
, column
);
3765 else if( row
> last
)
3766 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3768 m_clientArea
->ScrollTo( first
, column
);
3771 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3773 int row
= m_clientArea
->GetRowByItem(item
);
3776 if( column
== NULL
)
3777 return EnsureVisible(row
, -1);
3781 int len
= GetColumnCount();
3782 for( int i
= 0; i
< len
; i
++ )
3783 if( GetColumn(i
) == column
)
3788 EnsureVisible( row
, col
);
3794 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
3796 m_clientArea
->HitTest(point
, item
, column
);
3799 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
3801 return m_clientArea
->GetItemRect(item
, column
);
3804 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
3806 return m_clientArea
->GetItemByRow( row
);
3809 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
3811 return m_clientArea
->GetRowByItem( item
);
3815 // !wxUSE_GENERICDATAVIEWCTRL
3818 // wxUSE_DATAVIEWCTRL