1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Otto Wyss
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the cell padding on the top/bottom
60 static const int PADDING_TOPBOTTOM
= 1;
62 // the expander space margin
63 static const int EXPANDER_MARGIN
= 4;
65 //-----------------------------------------------------------------------------
66 // wxDataViewHeaderWindow
67 //-----------------------------------------------------------------------------
69 #define USE_NATIVE_HEADER_WINDOW 1
71 // NB: for some reason, this class must be dllexport'ed or we get warnings from
73 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
76 wxDataViewHeaderWindowBase()
79 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
80 const wxPoint
&pos
, const wxSize
&size
,
83 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
86 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
87 wxDataViewCtrl
*GetOwner() { return m_owner
; }
89 // called on column addition/removal
90 virtual void UpdateDisplay() { /* by default, do nothing */ }
92 // returns the n-th column
93 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
96 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
103 wxDataViewCtrl
*m_owner
;
105 // sends an event generated from the n-th wxDataViewColumn
106 void SendEvent(wxEventType type
, unsigned int n
);
109 // on wxMSW the header window (only that part however) can be made native!
110 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
112 #define COLUMN_WIDTH_OFFSET 2
113 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
115 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
119 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
121 const wxPoint
&pos
= wxDefaultPosition
,
122 const wxSize
&size
= wxDefaultSize
,
123 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
125 Create(parent
, id
, pos
, size
, name
);
128 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
,
130 const wxString
&name
);
132 ~wxDataViewHeaderWindowMSW();
134 // called when any column setting is changed and/or changed
136 virtual void UpdateDisplay();
138 // called when the main window gets scrolled
139 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
142 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
143 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
145 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
147 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
148 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
151 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
154 #else // !defined(__WXMSW__)
156 #define HEADER_WINDOW_HEIGHT 25
157 #define HEADER_HORIZ_BORDER 5
158 #define HEADER_VERT_BORDER 3
159 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
161 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
164 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
166 const wxPoint
&pos
= wxDefaultPosition
,
167 const wxSize
&size
= wxDefaultSize
,
168 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
171 Create(parent
, id
, pos
, size
, name
);
174 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
175 const wxPoint
&pos
, const wxSize
&size
,
176 const wxString
&name
);
178 ~wxGenericDataViewHeaderWindow()
180 delete m_resizeCursor
;
183 virtual void UpdateDisplay() { Refresh(); }
187 void OnPaint( wxPaintEvent
&event
);
188 void OnMouse( wxMouseEvent
&event
);
189 void OnSetFocus( wxFocusEvent
&event
);
194 // vars used for column resizing:
196 wxCursor
*m_resizeCursor
;
197 const wxCursor
*m_currentCursor
;
200 bool m_dirty
; // needs refresh?
201 int m_column
; // index of the column being resized
202 int m_currentX
; // divider line position in logical (unscrolled) coords
203 int m_minX
; // minimal position beyond which the divider line
204 // can't be dragged in logical coords
206 // the pen used to draw the current column width drag line
207 // when resizing the columsn
211 // internal utilities:
215 m_currentCursor
= (wxCursor
*) NULL
;
216 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
218 m_isDragging
= false;
221 m_column
= wxNOT_FOUND
;
225 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
226 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
230 void AdjustDC(wxDC
& dc
);
233 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
234 DECLARE_EVENT_TABLE()
237 #endif // defined(__WXMSW__)
239 //-----------------------------------------------------------------------------
240 // wxDataViewRenameTimer
241 //-----------------------------------------------------------------------------
243 class wxDataViewRenameTimer
: public wxTimer
246 wxDataViewMainWindow
*m_owner
;
249 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
253 //-----------------------------------------------------------------------------
254 // wxDataViewTreeNode
255 //-----------------------------------------------------------------------------
256 class wxDataViewTreeNode
;
257 WX_DEFINE_SORTED_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
258 WX_DEFINE_SORTED_ARRAY( void* , wxDataViewTreeLeaves
);
260 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
);
261 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
);
263 class wxDataViewTreeNode
266 wxDataViewTreeNode( wxDataViewTreeNode
* parent
)
267 :leaves( wxGenericTreeModelItemCmp
),
268 nodes(wxGenericTreeModelNodeCmp
)
269 { this->parent
= parent
;
277 //I don't know what I need to do in the destructure
278 ~wxDataViewTreeNode()
283 wxDataViewTreeNode
* GetParent() { return parent
; }
284 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
285 wxDataViewTreeNodes
& GetNodes() { return nodes
; }
286 wxDataViewTreeLeaves
& GetChildren() { return leaves
; }
288 void AddNode( wxDataViewTreeNode
* node
)
291 leaves
.Add( node
->GetItem().GetID() );
293 void AddLeaf( void * leaf
) { leaves
.Add( leaf
); }
295 wxDataViewItem
& GetItem() { return item
; }
296 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
298 unsigned int GetChildrenNumber() { return leaves
.GetCount(); }
299 unsigned int GetNodeNumber() { return nodes
.GetCount(); }
303 wxDataViewTreeNode
* node
= this;
304 while( node
->GetParent()->GetParent() != NULL
)
306 node
= node
->GetParent();
319 int len
= nodes
.GetCount();
321 for ( int i
= 0 ;i
< len
; i
++)
322 sum
+= nodes
[i
]->GetSubTreeCount();
324 sum
+= leaves
.GetCount();
327 ChangeSubTreeCount(-sum
);
333 ChangeSubTreeCount(sum
);
336 bool HasChildren() { return hasChildren
; }
337 void SetHasChildren( bool has
){ hasChildren
= has
; }
339 void SetSubTreeCount( int num
) { subTreeCount
= num
; }
340 int GetSubTreeCount() { return subTreeCount
; }
341 void ChangeSubTreeCount( int num
)
347 parent
->ChangeSubTreeCount(num
);
351 wxDataViewTreeNode
* parent
;
352 wxDataViewTreeNodes nodes
;
353 wxDataViewTreeLeaves leaves
;
360 //Below is the compare stuff
361 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
362 static wxDataViewModel
* g_model
;
364 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
* node1
, wxDataViewTreeNode
* node2
)
366 return g_model
->Compare( node1
->GetItem(), node2
->GetItem() );
369 int LINKAGEMODE
wxGenericTreeModelItemCmp( void * id1
, void * id2
)
371 return g_model
->Compare( id1
, id2
);
376 //-----------------------------------------------------------------------------
377 // wxDataViewMainWindow
378 //-----------------------------------------------------------------------------
380 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
382 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
383 WX_DEFINE_LIST(ItemList
);
385 class wxDataViewMainWindow
: public wxWindow
388 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
390 const wxPoint
&pos
= wxDefaultPosition
,
391 const wxSize
&size
= wxDefaultSize
,
392 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
393 virtual ~wxDataViewMainWindow();
395 // notifications from wxDataViewModel
396 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
397 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
398 bool ItemChanged( const wxDataViewItem
&item
);
399 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
402 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
403 wxDataViewCtrl
*GetOwner() { return m_owner
; }
404 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
406 void OnPaint( wxPaintEvent
&event
);
407 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
408 void OnChar( wxKeyEvent
&event
);
409 void OnMouse( wxMouseEvent
&event
);
410 void OnSetFocus( wxFocusEvent
&event
);
411 void OnKillFocus( wxFocusEvent
&event
);
413 void UpdateDisplay();
414 void RecalculateDisplay();
415 void OnInternalIdle();
417 void OnRenameTimer();
419 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
421 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
422 void ChangeCurrentRow( unsigned int row
);
424 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
425 bool IsEmpty() { return GetRowCount() == 0; }
427 int GetCountPerPage() const;
428 int GetEndOfLastCol() const;
429 unsigned int GetFirstVisibleRow() const;
430 //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
431 unsigned int GetLastVisibleRow();
432 unsigned int GetRowCount() ;
434 wxDataViewItem
GetSelection();
436 void Select( const wxArrayInt
& aSelections
);
437 void SelectAllRows( bool on
);
438 void SelectRow( unsigned int row
, bool on
);
439 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
440 void ReverseRowSelection( unsigned int row
);
441 bool IsRowSelected( unsigned int row
);
443 void RefreshRow( unsigned int row
);
444 void RefreshRows( unsigned int from
, unsigned int to
);
445 void RefreshRowsAfter( unsigned int firstRow
);
447 // returns the colour to be used for drawing the rules
448 wxColour
GetRuleColour() const
450 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
453 //void EnsureVisible( unsigned int row );
454 wxRect
GetLineRect( unsigned int row
) const;
456 //Some useful functions for row and item mapping
457 wxDataViewItem
GetItemByRow( unsigned int row
);
458 unsigned int GetRowByItem( const wxDataViewItem
& item
);
460 //Methods for building the mapping tree
461 void BuildTree( wxDataViewModel
* model
);
464 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
465 //We did not need this temporarily
466 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
468 int RecalculateCount() ;
470 void OnExpanding( unsigned int row
);
471 void OnCollapsing( unsigned int row
);
473 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
476 wxDataViewCtrl
*m_owner
;
480 wxDataViewColumn
*m_currentCol
;
481 unsigned int m_currentRow
;
482 wxDataViewSelection m_selection
;
484 wxDataViewRenameTimer
*m_renameTimer
;
492 // for double click logic
493 unsigned int m_lineLastClicked
,
494 m_lineBeforeLastClicked
,
495 m_lineSelectSingleOnUp
;
497 // the pen used to draw horiz/vertical rules
500 // the pen used to draw the expander and the lines
503 //This is the tree structure of the model
504 wxDataViewTreeNode
* m_root
;
507 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
508 DECLARE_EVENT_TABLE()
511 // ---------------------------------------------------------
512 // wxGenericDataViewModelNotifier
513 // ---------------------------------------------------------
515 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
518 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
519 { m_mainWindow
= mainWindow
; }
521 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
522 { return m_mainWindow
->ItemAdded( parent
, item
); }
523 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
524 { return m_mainWindow
->ItemDeleted( parent
, item
); }
525 virtual bool ItemChanged( const wxDataViewItem
& item
)
526 { return m_mainWindow
->ItemChanged(item
); }
527 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
528 { return m_mainWindow
->ValueChanged( item
, col
); }
529 virtual bool Cleared()
530 { return m_mainWindow
->Cleared(); }
532 wxDataViewMainWindow
*m_mainWindow
;
535 // ---------------------------------------------------------
536 // wxDataViewRenderer
537 // ---------------------------------------------------------
539 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
541 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
542 wxDataViewCellMode mode
,
544 wxDataViewRendererBase( varianttype
, mode
, align
)
551 wxDataViewRenderer::~wxDataViewRenderer()
557 wxDC
*wxDataViewRenderer::GetDC()
561 if (GetOwner() == NULL
)
563 if (GetOwner()->GetOwner() == NULL
)
565 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
571 // ---------------------------------------------------------
572 // wxDataViewCustomRenderer
573 // ---------------------------------------------------------
575 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
577 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
578 wxDataViewCellMode mode
, int align
) :
579 wxDataViewRenderer( varianttype
, mode
, align
)
583 // ---------------------------------------------------------
584 // wxDataViewTextRenderer
585 // ---------------------------------------------------------
587 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
589 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
590 wxDataViewCellMode mode
, int align
) :
591 wxDataViewCustomRenderer( varianttype
, mode
, align
)
595 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
597 m_text
= value
.GetString();
602 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
607 bool wxDataViewTextRenderer::HasEditorCtrl()
612 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
613 wxRect labelRect
, const wxVariant
&value
)
615 return new wxTextCtrl( parent
, wxID_ANY
, value
,
616 wxPoint(labelRect
.x
,labelRect
.y
),
617 wxSize(labelRect
.width
,labelRect
.height
) );
620 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
622 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
623 value
= text
->GetValue();
627 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
629 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
630 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
631 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
632 view
->GetForegroundColour();
634 dc
->SetTextForeground(col
);
635 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
640 wxSize
wxDataViewTextRenderer::GetSize() const
642 const wxDataViewCtrl
*view
= GetView();
646 view
->GetTextExtent( m_text
, &x
, &y
);
647 return wxSize( x
, y
);
649 return wxSize(80,20);
652 // ---------------------------------------------------------
653 // wxDataViewBitmapRenderer
654 // ---------------------------------------------------------
656 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
658 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
659 wxDataViewCellMode mode
, int align
) :
660 wxDataViewCustomRenderer( varianttype
, mode
, align
)
664 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
666 if (value
.GetType() == wxT("wxBitmap"))
668 if (value
.GetType() == wxT("wxIcon"))
674 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
679 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
682 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
683 else if (m_icon
.Ok())
684 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
689 wxSize
wxDataViewBitmapRenderer::GetSize() const
692 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
693 else if (m_icon
.Ok())
694 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
696 return wxSize(16,16);
699 // ---------------------------------------------------------
700 // wxDataViewToggleRenderer
701 // ---------------------------------------------------------
703 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
705 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
706 wxDataViewCellMode mode
, int align
) :
707 wxDataViewCustomRenderer( varianttype
, mode
, align
)
712 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
714 m_toggle
= value
.GetBool();
719 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
724 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
726 // User wxRenderer here
729 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
731 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
736 flags
|= wxCONTROL_CHECKED
;
737 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
738 flags
|= wxCONTROL_DISABLED
;
740 wxRendererNative::Get().DrawCheckBox(
741 GetOwner()->GetOwner(),
749 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
750 wxDataViewModel
*model
,
751 const wxDataViewItem
& item
, unsigned int col
)
753 bool value
= !m_toggle
;
754 wxVariant variant
= value
;
755 model
->SetValue( variant
, item
, col
);
756 model
->ValueChanged( item
, col
);
760 wxSize
wxDataViewToggleRenderer::GetSize() const
762 return wxSize(20,20);
765 // ---------------------------------------------------------
766 // wxDataViewProgressRenderer
767 // ---------------------------------------------------------
769 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
771 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
772 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
773 wxDataViewCustomRenderer( varianttype
, mode
, align
)
779 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
783 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
785 m_value
= (long) value
;
787 if (m_value
< 0) m_value
= 0;
788 if (m_value
> 100) m_value
= 100;
793 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
795 value
= (long) m_value
;
799 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
801 double pct
= (double)m_value
/ 100.0;
803 bar
.width
= (int)(cell
.width
* pct
);
804 dc
->SetPen( *wxTRANSPARENT_PEN
);
805 dc
->SetBrush( *wxBLUE_BRUSH
);
806 dc
->DrawRectangle( bar
);
808 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
809 dc
->SetPen( *wxBLACK_PEN
);
810 dc
->DrawRectangle( cell
);
815 wxSize
wxDataViewProgressRenderer::GetSize() const
817 return wxSize(40,12);
820 // ---------------------------------------------------------
821 // wxDataViewDateRenderer
822 // ---------------------------------------------------------
824 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
826 #if wxUSE_DATE_RENDERER_POPUP
828 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
831 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
832 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
833 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
838 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
839 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
840 sizer
->Add( m_cal
, 1, wxGROW
);
845 void OnCalendar( wxCalendarEvent
&event
);
847 wxCalendarCtrl
*m_cal
;
848 wxDataViewModel
*m_model
;
850 const wxDataViewItem
& m_item
;
853 virtual void OnDismiss()
858 DECLARE_EVENT_TABLE()
861 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
862 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
865 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
867 wxDateTime date
= event
.GetDate();
868 wxVariant value
= date
;
869 m_model
->SetValue( value
, m_item
, m_col
);
870 m_model
->ValueChanged( m_item
, m_col
);
874 #endif // wxUSE_DATE_RENDERER_POPUP
876 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
878 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
879 wxDataViewCellMode mode
, int align
) :
880 wxDataViewCustomRenderer( varianttype
, mode
, align
)
884 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
886 m_date
= value
.GetDateTime();
891 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
897 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
899 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
900 wxString tmp
= m_date
.FormatDate();
901 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
906 wxSize
wxDataViewDateRenderer::GetSize() const
908 const wxDataViewCtrl
* view
= GetView();
909 wxString tmp
= m_date
.FormatDate();
911 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
912 return wxSize(x
,y
+d
);
915 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
916 const wxDataViewItem
& item
, unsigned int col
)
919 model
->GetValue( variant
, item
, col
);
920 wxDateTime value
= variant
.GetDateTime();
922 #if wxUSE_DATE_RENDERER_POPUP
923 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
924 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
925 wxPoint pos
= wxGetMousePosition();
928 popup
->Popup( popup
->m_cal
);
929 #else // !wxUSE_DATE_RENDERER_POPUP
930 wxMessageBox(value
.Format());
931 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
935 // ---------------------------------------------------------
937 // ---------------------------------------------------------
939 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
941 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
942 unsigned int model_column
,
943 int width
, wxAlignment align
, int flags
) :
944 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
950 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
953 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
954 unsigned int model_column
,
955 int width
, wxAlignment align
, int flags
) :
956 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
961 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
964 wxDataViewColumn::~wxDataViewColumn()
968 void wxDataViewColumn::Init( int width
)
971 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
975 void wxDataViewColumn::SetResizeable( bool resizeable
)
978 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
980 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
983 void wxDataViewColumn::SetHidden( bool hidden
)
986 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
988 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
990 // tell our owner to e.g. update its scrollbars:
992 GetOwner()->OnColumnChange();
995 void wxDataViewColumn::SetSortable( bool sortable
)
998 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1000 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1002 // Update header button
1004 GetOwner()->OnColumnChange();
1007 void wxDataViewColumn::SetSortOrder( bool ascending
)
1009 m_ascending
= ascending
;
1011 // Update header button
1013 GetOwner()->OnColumnChange();
1016 bool wxDataViewColumn::IsSortOrderAscending() const
1021 void wxDataViewColumn::SetInternalWidth( int width
)
1025 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1026 if (m_owner
&& m_owner
->m_clientArea
)
1027 m_owner
->m_clientArea
->RecalculateDisplay();
1030 void wxDataViewColumn::SetWidth( int width
)
1032 m_owner
->m_headerArea
->UpdateDisplay();
1034 SetInternalWidth(width
);
1038 //-----------------------------------------------------------------------------
1039 // wxDataViewHeaderWindowBase
1040 //-----------------------------------------------------------------------------
1042 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1044 wxWindow
*parent
= GetParent();
1045 wxDataViewEvent
le(type
, parent
->GetId());
1047 le
.SetEventObject(parent
);
1049 le
.SetDataViewColumn(GetColumn(n
));
1050 le
.SetModel(GetOwner()->GetModel());
1052 // for events created by wxDataViewHeaderWindow the
1053 // row / value fields are not valid
1055 parent
->GetEventHandler()->ProcessEvent(le
);
1058 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1060 // implemented in msw/listctrl.cpp:
1061 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1063 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1065 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1066 const wxPoint
&pos
, const wxSize
&size
,
1067 const wxString
&name
)
1071 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1074 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1075 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1076 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1077 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1079 // create the native WC_HEADER window:
1080 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1081 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1082 m_hWnd
= CreateWindowEx(0,
1093 wxLogLastError(_T("CreateWindowEx"));
1097 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1098 // to call wxDataViewHeaderWindow::MSWOnNotify
1099 SubclassWin(m_hWnd
);
1101 // the following is required to get the default win's font for
1102 // header windows and must be done befor sending the HDM_LAYOUT msg
1109 // Retrieve the bounding rectangle of the parent window's
1110 // client area, and then request size and position values
1111 // from the header control.
1112 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1114 hdl
.prc
= &rcParent
;
1116 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1118 wxLogLastError(_T("SendMessage"));
1122 // Set the size, position, and visibility of the header control.
1123 SetWindowPos((HWND
)m_hWnd
,
1127 wp
.flags
| SWP_SHOWWINDOW
);
1129 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1130 // a wxBoxSizer and in order to avoid super-big header windows,
1131 // we need to set our height as fixed
1132 SetMinSize(wxSize(-1, wp
.cy
));
1133 SetMaxSize(wxSize(-1, wp
.cy
));
1138 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1143 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1145 // remove old columns
1146 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1147 Header_DeleteItem((HWND
)m_hWnd
, 0);
1149 // add the updated array of columns to the header control
1150 unsigned int cols
= GetOwner()->GetColumnCount();
1151 unsigned int added
= 0;
1152 for (unsigned int i
= 0; i
< cols
; i
++)
1154 wxDataViewColumn
*col
= GetColumn( i
);
1155 if (col
->IsHidden())
1156 continue; // don't add it!
1159 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1160 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1161 hdi
.cxy
= col
->GetWidth();
1162 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1163 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1165 // lParam is reserved for application's use:
1166 // we store there the column index to use it later in MSWOnNotify
1167 // (since columns may have been hidden)
1168 hdi
.lParam
= (LPARAM
)i
;
1170 // the native wxMSW implementation of the header window
1171 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1172 // on the right: to correct this effect we make the column
1173 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1175 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1177 switch (col
->GetAlignment())
1180 hdi
.fmt
|= HDF_LEFT
;
1182 case wxALIGN_CENTER
:
1183 case wxALIGN_CENTER_HORIZONTAL
:
1184 hdi
.fmt
|= HDF_CENTER
;
1187 hdi
.fmt
|= HDF_RIGHT
;
1191 // such alignment is not allowed for the column header!
1195 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1196 (WPARAM
)added
, (LPARAM
)&hdi
);
1201 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1205 // NOTE: we don't just return nmHDR->iItem because when there are
1206 // hidden columns, nmHDR->iItem may be different from
1207 // nmHDR->pitem->lParam
1209 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1211 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1216 item
.mask
= HDI_LPARAM
;
1217 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1219 return (unsigned int)item
.lParam
;
1222 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1224 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1226 // is it a message from the header?
1227 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1228 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1230 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1231 switch ( nmhdr
->code
)
1233 case HDN_BEGINTRACK
:
1234 // user has started to resize a column:
1235 // do we need to veto it?
1236 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1244 // user has started to reorder a column
1247 case HDN_ITEMCHANGING
:
1248 if (nmHDR
->pitem
!= NULL
&&
1249 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1251 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1252 if (nmHDR
->pitem
->cxy
< minWidth
)
1254 // do not allow the user to resize this column under
1255 // its minimal width:
1261 case HDN_ITEMCHANGED
: // user is resizing a column
1262 case HDN_ENDTRACK
: // user has finished resizing a column
1263 case HDN_ENDDRAG
: // user has finished reordering a column
1265 // update the width of the modified column:
1266 if (nmHDR
->pitem
!= NULL
&&
1267 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1269 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1270 unsigned int w
= nmHDR
->pitem
->cxy
;
1271 wxDataViewColumn
*col
= GetColumn(idx
);
1273 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1274 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1275 w
-= COLUMN_WIDTH_OFFSET
;
1277 if (w
>= (unsigned)col
->GetMinWidth())
1278 col
->SetInternalWidth(w
);
1284 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1285 wxEventType evt
= nmHDR
->iButton
== 0 ?
1286 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1287 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1288 SendEvent(evt
, idx
);
1294 // NOTE: for some reason (i.e. for a bug in Windows)
1295 // the HDN_ITEMCLICK notification is not sent on
1296 // right clicks, so we need to handle NM_RCLICK
1299 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1300 if (column
!= wxNOT_FOUND
)
1303 item
.mask
= HDI_LPARAM
;
1304 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1306 // 'idx' may be different from 'column' if there are
1307 // hidden columns...
1308 unsigned int idx
= (unsigned int)item
.lParam
;
1309 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1315 case HDN_GETDISPINFOW
:
1316 // see wxListCtrl::MSWOnNotify for more info!
1319 case HDN_ITEMDBLCLICK
:
1321 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1322 int w
= GetOwner()->GetBestColumnWidth(idx
);
1324 // update the native control:
1326 ZeroMemory(&hd
, sizeof(hd
));
1327 hd
.mask
= HDI_WIDTH
;
1329 Header_SetItem(GetHwnd(),
1330 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1333 // update the wxDataViewColumn class:
1334 GetColumn(idx
)->SetInternalWidth(w
);
1339 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1345 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1346 const wxRect
*WXUNUSED(rect
))
1348 wxSize ourSz
= GetClientSize();
1349 wxSize ownerSz
= m_owner
->GetClientSize();
1351 // where should the (logical) origin of this window be placed?
1353 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1355 // put this window on top of our parent and
1356 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1357 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1361 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1362 int WXUNUSED(w
), int WXUNUSED(h
),
1365 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1366 // the wxDataViewCtrl window gets resized: the following dummy call
1367 // to ScrollWindow() is required in order to get this header window
1368 // correctly repainted when it's (horizontally) scrolled:
1373 #else // !defined(__WXMSW__)
1375 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1376 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1377 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1378 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1379 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1382 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1383 const wxPoint
&pos
, const wxSize
&size
,
1384 const wxString
&name
)
1388 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1391 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1392 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1393 SetOwnForegroundColour( attr
.colFg
);
1394 SetOwnBackgroundColour( attr
.colBg
);
1396 SetOwnFont( attr
.font
);
1398 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1399 // a wxBoxSizer and in order to avoid super-big header windows,
1400 // we need to set our height as fixed
1401 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1402 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1407 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1410 GetClientSize( &w
, &h
);
1412 wxAutoBufferedPaintDC
dc( this );
1414 dc
.SetBackground(GetBackgroundColour());
1418 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1421 m_owner
->GetViewStart( &x
, NULL
);
1423 // account for the horz scrollbar offset
1424 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1426 dc
.SetFont( GetFont() );
1428 unsigned int cols
= GetOwner()->GetColumnCount();
1431 for (i
= 0; i
< cols
; i
++)
1433 wxDataViewColumn
*col
= GetColumn( i
);
1434 if (col
->IsHidden())
1435 continue; // skip it!
1437 int cw
= col
->GetWidth();
1440 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1441 if (col
->IsSortable())
1443 if (col
->IsSortOrderAscending())
1444 sortArrow
= wxHDR_SORT_ICON_UP
;
1446 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1449 wxRendererNative::Get().DrawHeaderButton
1453 wxRect(xpos
, 0, cw
, ch
-1),
1454 m_parent
->IsEnabled() ? 0
1455 : (int)wxCONTROL_DISABLED
,
1459 // align as required the column title:
1461 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1462 switch (col
->GetAlignment())
1465 x
+= HEADER_HORIZ_BORDER
;
1467 case wxALIGN_CENTER
:
1468 case wxALIGN_CENTER_HORIZONTAL
:
1469 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1472 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1476 // always center the title vertically:
1477 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1479 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1481 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1482 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1483 dc
.DrawText( col
->GetTitle(), x
, y
);
1484 dc
.DestroyClippingRegion();
1490 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1492 GetParent()->SetFocus();
1496 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1498 // we want to work with logical coords
1500 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1501 int y
= event
.GetY();
1505 // we don't draw the line beyond our window,
1506 // but we allow dragging it there
1508 GetClientSize( &w
, NULL
);
1509 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1512 // erase the line if it was drawn
1516 if (event
.ButtonUp())
1518 m_isDragging
= false;
1524 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1527 GetOwner()->Refresh();
1531 m_currentX
= wxMax(m_minX
+ 7, x
);
1533 // draw in the new location
1534 if (m_currentX
< w
) DrawCurrent();
1538 else // not dragging
1541 m_column
= wxNOT_FOUND
;
1543 bool hit_border
= false;
1545 // end of the current column
1548 // find the column where this event occured
1549 int countCol
= m_owner
->GetColumnCount();
1550 for (int column
= 0; column
< countCol
; column
++)
1552 wxDataViewColumn
*p
= GetColumn(column
);
1555 continue; // skip if not shown
1557 xpos
+= p
->GetWidth();
1559 if ((abs(x
-xpos
) < 3) && (y
< 22))
1567 // inside the column
1574 if (m_column
== wxNOT_FOUND
)
1577 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1578 if (event
.LeftDClick() && resizeable
)
1580 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1583 else if (event
.LeftDown() || event
.RightUp())
1585 if (hit_border
&& event
.LeftDown() && resizeable
)
1587 m_isDragging
= true;
1592 else // click on a column
1594 wxEventType evt
= event
.LeftDown() ?
1595 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1596 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1597 SendEvent(evt
, m_column
);
1600 else if (event
.Moving())
1602 if (hit_border
&& resizeable
)
1603 m_currentCursor
= m_resizeCursor
;
1605 m_currentCursor
= wxSTANDARD_CURSOR
;
1607 SetCursor(*m_currentCursor
);
1612 void wxGenericDataViewHeaderWindow::DrawCurrent()
1614 int x1
= m_currentX
;
1616 ClientToScreen (&x1
, &y1
);
1618 int x2
= m_currentX
-1;
1620 ++x2
; // but why ????
1623 m_owner
->GetClientSize( NULL
, &y2
);
1624 m_owner
->ClientToScreen( &x2
, &y2
);
1627 dc
.SetLogicalFunction(wxINVERT
);
1628 dc
.SetPen(m_penCurrent
);
1629 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1631 dc
.DrawLine(x1
, y1
, x2
, y2
);
1634 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1638 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1639 m_owner
->GetViewStart( &x
, NULL
);
1641 // shift the DC origin to match the position of the main window horizontal
1642 // scrollbar: this allows us to always use logical coords
1643 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1646 #endif // defined(__WXMSW__)
1648 //-----------------------------------------------------------------------------
1649 // wxDataViewRenameTimer
1650 //-----------------------------------------------------------------------------
1652 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1657 void wxDataViewRenameTimer::Notify()
1659 m_owner
->OnRenameTimer();
1662 //-----------------------------------------------------------------------------
1663 // wxDataViewMainWindow
1664 //-----------------------------------------------------------------------------
1666 //The tree building helper, declared firstly
1667 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1669 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1671 if (row1
> row2
) return 1;
1672 if (row1
== row2
) return 0;
1677 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1679 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1680 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1681 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1682 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1683 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1684 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1687 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1688 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1689 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1690 m_selection( wxDataViewSelectionCmp
)
1695 m_lastOnSame
= false;
1696 m_renameTimer
= new wxDataViewRenameTimer( this );
1698 // TODO: user better initial values/nothing selected
1699 m_currentCol
= NULL
;
1702 // TODO: we need to calculate this smartly
1709 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1712 m_dragStart
= wxPoint(0,0);
1713 m_lineLastClicked
= (unsigned int) -1;
1714 m_lineBeforeLastClicked
= (unsigned int) -1;
1715 m_lineSelectSingleOnUp
= (unsigned int) -1;
1719 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1720 SetBackgroundColour( *wxWHITE
);
1722 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1724 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1725 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1726 //Some new added code to deal with the tree structure
1727 m_root
= new wxDataViewTreeNode( NULL
);
1728 m_root
->SetHasChildren(true);
1730 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1735 wxDataViewMainWindow::~wxDataViewMainWindow()
1738 delete m_renameTimer
;
1741 void wxDataViewMainWindow::OnRenameTimer()
1743 // We have to call this here because changes may just have
1744 // been made and no screen update taken place.
1749 unsigned int cols
= GetOwner()->GetColumnCount();
1751 for (i
= 0; i
< cols
; i
++)
1753 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1755 continue; // skip it!
1757 if (c
== m_currentCol
)
1759 xpos
+= c
->GetWidth();
1761 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1762 m_currentCol
->GetWidth(), m_lineHeight
);
1764 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1765 &labelRect
.x
, &labelRect
.y
);
1767 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1768 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1771 //------------------------------------------------------------------
1772 // Helper class for do operation on the tree node
1773 //------------------------------------------------------------------
1780 //The return value control how the tree-walker tranverse the tree
1781 // 0: Job done, stop tranverse and return
1782 // 1: Ignore the current node's subtree and continue
1783 // 2: Job not done, continue
1784 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1785 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1786 virtual int operator() ( void * n
) = 0;
1790 class ItemAddJob
: public DoJob
1793 ItemAddJob( const wxDataViewItem
& parent
, const wxDataViewItem
& item
, int * count
)
1794 { this->parent
= parent
; this->item
= item
; m_count
= count
; }
1795 virtual ~ItemAddJob(){};
1797 virtual int operator() ( wxDataViewTreeNode
* node
)
1799 if( node
->GetItem() == parent
)
1801 node
->SetHasChildren( true );
1802 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1803 newnode
->SetItem(item
);
1804 node
->AppendChild( newnode
);
1813 wxDataViewItem parent
, item
;
1817 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1819 if( node
==NULL
|| !node
->HasChildren())
1822 wxDataViewTreeNodes nodes
= node
->GetNodes();
1823 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1825 int len_nodes
= nodes
.GetCount();
1826 int len
= leaves
.GetCount();
1827 int i
= 0, nodes_i
= 0;
1828 for( ; i
< len
; i
++ )
1830 void * n
= leaves
[i
];
1831 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1833 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1836 switch( func( nd
) )
1847 if( Walker( nd
, func
) )
1866 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1868 g_model
= GetOwner()->GetModel();
1870 wxDataViewTreeNode
* node
;
1871 node
= FindNode(parent
);
1876 node
->SetHasChildren( true );
1878 if( g_model
->IsContainer( item
) )
1880 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1881 newnode
->SetItem(item
);
1882 node
->AddNode( newnode
);
1885 node
->AddLeaf( item
.GetID() );
1887 node
->ChangeSubTreeCount(1);
1895 class ItemDeleteJob
: public DoJob
1898 ItemDeleteJob( const wxDataViewItem
& item
, int * count
) { m_item
= item
; m_count
= count
; }
1899 virtual ~ItemDeleteJob(){}
1900 virtual int operator() ( wxDataViewTreeNode
* node
)
1902 if( node
->GetItem() == m_item
)
1904 node
->GetParent()->GetChildren().Remove( node
);
1914 wxDataViewItem m_item
;
1918 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
1920 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
1921 const wxDataViewItem
& item
)
1923 g_model
= GetOwner()->GetModel();
1925 wxDataViewTreeNode
* node
;
1926 wxDataViewItem parent_item
= g_model
->GetParent( item
);
1927 node
= FindNode(parent_item
);
1929 if( node
== NULL
|| node
->GetChildren().Index( item
.GetID() ) == wxNOT_FOUND
)
1935 node
->GetChildren().Remove( item
.GetID() );
1936 if( GetOwner()->GetModel()->IsContainer( item
) )
1938 wxDataViewTreeNode
* n
;
1939 wxDataViewTreeNodes nodes
= node
->GetNodes();
1940 int len
= nodes
.GetCount();
1941 for( int i
= 0 ; i
< len
; i
++)
1943 if( nodes
[i
]->GetItem() == item
)
1949 node
->GetNodes().Remove( n
);
1950 sub
-= n
->GetSubTreeCount();
1951 DestroyTreeHelper(n
);
1954 if( node
->GetChildrenNumber() == 0)
1955 node
->SetHasChildren( false );
1957 //Make the row number invalid and get a new valid one when user call GetRowCount
1959 node
->ChangeSubTreeCount(sub
);
1960 //Change the current row to the last row if the current exceed the max row number
1961 if( m_currentRow
> GetRowCount() )
1962 m_currentRow
= m_count
- 1;
1968 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1970 g_model
= GetOwner()->GetModel();
1972 unsigned int row
= GetRowByItem(item
);
1977 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int WXUNUSED(col
) )
1979 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1980 /*#define MAX_VIRTUAL_WIDTH 100000
1982 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1983 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1984 Refresh( true, &rect );
1988 g_model
= GetOwner()->GetModel();
1990 unsigned int row
= GetRowByItem(item
);
1995 bool wxDataViewMainWindow::Cleared()
1997 g_model
= GetOwner()->GetModel();
2004 void wxDataViewMainWindow::UpdateDisplay()
2009 void wxDataViewMainWindow::OnInternalIdle()
2011 wxWindow::OnInternalIdle();
2015 RecalculateDisplay();
2020 void wxDataViewMainWindow::RecalculateDisplay()
2022 wxDataViewModel
*model
= GetOwner()->GetModel();
2029 int width
= GetEndOfLastCol();
2030 int height
= GetRowCount() * m_lineHeight
;
2032 SetVirtualSize( width
, height
);
2033 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2038 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2040 wxWindow::ScrollWindow( dx
, dy
, rect
);
2042 if (GetOwner()->m_headerArea
)
2043 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2046 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2048 wxDataViewModel
*model
= GetOwner()->GetModel();
2049 wxAutoBufferedPaintDC
dc( this );
2052 dc
.SetBackground(GetBackgroundColour());
2054 GetOwner()->PrepareDC( dc
);
2055 dc
.SetFont( GetFont() );
2057 wxRect update
= GetUpdateRegion().GetBox();
2058 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2060 // compute which items needs to be redrawn
2061 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2062 unsigned int item_count
=
2063 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2064 (int)(GetRowCount( )- item_start
) );
2065 unsigned int item_last
= item_start
+ item_count
;
2067 // compute which columns needs to be redrawn
2068 unsigned int cols
= GetOwner()->GetColumnCount();
2069 unsigned int col_start
= 0;
2070 unsigned int x_start
= 0;
2071 for (x_start
= 0; col_start
< cols
; col_start
++)
2073 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2074 if (col
->IsHidden())
2075 continue; // skip it!
2077 unsigned int w
= col
->GetWidth();
2078 if (x_start
+w
>= (unsigned int)update
.x
)
2084 unsigned int col_last
= col_start
;
2085 unsigned int x_last
= x_start
;
2086 for (; col_last
< cols
; col_last
++)
2088 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2089 if (col
->IsHidden())
2090 continue; // skip it!
2092 if (x_last
> (unsigned int)update
.GetRight())
2095 x_last
+= col
->GetWidth();
2098 // Draw horizontal rules if required
2099 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2101 dc
.SetPen(m_penRule
);
2102 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2104 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2106 int y
= i
* m_lineHeight
;
2107 dc
.DrawLine(x_start
, y
, x_last
, y
);
2111 // Draw vertical rules if required
2112 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2114 dc
.SetPen(m_penRule
);
2115 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2118 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2120 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2121 if (col
->IsHidden())
2122 continue; // skip it
2124 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2125 x
, item_last
* m_lineHeight
);
2127 x
+= col
->GetWidth();
2130 // Draw last vertical rule
2131 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2132 x
, item_last
* m_lineHeight
);
2135 // redraw the background for the items which are selected/current
2136 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2138 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2139 if (selected
|| item
== m_currentRow
)
2141 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2142 if (item
== m_currentRow
)
2143 flags
|= wxCONTROL_CURRENT
;
2145 flags
|= wxCONTROL_FOCUSED
;
2147 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2148 wxRendererNative::Get().DrawItemSelectionRect
2158 // redraw all cells for all rows which must be repainted and for all columns
2160 cell_rect
.x
= x_start
;
2161 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2162 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2164 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2165 wxDataViewRenderer
*cell
= col
->GetRenderer();
2166 cell_rect
.width
= col
->GetWidth();
2168 if (col
->IsHidden())
2169 continue; // skipt it!
2171 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2173 // get the cell value and set it into the renderer
2175 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2181 wxDataViewItem dataitem
= node
->GetItem();
2182 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2183 cell
->SetValue( value
);
2185 // update the y offset
2186 cell_rect
.y
= item
* m_lineHeight
;
2188 //Draw the expander here.
2189 int indent
= node
->GetIndentLevel();
2190 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
2192 //Calculate the indent first
2193 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2195 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2196 // change the cell_rect.x to the appropriate pos
2197 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2198 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2199 dc
.SetPen( m_penExpander
);
2200 dc
.SetBrush( wxNullBrush
);
2201 if( node
->HasChildren() )
2203 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2204 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2205 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2206 if( node
->IsOpen() )
2207 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2209 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2213 // I am wandering whether we should draw dot lines between tree nodes
2215 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2218 //force the expander column to left-center align
2219 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2223 // cannot be bigger than allocated space
2224 wxSize size
= cell
->GetSize();
2225 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2226 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2227 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2229 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2230 int align
= cell
->GetAlignment();
2232 // horizontal alignment:
2233 item_rect
.x
= cell_rect
.x
;
2234 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2235 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2236 else if (align
& wxALIGN_RIGHT
)
2237 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2238 //else: wxALIGN_LEFT is the default
2240 // vertical alignment:
2241 item_rect
.y
= cell_rect
.y
;
2242 if (align
& wxALIGN_CENTER_VERTICAL
)
2243 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2244 else if (align
& wxALIGN_BOTTOM
)
2245 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2246 //else: wxALIGN_TOP is the default
2249 item_rect
.x
+= PADDING_RIGHTLEFT
;
2250 item_rect
.y
+= PADDING_TOPBOTTOM
;
2251 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2252 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2254 //Here we add the tree indent
2255 item_rect
.x
+= indent
;
2258 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2259 state
|= wxDATAVIEW_CELL_SELECTED
;
2261 // TODO: it would be much more efficient to create a clipping
2262 // region for the entire column being rendered (in the OnPaint
2263 // of wxDataViewMainWindow) instead of a single clip region for
2264 // each cell. However it would mean that each renderer should
2265 // respect the given wxRect's top & bottom coords, eventually
2266 // violating only the left & right coords - however the user can
2267 // make its own renderer and thus we cannot be sure of that.
2268 dc
.SetClippingRegion( item_rect
);
2269 cell
->Render( item_rect
, &dc
, state
);
2270 dc
.DestroyClippingRegion();
2273 cell_rect
.x
+= cell_rect
.width
;
2277 int wxDataViewMainWindow::GetCountPerPage() const
2279 wxSize size
= GetClientSize();
2280 return size
.y
/ m_lineHeight
;
2283 int wxDataViewMainWindow::GetEndOfLastCol() const
2287 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2289 const wxDataViewColumn
*c
=
2290 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2293 width
+= c
->GetWidth();
2298 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2302 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2304 return y
/ m_lineHeight
;
2307 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2309 wxSize client_size
= GetClientSize();
2310 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2311 &client_size
.x
, &client_size
.y
);
2313 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2316 unsigned int wxDataViewMainWindow::GetRowCount()
2318 if ( m_count
== -1 )
2320 m_count
= RecalculateCount();
2322 GetVirtualSize( &width
, &height
);
2323 height
= m_count
* m_lineHeight
;
2325 SetVirtualSize( width
, height
);
2330 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2337 void wxDataViewMainWindow::SelectAllRows( bool on
)
2344 m_selection
.Clear();
2345 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2346 m_selection
.Add( i
);
2351 unsigned int first_visible
= GetFirstVisibleRow();
2352 unsigned int last_visible
= GetLastVisibleRow();
2354 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2356 unsigned int row
= m_selection
[i
];
2357 if ((row
>= first_visible
) && (row
<= last_visible
))
2360 m_selection
.Clear();
2364 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2366 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2370 m_selection
.Add( row
);
2378 m_selection
.Remove( row
);
2384 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2388 unsigned int tmp
= from
;
2394 for (i
= from
; i
<= to
; i
++)
2396 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2399 m_selection
.Add( i
);
2404 m_selection
.Remove( i
);
2407 RefreshRows( from
, to
);
2410 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2412 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2414 int n
= aSelections
[i
];
2416 m_selection
.Add( n
);
2421 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2423 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2424 m_selection
.Add( row
);
2426 m_selection
.Remove( row
);
2430 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2432 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2435 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2437 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2438 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2440 wxSize client_size
= GetClientSize();
2441 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2442 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2443 if (intersect_rect
.width
> 0)
2444 Refresh( true, &intersect_rect
);
2447 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2451 unsigned int tmp
= to
;
2456 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2457 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2459 wxSize client_size
= GetClientSize();
2460 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2461 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2462 if (intersect_rect
.width
> 0)
2463 Refresh( true, &intersect_rect
);
2466 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2468 unsigned int count
= GetRowCount();
2469 if (firstRow
> count
)
2472 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2473 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2475 wxSize client_size
= GetClientSize();
2476 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2477 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2478 if (intersect_rect
.width
> 0)
2479 Refresh( true, &intersect_rect
);
2482 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2484 wxCHECK_RET( newCurrent
< GetRowCount(),
2485 _T("invalid item index in OnArrowChar()") );
2487 // if there is no selection, we cannot move it anywhere
2488 if (!HasCurrentRow())
2491 unsigned int oldCurrent
= m_currentRow
;
2493 // in single selection we just ignore Shift as we can't select several
2495 if ( event
.ShiftDown() && !IsSingleSel() )
2497 RefreshRow( oldCurrent
);
2499 ChangeCurrentRow( newCurrent
);
2501 // select all the items between the old and the new one
2502 if ( oldCurrent
> newCurrent
)
2504 newCurrent
= oldCurrent
;
2505 oldCurrent
= m_currentRow
;
2508 SelectRows( oldCurrent
, newCurrent
, true );
2512 RefreshRow( oldCurrent
);
2514 // all previously selected items are unselected unless ctrl is held
2515 if ( !event
.ControlDown() )
2516 SelectAllRows(false);
2518 ChangeCurrentRow( newCurrent
);
2520 if ( !event
.ControlDown() )
2521 SelectRow( m_currentRow
, true );
2523 RefreshRow( m_currentRow
);
2526 //EnsureVisible( m_currentRow );
2529 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2533 rect
.y
= m_lineHeight
* row
;
2534 rect
.width
= GetEndOfLastCol();
2535 rect
.height
= m_lineHeight
;
2540 class RowToItemJob
: public DoJob
2543 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2544 virtual ~RowToItemJob(){};
2546 virtual int operator() ( wxDataViewTreeNode
* node
)
2549 if( current
== static_cast<int>(row
))
2551 ret
= node
->GetItem() ;
2555 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2557 current
+= node
->GetSubTreeCount();
2564 virtual int operator() ( void * n
)
2567 if( current
== static_cast<int>(row
))
2569 ret
= wxDataViewItem( n
) ;
2574 wxDataViewItem
GetResult(){ return ret
; }
2581 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2583 RowToItemJob
job( row
, -1 );
2584 Walker( m_root
, job
);
2585 return job
.GetResult();
2588 class RowToTreeNodeJob
: public DoJob
2591 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2594 this->current
= current
;
2598 virtual ~RowToTreeNodeJob(){};
2600 virtual int operator() ( wxDataViewTreeNode
* node
)
2603 if( current
== static_cast<int>(row
))
2609 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2611 current
+= node
->GetSubTreeCount();
2622 virtual int operator() ( void * n
)
2625 if( current
== static_cast<int>(row
))
2627 ret
= new wxDataViewTreeNode( parent
) ;
2628 ret
->SetItem( wxDataViewItem( n
));
2629 ret
->SetHasChildren(false);
2635 wxDataViewTreeNode
* GetResult(){ return ret
; }
2639 wxDataViewTreeNode
* ret
;
2640 wxDataViewTreeNode
* parent
;
2644 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2646 RowToTreeNodeJob
job( row
, -1, m_root
);
2647 Walker( m_root
, job
);
2648 return job
.GetResult();
2652 class CountJob
: public DoJob
2655 CountJob(){ count
= 0 ; }
2656 virtual ~CountJob(){};
2658 virtual int operator () ( wxDataViewTreeNode
* node
)
2661 if ( node
->IsOpen())
2667 unsigned int GetResult()
2676 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2678 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2681 if( node
->HasChildren())
2682 if( !node
->IsOpen())
2685 //Here I build the children of current node
2686 if( node
->GetChildrenNumber() == 0 )
2687 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2693 SelectRow( row
, false );
2694 SelectRow( row
+ 1, true );
2695 ChangeCurrentRow( row
+ 1 );
2702 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2704 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2707 wxDataViewTreeNode
* nd
= node
;
2709 if( node
->HasChildren() && node
->IsOpen() )
2714 //RefreshRows(row,GetLastVisibleRow());
2718 node
= node
->GetParent();
2721 int parent
= GetRowByItem( node
->GetItem()) ;
2722 SelectRow( row
, false);
2723 SelectRow(parent
, true );
2724 ChangeCurrentRow( parent
);
2727 if( !nd
->HasChildren())
2732 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2734 wxDataViewModel
* model
= GetOwner()->GetModel();
2738 //Compose the a parent-chain of the finding item
2740 list
.DeleteContents( true );
2741 wxDataViewItem
it( item
);
2744 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2745 list
.Insert( pItem
);
2746 it
= model
->GetParent( it
);
2749 //Find the item along the parent-chain.
2750 //This algorithm is designed to speed up the node-finding method
2752 wxDataViewTreeNode
* node
= m_root
;
2753 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2755 if( node
->HasChildren() )
2757 if( node
->GetChildrenNumber() == 0 )
2758 BuildTreeHelper(model
, node
->GetItem(), node
);
2760 int len
= node
->GetNodeNumber();
2761 wxDataViewTreeNodes nodes
= node
->GetNodes();
2763 for( ; j
< len
; j
++)
2765 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2771 // Whenever we can't find the node in any level, return NULL to indicate the item can't be found
2784 int wxDataViewMainWindow::RecalculateCount()
2786 return m_root
->GetSubTreeCount();
2789 class ItemToRowJob
: public DoJob
2792 ItemToRowJob(const wxDataViewItem
& item
, ItemList::Node
* node
)
2793 { this->item
= item
; ret
= 0 ; nd
= node
; }
2794 virtual ~ItemToRowJob(){};
2796 virtual int operator() ( wxDataViewTreeNode
* node
)
2799 if( node
->GetItem() == item
)
2804 if( nd
&& node
->GetItem() == *(nd
->GetData()))
2811 ret
+= node
->GetSubTreeCount();
2817 virtual int operator() ( void * n
)
2820 if( n
== item
.GetID() )
2824 //the row number is begin from zero
2825 int GetResult(){ return ret
-1 ; }
2827 ItemList::Node
* nd
;
2828 wxDataViewItem item
;
2833 unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2835 wxDataViewModel
* model
= GetOwner()->GetModel();
2839 //Compose the a parent-chain of the finding item
2841 list
.DeleteContents( true );
2842 wxDataViewItem
it( item
);
2845 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2846 list
.Insert( pItem
);
2847 it
= model
->GetParent( it
);
2850 ItemToRowJob
job( item
, list
.GetFirst() );
2851 Walker(m_root
, job
);
2852 return job
.GetResult();
2855 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2857 if( !model
->IsContainer( item
) )
2860 wxDataViewItem i
= model
->GetFirstChild( item
);
2865 if( model
->IsContainer( i
) )
2867 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2869 n
->SetHasChildren( true ) ;
2874 node
->AddLeaf( i
.GetID() );
2876 i
= model
->GetNextSibling( i
);
2878 node
->SetSubTreeCount( num
);
2879 wxDataViewTreeNode
* n
= node
->GetParent();
2881 n
->ChangeSubTreeCount(num
);
2885 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2887 //First we define a invalid item to fetch the top-level elements
2888 wxDataViewItem item
;
2889 g_model
= GetOwner()->GetModel();
2890 BuildTreeHelper( model
, item
, m_root
);
2894 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2896 if( node
->GetNodeNumber() != 0 )
2898 int len
= node
->GetNodeNumber();
2900 wxDataViewTreeNodes nodes
= node
->GetNodes();
2901 for( ; i
< len
; i
++ )
2903 DestroyTreeHelper(nodes
[i
]);
2909 void wxDataViewMainWindow::DestroyTree()
2911 DestroyTreeHelper(m_root
);
2912 m_root
->SetSubTreeCount(0);
2916 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2918 if (event
.GetKeyCode() == WXK_TAB
)
2920 wxNavigationKeyEvent nevent
;
2921 nevent
.SetWindowChange( event
.ControlDown() );
2922 nevent
.SetDirection( !event
.ShiftDown() );
2923 nevent
.SetEventObject( GetParent()->GetParent() );
2924 nevent
.SetCurrentFocus( m_parent
);
2925 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
2929 // no item -> nothing to do
2930 if (!HasCurrentRow())
2936 // don't use m_linesPerPage directly as it might not be computed yet
2937 const int pageSize
= GetCountPerPage();
2938 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2940 switch ( event
.GetKeyCode() )
2943 if ( m_currentRow
> 0 )
2944 OnArrowChar( m_currentRow
- 1, event
);
2948 if ( m_currentRow
< GetRowCount() - 1 )
2949 OnArrowChar( m_currentRow
+ 1, event
);
2951 //Add the process for tree expanding/collapsing
2953 OnCollapsing(m_currentRow
);
2956 OnExpanding( m_currentRow
);
2960 OnArrowChar( GetRowCount() - 1, event
);
2965 OnArrowChar( 0, event
);
2970 int steps
= pageSize
- 1;
2971 int index
= m_currentRow
- steps
;
2975 OnArrowChar( index
, event
);
2981 int steps
= pageSize
- 1;
2982 unsigned int index
= m_currentRow
+ steps
;
2983 unsigned int count
= GetRowCount();
2984 if ( index
>= count
)
2987 OnArrowChar( index
, event
);
2996 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2998 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3000 // let the base handle mouse wheel events.
3005 int x
= event
.GetX();
3006 int y
= event
.GetY();
3007 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3009 wxDataViewColumn
*col
= NULL
;
3012 unsigned int cols
= GetOwner()->GetColumnCount();
3014 for (i
= 0; i
< cols
; i
++)
3016 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3018 continue; // skip it!
3020 if (x
< xpos
+ c
->GetWidth())
3025 xpos
+= c
->GetWidth();
3029 wxDataViewRenderer
*cell
= col
->GetRenderer();
3031 unsigned int current
= y
/ m_lineHeight
;
3033 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3035 // Unselect all if below the last row ?
3039 wxDataViewModel
*model
= GetOwner()->GetModel();
3041 if (event
.Dragging())
3043 if (m_dragCount
== 0)
3045 // we have to report the raw, physical coords as we want to be
3046 // able to call HitTest(event.m_pointDrag) from the user code to
3047 // get the item being dragged
3048 m_dragStart
= event
.GetPosition();
3053 if (m_dragCount
!= 3)
3056 if (event
.LeftIsDown())
3058 // Notify cell about drag
3067 bool forceClick
= false;
3069 if (event
.ButtonDClick())
3071 m_renameTimer
->Stop();
3072 m_lastOnSame
= false;
3075 if (event
.LeftDClick())
3077 if ( current
== m_lineLastClicked
)
3079 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3081 wxDataViewItem item
= GetItemByRow(current
);
3083 model
->GetValue( value
, item
, col
->GetModelColumn() );
3084 cell
->SetValue( value
);
3085 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3086 col
->GetWidth(), m_lineHeight
);
3087 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3093 // The first click was on another item, so don't interpret this as
3094 // a double click, but as a simple click instead
3101 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3103 // select single line
3104 SelectAllRows( false );
3105 SelectRow( m_lineSelectSingleOnUp
, true );
3108 //Process the event of user clicking the expander
3109 bool expander
= false;
3110 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3111 if( node
!=NULL
&& node
->HasChildren() )
3113 int indent
= node
->GetIndentLevel();
3114 indent
= GetOwner()->GetIndent()*indent
;
3115 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3116 if( rect
.Contains( x
, y
) )
3119 if( node
->IsOpen() )
3120 OnCollapsing(current
);
3122 OnExpanding( current
);
3126 //If the user click the expander, we do not do editing even if the column with expander are editable
3127 if (m_lastOnSame
&& !expander
)
3129 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3130 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3132 m_renameTimer
->Start( 100, true );
3136 m_lastOnSame
= false;
3137 m_lineSelectSingleOnUp
= (unsigned int)-1;
3141 // This is necessary, because after a DnD operation in
3142 // from and to ourself, the up event is swallowed by the
3143 // DnD code. So on next non-up event (which means here and
3144 // now) m_lineSelectSingleOnUp should be reset.
3145 m_lineSelectSingleOnUp
= (unsigned int)-1;
3148 if (event
.RightDown())
3150 m_lineBeforeLastClicked
= m_lineLastClicked
;
3151 m_lineLastClicked
= current
;
3153 // If the item is already selected, do not update the selection.
3154 // Multi-selections should not be cleared if a selected item is clicked.
3155 if (!IsRowSelected(current
))
3157 SelectAllRows(false);
3158 ChangeCurrentRow(current
);
3159 SelectRow(m_currentRow
,true);
3162 // notify cell about right click
3165 // Allow generation of context menu event
3168 else if (event
.MiddleDown())
3170 // notify cell about middle click
3173 if (event
.LeftDown() || forceClick
)
3179 m_lineBeforeLastClicked
= m_lineLastClicked
;
3180 m_lineLastClicked
= current
;
3182 unsigned int oldCurrentRow
= m_currentRow
;
3183 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3185 bool cmdModifierDown
= event
.CmdDown();
3186 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3188 if ( IsSingleSel() || !IsRowSelected(current
) )
3190 SelectAllRows( false );
3192 ChangeCurrentRow(current
);
3194 SelectRow(m_currentRow
,true);
3196 else // multi sel & current is highlighted & no mod keys
3198 m_lineSelectSingleOnUp
= current
;
3199 ChangeCurrentRow(current
); // change focus
3202 else // multi sel & either ctrl or shift is down
3204 if (cmdModifierDown
)
3206 ChangeCurrentRow(current
);
3208 ReverseRowSelection(m_currentRow
);
3210 else if (event
.ShiftDown())
3212 ChangeCurrentRow(current
);
3214 unsigned int lineFrom
= oldCurrentRow
,
3217 if ( lineTo
< lineFrom
)
3220 lineFrom
= m_currentRow
;
3223 SelectRows(lineFrom
, lineTo
, true);
3225 else // !ctrl, !shift
3227 // test in the enclosing if should make it impossible
3228 wxFAIL_MSG( _T("how did we get here?") );
3232 if (m_currentRow
!= oldCurrentRow
)
3233 RefreshRow( oldCurrentRow
);
3235 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3237 // Update selection here...
3240 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3241 (current
== oldCurrentRow
)) && oldWasSelected
;
3245 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3249 if (HasCurrentRow())
3255 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3259 if (HasCurrentRow())
3265 wxDataViewItem
wxDataViewMainWindow::GetSelection()
3267 if( m_selection
.GetCount() != 1 )
3268 return wxDataViewItem();
3270 return GetItemByRow( m_selection
.Item(0));
3273 //-----------------------------------------------------------------------------
3275 //-----------------------------------------------------------------------------
3277 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3279 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3280 EVT_SIZE(wxDataViewCtrl::OnSize
)
3283 wxDataViewCtrl::~wxDataViewCtrl()
3286 GetModel()->RemoveNotifier( m_notifier
);
3289 void wxDataViewCtrl::Init()
3294 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3295 const wxPoint
& pos
, const wxSize
& size
,
3296 long style
, const wxValidator
& validator
)
3298 if (!wxControl::Create( parent
, id
, pos
, size
,
3299 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3305 MacSetClipChildren( true ) ;
3308 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3310 if (HasFlag(wxDV_NO_HEADER
))
3311 m_headerArea
= NULL
;
3313 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3315 SetTargetWindow( m_clientArea
);
3317 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3319 sizer
->Add( m_headerArea
, 0, wxGROW
);
3320 sizer
->Add( m_clientArea
, 1, wxGROW
);
3327 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3331 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3334 // we need to process arrows ourselves for scrolling
3335 if ( nMsg
== WM_GETDLGCODE
)
3337 rc
|= DLGC_WANTARROWS
;
3345 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3347 // We need to override OnSize so that our scrolled
3348 // window a) does call Layout() to use sizers for
3349 // positioning the controls but b) does not query
3350 // the sizer for their size and use that for setting
3351 // the scrollable area as set that ourselves by
3352 // calling SetScrollbar() further down.
3359 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3361 if (!wxDataViewCtrlBase::AssociateModel( model
))
3364 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3366 model
->AddNotifier( m_notifier
);
3368 m_clientArea
->BuildTree(model
);
3370 m_clientArea
->UpdateDisplay();
3375 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3377 if (!wxDataViewCtrlBase::AppendColumn(col
))
3384 void wxDataViewCtrl::OnColumnChange()
3387 m_headerArea
->UpdateDisplay();
3389 m_clientArea
->UpdateDisplay();
3392 void wxDataViewCtrl::DoSetExpanderColumn()
3394 m_clientArea
->UpdateDisplay();
3397 void wxDataViewCtrl::DoSetIndent()
3399 m_clientArea
->UpdateDisplay();
3402 wxDataViewItem
wxDataViewCtrl::GetSelection()
3404 return m_clientArea
->GetSelection();
3407 /********************************************************************
3408 void wxDataViewCtrl::SetSelection( int row )
3410 m_clientArea->SelectRow(row, true);
3413 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
3415 m_clientArea->SelectRows(from, to, true);
3418 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
3420 m_clientArea->Select(aSelections);
3423 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
3428 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
3435 int wxDataViewCtrl::GetSelection() const
3442 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
3448 *********************************************************************/
3450 // !wxUSE_GENERICDATAVIEWCTRL
3453 // wxUSE_DATAVIEWCTRL