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
)
278 m_hasChildren
= false;
282 ~wxDataViewTreeNode()
286 wxDataViewTreeNode
* GetParent() { return m_parent
; }
287 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
288 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
289 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
291 void AddNode( wxDataViewTreeNode
* node
)
293 m_leaves
.Add( node
->GetItem().GetID() );
295 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
298 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
300 void AddLeaf( void * leaf
)
302 m_leaves
.Add( leaf
);
304 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
307 wxDataViewItem
& GetItem() { return m_item
; }
308 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
310 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
311 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
315 wxDataViewTreeNode
* node
= this;
316 while( node
->GetParent()->GetParent() != NULL
)
318 node
= node
->GetParent();
331 int len
= m_nodes
.GetCount();
333 for ( int i
= 0 ;i
< len
; i
++)
334 sum
+= m_nodes
[i
]->GetSubTreeCount();
336 sum
+= m_leaves
.GetCount();
339 ChangeSubTreeCount(-sum
);
345 ChangeSubTreeCount(sum
);
348 bool HasChildren() { return m_hasChildren
; }
349 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
351 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
352 int GetSubTreeCount() { return m_subTreeCount
; }
353 void ChangeSubTreeCount( int num
)
357 m_subTreeCount
+= num
;
359 m_parent
->ChangeSubTreeCount(num
);
366 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
367 int len
= m_nodes
.GetCount();
368 for (int i
= 0; i
< len
; i
++)
369 m_nodes
[i
]->Resort();
370 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
375 wxDataViewTreeNode
*m_parent
;
376 wxDataViewTreeNodes m_nodes
;
377 wxDataViewTreeLeaves m_leaves
;
378 wxDataViewItem m_item
;
384 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
386 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
389 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
391 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
396 //-----------------------------------------------------------------------------
397 // wxDataViewMainWindow
398 //-----------------------------------------------------------------------------
400 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
402 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
403 WX_DEFINE_LIST(ItemList
);
405 class wxDataViewMainWindow
: public wxWindow
408 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
410 const wxPoint
&pos
= wxDefaultPosition
,
411 const wxSize
&size
= wxDefaultSize
,
412 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
413 virtual ~wxDataViewMainWindow();
415 // notifications from wxDataViewModel
416 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
417 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
418 bool ItemChanged( const wxDataViewItem
&item
);
419 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
430 g_model
= GetOwner()->GetModel();
431 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
434 if (g_model
->HasDefaultCompare())
442 g_column
= col
->GetModelColumn();
443 g_asending
= col
->IsSortOrderAscending();
446 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
447 wxDataViewCtrl
*GetOwner() { return m_owner
; }
448 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
450 void OnPaint( wxPaintEvent
&event
);
451 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
452 void OnChar( wxKeyEvent
&event
);
453 void OnMouse( wxMouseEvent
&event
);
454 void OnSetFocus( wxFocusEvent
&event
);
455 void OnKillFocus( wxFocusEvent
&event
);
457 void UpdateDisplay();
458 void RecalculateDisplay();
459 void OnInternalIdle();
461 void OnRenameTimer();
463 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
464 void ScrollTo( int rows
, int column
);
466 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
467 void ChangeCurrentRow( unsigned int row
);
469 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
470 bool IsEmpty() { return GetRowCount() == 0; }
472 int GetCountPerPage() const;
473 int GetEndOfLastCol() const;
474 unsigned int GetFirstVisibleRow() const;
475 //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
476 unsigned int GetLastVisibleRow();
477 unsigned int GetRowCount() ;
479 wxDataViewItem
GetSelection() const;
480 wxDataViewSelection
GetSelections(){ return m_selection
; }
481 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
482 void Select( const wxArrayInt
& aSelections
);
483 void SelectAllRows( bool on
);
484 void SelectRow( unsigned int row
, bool on
);
485 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
486 void ReverseRowSelection( unsigned int row
);
487 bool IsRowSelected( unsigned int row
);
488 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
490 void RefreshRow( unsigned int row
);
491 void RefreshRows( unsigned int from
, unsigned int to
);
492 void RefreshRowsAfter( unsigned int firstRow
);
494 // returns the colour to be used for drawing the rules
495 wxColour
GetRuleColour() const
497 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
500 wxRect
GetLineRect( unsigned int row
) const;
502 //Some useful functions for row and item mapping
503 wxDataViewItem
GetItemByRow( unsigned int row
) const;
504 int GetRowByItem( const wxDataViewItem
& item
);
506 //Methods for building the mapping tree
507 void BuildTree( wxDataViewModel
* model
);
509 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
510 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
512 void Expand( unsigned int row
) { OnExpanding( row
); }
513 void Collapse( unsigned int row
) { OnCollapsing( row
); }
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
;
558 //This is the tree node under the cursor
559 wxDataViewTreeNode
* m_underMouse
;
561 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
562 DECLARE_EVENT_TABLE()
565 // ---------------------------------------------------------
566 // wxGenericDataViewModelNotifier
567 // ---------------------------------------------------------
569 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
572 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
573 { m_mainWindow
= mainWindow
; }
575 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
576 { return m_mainWindow
->ItemAdded( parent
, item
); }
577 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
578 { return m_mainWindow
->ItemDeleted( parent
, item
); }
579 virtual bool ItemChanged( const wxDataViewItem
& item
)
580 { return m_mainWindow
->ItemChanged(item
); }
581 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
582 { return m_mainWindow
->ValueChanged( item
, col
); }
583 virtual bool Cleared()
584 { return m_mainWindow
->Cleared(); }
585 virtual void Resort()
586 { m_mainWindow
->Resort(); }
588 wxDataViewMainWindow
*m_mainWindow
;
591 // ---------------------------------------------------------
592 // wxDataViewRenderer
593 // ---------------------------------------------------------
595 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
597 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
598 wxDataViewCellMode mode
,
600 wxDataViewRendererBase( varianttype
, mode
, align
)
607 wxDataViewRenderer::~wxDataViewRenderer()
613 wxDC
*wxDataViewRenderer::GetDC()
617 if (GetOwner() == NULL
)
619 if (GetOwner()->GetOwner() == NULL
)
621 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
627 // ---------------------------------------------------------
628 // wxDataViewCustomRenderer
629 // ---------------------------------------------------------
631 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
633 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
634 wxDataViewCellMode mode
, int align
) :
635 wxDataViewRenderer( varianttype
, mode
, align
)
639 // ---------------------------------------------------------
640 // wxDataViewTextRenderer
641 // ---------------------------------------------------------
643 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
645 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
646 wxDataViewCellMode mode
, int align
) :
647 wxDataViewCustomRenderer( varianttype
, mode
, align
)
651 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
653 m_text
= value
.GetString();
658 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
663 bool wxDataViewTextRenderer::HasEditorCtrl()
668 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
669 wxRect labelRect
, const wxVariant
&value
)
671 return new wxTextCtrl( parent
, wxID_ANY
, value
,
672 wxPoint(labelRect
.x
,labelRect
.y
),
673 wxSize(labelRect
.width
,labelRect
.height
) );
676 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
678 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
679 value
= text
->GetValue();
683 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
685 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
686 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
687 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
688 view
->GetForegroundColour();
690 dc
->SetTextForeground(col
);
691 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
696 wxSize
wxDataViewTextRenderer::GetSize() const
698 const wxDataViewCtrl
*view
= GetView();
702 view
->GetTextExtent( m_text
, &x
, &y
);
703 return wxSize( x
, y
);
705 return wxSize(80,20);
708 // ---------------------------------------------------------
709 // wxDataViewBitmapRenderer
710 // ---------------------------------------------------------
712 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
714 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
715 wxDataViewCellMode mode
, int align
) :
716 wxDataViewCustomRenderer( varianttype
, mode
, align
)
720 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
722 if (value
.GetType() == wxT("wxBitmap"))
724 if (value
.GetType() == wxT("wxIcon"))
730 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
735 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
738 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
739 else if (m_icon
.Ok())
740 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
745 wxSize
wxDataViewBitmapRenderer::GetSize() const
748 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
749 else if (m_icon
.Ok())
750 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
752 return wxSize(16,16);
755 // ---------------------------------------------------------
756 // wxDataViewToggleRenderer
757 // ---------------------------------------------------------
759 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
761 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
762 wxDataViewCellMode mode
, int align
) :
763 wxDataViewCustomRenderer( varianttype
, mode
, align
)
768 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
770 m_toggle
= value
.GetBool();
775 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
780 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
782 // User wxRenderer here
785 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
787 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
792 flags
|= wxCONTROL_CHECKED
;
793 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
794 flags
|= wxCONTROL_DISABLED
;
796 wxRendererNative::Get().DrawCheckBox(
797 GetOwner()->GetOwner(),
805 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
806 wxDataViewModel
*model
,
807 const wxDataViewItem
& item
, unsigned int col
)
809 bool value
= !m_toggle
;
810 wxVariant variant
= value
;
811 model
->SetValue( variant
, item
, col
);
812 model
->ValueChanged( item
, col
);
816 wxSize
wxDataViewToggleRenderer::GetSize() const
818 return wxSize(20,20);
821 // ---------------------------------------------------------
822 // wxDataViewProgressRenderer
823 // ---------------------------------------------------------
825 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
827 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
828 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
829 wxDataViewCustomRenderer( varianttype
, mode
, align
)
835 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
839 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
841 m_value
= (long) value
;
843 if (m_value
< 0) m_value
= 0;
844 if (m_value
> 100) m_value
= 100;
849 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
851 value
= (long) m_value
;
855 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
857 double pct
= (double)m_value
/ 100.0;
859 bar
.width
= (int)(cell
.width
* pct
);
860 dc
->SetPen( *wxTRANSPARENT_PEN
);
861 dc
->SetBrush( *wxBLUE_BRUSH
);
862 dc
->DrawRectangle( bar
);
864 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
865 dc
->SetPen( *wxBLACK_PEN
);
866 dc
->DrawRectangle( cell
);
871 wxSize
wxDataViewProgressRenderer::GetSize() const
873 return wxSize(40,12);
876 // ---------------------------------------------------------
877 // wxDataViewDateRenderer
878 // ---------------------------------------------------------
880 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
882 #if wxUSE_DATE_RENDERER_POPUP
884 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
887 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
888 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
889 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
894 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
895 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
896 sizer
->Add( m_cal
, 1, wxGROW
);
901 void OnCalendar( wxCalendarEvent
&event
);
903 wxCalendarCtrl
*m_cal
;
904 wxDataViewModel
*m_model
;
906 const wxDataViewItem
& m_item
;
909 virtual void OnDismiss()
914 DECLARE_EVENT_TABLE()
917 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
918 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
921 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
923 wxDateTime date
= event
.GetDate();
924 wxVariant value
= date
;
925 m_model
->SetValue( value
, m_item
, m_col
);
926 m_model
->ValueChanged( m_item
, m_col
);
930 #endif // wxUSE_DATE_RENDERER_POPUP
932 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
934 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
935 wxDataViewCellMode mode
, int align
) :
936 wxDataViewCustomRenderer( varianttype
, mode
, align
)
940 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
942 m_date
= value
.GetDateTime();
947 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
953 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
955 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
956 wxString tmp
= m_date
.FormatDate();
957 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
962 wxSize
wxDataViewDateRenderer::GetSize() const
964 const wxDataViewCtrl
* view
= GetView();
965 wxString tmp
= m_date
.FormatDate();
967 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
968 return wxSize(x
,y
+d
);
971 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
972 const wxDataViewItem
& item
, unsigned int col
)
975 model
->GetValue( variant
, item
, col
);
976 wxDateTime value
= variant
.GetDateTime();
978 #if wxUSE_DATE_RENDERER_POPUP
979 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
980 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
981 wxPoint pos
= wxGetMousePosition();
984 popup
->Popup( popup
->m_cal
);
985 #else // !wxUSE_DATE_RENDERER_POPUP
986 wxMessageBox(value
.Format());
987 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
991 // ---------------------------------------------------------
992 // wxDataViewIconTextRenderer
993 // ---------------------------------------------------------
995 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
997 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
998 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
999 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1002 SetAlignment(align
);
1005 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1009 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1015 bool wxDataViewIconTextRenderer::GetValue( wxVariant
&value
) const
1020 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1022 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
1024 dc
->SetFont( view
->GetFont() );
1026 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
1027 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
1028 view
->GetForegroundColour();
1030 dc
->SetTextForeground(col
);
1032 const wxIcon
&icon
= m_value
.GetIcon();
1035 dc
->DrawIcon( icon
, cell
.x
, cell
.y
); // TODO centre
1036 cell
.x
+= icon
.GetWidth()+4;
1039 dc
->DrawText( m_value
.GetText(), cell
.x
, cell
.y
);
1044 wxSize
wxDataViewIconTextRenderer::GetSize() const
1046 return wxSize(80,16); // TODO
1049 wxControl
* wxDataViewIconTextRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1054 bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1059 // ---------------------------------------------------------
1061 // ---------------------------------------------------------
1063 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1065 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1066 unsigned int model_column
,
1067 int width
, wxAlignment align
, int flags
) :
1068 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1070 SetAlignment(align
);
1074 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1077 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1078 unsigned int model_column
,
1079 int width
, wxAlignment align
, int flags
) :
1080 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1082 SetAlignment(align
);
1085 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1088 wxDataViewColumn::~wxDataViewColumn()
1092 void wxDataViewColumn::Init( int width
)
1095 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1099 void wxDataViewColumn::SetResizeable( bool resizeable
)
1102 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1104 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1107 void wxDataViewColumn::SetHidden( bool hidden
)
1110 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1112 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1114 // tell our owner to e.g. update its scrollbars:
1116 GetOwner()->OnColumnChange();
1119 void wxDataViewColumn::SetSortable( bool sortable
)
1122 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1124 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1126 // Update header button
1128 GetOwner()->OnColumnChange();
1131 void wxDataViewColumn::SetSortOrder( bool ascending
)
1133 m_ascending
= ascending
;
1135 // Update header button
1137 GetOwner()->OnColumnChange();
1140 bool wxDataViewColumn::IsSortOrderAscending() const
1145 void wxDataViewColumn::SetInternalWidth( int width
)
1149 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1150 if (m_owner
&& m_owner
->m_clientArea
)
1151 m_owner
->m_clientArea
->RecalculateDisplay();
1154 void wxDataViewColumn::SetWidth( int width
)
1156 m_owner
->m_headerArea
->UpdateDisplay();
1158 SetInternalWidth(width
);
1162 //-----------------------------------------------------------------------------
1163 // wxDataViewHeaderWindowBase
1164 //-----------------------------------------------------------------------------
1166 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1168 wxWindow
*parent
= GetParent();
1169 wxDataViewEvent
le(type
, parent
->GetId());
1171 le
.SetEventObject(parent
);
1173 le
.SetDataViewColumn(GetColumn(n
));
1174 le
.SetModel(GetOwner()->GetModel());
1176 // for events created by wxDataViewHeaderWindow the
1177 // row / value fields are not valid
1179 parent
->GetEventHandler()->ProcessEvent(le
);
1182 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1184 // implemented in msw/listctrl.cpp:
1185 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1187 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1189 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1190 const wxPoint
&pos
, const wxSize
&size
,
1191 const wxString
&name
)
1195 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1198 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1199 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1200 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1201 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1203 // create the native WC_HEADER window:
1204 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1205 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1206 m_hWnd
= CreateWindowEx(0,
1217 wxLogLastError(_T("CreateWindowEx"));
1221 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1222 // to call wxDataViewHeaderWindow::MSWOnNotify
1223 SubclassWin(m_hWnd
);
1225 // the following is required to get the default win's font for
1226 // header windows and must be done befor sending the HDM_LAYOUT msg
1233 // Retrieve the bounding rectangle of the parent window's
1234 // client area, and then request size and position values
1235 // from the header control.
1236 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1238 hdl
.prc
= &rcParent
;
1240 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1242 wxLogLastError(_T("SendMessage"));
1246 // Set the size, position, and visibility of the header control.
1247 SetWindowPos((HWND
)m_hWnd
,
1251 wp
.flags
| SWP_SHOWWINDOW
);
1253 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1254 // a wxBoxSizer and in order to avoid super-big header windows,
1255 // we need to set our height as fixed
1256 SetMinSize(wxSize(-1, wp
.cy
));
1257 SetMaxSize(wxSize(-1, wp
.cy
));
1262 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1267 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1269 // remove old columns
1270 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1271 Header_DeleteItem((HWND
)m_hWnd
, 0);
1273 // add the updated array of columns to the header control
1274 unsigned int cols
= GetOwner()->GetColumnCount();
1275 unsigned int added
= 0;
1276 wxDataViewModel
* model
= GetOwner()->GetModel();
1277 for (unsigned int i
= 0; i
< cols
; i
++)
1279 wxDataViewColumn
*col
= GetColumn( i
);
1280 if (col
->IsHidden())
1281 continue; // don't add it!
1284 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1285 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1286 hdi
.cxy
= col
->GetWidth();
1287 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1288 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1289 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1292 if(model
&& m_owner
->GetSortingColumn() == col
)
1294 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1295 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1296 //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
1300 // lParam is reserved for application's use:
1301 // we store there the column index to use it later in MSWOnNotify
1302 // (since columns may have been hidden)
1303 hdi
.lParam
= (LPARAM
)i
;
1305 // the native wxMSW implementation of the header window
1306 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1307 // on the right: to correct this effect we make the column
1308 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1310 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1312 switch (col
->GetAlignment())
1315 hdi
.fmt
|= HDF_LEFT
;
1317 case wxALIGN_CENTER
:
1318 case wxALIGN_CENTER_HORIZONTAL
:
1319 hdi
.fmt
|= HDF_CENTER
;
1322 hdi
.fmt
|= HDF_RIGHT
;
1326 // such alignment is not allowed for the column header!
1330 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1331 (WPARAM
)added
, (LPARAM
)&hdi
);
1336 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1340 // NOTE: we don't just return nmHDR->iItem because when there are
1341 // hidden columns, nmHDR->iItem may be different from
1342 // nmHDR->pitem->lParam
1344 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1346 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1351 item
.mask
= HDI_LPARAM
;
1352 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1354 return (unsigned int)item
.lParam
;
1357 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1359 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1361 // is it a message from the header?
1362 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1363 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1365 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1366 switch ( nmhdr
->code
)
1368 case HDN_BEGINTRACK
:
1369 // user has started to resize a column:
1370 // do we need to veto it?
1371 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1379 // user has started to reorder a column
1382 case HDN_ITEMCHANGING
:
1383 if (nmHDR
->pitem
!= NULL
&&
1384 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1386 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1387 if (nmHDR
->pitem
->cxy
< minWidth
)
1389 // do not allow the user to resize this column under
1390 // its minimal width:
1396 case HDN_ITEMCHANGED
: // user is resizing a column
1397 case HDN_ENDTRACK
: // user has finished resizing a column
1398 case HDN_ENDDRAG
: // user has finished reordering a column
1400 // update the width of the modified column:
1401 if (nmHDR
->pitem
!= NULL
&&
1402 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1404 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1405 unsigned int w
= nmHDR
->pitem
->cxy
;
1406 wxDataViewColumn
*col
= GetColumn(idx
);
1408 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1409 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1410 w
-= COLUMN_WIDTH_OFFSET
;
1412 if (w
>= (unsigned)col
->GetMinWidth())
1413 col
->SetInternalWidth(w
);
1419 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1420 wxDataViewModel
* model
= GetOwner()->GetModel();
1422 if(nmHDR
->iButton
== 0)
1424 wxDataViewColumn
*col
= GetColumn(idx
);
1425 if(col
->IsSortable())
1427 if(model
&& m_owner
->GetSortingColumn() == col
)
1429 bool order
= col
->IsSortOrderAscending();
1430 col
->SetSortOrder(!order
);
1434 m_owner
->SetSortingColumn(col
);
1442 wxEventType evt
= nmHDR
->iButton
== 0 ?
1443 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1444 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1445 SendEvent(evt
, idx
);
1451 // NOTE: for some reason (i.e. for a bug in Windows)
1452 // the HDN_ITEMCLICK notification is not sent on
1453 // right clicks, so we need to handle NM_RCLICK
1456 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1457 if (column
!= wxNOT_FOUND
)
1460 item
.mask
= HDI_LPARAM
;
1461 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1463 // 'idx' may be different from 'column' if there are
1464 // hidden columns...
1465 unsigned int idx
= (unsigned int)item
.lParam
;
1466 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1472 case HDN_GETDISPINFOW
:
1473 // see wxListCtrl::MSWOnNotify for more info!
1476 case HDN_ITEMDBLCLICK
:
1478 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1479 int w
= GetOwner()->GetBestColumnWidth(idx
);
1481 // update the native control:
1483 ZeroMemory(&hd
, sizeof(hd
));
1484 hd
.mask
= HDI_WIDTH
;
1486 Header_SetItem(GetHwnd(),
1487 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1490 // update the wxDataViewColumn class:
1491 GetColumn(idx
)->SetInternalWidth(w
);
1496 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1502 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1503 const wxRect
*WXUNUSED(rect
))
1505 wxSize ourSz
= GetClientSize();
1506 wxSize ownerSz
= m_owner
->GetClientSize();
1508 // where should the (logical) origin of this window be placed?
1510 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1512 // put this window on top of our parent and
1513 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1514 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1518 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1519 int WXUNUSED(w
), int WXUNUSED(h
),
1522 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1523 // the wxDataViewCtrl window gets resized: the following dummy call
1524 // to ScrollWindow() is required in order to get this header window
1525 // correctly repainted when it's (horizontally) scrolled:
1530 #else // !defined(__WXMSW__)
1532 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1533 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1534 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1535 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1536 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1539 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1540 const wxPoint
&pos
, const wxSize
&size
,
1541 const wxString
&name
)
1545 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1548 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1549 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1550 SetOwnForegroundColour( attr
.colFg
);
1551 SetOwnBackgroundColour( attr
.colBg
);
1553 SetOwnFont( attr
.font
);
1555 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1556 // a wxBoxSizer and in order to avoid super-big header windows,
1557 // we need to set our height as fixed
1558 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1559 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1564 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1567 GetClientSize( &w
, &h
);
1569 wxAutoBufferedPaintDC
dc( this );
1571 dc
.SetBackground(GetBackgroundColour());
1575 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1578 m_owner
->GetViewStart( &x
, NULL
);
1580 // account for the horz scrollbar offset
1581 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1583 dc
.SetFont( GetFont() );
1585 unsigned int cols
= GetOwner()->GetColumnCount();
1588 for (i
= 0; i
< cols
; i
++)
1590 wxDataViewColumn
*col
= GetColumn( i
);
1591 if (col
->IsHidden())
1592 continue; // skip it!
1594 int cw
= col
->GetWidth();
1597 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1598 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1600 if (col
->IsSortOrderAscending())
1601 sortArrow
= wxHDR_SORT_ICON_UP
;
1603 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1607 if (m_parent
->IsEnabled())
1609 if ((int) i
== m_hover
)
1610 state
= wxCONTROL_CURRENT
;
1614 state
= (int) wxCONTROL_DISABLED
;
1617 wxRendererNative::Get().DrawHeaderButton
1621 wxRect(xpos
, 0, cw
, ch
-1),
1626 // align as required the column title:
1628 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1629 switch (col
->GetAlignment())
1632 x
+= HEADER_HORIZ_BORDER
;
1635 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1638 case wxALIGN_CENTER
:
1639 case wxALIGN_CENTER_HORIZONTAL
:
1640 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1644 // always center the title vertically:
1645 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1647 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1649 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1650 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1651 dc
.DrawText( col
->GetTitle(), x
, y
);
1652 dc
.DestroyClippingRegion();
1658 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1660 GetParent()->SetFocus();
1664 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1666 // we want to work with logical coords
1668 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1669 int y
= event
.GetY();
1673 // we don't draw the line beyond our window,
1674 // but we allow dragging it there
1676 GetClientSize( &w
, NULL
);
1677 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1680 if (event
.ButtonUp())
1682 m_isDragging
= false;
1688 m_currentX
= wxMax(m_minX
+ 7, x
);
1692 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1694 GetOwner()->Refresh();
1698 else // not dragging
1701 m_column
= wxNOT_FOUND
;
1703 bool hit_border
= false;
1705 // end of the current column
1708 // find the column where this event occured
1709 int countCol
= m_owner
->GetColumnCount();
1710 for (int column
= 0; column
< countCol
; column
++)
1712 wxDataViewColumn
*p
= GetColumn(column
);
1715 continue; // skip if not shown
1717 xpos
+= p
->GetWidth();
1719 if ((abs(x
-xpos
) < 3) && (y
< 22))
1727 // inside the column
1734 int old_hover
= m_hover
;
1736 if (event
.Leaving())
1737 m_hover
= wxNOT_FOUND
;
1738 if (old_hover
!= m_hover
)
1741 if (m_column
== wxNOT_FOUND
)
1744 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1745 if (event
.LeftDClick() && resizeable
)
1747 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1750 else if (event
.LeftDown() || event
.RightUp())
1752 if (hit_border
&& event
.LeftDown() && resizeable
)
1754 m_isDragging
= true;
1758 else // click on a column
1760 wxDataViewModel
* model
= GetOwner()->GetModel();
1761 wxEventType evt
= event
.LeftDown() ?
1762 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1763 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1764 SendEvent(evt
, m_column
);
1766 //Left click the header
1767 if(event
.LeftDown())
1769 wxDataViewColumn
*col
= GetColumn(m_column
);
1770 if(col
->IsSortable())
1772 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1773 if(model
&& sortCol
== col
)
1775 bool order
= col
->IsSortOrderAscending();
1776 col
->SetSortOrder(!order
);
1780 m_owner
->SetSortingColumn(col
);
1786 //Send the column sorted event
1787 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1791 else if (event
.Moving())
1793 if (hit_border
&& resizeable
)
1794 m_currentCursor
= m_resizeCursor
;
1796 m_currentCursor
= wxSTANDARD_CURSOR
;
1798 SetCursor(*m_currentCursor
);
1803 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1804 void wxGenericDataViewHeaderWindow::DrawCurrent()
1807 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1809 int x1
= m_currentX
;
1811 ClientToScreen (&x1
, &y1
);
1813 int x2
= m_currentX
-1;
1815 ++x2
; // but why ????
1818 m_owner
->GetClientSize( NULL
, &y2
);
1819 m_owner
->ClientToScreen( &x2
, &y2
);
1822 dc
.SetLogicalFunction(wxINVERT
);
1823 dc
.SetPen(m_penCurrent
);
1824 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1826 dc
.DrawLine(x1
, y1
, x2
, y2
);
1830 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1834 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1835 m_owner
->GetViewStart( &x
, NULL
);
1837 // shift the DC origin to match the position of the main window horizontal
1838 // scrollbar: this allows us to always use logical coords
1839 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1842 #endif // defined(__WXMSW__)
1844 //-----------------------------------------------------------------------------
1845 // wxDataViewRenameTimer
1846 //-----------------------------------------------------------------------------
1848 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1853 void wxDataViewRenameTimer::Notify()
1855 m_owner
->OnRenameTimer();
1858 //-----------------------------------------------------------------------------
1859 // wxDataViewMainWindow
1860 //-----------------------------------------------------------------------------
1862 //The tree building helper, declared firstly
1863 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1865 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1867 if (row1
> row2
) return 1;
1868 if (row1
== row2
) return 0;
1873 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1875 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1876 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1877 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1878 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1879 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1880 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1883 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1884 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1885 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1886 m_selection( wxDataViewSelectionCmp
)
1891 m_lastOnSame
= false;
1892 m_renameTimer
= new wxDataViewRenameTimer( this );
1894 // TODO: user better initial values/nothing selected
1895 m_currentCol
= NULL
;
1898 // TODO: we need to calculate this smartly
1907 m_dragStart
= wxPoint(0,0);
1908 m_lineLastClicked
= (unsigned int) -1;
1909 m_lineBeforeLastClicked
= (unsigned int) -1;
1910 m_lineSelectSingleOnUp
= (unsigned int) -1;
1914 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1915 SetBackgroundColour( *wxWHITE
);
1917 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1919 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1920 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1921 //Some new added code to deal with the tree structure
1922 m_root
= new wxDataViewTreeNode( NULL
);
1923 m_root
->SetHasChildren(true);
1925 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1927 m_underMouse
= NULL
;
1931 wxDataViewMainWindow::~wxDataViewMainWindow()
1934 delete m_renameTimer
;
1937 void wxDataViewMainWindow::OnRenameTimer()
1939 // We have to call this here because changes may just have
1940 // been made and no screen update taken place.
1945 unsigned int cols
= GetOwner()->GetColumnCount();
1947 for (i
= 0; i
< cols
; i
++)
1949 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1951 continue; // skip it!
1953 if (c
== m_currentCol
)
1955 xpos
+= c
->GetWidth();
1957 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1958 m_currentCol
->GetWidth(), m_lineHeight
);
1960 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1961 &labelRect
.x
, &labelRect
.y
);
1963 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1964 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1968 //------------------------------------------------------------------
1969 // Helper class for do operation on the tree node
1970 //------------------------------------------------------------------
1977 //The return value control how the tree-walker tranverse the tree
1978 // 0: Job done, stop tranverse and return
1979 // 1: Ignore the current node's subtree and continue
1980 // 2: Job not done, continue
1981 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1982 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1983 virtual int operator() ( void * n
) = 0;
1986 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1991 switch( func( node
) )
2002 wxDataViewTreeNodes nodes
= node
->GetNodes();
2003 wxDataViewTreeLeaves leaves
= node
->GetChildren();
2005 int len_nodes
= nodes
.GetCount();
2006 int len
= leaves
.GetCount();
2007 int i
= 0, nodes_i
= 0;
2009 for( ; i
< len
; i
++ )
2011 void * n
= leaves
[i
];
2012 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
2014 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2017 if( Walker( nd
, func
) )
2036 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2040 wxDataViewTreeNode
* node
;
2041 node
= FindNode(parent
);
2046 node
->SetHasChildren( true );
2048 if( g_model
->IsContainer( item
) )
2050 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2051 newnode
->SetItem(item
);
2052 newnode
->SetHasChildren( true );
2053 node
->AddNode( newnode
);
2056 node
->AddLeaf( item
.GetID() );
2058 node
->ChangeSubTreeCount(1);
2066 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2068 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2069 const wxDataViewItem
& item
)
2071 wxDataViewTreeNode
* node
= FindNode(parent
);
2073 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2074 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2077 node
->GetChildren().Remove( item
.GetID() );
2078 //Manuplate selection
2079 if( m_selection
.GetCount() > 1 )
2081 m_selection
.Empty();
2083 bool isContainer
= false;
2084 wxDataViewTreeNodes nds
= node
->GetNodes();
2085 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2087 if (nds
[i
]->GetItem() == item
)
2095 wxDataViewTreeNode
* n
= NULL
;
2096 wxDataViewTreeNodes nodes
= node
->GetNodes();
2097 int len
= nodes
.GetCount();
2098 for( int i
= 0 ; i
< len
; i
++)
2100 if( nodes
[i
]->GetItem() == item
)
2107 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2109 node
->GetNodes().Remove( n
);
2110 sub
-= n
->GetSubTreeCount();
2111 DestroyTreeHelper(n
);
2113 //Make the row number invalid and get a new valid one when user call GetRowCount
2115 node
->ChangeSubTreeCount(sub
);
2116 if( node
->GetChildrenNumber() == 0)
2118 node
->GetParent()->GetNodes().Remove( node
);
2122 //Change the current row to the last row if the current exceed the max row number
2123 if( m_currentRow
> GetRowCount() )
2124 m_currentRow
= m_count
- 1;
2131 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2137 wxWindow
*parent
= GetParent();
2138 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2139 le
.SetEventObject(parent
);
2140 le
.SetModel(GetOwner()->GetModel());
2142 parent
->GetEventHandler()->ProcessEvent(le
);
2147 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2149 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2150 /*#define MAX_VIRTUAL_WIDTH 100000
2152 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2153 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2154 Refresh( true, &rect );
2162 wxWindow
*parent
= GetParent();
2163 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2164 le
.SetEventObject(parent
);
2165 le
.SetModel(GetOwner()->GetModel());
2168 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2169 parent
->GetEventHandler()->ProcessEvent(le
);
2174 bool wxDataViewMainWindow::Cleared()
2184 void wxDataViewMainWindow::UpdateDisplay()
2189 void wxDataViewMainWindow::OnInternalIdle()
2191 wxWindow::OnInternalIdle();
2195 RecalculateDisplay();
2200 void wxDataViewMainWindow::RecalculateDisplay()
2202 wxDataViewModel
*model
= GetOwner()->GetModel();
2209 int width
= GetEndOfLastCol();
2210 int height
= GetRowCount() * m_lineHeight
;
2212 SetVirtualSize( width
, height
);
2213 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2218 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2220 wxWindow::ScrollWindow( dx
, dy
, rect
);
2222 if (GetOwner()->m_headerArea
)
2223 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2226 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2229 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2230 int sy
= rows
*m_lineHeight
/y
;
2234 wxRect rect
= GetClientRect();
2236 int x_start
= 0, x_end
= 0, w
= 0;
2238 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2239 for (x_start
= 0; colnum
< column
; colnum
++)
2241 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2242 if (col
->IsHidden())
2243 continue; // skip it!
2245 w
= col
->GetWidth();
2249 x_end
= x_start
+ w
;
2250 xe
= xx
+ rect
.width
;
2253 sx
= ( xx
+ x_end
- xe
)/x
;
2260 m_owner
->Scroll( sx
, sy
);
2263 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2265 wxDataViewModel
*model
= GetOwner()->GetModel();
2266 wxAutoBufferedPaintDC
dc( this );
2269 dc
.SetBackground(GetBackgroundColour());
2271 GetOwner()->PrepareDC( dc
);
2272 dc
.SetFont( GetFont() );
2274 wxRect update
= GetUpdateRegion().GetBox();
2275 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2277 // compute which items needs to be redrawn
2278 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2279 unsigned int item_count
=
2280 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2281 (int)(GetRowCount( )- item_start
) );
2282 unsigned int item_last
= item_start
+ item_count
;
2284 // compute which columns needs to be redrawn
2285 unsigned int cols
= GetOwner()->GetColumnCount();
2286 unsigned int col_start
= 0;
2287 unsigned int x_start
= 0;
2288 for (x_start
= 0; col_start
< cols
; col_start
++)
2290 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2291 if (col
->IsHidden())
2292 continue; // skip it!
2294 unsigned int w
= col
->GetWidth();
2295 if (x_start
+w
>= (unsigned int)update
.x
)
2301 unsigned int col_last
= col_start
;
2302 unsigned int x_last
= x_start
;
2303 for (; col_last
< cols
; col_last
++)
2305 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2306 if (col
->IsHidden())
2307 continue; // skip it!
2309 if (x_last
> (unsigned int)update
.GetRight())
2312 x_last
+= col
->GetWidth();
2315 // Draw horizontal rules if required
2316 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2318 dc
.SetPen(m_penRule
);
2319 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2321 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2323 int y
= i
* m_lineHeight
;
2324 dc
.DrawLine(x_start
, y
, x_last
, y
);
2328 // Draw vertical rules if required
2329 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2331 dc
.SetPen(m_penRule
);
2332 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2335 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2337 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2338 if (col
->IsHidden())
2339 continue; // skip it
2341 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2342 x
, item_last
* m_lineHeight
);
2344 x
+= col
->GetWidth();
2347 // Draw last vertical rule
2348 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2349 x
, item_last
* m_lineHeight
);
2352 // redraw the background for the items which are selected/current
2353 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2355 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2356 if (selected
|| item
== m_currentRow
)
2358 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2359 if (item
== m_currentRow
)
2360 flags
|= wxCONTROL_CURRENT
;
2362 flags
|= wxCONTROL_FOCUSED
;
2364 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2365 wxRendererNative::Get().DrawItemSelectionRect
2375 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2378 // TODO: last column for RTL support
2379 expander
= GetOwner()->GetColumn( 0 );
2380 GetOwner()->SetExpanderColumn(expander
);
2383 // redraw all cells for all rows which must be repainted and for all columns
2385 cell_rect
.x
= x_start
;
2386 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2387 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2389 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2390 wxDataViewRenderer
*cell
= col
->GetRenderer();
2391 cell_rect
.width
= col
->GetWidth();
2393 if (col
->IsHidden())
2394 continue; // skipt it!
2397 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2399 // get the cell value and set it into the renderer
2401 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2405 wxDataViewItem dataitem
= node
->GetItem();
2407 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
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 wxDataViewItem item
= GetItemByRow(current
);
3455 bool ignore_other_columns
=
3456 ((GetOwner()->GetExpanderColumn() != col
) &&
3457 (model
->IsContainer(item
)) &&
3458 (!model
->HasContainerColumns(item
)));
3460 if (event
.LeftDClick())
3462 if ( current
== m_lineLastClicked
)
3464 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3467 model
->GetValue( value
, item
, col
->GetModelColumn() );
3468 cell
->SetValue( value
);
3469 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3470 col
->GetWidth(), m_lineHeight
);
3471 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3476 wxWindow
*parent
= GetParent();
3477 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3478 le
.SetItem( GetItemByRow(current
) );
3479 le
.SetEventObject(parent
);
3480 le
.SetModel(GetOwner()->GetModel());
3482 parent
->GetEventHandler()->ProcessEvent(le
);
3488 // The first click was on another item, so don't interpret this as
3489 // a double click, but as a simple click instead
3496 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3498 // select single line
3499 SelectAllRows( false );
3500 SelectRow( m_lineSelectSingleOnUp
, true );
3503 //Process the event of user clicking the expander
3504 bool expander
= false;
3505 if (GetOwner()->GetExpanderColumn() == col
)
3507 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3508 if( node
!=NULL
&& node
->HasChildren() )
3510 int indent
= node
->GetIndentLevel();
3511 indent
= GetOwner()->GetIndent()*indent
;
3512 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3513 if( rect
.Contains( x
, y
) )
3516 if( node
->IsOpen() )
3517 OnCollapsing(current
);
3519 OnExpanding( current
);
3523 //If the user click the expander, we do not do editing even if the column with expander are editable
3524 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3526 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3527 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3529 m_renameTimer
->Start( 100, true );
3533 m_lastOnSame
= false;
3534 m_lineSelectSingleOnUp
= (unsigned int)-1;
3538 // This is necessary, because after a DnD operation in
3539 // from and to ourself, the up event is swallowed by the
3540 // DnD code. So on next non-up event (which means here and
3541 // now) m_lineSelectSingleOnUp should be reset.
3542 m_lineSelectSingleOnUp
= (unsigned int)-1;
3545 if (event
.RightDown())
3547 m_lineBeforeLastClicked
= m_lineLastClicked
;
3548 m_lineLastClicked
= current
;
3550 // If the item is already selected, do not update the selection.
3551 // Multi-selections should not be cleared if a selected item is clicked.
3552 if (!IsRowSelected(current
))
3554 SelectAllRows(false);
3555 ChangeCurrentRow(current
);
3556 SelectRow(m_currentRow
,true);
3557 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3560 // notify cell about right click
3563 // Allow generation of context menu event
3566 else if (event
.MiddleDown())
3568 // notify cell about middle click
3571 if (event
.LeftDown() || forceClick
)
3575 m_lineBeforeLastClicked
= m_lineLastClicked
;
3576 m_lineLastClicked
= current
;
3578 unsigned int oldCurrentRow
= m_currentRow
;
3579 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3581 bool cmdModifierDown
= event
.CmdDown();
3582 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3584 if ( IsSingleSel() || !IsRowSelected(current
) )
3586 SelectAllRows( false );
3587 ChangeCurrentRow(current
);
3588 SelectRow(m_currentRow
,true);
3589 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3591 else // multi sel & current is highlighted & no mod keys
3593 m_lineSelectSingleOnUp
= current
;
3594 ChangeCurrentRow(current
); // change focus
3597 else // multi sel & either ctrl or shift is down
3599 if (cmdModifierDown
)
3601 ChangeCurrentRow(current
);
3602 ReverseRowSelection(m_currentRow
);
3603 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3605 else if (event
.ShiftDown())
3607 ChangeCurrentRow(current
);
3609 unsigned int lineFrom
= oldCurrentRow
,
3612 if ( lineTo
< lineFrom
)
3615 lineFrom
= m_currentRow
;
3618 SelectRows(lineFrom
, lineTo
, true);
3619 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3621 else // !ctrl, !shift
3623 // test in the enclosing if should make it impossible
3624 wxFAIL_MSG( _T("how did we get here?") );
3628 if (m_currentRow
!= oldCurrentRow
)
3629 RefreshRow( oldCurrentRow
);
3631 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3633 // Update selection here...
3636 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3637 (current
== oldCurrentRow
)) && oldWasSelected
;
3641 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3645 if (HasCurrentRow())
3651 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3655 if (HasCurrentRow())
3661 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3663 if( m_selection
.GetCount() != 1 )
3664 return wxDataViewItem();
3666 return GetItemByRow( m_selection
.Item(0));
3669 //-----------------------------------------------------------------------------
3671 //-----------------------------------------------------------------------------
3672 WX_DEFINE_LIST(wxDataViewColumnList
);
3674 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3676 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3677 EVT_SIZE(wxDataViewCtrl::OnSize
)
3680 wxDataViewCtrl::~wxDataViewCtrl()
3683 GetModel()->RemoveNotifier( m_notifier
);
3686 void wxDataViewCtrl::Init()
3691 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3692 const wxPoint
& pos
, const wxSize
& size
,
3693 long style
, const wxValidator
& validator
)
3695 if (!wxControl::Create( parent
, id
, pos
, size
,
3696 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3699 SetInitialSize(size
);
3704 MacSetClipChildren( true ) ;
3707 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3709 if (HasFlag(wxDV_NO_HEADER
))
3710 m_headerArea
= NULL
;
3712 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3714 SetTargetWindow( m_clientArea
);
3716 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3718 sizer
->Add( m_headerArea
, 0, wxGROW
);
3719 sizer
->Add( m_clientArea
, 1, wxGROW
);
3726 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3730 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3733 // we need to process arrows ourselves for scrolling
3734 if ( nMsg
== WM_GETDLGCODE
)
3736 rc
|= DLGC_WANTARROWS
;
3744 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3746 // We need to override OnSize so that our scrolled
3747 // window a) does call Layout() to use sizers for
3748 // positioning the controls but b) does not query
3749 // the sizer for their size and use that for setting
3750 // the scrollable area as set that ourselves by
3751 // calling SetScrollbar() further down.
3758 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3760 if (!wxDataViewCtrlBase::AssociateModel( model
))
3763 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3765 model
->AddNotifier( m_notifier
);
3767 m_clientArea
->BuildTree(model
);
3769 m_clientArea
->UpdateDisplay();
3774 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3776 if (!wxDataViewCtrlBase::AppendColumn(col
))
3779 m_cols
.Append( col
);
3784 void wxDataViewCtrl::OnColumnChange()
3787 m_headerArea
->UpdateDisplay();
3789 m_clientArea
->UpdateDisplay();
3792 void wxDataViewCtrl::DoSetExpanderColumn()
3794 m_clientArea
->UpdateDisplay();
3797 void wxDataViewCtrl::DoSetIndent()
3799 m_clientArea
->UpdateDisplay();
3802 unsigned int wxDataViewCtrl::GetColumnCount() const
3804 return m_cols
.GetCount();
3807 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3809 wxDataViewColumnList::const_iterator iter
;
3811 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3816 if ((*iter
)->IsHidden())
3823 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3825 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3836 bool wxDataViewCtrl::ClearColumns()
3843 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3845 int ret
= 0, dead
= 0;
3846 int len
= GetColumnCount();
3847 for (int i
=0; i
<len
; i
++)
3849 wxDataViewColumn
* col
= GetColumn(i
);
3850 if (col
->IsHidden())
3852 ret
+= col
->GetWidth();
3855 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3862 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3867 //Selection code with wxDataViewItem as parameters
3868 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3870 return m_clientArea
->GetSelection();
3873 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3876 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3877 int len
= selection
.GetCount();
3878 for( int i
= 0; i
< len
; i
++)
3880 unsigned int row
= selection
[i
];
3881 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3886 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3888 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3889 int len
= sel
.GetCount();
3890 for( int i
= 0; i
< len
; i
++ )
3892 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3894 selection
.Add( static_cast<unsigned int>(row
) );
3896 m_clientArea
->SetSelections( selection
);
3899 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3901 int row
= m_clientArea
->GetRowByItem( item
);
3904 //Unselect all rows before select another in the single select mode
3905 if (m_clientArea
->IsSingleSel())
3906 m_clientArea
->SelectAllRows(false);
3907 m_clientArea
->SelectRow(row
, true);
3911 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3913 int row
= m_clientArea
->GetRowByItem( item
);
3915 m_clientArea
->SelectRow(row
, false);
3918 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3920 int row
= m_clientArea
->GetRowByItem( item
);
3923 return m_clientArea
->IsRowSelected(row
);
3928 //Selection code with row number as parameter
3929 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3932 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3933 int len
= selection
.GetCount();
3934 for( int i
= 0; i
< len
; i
++)
3936 unsigned int row
= selection
[i
];
3942 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3944 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3945 int len
= sel
.GetCount();
3946 for( int i
= 0; i
< len
; i
++ )
3950 selection
.Add( static_cast<unsigned int>(row
) );
3952 m_clientArea
->SetSelections( selection
);
3955 void wxDataViewCtrl::Select( int row
)
3959 if (m_clientArea
->IsSingleSel())
3960 m_clientArea
->SelectAllRows(false);
3961 m_clientArea
->SelectRow( row
, true );
3965 void wxDataViewCtrl::Unselect( int row
)
3968 m_clientArea
->SelectRow(row
, false);
3971 bool wxDataViewCtrl::IsSelected( int row
) const
3974 return m_clientArea
->IsRowSelected(row
);
3978 void wxDataViewCtrl::SelectRange( int from
, int to
)
3981 for( int i
= from
; i
< to
; i
++ )
3983 m_clientArea
->Select(sel
);
3986 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3988 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3989 for( int i
= from
; i
< to
; i
++ )
3990 if( sel
.Index( i
) != wxNOT_FOUND
)
3992 m_clientArea
->SetSelections(sel
);
3995 void wxDataViewCtrl::SelectAll()
3997 m_clientArea
->SelectAllRows(true);
4000 void wxDataViewCtrl::UnselectAll()
4002 m_clientArea
->SelectAllRows(false);
4005 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4009 if( row
> (int) m_clientArea
->GetRowCount() )
4010 row
= m_clientArea
->GetRowCount();
4012 int first
= m_clientArea
->GetFirstVisibleRow();
4013 int last
= m_clientArea
->GetLastVisibleRow();
4015 m_clientArea
->ScrollTo( row
, column
);
4016 else if( row
> last
)
4017 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4019 m_clientArea
->ScrollTo( first
, column
);
4022 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4024 int row
= m_clientArea
->GetRowByItem(item
);
4027 if( column
== NULL
)
4028 EnsureVisible(row
, -1);
4032 int len
= GetColumnCount();
4033 for( int i
= 0; i
< len
; i
++ )
4034 if( GetColumn(i
) == column
)
4039 EnsureVisible( row
, col
);
4045 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4047 m_clientArea
->HitTest(point
, item
, column
);
4050 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4052 return m_clientArea
->GetItemRect(item
, column
);
4055 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4057 return m_clientArea
->GetItemByRow( row
);
4060 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4062 return m_clientArea
->GetRowByItem( item
);
4065 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4067 int row
= m_clientArea
->GetRowByItem( item
);
4069 m_clientArea
->Expand(row
);
4072 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4074 int row
= m_clientArea
->GetRowByItem( item
);
4076 m_clientArea
->Collapse(row
);
4080 // !wxUSE_GENERICDATAVIEWCTRL
4083 // wxUSE_DATAVIEWCTRL