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
&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
& item
)
524 { return m_mainWindow
->ItemDeleted( 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
& item
)
1922 g_model
= GetOwner()->GetModel();
1924 wxDataViewTreeNode
* node
;
1925 wxDataViewItem parent_item
= g_model
->GetParent( item
);
1926 node
= FindNode(parent_item
);
1928 if( node
== NULL
|| node
->GetChildren().Index( item
.GetID() ) == wxNOT_FOUND
)
1934 node
->GetChildren().Remove( item
.GetID() );
1935 if( GetOwner()->GetModel()->IsContainer( item
) )
1937 wxDataViewTreeNode
* n
;
1938 wxDataViewTreeNodes nodes
= node
->GetNodes();
1939 int len
= nodes
.GetCount();
1940 for( int i
= 0 ; i
< len
; i
++)
1942 if( nodes
[i
]->GetItem() == item
)
1948 node
->GetNodes().Remove( n
);
1949 sub
-= n
->GetSubTreeCount();
1950 DestroyTreeHelper(n
);
1953 if( node
->GetChildrenNumber() == 0)
1954 node
->SetHasChildren( false );
1956 //Make the row number invalid and get a new valid one when user call GetRowCount
1958 node
->ChangeSubTreeCount(sub
);
1959 //Change the current row to the last row if the current exceed the max row number
1960 if( m_currentRow
> GetRowCount() )
1961 m_currentRow
= m_count
- 1;
1967 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1969 g_model
= GetOwner()->GetModel();
1971 unsigned int row
= GetRowByItem(item
);
1976 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int WXUNUSED(col
) )
1978 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1979 /*#define MAX_VIRTUAL_WIDTH 100000
1981 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1982 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1983 Refresh( true, &rect );
1987 g_model
= GetOwner()->GetModel();
1989 unsigned int row
= GetRowByItem(item
);
1994 bool wxDataViewMainWindow::Cleared()
1996 g_model
= GetOwner()->GetModel();
2003 void wxDataViewMainWindow::UpdateDisplay()
2008 void wxDataViewMainWindow::OnInternalIdle()
2010 wxWindow::OnInternalIdle();
2014 RecalculateDisplay();
2019 void wxDataViewMainWindow::RecalculateDisplay()
2021 wxDataViewModel
*model
= GetOwner()->GetModel();
2028 int width
= GetEndOfLastCol();
2029 int height
= GetRowCount() * m_lineHeight
;
2031 SetVirtualSize( width
, height
);
2032 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2037 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2039 wxWindow::ScrollWindow( dx
, dy
, rect
);
2041 if (GetOwner()->m_headerArea
)
2042 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2045 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2047 wxDataViewModel
*model
= GetOwner()->GetModel();
2048 wxAutoBufferedPaintDC
dc( this );
2051 dc
.SetBackground(GetBackgroundColour());
2053 GetOwner()->PrepareDC( dc
);
2054 dc
.SetFont( GetFont() );
2056 wxRect update
= GetUpdateRegion().GetBox();
2057 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2059 // compute which items needs to be redrawn
2060 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2061 unsigned int item_count
=
2062 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2063 (int)(GetRowCount( )- item_start
) );
2064 unsigned int item_last
= item_start
+ item_count
;
2066 // compute which columns needs to be redrawn
2067 unsigned int cols
= GetOwner()->GetColumnCount();
2068 unsigned int col_start
= 0;
2069 unsigned int x_start
= 0;
2070 for (x_start
= 0; col_start
< cols
; col_start
++)
2072 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2073 if (col
->IsHidden())
2074 continue; // skip it!
2076 unsigned int w
= col
->GetWidth();
2077 if (x_start
+w
>= (unsigned int)update
.x
)
2083 unsigned int col_last
= col_start
;
2084 unsigned int x_last
= x_start
;
2085 for (; col_last
< cols
; col_last
++)
2087 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2088 if (col
->IsHidden())
2089 continue; // skip it!
2091 if (x_last
> (unsigned int)update
.GetRight())
2094 x_last
+= col
->GetWidth();
2097 // Draw horizontal rules if required
2098 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2100 dc
.SetPen(m_penRule
);
2101 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2103 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2105 int y
= i
* m_lineHeight
;
2106 dc
.DrawLine(x_start
, y
, x_last
, y
);
2110 // Draw vertical rules if required
2111 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2113 dc
.SetPen(m_penRule
);
2114 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2117 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2119 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2120 if (col
->IsHidden())
2121 continue; // skip it
2123 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2124 x
, item_last
* m_lineHeight
);
2126 x
+= col
->GetWidth();
2129 // Draw last vertical rule
2130 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2131 x
, item_last
* m_lineHeight
);
2134 // redraw the background for the items which are selected/current
2135 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2137 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2138 if (selected
|| item
== m_currentRow
)
2140 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2141 if (item
== m_currentRow
)
2142 flags
|= wxCONTROL_CURRENT
;
2144 flags
|= wxCONTROL_FOCUSED
;
2146 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2147 wxRendererNative::Get().DrawItemSelectionRect
2157 // redraw all cells for all rows which must be repainted and for all columns
2159 cell_rect
.x
= x_start
;
2160 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2161 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2163 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2164 wxDataViewRenderer
*cell
= col
->GetRenderer();
2165 cell_rect
.width
= col
->GetWidth();
2167 if (col
->IsHidden())
2168 continue; // skipt it!
2170 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2172 // get the cell value and set it into the renderer
2174 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2180 wxDataViewItem dataitem
= node
->GetItem();
2181 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2182 cell
->SetValue( value
);
2184 // update the y offset
2185 cell_rect
.y
= item
* m_lineHeight
;
2187 //Draw the expander here.
2188 int indent
= node
->GetIndentLevel();
2189 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
2191 //Calculate the indent first
2192 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2194 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2195 // change the cell_rect.x to the appropriate pos
2196 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2197 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2198 dc
.SetPen( m_penExpander
);
2199 dc
.SetBrush( wxNullBrush
);
2200 if( node
->HasChildren() )
2202 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2203 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2204 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2205 if( node
->IsOpen() )
2206 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2208 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2212 // I am wandering whether we should draw dot lines between tree nodes
2214 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2217 //force the expander column to left-center align
2218 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2222 // cannot be bigger than allocated space
2223 wxSize size
= cell
->GetSize();
2224 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2225 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2226 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2228 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2229 int align
= cell
->GetAlignment();
2231 // horizontal alignment:
2232 item_rect
.x
= cell_rect
.x
;
2233 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2234 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2235 else if (align
& wxALIGN_RIGHT
)
2236 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2237 //else: wxALIGN_LEFT is the default
2239 // vertical alignment:
2240 item_rect
.y
= cell_rect
.y
;
2241 if (align
& wxALIGN_CENTER_VERTICAL
)
2242 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2243 else if (align
& wxALIGN_BOTTOM
)
2244 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2245 //else: wxALIGN_TOP is the default
2248 item_rect
.x
+= PADDING_RIGHTLEFT
;
2249 item_rect
.y
+= PADDING_TOPBOTTOM
;
2250 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2251 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2253 //Here we add the tree indent
2254 item_rect
.x
+= indent
;
2257 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2258 state
|= wxDATAVIEW_CELL_SELECTED
;
2260 // TODO: it would be much more efficient to create a clipping
2261 // region for the entire column being rendered (in the OnPaint
2262 // of wxDataViewMainWindow) instead of a single clip region for
2263 // each cell. However it would mean that each renderer should
2264 // respect the given wxRect's top & bottom coords, eventually
2265 // violating only the left & right coords - however the user can
2266 // make its own renderer and thus we cannot be sure of that.
2267 dc
.SetClippingRegion( item_rect
);
2268 cell
->Render( item_rect
, &dc
, state
);
2269 dc
.DestroyClippingRegion();
2272 cell_rect
.x
+= cell_rect
.width
;
2276 int wxDataViewMainWindow::GetCountPerPage() const
2278 wxSize size
= GetClientSize();
2279 return size
.y
/ m_lineHeight
;
2282 int wxDataViewMainWindow::GetEndOfLastCol() const
2286 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2288 const wxDataViewColumn
*c
=
2289 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2292 width
+= c
->GetWidth();
2297 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2301 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2303 return y
/ m_lineHeight
;
2306 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2308 wxSize client_size
= GetClientSize();
2309 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2310 &client_size
.x
, &client_size
.y
);
2312 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2315 unsigned int wxDataViewMainWindow::GetRowCount()
2317 if ( m_count
== -1 )
2319 m_count
= RecalculateCount();
2321 GetVirtualSize( &width
, &height
);
2322 height
= m_count
* m_lineHeight
;
2324 SetVirtualSize( width
, height
);
2329 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2336 void wxDataViewMainWindow::SelectAllRows( bool on
)
2343 m_selection
.Clear();
2344 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2345 m_selection
.Add( i
);
2350 unsigned int first_visible
= GetFirstVisibleRow();
2351 unsigned int last_visible
= GetLastVisibleRow();
2353 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2355 unsigned int row
= m_selection
[i
];
2356 if ((row
>= first_visible
) && (row
<= last_visible
))
2359 m_selection
.Clear();
2363 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2365 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2369 m_selection
.Add( row
);
2377 m_selection
.Remove( row
);
2383 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2387 unsigned int tmp
= from
;
2393 for (i
= from
; i
<= to
; i
++)
2395 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2398 m_selection
.Add( i
);
2403 m_selection
.Remove( i
);
2406 RefreshRows( from
, to
);
2409 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2411 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2413 int n
= aSelections
[i
];
2415 m_selection
.Add( n
);
2420 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2422 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2423 m_selection
.Add( row
);
2425 m_selection
.Remove( row
);
2429 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2431 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2434 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2436 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2437 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2439 wxSize client_size
= GetClientSize();
2440 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2441 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2442 if (intersect_rect
.width
> 0)
2443 Refresh( true, &intersect_rect
);
2446 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2450 unsigned int tmp
= to
;
2455 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2456 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2458 wxSize client_size
= GetClientSize();
2459 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2460 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2461 if (intersect_rect
.width
> 0)
2462 Refresh( true, &intersect_rect
);
2465 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2467 unsigned int count
= GetRowCount();
2468 if (firstRow
> count
)
2471 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2472 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2474 wxSize client_size
= GetClientSize();
2475 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2476 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2477 if (intersect_rect
.width
> 0)
2478 Refresh( true, &intersect_rect
);
2481 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2483 wxCHECK_RET( newCurrent
< GetRowCount(),
2484 _T("invalid item index in OnArrowChar()") );
2486 // if there is no selection, we cannot move it anywhere
2487 if (!HasCurrentRow())
2490 unsigned int oldCurrent
= m_currentRow
;
2492 // in single selection we just ignore Shift as we can't select several
2494 if ( event
.ShiftDown() && !IsSingleSel() )
2496 RefreshRow( oldCurrent
);
2498 ChangeCurrentRow( newCurrent
);
2500 // select all the items between the old and the new one
2501 if ( oldCurrent
> newCurrent
)
2503 newCurrent
= oldCurrent
;
2504 oldCurrent
= m_currentRow
;
2507 SelectRows( oldCurrent
, newCurrent
, true );
2511 RefreshRow( oldCurrent
);
2513 // all previously selected items are unselected unless ctrl is held
2514 if ( !event
.ControlDown() )
2515 SelectAllRows(false);
2517 ChangeCurrentRow( newCurrent
);
2519 if ( !event
.ControlDown() )
2520 SelectRow( m_currentRow
, true );
2522 RefreshRow( m_currentRow
);
2525 //EnsureVisible( m_currentRow );
2528 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2532 rect
.y
= m_lineHeight
* row
;
2533 rect
.width
= GetEndOfLastCol();
2534 rect
.height
= m_lineHeight
;
2539 class RowToItemJob
: public DoJob
2542 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2543 virtual ~RowToItemJob(){};
2545 virtual int operator() ( wxDataViewTreeNode
* node
)
2548 if( current
== static_cast<int>(row
))
2550 ret
= node
->GetItem() ;
2554 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2556 current
+= node
->GetSubTreeCount();
2563 virtual int operator() ( void * n
)
2566 if( current
== static_cast<int>(row
))
2568 ret
= wxDataViewItem( n
) ;
2573 wxDataViewItem
GetResult(){ return ret
; }
2580 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2582 RowToItemJob
job( row
, -1 );
2583 Walker( m_root
, job
);
2584 return job
.GetResult();
2587 class RowToTreeNodeJob
: public DoJob
2590 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2593 this->current
= current
;
2597 virtual ~RowToTreeNodeJob(){};
2599 virtual int operator() ( wxDataViewTreeNode
* node
)
2602 if( current
== static_cast<int>(row
))
2608 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2610 current
+= node
->GetSubTreeCount();
2621 virtual int operator() ( void * n
)
2624 if( current
== static_cast<int>(row
))
2626 ret
= new wxDataViewTreeNode( parent
) ;
2627 ret
->SetItem( wxDataViewItem( n
));
2628 ret
->SetHasChildren(false);
2634 wxDataViewTreeNode
* GetResult(){ return ret
; }
2638 wxDataViewTreeNode
* ret
;
2639 wxDataViewTreeNode
* parent
;
2643 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2645 RowToTreeNodeJob
job( row
, -1, m_root
);
2646 Walker( m_root
, job
);
2647 return job
.GetResult();
2651 class CountJob
: public DoJob
2654 CountJob(){ count
= 0 ; }
2655 virtual ~CountJob(){};
2657 virtual int operator () ( wxDataViewTreeNode
* node
)
2660 if ( node
->IsOpen())
2666 unsigned int GetResult()
2675 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2677 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2680 if( node
->HasChildren())
2681 if( !node
->IsOpen())
2684 //Here I build the children of current node
2685 if( node
->GetChildrenNumber() == 0 )
2686 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2692 SelectRow( row
, false );
2693 SelectRow( row
+ 1, true );
2694 ChangeCurrentRow( row
+ 1 );
2701 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2703 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2706 wxDataViewTreeNode
* nd
= node
;
2708 if( node
->HasChildren() && node
->IsOpen() )
2713 //RefreshRows(row,GetLastVisibleRow());
2717 node
= node
->GetParent();
2720 int parent
= GetRowByItem( node
->GetItem()) ;
2721 SelectRow( row
, false);
2722 SelectRow(parent
, true );
2723 ChangeCurrentRow( parent
);
2726 if( !nd
->HasChildren())
2731 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2733 wxDataViewModel
* model
= GetOwner()->GetModel();
2737 //Compose the a parent-chain of the finding item
2739 list
.DeleteContents( true );
2740 wxDataViewItem
it( item
);
2743 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2744 list
.Insert( pItem
);
2745 it
= model
->GetParent( it
);
2748 //Find the item along the parent-chain.
2749 //This algorithm is designed to speed up the node-finding method
2751 wxDataViewTreeNode
* node
= m_root
;
2752 for( ItemList::Node
* n
= list
.GetFirst(); n
; n
= n
->GetNext() )
2754 if( node
->HasChildren() )
2756 if( node
->GetChildrenNumber() == 0 )
2757 BuildTreeHelper(model
, node
->GetItem(), node
);
2759 int len
= node
->GetNodeNumber();
2760 wxDataViewTreeNodes nodes
= node
->GetNodes();
2762 for( ; j
< len
; j
++)
2764 if( nodes
[j
]->GetItem() == *(n
->GetData()))
2770 // Whenever we can't find the node in any level, return NULL to indicate the item can't be found
2783 int wxDataViewMainWindow::RecalculateCount()
2785 return m_root
->GetSubTreeCount();
2788 class ItemToRowJob
: public DoJob
2791 ItemToRowJob(const wxDataViewItem
& item
, ItemList::Node
* node
)
2792 { this->item
= item
; ret
= 0 ; nd
= node
; }
2793 virtual ~ItemToRowJob(){};
2795 virtual int operator() ( wxDataViewTreeNode
* node
)
2798 if( node
->GetItem() == item
)
2803 if( nd
&& node
->GetItem() == *(nd
->GetData()))
2810 ret
+= node
->GetSubTreeCount();
2816 virtual int operator() ( void * n
)
2819 if( n
== item
.GetID() )
2823 //the row number is begin from zero
2824 int GetResult(){ return ret
-1 ; }
2826 ItemList::Node
* nd
;
2827 wxDataViewItem item
;
2832 unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2834 wxDataViewModel
* model
= GetOwner()->GetModel();
2838 //Compose the a parent-chain of the finding item
2840 list
.DeleteContents( true );
2841 wxDataViewItem
it( item
);
2844 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2845 list
.Insert( pItem
);
2846 it
= model
->GetParent( it
);
2849 ItemToRowJob
job( item
, list
.GetFirst() );
2850 Walker(m_root
, job
);
2851 return job
.GetResult();
2854 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2856 if( !model
->IsContainer( item
) )
2859 wxDataViewItem i
= model
->GetFirstChild( item
);
2864 if( model
->IsContainer( i
) )
2866 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2868 n
->SetHasChildren( true ) ;
2873 node
->AddLeaf( i
.GetID() );
2875 i
= model
->GetNextSibling( i
);
2877 node
->SetSubTreeCount( num
);
2878 wxDataViewTreeNode
* n
= node
->GetParent();
2880 n
->ChangeSubTreeCount(num
);
2884 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2886 //First we define a invalid item to fetch the top-level elements
2887 wxDataViewItem item
;
2888 g_model
= GetOwner()->GetModel();
2889 BuildTreeHelper( model
, item
, m_root
);
2893 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2895 if( node
->GetNodeNumber() != 0 )
2897 int len
= node
->GetNodeNumber();
2899 wxDataViewTreeNodes nodes
= node
->GetNodes();
2900 for( ; i
< len
; i
++ )
2902 DestroyTreeHelper(nodes
[i
]);
2908 void wxDataViewMainWindow::DestroyTree()
2910 DestroyTreeHelper(m_root
);
2911 m_root
->SetSubTreeCount(0);
2915 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2917 if (event
.GetKeyCode() == WXK_TAB
)
2919 wxNavigationKeyEvent nevent
;
2920 nevent
.SetWindowChange( event
.ControlDown() );
2921 nevent
.SetDirection( !event
.ShiftDown() );
2922 nevent
.SetEventObject( GetParent()->GetParent() );
2923 nevent
.SetCurrentFocus( m_parent
);
2924 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
2928 // no item -> nothing to do
2929 if (!HasCurrentRow())
2935 // don't use m_linesPerPage directly as it might not be computed yet
2936 const int pageSize
= GetCountPerPage();
2937 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2939 switch ( event
.GetKeyCode() )
2942 if ( m_currentRow
> 0 )
2943 OnArrowChar( m_currentRow
- 1, event
);
2947 if ( m_currentRow
< GetRowCount() - 1 )
2948 OnArrowChar( m_currentRow
+ 1, event
);
2950 //Add the process for tree expanding/collapsing
2952 OnCollapsing(m_currentRow
);
2955 OnExpanding( m_currentRow
);
2959 OnArrowChar( GetRowCount() - 1, event
);
2964 OnArrowChar( 0, event
);
2969 int steps
= pageSize
- 1;
2970 int index
= m_currentRow
- steps
;
2974 OnArrowChar( index
, event
);
2980 int steps
= pageSize
- 1;
2981 unsigned int index
= m_currentRow
+ steps
;
2982 unsigned int count
= GetRowCount();
2983 if ( index
>= count
)
2986 OnArrowChar( index
, event
);
2995 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2997 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2999 // let the base handle mouse wheel events.
3004 int x
= event
.GetX();
3005 int y
= event
.GetY();
3006 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3008 wxDataViewColumn
*col
= NULL
;
3011 unsigned int cols
= GetOwner()->GetColumnCount();
3013 for (i
= 0; i
< cols
; i
++)
3015 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3017 continue; // skip it!
3019 if (x
< xpos
+ c
->GetWidth())
3024 xpos
+= c
->GetWidth();
3028 wxDataViewRenderer
*cell
= col
->GetRenderer();
3030 unsigned int current
= y
/ m_lineHeight
;
3032 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3034 // Unselect all if below the last row ?
3038 wxDataViewModel
*model
= GetOwner()->GetModel();
3040 if (event
.Dragging())
3042 if (m_dragCount
== 0)
3044 // we have to report the raw, physical coords as we want to be
3045 // able to call HitTest(event.m_pointDrag) from the user code to
3046 // get the item being dragged
3047 m_dragStart
= event
.GetPosition();
3052 if (m_dragCount
!= 3)
3055 if (event
.LeftIsDown())
3057 // Notify cell about drag
3066 bool forceClick
= false;
3068 if (event
.ButtonDClick())
3070 m_renameTimer
->Stop();
3071 m_lastOnSame
= false;
3074 if (event
.LeftDClick())
3076 if ( current
== m_lineLastClicked
)
3078 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
3080 wxDataViewItem item
= GetItemByRow(current
);
3082 model
->GetValue( value
, item
, col
->GetModelColumn() );
3083 cell
->SetValue( value
);
3084 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3085 col
->GetWidth(), m_lineHeight
);
3086 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3092 // The first click was on another item, so don't interpret this as
3093 // a double click, but as a simple click instead
3100 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3102 // select single line
3103 SelectAllRows( false );
3104 SelectRow( m_lineSelectSingleOnUp
, true );
3107 //Process the event of user clicking the expander
3108 bool expander
= false;
3109 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3110 if( node
!=NULL
&& node
->HasChildren() )
3112 int indent
= node
->GetIndentLevel();
3113 indent
= GetOwner()->GetIndent()*indent
;
3114 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3115 if( rect
.Contains( x
, y
) )
3118 if( node
->IsOpen() )
3119 OnCollapsing(current
);
3121 OnExpanding( current
);
3125 //If the user click the expander, we do not do editing even if the column with expander are editable
3126 if (m_lastOnSame
&& !expander
)
3128 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3129 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3131 m_renameTimer
->Start( 100, true );
3135 m_lastOnSame
= false;
3136 m_lineSelectSingleOnUp
= (unsigned int)-1;
3140 // This is necessary, because after a DnD operation in
3141 // from and to ourself, the up event is swallowed by the
3142 // DnD code. So on next non-up event (which means here and
3143 // now) m_lineSelectSingleOnUp should be reset.
3144 m_lineSelectSingleOnUp
= (unsigned int)-1;
3147 if (event
.RightDown())
3149 m_lineBeforeLastClicked
= m_lineLastClicked
;
3150 m_lineLastClicked
= current
;
3152 // If the item is already selected, do not update the selection.
3153 // Multi-selections should not be cleared if a selected item is clicked.
3154 if (!IsRowSelected(current
))
3156 SelectAllRows(false);
3157 ChangeCurrentRow(current
);
3158 SelectRow(m_currentRow
,true);
3161 // notify cell about right click
3164 // Allow generation of context menu event
3167 else if (event
.MiddleDown())
3169 // notify cell about middle click
3172 if (event
.LeftDown() || forceClick
)
3178 m_lineBeforeLastClicked
= m_lineLastClicked
;
3179 m_lineLastClicked
= current
;
3181 unsigned int oldCurrentRow
= m_currentRow
;
3182 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3184 bool cmdModifierDown
= event
.CmdDown();
3185 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3187 if ( IsSingleSel() || !IsRowSelected(current
) )
3189 SelectAllRows( false );
3191 ChangeCurrentRow(current
);
3193 SelectRow(m_currentRow
,true);
3195 else // multi sel & current is highlighted & no mod keys
3197 m_lineSelectSingleOnUp
= current
;
3198 ChangeCurrentRow(current
); // change focus
3201 else // multi sel & either ctrl or shift is down
3203 if (cmdModifierDown
)
3205 ChangeCurrentRow(current
);
3207 ReverseRowSelection(m_currentRow
);
3209 else if (event
.ShiftDown())
3211 ChangeCurrentRow(current
);
3213 unsigned int lineFrom
= oldCurrentRow
,
3216 if ( lineTo
< lineFrom
)
3219 lineFrom
= m_currentRow
;
3222 SelectRows(lineFrom
, lineTo
, true);
3224 else // !ctrl, !shift
3226 // test in the enclosing if should make it impossible
3227 wxFAIL_MSG( _T("how did we get here?") );
3231 if (m_currentRow
!= oldCurrentRow
)
3232 RefreshRow( oldCurrentRow
);
3234 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3236 // Update selection here...
3239 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3240 (current
== oldCurrentRow
)) && oldWasSelected
;
3244 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3248 if (HasCurrentRow())
3254 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3258 if (HasCurrentRow())
3264 wxDataViewItem
wxDataViewMainWindow::GetSelection()
3266 if( m_selection
.GetCount() != 1 )
3267 return wxDataViewItem();
3269 return GetItemByRow( m_selection
.Item(0));
3272 //-----------------------------------------------------------------------------
3274 //-----------------------------------------------------------------------------
3276 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3278 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3279 EVT_SIZE(wxDataViewCtrl::OnSize
)
3282 wxDataViewCtrl::~wxDataViewCtrl()
3285 GetModel()->RemoveNotifier( m_notifier
);
3288 void wxDataViewCtrl::Init()
3293 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3294 const wxPoint
& pos
, const wxSize
& size
,
3295 long style
, const wxValidator
& validator
)
3297 if (!wxControl::Create( parent
, id
, pos
, size
,
3298 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
3304 MacSetClipChildren( true ) ;
3307 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3309 if (HasFlag(wxDV_NO_HEADER
))
3310 m_headerArea
= NULL
;
3312 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3314 SetTargetWindow( m_clientArea
);
3316 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3318 sizer
->Add( m_headerArea
, 0, wxGROW
);
3319 sizer
->Add( m_clientArea
, 1, wxGROW
);
3326 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3330 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3333 // we need to process arrows ourselves for scrolling
3334 if ( nMsg
== WM_GETDLGCODE
)
3336 rc
|= DLGC_WANTARROWS
;
3344 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3346 // We need to override OnSize so that our scrolled
3347 // window a) does call Layout() to use sizers for
3348 // positioning the controls but b) does not query
3349 // the sizer for their size and use that for setting
3350 // the scrollable area as set that ourselves by
3351 // calling SetScrollbar() further down.
3358 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3360 if (!wxDataViewCtrlBase::AssociateModel( model
))
3363 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3365 model
->AddNotifier( m_notifier
);
3367 m_clientArea
->BuildTree(model
);
3369 m_clientArea
->UpdateDisplay();
3374 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3376 if (!wxDataViewCtrlBase::AppendColumn(col
))
3383 void wxDataViewCtrl::OnColumnChange()
3386 m_headerArea
->UpdateDisplay();
3388 m_clientArea
->UpdateDisplay();
3391 void wxDataViewCtrl::DoSetExpanderColumn()
3393 m_clientArea
->UpdateDisplay();
3396 void wxDataViewCtrl::DoSetIndent()
3398 m_clientArea
->UpdateDisplay();
3401 wxDataViewItem
wxDataViewCtrl::GetSelection()
3403 return m_clientArea
->GetSelection();
3406 /********************************************************************
3407 void wxDataViewCtrl::SetSelection( int row )
3409 m_clientArea->SelectRow(row, true);
3412 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
3414 m_clientArea->SelectRows(from, to, true);
3417 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
3419 m_clientArea->Select(aSelections);
3422 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
3427 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
3434 int wxDataViewCtrl::GetSelection() const
3441 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
3447 *********************************************************************/
3449 // !wxUSE_GENERICDATAVIEWCTRL
3452 // wxUSE_DATAVIEWCTRL