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_ARRAY_PTR( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
259 class wxDataViewTreeNode
262 wxDataViewTreeNode( wxDataViewTreeNode
* parent
)
263 { this->parent
= parent
;
270 //I don't know what I need to do in the destructure
271 ~wxDataViewTreeNode()
274 wxDataViewTreeNode
* GetParent() { return parent
; }
275 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
276 wxDataViewTreeNodes
& GetChildren() { return children
; }
277 void SetChildren( wxDataViewTreeNodes children
) { this->children
= children
; }
279 wxDataViewTreeNode
* GetChild( unsigned int n
) { return children
.Item( n
); }
280 void InsertChild( wxDataViewTreeNode
* child
, unsigned int n
) { children
.Insert( child
, n
); }
281 void AppendChild( wxDataViewTreeNode
* child
) { children
.Add( child
); }
283 wxDataViewItem
& GetItem() { return item
; }
284 void SetItem( const wxDataViewItem
& item
) { this->item
= item
; }
286 unsigned int GetChildrenNumber() { return children
.GetCount(); }
290 wxDataViewTreeNode
* node
= this;
291 while( node
->GetParent()->GetParent() != NULL
)
293 node
= node
->GetParent();
299 bool IsOpen() { return open
; }
300 void ToggleOpen(){ open
= !open
; }
301 bool HasChildren() { return hasChildren
; }
302 void SetHasChildren( bool has
){ hasChildren
= has
; }
304 wxDataViewTreeNode
* parent
;
305 wxDataViewTreeNodes children
;
311 //-----------------------------------------------------------------------------
312 // wxDataViewMainWindow
313 //-----------------------------------------------------------------------------
315 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
317 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
318 WX_DEFINE_LIST(ItemList
);
320 class wxDataViewMainWindow
: public wxWindow
323 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
325 const wxPoint
&pos
= wxDefaultPosition
,
326 const wxSize
&size
= wxDefaultSize
,
327 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
328 virtual ~wxDataViewMainWindow();
330 // notifications from wxDataViewModel
331 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
332 bool ItemDeleted( const wxDataViewItem
&item
);
333 bool ItemChanged( const wxDataViewItem
&item
);
334 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
337 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
338 wxDataViewCtrl
*GetOwner() { return m_owner
; }
339 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
341 void OnPaint( wxPaintEvent
&event
);
342 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
343 void OnChar( wxKeyEvent
&event
);
344 void OnMouse( wxMouseEvent
&event
);
345 void OnSetFocus( wxFocusEvent
&event
);
346 void OnKillFocus( wxFocusEvent
&event
);
348 void UpdateDisplay();
349 void RecalculateDisplay();
350 void OnInternalIdle();
352 void OnRenameTimer();
354 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
356 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
357 void ChangeCurrentRow( unsigned int row
);
359 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
360 bool IsEmpty() { return GetRowCount() == 0; }
362 int GetCountPerPage() const;
363 int GetEndOfLastCol() const;
364 unsigned int GetFirstVisibleRow() const;
365 //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
366 unsigned int GetLastVisibleRow();
367 unsigned int GetRowCount() ;
369 wxDataViewItem
GetSelection();
371 void Select( const wxArrayInt
& aSelections
);
372 void SelectAllRows( bool on
);
373 void SelectRow( unsigned int row
, bool on
);
374 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
375 void ReverseRowSelection( unsigned int row
);
376 bool IsRowSelected( unsigned int row
);
378 void RefreshRow( unsigned int row
);
379 void RefreshRows( unsigned int from
, unsigned int to
);
380 void RefreshRowsAfter( unsigned int firstRow
);
382 // returns the colour to be used for drawing the rules
383 wxColour
GetRuleColour() const
385 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
388 //void EnsureVisible( unsigned int row );
389 wxRect
GetLineRect( unsigned int row
) const;
391 //Some useful functions for row and item mapping
392 wxDataViewItem
GetItemByRow( unsigned int row
);
393 unsigned int GetRowByItem( const wxDataViewItem
& item
);
395 //Methods for building the mapping tree
396 void BuildTree( wxDataViewModel
* model
);
399 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
400 //We did not need this temporarily
401 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
403 int RecalculateCount() ;
405 void OnExpanding( unsigned int row
);
406 void OnCollapsing( unsigned int row
);
408 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
411 wxDataViewCtrl
*m_owner
;
415 wxDataViewColumn
*m_currentCol
;
416 unsigned int m_currentRow
;
417 wxDataViewSelection m_selection
;
419 wxDataViewRenameTimer
*m_renameTimer
;
427 // for double click logic
428 unsigned int m_lineLastClicked
,
429 m_lineBeforeLastClicked
,
430 m_lineSelectSingleOnUp
;
432 // the pen used to draw horiz/vertical rules
435 // the pen used to draw the expander and the lines
438 //This is the tree structure of the model
439 wxDataViewTreeNode
* m_root
;
442 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
443 DECLARE_EVENT_TABLE()
446 // ---------------------------------------------------------
447 // wxGenericDataViewModelNotifier
448 // ---------------------------------------------------------
450 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
453 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
454 { m_mainWindow
= mainWindow
; }
456 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
457 { return m_mainWindow
->ItemAdded( parent
, item
); }
458 virtual bool ItemDeleted( const wxDataViewItem
& item
)
459 { return m_mainWindow
->ItemDeleted( item
); }
460 virtual bool ItemChanged( const wxDataViewItem
& item
)
461 { return m_mainWindow
->ItemChanged(item
); }
462 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
463 { return m_mainWindow
->ValueChanged( item
, col
); }
464 virtual bool Cleared()
465 { return m_mainWindow
->Cleared(); }
467 wxDataViewMainWindow
*m_mainWindow
;
470 // ---------------------------------------------------------
471 // wxDataViewRenderer
472 // ---------------------------------------------------------
474 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
476 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
477 wxDataViewCellMode mode
,
479 wxDataViewRendererBase( varianttype
, mode
, align
)
486 wxDataViewRenderer::~wxDataViewRenderer()
492 wxDC
*wxDataViewRenderer::GetDC()
496 if (GetOwner() == NULL
)
498 if (GetOwner()->GetOwner() == NULL
)
500 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
506 // ---------------------------------------------------------
507 // wxDataViewCustomRenderer
508 // ---------------------------------------------------------
510 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
512 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
513 wxDataViewCellMode mode
, int align
) :
514 wxDataViewRenderer( varianttype
, mode
, align
)
518 // ---------------------------------------------------------
519 // wxDataViewTextRenderer
520 // ---------------------------------------------------------
522 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
524 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
525 wxDataViewCellMode mode
, int align
) :
526 wxDataViewCustomRenderer( varianttype
, mode
, align
)
530 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
532 m_text
= value
.GetString();
537 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
542 bool wxDataViewTextRenderer::HasEditorCtrl()
547 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
548 wxRect labelRect
, const wxVariant
&value
)
550 return new wxTextCtrl( parent
, wxID_ANY
, value
,
551 wxPoint(labelRect
.x
,labelRect
.y
),
552 wxSize(labelRect
.width
,labelRect
.height
) );
555 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
557 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
558 value
= text
->GetValue();
562 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
564 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
565 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
566 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
567 view
->GetForegroundColour();
569 dc
->SetTextForeground(col
);
570 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
575 wxSize
wxDataViewTextRenderer::GetSize() const
577 const wxDataViewCtrl
*view
= GetView();
581 view
->GetTextExtent( m_text
, &x
, &y
);
582 return wxSize( x
, y
);
584 return wxSize(80,20);
587 // ---------------------------------------------------------
588 // wxDataViewBitmapRenderer
589 // ---------------------------------------------------------
591 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
593 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
594 wxDataViewCellMode mode
, int align
) :
595 wxDataViewCustomRenderer( varianttype
, mode
, align
)
599 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
601 if (value
.GetType() == wxT("wxBitmap"))
603 if (value
.GetType() == wxT("wxIcon"))
609 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
614 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
617 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
618 else if (m_icon
.Ok())
619 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
624 wxSize
wxDataViewBitmapRenderer::GetSize() const
627 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
628 else if (m_icon
.Ok())
629 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
631 return wxSize(16,16);
634 // ---------------------------------------------------------
635 // wxDataViewToggleRenderer
636 // ---------------------------------------------------------
638 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
640 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
641 wxDataViewCellMode mode
, int align
) :
642 wxDataViewCustomRenderer( varianttype
, mode
, align
)
647 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
649 m_toggle
= value
.GetBool();
654 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
659 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
661 // User wxRenderer here
664 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
666 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
671 flags
|= wxCONTROL_CHECKED
;
672 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
673 flags
|= wxCONTROL_DISABLED
;
675 wxRendererNative::Get().DrawCheckBox(
676 GetOwner()->GetOwner(),
684 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
685 wxDataViewModel
*model
,
686 const wxDataViewItem
& item
, unsigned int col
)
688 bool value
= !m_toggle
;
689 wxVariant variant
= value
;
690 model
->SetValue( variant
, item
, col
);
691 model
->ValueChanged( item
, col
);
695 wxSize
wxDataViewToggleRenderer::GetSize() const
697 return wxSize(20,20);
700 // ---------------------------------------------------------
701 // wxDataViewProgressRenderer
702 // ---------------------------------------------------------
704 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
706 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
707 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
708 wxDataViewCustomRenderer( varianttype
, mode
, align
)
714 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
718 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
720 m_value
= (long) value
;
722 if (m_value
< 0) m_value
= 0;
723 if (m_value
> 100) m_value
= 100;
728 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
730 value
= (long) m_value
;
734 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
736 double pct
= (double)m_value
/ 100.0;
738 bar
.width
= (int)(cell
.width
* pct
);
739 dc
->SetPen( *wxTRANSPARENT_PEN
);
740 dc
->SetBrush( *wxBLUE_BRUSH
);
741 dc
->DrawRectangle( bar
);
743 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
744 dc
->SetPen( *wxBLACK_PEN
);
745 dc
->DrawRectangle( cell
);
750 wxSize
wxDataViewProgressRenderer::GetSize() const
752 return wxSize(40,12);
755 // ---------------------------------------------------------
756 // wxDataViewDateRenderer
757 // ---------------------------------------------------------
759 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
761 #if wxUSE_DATE_RENDERER_POPUP
763 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
766 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
767 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
768 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
773 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
774 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
775 sizer
->Add( m_cal
, 1, wxGROW
);
780 void OnCalendar( wxCalendarEvent
&event
);
782 wxCalendarCtrl
*m_cal
;
783 wxDataViewModel
*m_model
;
785 const wxDataViewItem
& m_item
;
788 virtual void OnDismiss()
793 DECLARE_EVENT_TABLE()
796 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
797 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
800 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
802 wxDateTime date
= event
.GetDate();
803 wxVariant value
= date
;
804 m_model
->SetValue( value
, m_item
, m_col
);
805 m_model
->ValueChanged( m_item
, m_col
);
809 #endif // wxUSE_DATE_RENDERER_POPUP
811 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
813 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
814 wxDataViewCellMode mode
, int align
) :
815 wxDataViewCustomRenderer( varianttype
, mode
, align
)
819 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
821 m_date
= value
.GetDateTime();
826 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
832 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
834 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
835 wxString tmp
= m_date
.FormatDate();
836 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
841 wxSize
wxDataViewDateRenderer::GetSize() const
843 const wxDataViewCtrl
* view
= GetView();
844 wxString tmp
= m_date
.FormatDate();
846 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
847 return wxSize(x
,y
+d
);
850 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
851 const wxDataViewItem
& item
, unsigned int col
)
854 model
->GetValue( variant
, item
, col
);
855 wxDateTime value
= variant
.GetDateTime();
857 #if wxUSE_DATE_RENDERER_POPUP
858 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
859 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
860 wxPoint pos
= wxGetMousePosition();
863 popup
->Popup( popup
->m_cal
);
864 #else // !wxUSE_DATE_RENDERER_POPUP
865 wxMessageBox(value
.Format());
866 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
870 // ---------------------------------------------------------
872 // ---------------------------------------------------------
874 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
876 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
877 unsigned int model_column
,
878 int width
, wxAlignment align
, int flags
) :
879 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
885 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
888 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
889 unsigned int model_column
,
890 int width
, wxAlignment align
, int flags
) :
891 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
896 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
899 wxDataViewColumn::~wxDataViewColumn()
903 void wxDataViewColumn::Init( int width
)
906 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
910 void wxDataViewColumn::SetResizeable( bool resizeable
)
913 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
915 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
918 void wxDataViewColumn::SetHidden( bool hidden
)
921 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
923 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
925 // tell our owner to e.g. update its scrollbars:
927 GetOwner()->OnColumnChange();
930 void wxDataViewColumn::SetSortable( bool sortable
)
933 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
935 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
937 // Update header button
939 GetOwner()->OnColumnChange();
942 void wxDataViewColumn::SetSortOrder( bool ascending
)
944 m_ascending
= ascending
;
946 // Update header button
948 GetOwner()->OnColumnChange();
951 bool wxDataViewColumn::IsSortOrderAscending() const
956 void wxDataViewColumn::SetInternalWidth( int width
)
960 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
961 if (m_owner
&& m_owner
->m_clientArea
)
962 m_owner
->m_clientArea
->RecalculateDisplay();
965 void wxDataViewColumn::SetWidth( int width
)
967 m_owner
->m_headerArea
->UpdateDisplay();
969 SetInternalWidth(width
);
973 //-----------------------------------------------------------------------------
974 // wxDataViewHeaderWindowBase
975 //-----------------------------------------------------------------------------
977 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
979 wxWindow
*parent
= GetParent();
980 wxDataViewEvent
le(type
, parent
->GetId());
982 le
.SetEventObject(parent
);
984 le
.SetDataViewColumn(GetColumn(n
));
985 le
.SetModel(GetOwner()->GetModel());
987 // for events created by wxDataViewHeaderWindow the
988 // row / value fields are not valid
990 parent
->GetEventHandler()->ProcessEvent(le
);
993 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
995 // implemented in msw/listctrl.cpp:
996 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
998 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1000 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1001 const wxPoint
&pos
, const wxSize
&size
,
1002 const wxString
&name
)
1006 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1009 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1010 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1011 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1012 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1014 // create the native WC_HEADER window:
1015 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1016 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1017 m_hWnd
= CreateWindowEx(0,
1028 wxLogLastError(_T("CreateWindowEx"));
1032 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1033 // to call wxDataViewHeaderWindow::MSWOnNotify
1034 SubclassWin(m_hWnd
);
1036 // the following is required to get the default win's font for
1037 // header windows and must be done befor sending the HDM_LAYOUT msg
1044 // Retrieve the bounding rectangle of the parent window's
1045 // client area, and then request size and position values
1046 // from the header control.
1047 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1049 hdl
.prc
= &rcParent
;
1051 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1053 wxLogLastError(_T("SendMessage"));
1057 // Set the size, position, and visibility of the header control.
1058 SetWindowPos((HWND
)m_hWnd
,
1062 wp
.flags
| SWP_SHOWWINDOW
);
1064 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1065 // a wxBoxSizer and in order to avoid super-big header windows,
1066 // we need to set our height as fixed
1067 SetMinSize(wxSize(-1, wp
.cy
));
1068 SetMaxSize(wxSize(-1, wp
.cy
));
1073 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1078 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1080 // remove old columns
1081 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1082 Header_DeleteItem((HWND
)m_hWnd
, 0);
1084 // add the updated array of columns to the header control
1085 unsigned int cols
= GetOwner()->GetColumnCount();
1086 unsigned int added
= 0;
1087 for (unsigned int i
= 0; i
< cols
; i
++)
1089 wxDataViewColumn
*col
= GetColumn( i
);
1090 if (col
->IsHidden())
1091 continue; // don't add it!
1094 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1095 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1096 hdi
.cxy
= col
->GetWidth();
1097 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1098 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1100 // lParam is reserved for application's use:
1101 // we store there the column index to use it later in MSWOnNotify
1102 // (since columns may have been hidden)
1103 hdi
.lParam
= (LPARAM
)i
;
1105 // the native wxMSW implementation of the header window
1106 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1107 // on the right: to correct this effect we make the column
1108 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1110 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1112 switch (col
->GetAlignment())
1115 hdi
.fmt
|= HDF_LEFT
;
1117 case wxALIGN_CENTER
:
1118 case wxALIGN_CENTER_HORIZONTAL
:
1119 hdi
.fmt
|= HDF_CENTER
;
1122 hdi
.fmt
|= HDF_RIGHT
;
1126 // such alignment is not allowed for the column header!
1130 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1131 (WPARAM
)added
, (LPARAM
)&hdi
);
1136 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1140 // NOTE: we don't just return nmHDR->iItem because when there are
1141 // hidden columns, nmHDR->iItem may be different from
1142 // nmHDR->pitem->lParam
1144 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1146 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1151 item
.mask
= HDI_LPARAM
;
1152 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1154 return (unsigned int)item
.lParam
;
1157 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1159 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1161 // is it a message from the header?
1162 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1163 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1165 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1166 switch ( nmhdr
->code
)
1168 case HDN_BEGINTRACK
:
1169 // user has started to resize a column:
1170 // do we need to veto it?
1171 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1179 // user has started to reorder a column
1182 case HDN_ITEMCHANGING
:
1183 if (nmHDR
->pitem
!= NULL
&&
1184 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1186 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1187 if (nmHDR
->pitem
->cxy
< minWidth
)
1189 // do not allow the user to resize this column under
1190 // its minimal width:
1196 case HDN_ITEMCHANGED
: // user is resizing a column
1197 case HDN_ENDTRACK
: // user has finished resizing a column
1198 case HDN_ENDDRAG
: // user has finished reordering a column
1200 // update the width of the modified column:
1201 if (nmHDR
->pitem
!= NULL
&&
1202 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1204 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1205 unsigned int w
= nmHDR
->pitem
->cxy
;
1206 wxDataViewColumn
*col
= GetColumn(idx
);
1208 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1209 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1210 w
-= COLUMN_WIDTH_OFFSET
;
1212 if (w
>= (unsigned)col
->GetMinWidth())
1213 col
->SetInternalWidth(w
);
1219 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1220 wxEventType evt
= nmHDR
->iButton
== 0 ?
1221 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1222 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1223 SendEvent(evt
, idx
);
1229 // NOTE: for some reason (i.e. for a bug in Windows)
1230 // the HDN_ITEMCLICK notification is not sent on
1231 // right clicks, so we need to handle NM_RCLICK
1234 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1235 if (column
!= wxNOT_FOUND
)
1238 item
.mask
= HDI_LPARAM
;
1239 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1241 // 'idx' may be different from 'column' if there are
1242 // hidden columns...
1243 unsigned int idx
= (unsigned int)item
.lParam
;
1244 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1250 case HDN_GETDISPINFOW
:
1251 // see wxListCtrl::MSWOnNotify for more info!
1254 case HDN_ITEMDBLCLICK
:
1256 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1257 int w
= GetOwner()->GetBestColumnWidth(idx
);
1259 // update the native control:
1261 ZeroMemory(&hd
, sizeof(hd
));
1262 hd
.mask
= HDI_WIDTH
;
1264 Header_SetItem(GetHwnd(),
1265 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1268 // update the wxDataViewColumn class:
1269 GetColumn(idx
)->SetInternalWidth(w
);
1274 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1280 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1281 const wxRect
*WXUNUSED(rect
))
1283 wxSize ourSz
= GetClientSize();
1284 wxSize ownerSz
= m_owner
->GetClientSize();
1286 // where should the (logical) origin of this window be placed?
1288 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1290 // put this window on top of our parent and
1291 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1292 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1296 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1297 int WXUNUSED(w
), int WXUNUSED(h
),
1300 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1301 // the wxDataViewCtrl window gets resized: the following dummy call
1302 // to ScrollWindow() is required in order to get this header window
1303 // correctly repainted when it's (horizontally) scrolled:
1308 #else // !defined(__WXMSW__)
1310 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1311 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1312 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1313 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1314 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1317 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1318 const wxPoint
&pos
, const wxSize
&size
,
1319 const wxString
&name
)
1323 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1326 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1327 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1328 SetOwnForegroundColour( attr
.colFg
);
1329 SetOwnBackgroundColour( attr
.colBg
);
1331 SetOwnFont( attr
.font
);
1333 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1334 // a wxBoxSizer and in order to avoid super-big header windows,
1335 // we need to set our height as fixed
1336 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1337 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1342 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1345 GetClientSize( &w
, &h
);
1347 wxAutoBufferedPaintDC
dc( this );
1349 dc
.SetBackground(GetBackgroundColour());
1353 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1356 m_owner
->GetViewStart( &x
, NULL
);
1358 // account for the horz scrollbar offset
1359 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1361 dc
.SetFont( GetFont() );
1363 unsigned int cols
= GetOwner()->GetColumnCount();
1366 for (i
= 0; i
< cols
; i
++)
1368 wxDataViewColumn
*col
= GetColumn( i
);
1369 if (col
->IsHidden())
1370 continue; // skip it!
1372 int cw
= col
->GetWidth();
1375 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1376 if (col
->IsSortable())
1378 if (col
->IsSortOrderAscending())
1379 sortArrow
= wxHDR_SORT_ICON_UP
;
1381 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1384 wxRendererNative::Get().DrawHeaderButton
1388 wxRect(xpos
, 0, cw
, ch
-1),
1389 m_parent
->IsEnabled() ? 0
1390 : (int)wxCONTROL_DISABLED
,
1394 // align as required the column title:
1396 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1397 switch (col
->GetAlignment())
1400 x
+= HEADER_HORIZ_BORDER
;
1402 case wxALIGN_CENTER
:
1403 case wxALIGN_CENTER_HORIZONTAL
:
1404 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1407 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1411 // always center the title vertically:
1412 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1414 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1416 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1417 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1418 dc
.DrawText( col
->GetTitle(), x
, y
);
1419 dc
.DestroyClippingRegion();
1425 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1427 GetParent()->SetFocus();
1431 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1433 // we want to work with logical coords
1435 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1436 int y
= event
.GetY();
1440 // we don't draw the line beyond our window,
1441 // but we allow dragging it there
1443 GetClientSize( &w
, NULL
);
1444 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1447 // erase the line if it was drawn
1451 if (event
.ButtonUp())
1453 m_isDragging
= false;
1459 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1462 GetOwner()->Refresh();
1466 m_currentX
= wxMax(m_minX
+ 7, x
);
1468 // draw in the new location
1469 if (m_currentX
< w
) DrawCurrent();
1473 else // not dragging
1476 m_column
= wxNOT_FOUND
;
1478 bool hit_border
= false;
1480 // end of the current column
1483 // find the column where this event occured
1484 int countCol
= m_owner
->GetColumnCount();
1485 for (int column
= 0; column
< countCol
; column
++)
1487 wxDataViewColumn
*p
= GetColumn(column
);
1490 continue; // skip if not shown
1492 xpos
+= p
->GetWidth();
1494 if ((abs(x
-xpos
) < 3) && (y
< 22))
1502 // inside the column
1509 if (m_column
== wxNOT_FOUND
)
1512 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1513 if (event
.LeftDClick() && resizeable
)
1515 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1518 else if (event
.LeftDown() || event
.RightUp())
1520 if (hit_border
&& event
.LeftDown() && resizeable
)
1522 m_isDragging
= true;
1527 else // click on a column
1529 wxEventType evt
= event
.LeftDown() ?
1530 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1531 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1532 SendEvent(evt
, m_column
);
1535 else if (event
.Moving())
1537 if (hit_border
&& resizeable
)
1538 m_currentCursor
= m_resizeCursor
;
1540 m_currentCursor
= wxSTANDARD_CURSOR
;
1542 SetCursor(*m_currentCursor
);
1547 void wxGenericDataViewHeaderWindow::DrawCurrent()
1549 int x1
= m_currentX
;
1551 ClientToScreen (&x1
, &y1
);
1553 int x2
= m_currentX
-1;
1555 ++x2
; // but why ????
1558 m_owner
->GetClientSize( NULL
, &y2
);
1559 m_owner
->ClientToScreen( &x2
, &y2
);
1562 dc
.SetLogicalFunction(wxINVERT
);
1563 dc
.SetPen(m_penCurrent
);
1564 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1566 dc
.DrawLine(x1
, y1
, x2
, y2
);
1569 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1573 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1574 m_owner
->GetViewStart( &x
, NULL
);
1576 // shift the DC origin to match the position of the main window horizontal
1577 // scrollbar: this allows us to always use logical coords
1578 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1581 #endif // defined(__WXMSW__)
1583 //-----------------------------------------------------------------------------
1584 // wxDataViewRenameTimer
1585 //-----------------------------------------------------------------------------
1587 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1592 void wxDataViewRenameTimer::Notify()
1594 m_owner
->OnRenameTimer();
1597 //-----------------------------------------------------------------------------
1598 // wxDataViewMainWindow
1599 //-----------------------------------------------------------------------------
1601 //The tree building helper, declared firstly
1602 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1604 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1606 if (row1
> row2
) return 1;
1607 if (row1
== row2
) return 0;
1612 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1614 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1615 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1616 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1617 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1618 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1619 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1622 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1623 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1624 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1625 m_selection( wxDataViewSelectionCmp
)
1630 m_lastOnSame
= false;
1631 m_renameTimer
= new wxDataViewRenameTimer( this );
1633 // TODO: user better initial values/nothing selected
1634 m_currentCol
= NULL
;
1637 // TODO: we need to calculate this smartly
1644 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1647 m_dragStart
= wxPoint(0,0);
1648 m_lineLastClicked
= (unsigned int) -1;
1649 m_lineBeforeLastClicked
= (unsigned int) -1;
1650 m_lineSelectSingleOnUp
= (unsigned int) -1;
1654 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1655 SetBackgroundColour( *wxWHITE
);
1657 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1659 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1660 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1661 //Some new added code to deal with the tree structure
1662 m_root
= new wxDataViewTreeNode( NULL
);
1663 m_root
->SetHasChildren(true);
1665 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1670 wxDataViewMainWindow::~wxDataViewMainWindow()
1673 delete m_renameTimer
;
1676 void wxDataViewMainWindow::OnRenameTimer()
1678 // We have to call this here because changes may just have
1679 // been made and no screen update taken place.
1684 unsigned int cols
= GetOwner()->GetColumnCount();
1686 for (i
= 0; i
< cols
; i
++)
1688 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1690 continue; // skip it!
1692 if (c
== m_currentCol
)
1694 xpos
+= c
->GetWidth();
1696 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1697 m_currentCol
->GetWidth(), m_lineHeight
);
1699 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1700 &labelRect
.x
, &labelRect
.y
);
1702 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1703 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1706 //------------------------------------------------------------------
1707 // Helper class for do operation on the tree node
1708 //------------------------------------------------------------------
1715 //The return value control how the tree-walker tranverse the tree
1716 // 0: Job done, stop tranverse and return
1717 // 1: Ignore the current node's subtree and continue
1718 // 2: Job not done, continue
1719 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1720 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1723 class ItemAddJob
: public DoJob
1726 ItemAddJob( const wxDataViewItem
& parent
, const wxDataViewItem
& item
, int * count
)
1727 { this->parent
= parent
; this->item
= item
; m_count
= count
; }
1728 virtual ~ItemAddJob(){};
1730 virtual int operator() ( wxDataViewTreeNode
* node
)
1732 if( node
->GetItem() == parent
)
1734 node
->SetHasChildren( true );
1735 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1736 newnode
->SetItem(item
);
1737 node
->AppendChild( newnode
);
1746 wxDataViewItem parent
, item
;
1749 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1751 if( node
==NULL
|| !node
->HasChildren())
1754 wxDataViewTreeNodes nodes
= node
->GetChildren();
1755 int len
= node
->GetChildrenNumber();
1757 for( ; i
< len
; i
++ )
1759 wxDataViewTreeNode
* n
= nodes
[i
];
1771 if( Walker( n
, func
) )
1777 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1779 wxDataViewTreeNode
* node
;
1780 node
= FindNode(parent
);
1785 node
->SetHasChildren( true );
1786 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1787 newnode
->SetItem(item
);
1788 node
->AppendChild( newnode
);
1794 class ItemDeleteJob
: public DoJob
1797 ItemDeleteJob( const wxDataViewItem
& item
, int * count
) { m_item
= item
; m_count
= count
; }
1798 virtual ~ItemDeleteJob(){}
1799 virtual int operator() ( wxDataViewTreeNode
* node
)
1801 if( node
->GetItem() == m_item
)
1803 node
->GetParent()->GetChildren().Remove( node
);
1813 wxDataViewItem m_item
;
1816 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& item
)
1818 wxDataViewTreeNode
* node
;
1819 node
= FindNode(item
);
1826 wxDataViewTreeNode
* parent
= node
->GetParent();
1827 parent
->GetChildren().Remove( node
);
1828 if( parent
->GetChildrenNumber() == 0)
1829 parent
->SetHasChildren( false );
1836 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1838 unsigned int row
= GetRowByItem(item
);
1843 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int WXUNUSED(col
) )
1845 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1846 /*#define MAX_VIRTUAL_WIDTH 100000
1848 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1849 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1850 Refresh( true, &rect );
1854 unsigned int row
= GetRowByItem(item
);
1859 bool wxDataViewMainWindow::Cleared()
1866 void wxDataViewMainWindow::UpdateDisplay()
1871 void wxDataViewMainWindow::OnInternalIdle()
1873 wxWindow::OnInternalIdle();
1877 RecalculateDisplay();
1882 void wxDataViewMainWindow::RecalculateDisplay()
1884 wxDataViewModel
*model
= GetOwner()->GetModel();
1891 int width
= GetEndOfLastCol();
1892 int height
= GetRowCount() * m_lineHeight
;
1894 SetVirtualSize( width
, height
);
1895 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1900 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1902 wxWindow::ScrollWindow( dx
, dy
, rect
);
1904 if (GetOwner()->m_headerArea
)
1905 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1908 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1910 wxDataViewModel
*model
= GetOwner()->GetModel();
1911 wxAutoBufferedPaintDC
dc( this );
1914 dc
.SetBackground(GetBackgroundColour());
1916 GetOwner()->PrepareDC( dc
);
1917 dc
.SetFont( GetFont() );
1919 wxRect update
= GetUpdateRegion().GetBox();
1920 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1922 // compute which items needs to be redrawn
1923 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1924 unsigned int item_count
=
1925 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1926 (int)(GetRowCount( )- item_start
) );
1927 unsigned int item_last
= item_start
+ item_count
;
1929 // compute which columns needs to be redrawn
1930 unsigned int cols
= GetOwner()->GetColumnCount();
1931 unsigned int col_start
= 0;
1932 unsigned int x_start
= 0;
1933 for (x_start
= 0; col_start
< cols
; col_start
++)
1935 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
1936 if (col
->IsHidden())
1937 continue; // skip it!
1939 unsigned int w
= col
->GetWidth();
1940 if (x_start
+w
>= (unsigned int)update
.x
)
1946 unsigned int col_last
= col_start
;
1947 unsigned int x_last
= x_start
;
1948 for (; col_last
< cols
; col_last
++)
1950 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
1951 if (col
->IsHidden())
1952 continue; // skip it!
1954 if (x_last
> (unsigned int)update
.GetRight())
1957 x_last
+= col
->GetWidth();
1960 // Draw horizontal rules if required
1961 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1963 dc
.SetPen(m_penRule
);
1964 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1966 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1968 int y
= i
* m_lineHeight
;
1969 dc
.DrawLine(x_start
, y
, x_last
, y
);
1973 // Draw vertical rules if required
1974 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1976 dc
.SetPen(m_penRule
);
1977 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1980 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1982 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
1983 if (col
->IsHidden())
1984 continue; // skip it
1986 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1987 x
, item_last
* m_lineHeight
);
1989 x
+= col
->GetWidth();
1992 // Draw last vertical rule
1993 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1994 x
, item_last
* m_lineHeight
);
1997 // redraw the background for the items which are selected/current
1998 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2000 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2001 if (selected
|| item
== m_currentRow
)
2003 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2004 if (item
== m_currentRow
)
2005 flags
|= wxCONTROL_CURRENT
;
2007 flags
|= wxCONTROL_FOCUSED
;
2009 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2010 wxRendererNative::Get().DrawItemSelectionRect
2020 // redraw all cells for all rows which must be repainted and for all columns
2022 cell_rect
.x
= x_start
;
2023 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2024 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2026 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2027 wxDataViewRenderer
*cell
= col
->GetRenderer();
2028 cell_rect
.width
= col
->GetWidth();
2030 if (col
->IsHidden())
2031 continue; // skipt it!
2033 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2035 // get the cell value and set it into the renderer
2037 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2041 wxDataViewItem dataitem
= node
->GetItem();
2042 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2043 cell
->SetValue( value
);
2045 // update the y offset
2046 cell_rect
.y
= item
* m_lineHeight
;
2048 //Draw the expander here. Please notice that I use const number for all pixel data. When the final API are determined
2049 //I will change this to the data member of the class wxDataViewCtrl
2050 int indent
= node
->GetIndentLevel();
2051 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
2053 //Calculate the indent first
2054 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2056 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2057 // change the cell_rect.x to the appropriate pos
2058 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2059 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2060 dc
.SetPen( m_penExpander
);
2061 dc
.SetBrush( wxNullBrush
);
2062 if( node
->HasChildren() )
2064 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2065 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2066 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2067 if( node
->IsOpen() )
2068 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2070 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2074 // I am wandering whether we should draw dot lines between tree nodes
2077 //force the expander column to left-center align
2078 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2082 // cannot be bigger than allocated space
2083 wxSize size
= cell
->GetSize();
2084 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2085 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2086 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2088 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2089 int align
= cell
->GetAlignment();
2091 // horizontal alignment:
2092 item_rect
.x
= cell_rect
.x
;
2093 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2094 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2095 else if (align
& wxALIGN_RIGHT
)
2096 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2097 //else: wxALIGN_LEFT is the default
2099 // vertical alignment:
2100 item_rect
.y
= cell_rect
.y
;
2101 if (align
& wxALIGN_CENTER_VERTICAL
)
2102 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2103 else if (align
& wxALIGN_BOTTOM
)
2104 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2105 //else: wxALIGN_TOP is the default
2108 item_rect
.x
+= PADDING_RIGHTLEFT
;
2109 item_rect
.y
+= PADDING_TOPBOTTOM
;
2110 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2111 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2113 //Here we add the tree indent
2114 item_rect
.x
+= indent
;
2117 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2118 state
|= wxDATAVIEW_CELL_SELECTED
;
2120 // TODO: it would be much more efficient to create a clipping
2121 // region for the entire column being rendered (in the OnPaint
2122 // of wxDataViewMainWindow) instead of a single clip region for
2123 // each cell. However it would mean that each renderer should
2124 // respect the given wxRect's top & bottom coords, eventually
2125 // violating only the left & right coords - however the user can
2126 // make its own renderer and thus we cannot be sure of that.
2127 dc
.SetClippingRegion( item_rect
);
2128 cell
->Render( item_rect
, &dc
, state
);
2129 dc
.DestroyClippingRegion();
2132 cell_rect
.x
+= cell_rect
.width
;
2136 int wxDataViewMainWindow::GetCountPerPage() const
2138 wxSize size
= GetClientSize();
2139 return size
.y
/ m_lineHeight
;
2142 int wxDataViewMainWindow::GetEndOfLastCol() const
2146 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2148 const wxDataViewColumn
*c
=
2149 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2152 width
+= c
->GetWidth();
2157 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2161 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2163 return y
/ m_lineHeight
;
2166 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2168 wxSize client_size
= GetClientSize();
2169 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2170 &client_size
.x
, &client_size
.y
);
2172 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2175 unsigned int wxDataViewMainWindow::GetRowCount()
2177 if ( m_count
== -1 )
2179 m_count
= RecalculateCount();
2181 GetVirtualSize( &width
, &height
);
2182 height
= m_count
* m_lineHeight
;
2184 SetVirtualSize( width
, height
);
2189 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2196 void wxDataViewMainWindow::SelectAllRows( bool on
)
2203 m_selection
.Clear();
2204 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2205 m_selection
.Add( i
);
2210 unsigned int first_visible
= GetFirstVisibleRow();
2211 unsigned int last_visible
= GetLastVisibleRow();
2213 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2215 unsigned int row
= m_selection
[i
];
2216 if ((row
>= first_visible
) && (row
<= last_visible
))
2219 m_selection
.Clear();
2223 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2225 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2229 m_selection
.Add( row
);
2237 m_selection
.Remove( row
);
2243 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2247 unsigned int tmp
= from
;
2253 for (i
= from
; i
<= to
; i
++)
2255 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2258 m_selection
.Add( i
);
2263 m_selection
.Remove( i
);
2266 RefreshRows( from
, to
);
2269 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2271 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2273 int n
= aSelections
[i
];
2275 m_selection
.Add( n
);
2280 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2282 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2283 m_selection
.Add( row
);
2285 m_selection
.Remove( row
);
2289 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2291 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2294 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2296 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2297 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2299 wxSize client_size
= GetClientSize();
2300 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2301 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2302 if (intersect_rect
.width
> 0)
2303 Refresh( true, &intersect_rect
);
2306 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2310 unsigned int tmp
= to
;
2315 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2316 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2318 wxSize client_size
= GetClientSize();
2319 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2320 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2321 if (intersect_rect
.width
> 0)
2322 Refresh( true, &intersect_rect
);
2325 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2327 unsigned int count
= GetRowCount();
2328 if (firstRow
> count
)
2331 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2332 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2334 wxSize client_size
= GetClientSize();
2335 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2336 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2337 if (intersect_rect
.width
> 0)
2338 Refresh( true, &intersect_rect
);
2341 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2343 wxCHECK_RET( newCurrent
< GetRowCount(),
2344 _T("invalid item index in OnArrowChar()") );
2346 // if there is no selection, we cannot move it anywhere
2347 if (!HasCurrentRow())
2350 unsigned int oldCurrent
= m_currentRow
;
2352 // in single selection we just ignore Shift as we can't select several
2354 if ( event
.ShiftDown() && !IsSingleSel() )
2356 RefreshRow( oldCurrent
);
2358 ChangeCurrentRow( newCurrent
);
2360 // select all the items between the old and the new one
2361 if ( oldCurrent
> newCurrent
)
2363 newCurrent
= oldCurrent
;
2364 oldCurrent
= m_currentRow
;
2367 SelectRows( oldCurrent
, newCurrent
, true );
2371 RefreshRow( oldCurrent
);
2373 // all previously selected items are unselected unless ctrl is held
2374 if ( !event
.ControlDown() )
2375 SelectAllRows(false);
2377 ChangeCurrentRow( newCurrent
);
2379 if ( !event
.ControlDown() )
2380 SelectRow( m_currentRow
, true );
2382 RefreshRow( m_currentRow
);
2385 //EnsureVisible( m_currentRow );
2388 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2392 rect
.y
= m_lineHeight
* row
;
2393 rect
.width
= GetEndOfLastCol();
2394 rect
.height
= m_lineHeight
;
2399 class RowToItemJob
: public DoJob
2402 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2403 virtual ~RowToItemJob(){};
2405 virtual int operator() ( wxDataViewTreeNode
* node
)
2407 if( current
== static_cast<int>(row
))
2409 ret
= node
->GetItem() ;
2413 if ( node
->IsOpen())
2419 wxDataViewItem
GetResult(){ return ret
; }
2426 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2428 RowToItemJob
job( row
, 0 );
2429 Walker( m_root
, job
);
2430 return job
.GetResult();
2433 class RowToTreeNodeJob
: public DoJob
2436 RowToTreeNodeJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
; ret
= NULL
; }
2437 virtual ~RowToTreeNodeJob(){};
2439 virtual int operator() ( wxDataViewTreeNode
* node
)
2441 if( current
== static_cast<int>(row
))
2447 if ( node
->IsOpen())
2453 wxDataViewTreeNode
* GetResult(){ return ret
; }
2457 wxDataViewTreeNode
* ret
;
2461 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2463 RowToTreeNodeJob
job( row
, 0 );
2464 Walker( m_root
, job
);
2465 return job
.GetResult();
2468 class CountJob
: public DoJob
2471 CountJob(){ count
= 0 ; }
2472 virtual ~CountJob(){};
2474 virtual int operator () ( wxDataViewTreeNode
* node
)
2477 if ( node
->IsOpen())
2483 unsigned int GetResult()
2491 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2493 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2496 if( node
->HasChildren())
2497 if( !node
->IsOpen())
2500 //Here I build the children of current node
2501 if( node
->GetChildrenNumber() == 0 )
2502 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2508 SelectRow( row
, false );
2509 SelectRow( row
+ 1, true );
2510 ChangeCurrentRow( row
+ 1 );
2515 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2517 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2520 if( node
->HasChildren() && node
->IsOpen() )
2525 //RefreshRows(row,GetLastVisibleRow());
2529 node
= node
->GetParent();
2532 int parent
= GetRowByItem( node
->GetItem()) ;
2533 SelectRow( row
, false);
2534 SelectRow(parent
, true );
2535 ChangeCurrentRow( parent
);
2541 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2543 wxDataViewModel
* model
= GetOwner()->GetModel();
2547 //Compose the a parent-chain of the finding item
2549 list
.DeleteContents( true );
2550 wxDataViewItem
it( item
);
2553 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2554 list
.Insert( pItem
);
2555 it
= model
->GetParent( it
);
2558 //Find the item along the parent-chain.
2559 //This algorithm is designed to speed up the node-finding method
2561 wxDataViewTreeNode
* node
= m_root
;
2562 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2564 if( node
->HasChildren() )
2566 if( node
->GetChildrenNumber() == 0 )
2567 BuildTreeHelper(model
, node
->GetItem(), node
);
2569 int len
= node
->GetChildrenNumber();
2570 wxDataViewTreeNodes nodes
= node
->GetChildren();
2572 for( ; j
< len
; j
++)
2574 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2580 // Whenever we can't find the node in any level, return NULL to indicate the item can't be found
2593 int wxDataViewMainWindow::RecalculateCount()
2596 Walker( m_root
, job
);
2597 return job
.GetResult();
2600 class ItemToRowJob
: public DoJob
2603 ItemToRowJob(const wxDataViewItem
& item
){ this->item
= item
; ret
= 0 ; }
2604 virtual ~ItemToRowJob(){};
2606 virtual int operator() ( wxDataViewTreeNode
* node
)
2609 if( node
->GetItem() == item
)
2618 //the row number is begin from zero
2619 int GetResult(){ return ret
-1 ; }
2621 wxDataViewItem item
;
2625 unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2627 ItemToRowJob
job( item
);
2628 Walker(m_root
, job
);
2629 return job
.GetResult();
2632 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2634 if( !model
->IsContainer( item
) )
2637 wxDataViewItem i
= model
->GetFirstChild( item
);
2640 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2642 n
->SetHasChildren( model
->IsContainer( i
)) ;
2643 node
->AppendChild(n
);
2644 //BuildTreeHelper( model, i, n) ;
2645 i
= model
->GetNextSibling( i
);
2649 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2651 //First we define a invalid item to fetch the top-level elements
2652 wxDataViewItem item
;
2653 BuildTreeHelper( model
, item
, m_root
);
2657 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2659 if( node
->HasChildren() )
2661 int len
= node
->GetChildrenNumber();
2663 wxDataViewTreeNodes nodes
= node
->GetChildren();
2664 for( ; i
< len
; i
++ )
2666 DestroyTreeHelper(nodes
[i
]);
2672 void wxDataViewMainWindow::DestroyTree()
2674 DestroyTreeHelper(m_root
);
2678 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2680 if (event
.GetKeyCode() == WXK_TAB
)
2682 wxNavigationKeyEvent nevent
;
2683 nevent
.SetWindowChange( event
.ControlDown() );
2684 nevent
.SetDirection( !event
.ShiftDown() );
2685 nevent
.SetEventObject( GetParent()->GetParent() );
2686 nevent
.SetCurrentFocus( m_parent
);
2687 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
2691 // no item -> nothing to do
2692 if (!HasCurrentRow())
2698 // don't use m_linesPerPage directly as it might not be computed yet
2699 const int pageSize
= GetCountPerPage();
2700 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2702 switch ( event
.GetKeyCode() )
2705 if ( m_currentRow
> 0 )
2706 OnArrowChar( m_currentRow
- 1, event
);
2710 if ( m_currentRow
< GetRowCount() - 1 )
2711 OnArrowChar( m_currentRow
+ 1, event
);
2713 //Add the process for tree expanding/collapsing
2715 OnCollapsing(m_currentRow
);
2718 OnExpanding( m_currentRow
);
2722 OnArrowChar( GetRowCount() - 1, event
);
2727 OnArrowChar( 0, event
);
2732 int steps
= pageSize
- 1;
2733 int index
= m_currentRow
- steps
;
2737 OnArrowChar( index
, event
);
2743 int steps
= pageSize
- 1;
2744 unsigned int index
= m_currentRow
+ steps
;
2745 unsigned int count
= GetRowCount();
2746 if ( index
>= count
)
2749 OnArrowChar( index
, event
);
2758 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2760 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2762 // let the base handle mouse wheel events.
2767 int x
= event
.GetX();
2768 int y
= event
.GetY();
2769 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2771 wxDataViewColumn
*col
= NULL
;
2774 unsigned int cols
= GetOwner()->GetColumnCount();
2776 for (i
= 0; i
< cols
; i
++)
2778 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2780 continue; // skip it!
2782 if (x
< xpos
+ c
->GetWidth())
2787 xpos
+= c
->GetWidth();
2791 wxDataViewRenderer
*cell
= col
->GetRenderer();
2793 unsigned int current
= y
/ m_lineHeight
;
2795 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
2797 // Unselect all if below the last row ?
2801 wxDataViewModel
*model
= GetOwner()->GetModel();
2803 if (event
.Dragging())
2805 if (m_dragCount
== 0)
2807 // we have to report the raw, physical coords as we want to be
2808 // able to call HitTest(event.m_pointDrag) from the user code to
2809 // get the item being dragged
2810 m_dragStart
= event
.GetPosition();
2815 if (m_dragCount
!= 3)
2818 if (event
.LeftIsDown())
2820 // Notify cell about drag
2829 bool forceClick
= false;
2831 if (event
.ButtonDClick())
2833 m_renameTimer
->Stop();
2834 m_lastOnSame
= false;
2837 if (event
.LeftDClick())
2839 if ( current
== m_lineLastClicked
)
2841 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
2843 wxDataViewItem item
= GetItemByRow(current
);
2845 model
->GetValue( value
, item
, col
->GetModelColumn() );
2846 cell
->SetValue( value
);
2847 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
2848 col
->GetWidth(), m_lineHeight
);
2849 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
2855 // The first click was on another item, so don't interpret this as
2856 // a double click, but as a simple click instead
2863 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
2865 // select single line
2866 SelectAllRows( false );
2867 SelectRow( m_lineSelectSingleOnUp
, true );
2870 //Process the event of user clicking the expander
2871 bool expander
= false;
2872 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2873 if( node
!=NULL
&& node
->HasChildren() )
2875 int indent
= node
->GetIndentLevel();
2876 indent
= GetOwner()->GetIndent()*indent
;
2877 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
2878 if( rect
.Contains( x
, y
) )
2881 if( node
->IsOpen() )
2882 OnCollapsing(current
);
2884 OnExpanding( current
);
2888 //If the user click the expander, we do not do editing even if the column with expander are editable
2889 if (m_lastOnSame
&& !expander
)
2891 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
2892 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
2894 m_renameTimer
->Start( 100, true );
2898 m_lastOnSame
= false;
2899 m_lineSelectSingleOnUp
= (unsigned int)-1;
2903 // This is necessary, because after a DnD operation in
2904 // from and to ourself, the up event is swallowed by the
2905 // DnD code. So on next non-up event (which means here and
2906 // now) m_lineSelectSingleOnUp should be reset.
2907 m_lineSelectSingleOnUp
= (unsigned int)-1;
2910 if (event
.RightDown())
2912 m_lineBeforeLastClicked
= m_lineLastClicked
;
2913 m_lineLastClicked
= current
;
2915 // If the item is already selected, do not update the selection.
2916 // Multi-selections should not be cleared if a selected item is clicked.
2917 if (!IsRowSelected(current
))
2919 SelectAllRows(false);
2920 ChangeCurrentRow(current
);
2921 SelectRow(m_currentRow
,true);
2924 // notify cell about right click
2927 // Allow generation of context menu event
2930 else if (event
.MiddleDown())
2932 // notify cell about middle click
2935 if (event
.LeftDown() || forceClick
)
2941 m_lineBeforeLastClicked
= m_lineLastClicked
;
2942 m_lineLastClicked
= current
;
2944 unsigned int oldCurrentRow
= m_currentRow
;
2945 bool oldWasSelected
= IsRowSelected(m_currentRow
);
2947 bool cmdModifierDown
= event
.CmdDown();
2948 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2950 if ( IsSingleSel() || !IsRowSelected(current
) )
2952 SelectAllRows( false );
2954 ChangeCurrentRow(current
);
2956 SelectRow(m_currentRow
,true);
2958 else // multi sel & current is highlighted & no mod keys
2960 m_lineSelectSingleOnUp
= current
;
2961 ChangeCurrentRow(current
); // change focus
2964 else // multi sel & either ctrl or shift is down
2966 if (cmdModifierDown
)
2968 ChangeCurrentRow(current
);
2970 ReverseRowSelection(m_currentRow
);
2972 else if (event
.ShiftDown())
2974 ChangeCurrentRow(current
);
2976 unsigned int lineFrom
= oldCurrentRow
,
2979 if ( lineTo
< lineFrom
)
2982 lineFrom
= m_currentRow
;
2985 SelectRows(lineFrom
, lineTo
, true);
2987 else // !ctrl, !shift
2989 // test in the enclosing if should make it impossible
2990 wxFAIL_MSG( _T("how did we get here?") );
2994 if (m_currentRow
!= oldCurrentRow
)
2995 RefreshRow( oldCurrentRow
);
2997 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
2999 // Update selection here...
3002 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3003 (current
== oldCurrentRow
)) && oldWasSelected
;
3007 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3011 if (HasCurrentRow())
3017 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3021 if (HasCurrentRow())
3027 wxDataViewItem
wxDataViewMainWindow::GetSelection()
3029 if( m_selection
.GetCount() != 1 )
3030 return wxDataViewItem();
3031 return GetItemByRow( m_selection
.Item( 0 ) );
3034 //-----------------------------------------------------------------------------
3036 //-----------------------------------------------------------------------------
3038 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3040 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3041 EVT_SIZE(wxDataViewCtrl::OnSize
)
3044 wxDataViewCtrl::~wxDataViewCtrl()
3047 GetModel()->RemoveNotifier( m_notifier
);
3050 void wxDataViewCtrl::Init()
3055 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3056 const wxPoint
& pos
, const wxSize
& size
,
3057 long style
, const wxValidator
& validator
)
3059 if (!wxControl::Create( parent
, id
, pos
, size
,
3060 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3066 MacSetClipChildren( true ) ;
3069 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3071 if (HasFlag(wxDV_NO_HEADER
))
3072 m_headerArea
= NULL
;
3074 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3076 SetTargetWindow( m_clientArea
);
3078 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3080 sizer
->Add( m_headerArea
, 0, wxGROW
);
3081 sizer
->Add( m_clientArea
, 1, wxGROW
);
3088 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3092 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3095 // we need to process arrows ourselves for scrolling
3096 if ( nMsg
== WM_GETDLGCODE
)
3098 rc
|= DLGC_WANTARROWS
;
3106 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3108 // We need to override OnSize so that our scrolled
3109 // window a) does call Layout() to use sizers for
3110 // positioning the controls but b) does not query
3111 // the sizer for their size and use that for setting
3112 // the scrollable area as set that ourselves by
3113 // calling SetScrollbar() further down.
3120 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3122 if (!wxDataViewCtrlBase::AssociateModel( model
))
3125 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3127 model
->AddNotifier( m_notifier
);
3129 m_clientArea
->BuildTree(model
);
3131 m_clientArea
->UpdateDisplay();
3136 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3138 if (!wxDataViewCtrlBase::AppendColumn(col
))
3145 void wxDataViewCtrl::OnColumnChange()
3148 m_headerArea
->UpdateDisplay();
3150 m_clientArea
->UpdateDisplay();
3153 void wxDataViewCtrl::DoSetExpanderColumn()
3155 m_clientArea
->UpdateDisplay();
3158 void wxDataViewCtrl::DoSetIndent()
3160 m_clientArea
->UpdateDisplay();
3163 wxDataViewItem
wxDataViewCtrl::GetSelection()
3165 return m_clientArea
->GetSelection();
3168 /********************************************************************
3169 void wxDataViewCtrl::SetSelection( int row )
3171 m_clientArea->SelectRow(row, true);
3174 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
3176 m_clientArea->SelectRows(from, to, true);
3179 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
3181 m_clientArea->Select(aSelections);
3184 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
3189 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
3196 int wxDataViewCtrl::GetSelection() const
3203 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
3209 *********************************************************************/
3211 // !wxUSE_GENERICDATAVIEWCTRL
3214 // wxUSE_DATAVIEWCTRL