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 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
52 static const int SCROLL_UNIT_X
= 15;
54 // the cell padding on the left/right
55 static const int PADDING_RIGHTLEFT
= 3;
57 // the cell padding on the top/bottom
58 static const int PADDING_TOPBOTTOM
= 1;
60 // the expander space margin
61 static const int EXPANDER_MARGIN
= 4;
63 bool operator == ( const wxDataViewItem
& left
, const wxDataViewItem
& right
)
65 return left
.GetID() == right
.GetID();
68 //-----------------------------------------------------------------------------
69 // wxDataViewHeaderWindow
70 //-----------------------------------------------------------------------------
72 #define USE_NATIVE_HEADER_WINDOW 1
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called when the main window gets scrolled
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
146 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
148 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
150 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
151 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
154 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
157 #else // !defined(__WXMSW__)
159 #define HEADER_WINDOW_HEIGHT 25
160 #define HEADER_HORIZ_BORDER 5
161 #define HEADER_VERT_BORDER 3
162 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
164 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
167 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
169 const wxPoint
&pos
= wxDefaultPosition
,
170 const wxSize
&size
= wxDefaultSize
,
171 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
174 Create(parent
, id
, pos
, size
, name
);
177 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
,
179 const wxString
&name
);
181 ~wxGenericDataViewHeaderWindow()
183 delete m_resizeCursor
;
186 virtual void UpdateDisplay() { Refresh(); }
190 void OnPaint( wxPaintEvent
&event
);
191 void OnMouse( wxMouseEvent
&event
);
192 void OnSetFocus( wxFocusEvent
&event
);
197 // vars used for column resizing:
199 wxCursor
*m_resizeCursor
;
200 const wxCursor
*m_currentCursor
;
203 bool m_dirty
; // needs refresh?
204 int m_column
; // index of the column being resized
205 int m_currentX
; // divider line position in logical (unscrolled) coords
206 int m_minX
; // minimal position beyond which the divider line
207 // can't be dragged in logical coords
209 // the pen used to draw the current column width drag line
210 // when resizing the columsn
214 // internal utilities:
218 m_currentCursor
= (wxCursor
*) NULL
;
219 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
221 m_isDragging
= false;
224 m_column
= wxNOT_FOUND
;
228 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
229 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
233 void AdjustDC(wxDC
& dc
);
236 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
237 DECLARE_EVENT_TABLE()
240 #endif // defined(__WXMSW__)
242 //-----------------------------------------------------------------------------
243 // wxDataViewRenameTimer
244 //-----------------------------------------------------------------------------
246 class wxDataViewRenameTimer
: public wxTimer
249 wxDataViewMainWindow
*m_owner
;
252 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
256 //-----------------------------------------------------------------------------
257 // wxDataViewTreeNode
258 //-----------------------------------------------------------------------------
259 class wxDataViewTreeNode
;
260 WX_DEFINE_ARRAY_PTR( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
262 class wxDataViewTreeNode
265 wxDataViewTreeNode( wxDataViewTreeNode
* parent
)
266 { this->parent
= parent
;
272 //I don't know what I need to do in the destructure
273 ~wxDataViewTreeNode()
276 wxDataViewTreeNode
* GetParent() { return parent
; }
277 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
278 wxDataViewTreeNodes
GetChildren() { return children
; }
279 void SetChildren( wxDataViewTreeNodes children
) { this->children
= children
; }
281 wxDataViewTreeNode
* GetChild( unsigned int n
) { return children
.Item( n
); }
282 void InsertChild( wxDataViewTreeNode
* child
, unsigned int n
) { children
.Insert( child
, n
); }
283 void AppendChild( wxDataViewTreeNode
* child
) { children
.Add( child
); }
285 wxDataViewItem
& GetItem() { return item
; }
286 void SetItem( wxDataViewItem
& item
) { this->item
= item
; }
288 unsigned int GetChildrenNumber() { return children
.GetCount(); }
292 wxDataViewTreeNode
* node
= this;
293 while( node
->GetParent()->GetParent() != NULL
)
295 node
= node
->GetParent();
301 bool IsOpen() { return open
; }
302 void ToggleOpen(){ open
= !open
; }
303 bool HasChildren() { return children
.GetCount() != 0; }
305 wxDataViewTreeNode
* parent
;
306 wxDataViewTreeNodes children
;
311 //-----------------------------------------------------------------------------
312 // wxDataViewMainWindow
313 //-----------------------------------------------------------------------------
315 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
318 class wxDataViewMainWindow
: public wxWindow
321 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
323 const wxPoint
&pos
= wxDefaultPosition
,
324 const wxSize
&size
= wxDefaultSize
,
325 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
326 virtual ~wxDataViewMainWindow();
328 // notifications from wxDataViewModel
329 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
330 bool ItemDeleted( const wxDataViewItem
&item
);
331 bool ItemChanged( const wxDataViewItem
&item
);
332 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
335 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
336 wxDataViewCtrl
*GetOwner() { return m_owner
; }
337 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
339 void OnPaint( wxPaintEvent
&event
);
340 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
341 void OnChar( wxKeyEvent
&event
);
342 void OnMouse( wxMouseEvent
&event
);
343 void OnSetFocus( wxFocusEvent
&event
);
344 void OnKillFocus( wxFocusEvent
&event
);
346 void UpdateDisplay();
347 void RecalculateDisplay();
348 void OnInternalIdle();
350 void OnRenameTimer();
352 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
354 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
355 void ChangeCurrentRow( unsigned int row
);
357 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
358 bool IsEmpty() { return GetRowCount() == 0; }
360 int GetCountPerPage() const;
361 int GetEndOfLastCol() const;
362 unsigned int GetFirstVisibleRow() const;
363 //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
364 unsigned int GetLastVisibleRow();
365 unsigned int GetRowCount() ;
367 void Select( const wxArrayInt
& aSelections
);
368 void SelectAllRows( bool on
);
369 void SelectRow( unsigned int row
, bool on
);
370 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
371 void ReverseRowSelection( unsigned int row
);
372 bool IsRowSelected( unsigned int row
);
374 void RefreshRow( unsigned int row
);
375 void RefreshRows( unsigned int from
, unsigned int to
);
376 void RefreshRowsAfter( unsigned int firstRow
);
378 // returns the colour to be used for drawing the rules
379 wxColour
GetRuleColour() const
381 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
384 //void EnsureVisible( unsigned int row );
385 wxRect
GetLineRect( unsigned int row
) const;
387 //Some useful functions for row and item mapping
388 wxDataViewItem
GetItemByRow( unsigned int row
);
389 unsigned int GetRowByItem( const wxDataViewItem
& item
);
391 //Methods for building the mapping tree
392 void BuildTree( wxDataViewModel
* model
);
395 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
396 wxDataViewTreeNode
* GetTreeNodeByItem( const wxDataViewItem
& item
) { return NULL
; }
398 int RecalculateCount() ;
400 void OnExpanding( unsigned int row
);
401 void OnCollapsing( unsigned int row
);
404 wxDataViewCtrl
*m_owner
;
408 wxDataViewColumn
*m_currentCol
;
409 unsigned int m_currentRow
;
410 wxDataViewSelection m_selection
;
412 wxDataViewRenameTimer
*m_renameTimer
;
420 // for double click logic
421 unsigned int m_lineLastClicked
,
422 m_lineBeforeLastClicked
,
423 m_lineSelectSingleOnUp
;
425 // the pen used to draw horiz/vertical rules
428 // the pen used to draw the expander and the lines
431 //This is the tree structure of the model
432 wxDataViewTreeNode
* m_root
;
435 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
436 DECLARE_EVENT_TABLE()
439 // ---------------------------------------------------------
440 // wxGenericDataViewModelNotifier
441 // ---------------------------------------------------------
443 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
446 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
447 { m_mainWindow
= mainWindow
; }
449 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
450 { return m_mainWindow
->ItemAdded( parent
, item
); }
451 virtual bool ItemDeleted( const wxDataViewItem
& item
)
452 { return m_mainWindow
->ItemDeleted( item
); }
453 virtual bool ItemChanged( const wxDataViewItem
& item
)
454 { return m_mainWindow
->ItemChanged(item
); }
455 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
456 { return m_mainWindow
->ValueChanged( item
, col
); }
457 virtual bool Cleared()
458 { return m_mainWindow
->Cleared(); }
460 wxDataViewMainWindow
*m_mainWindow
;
463 // ---------------------------------------------------------
464 // wxDataViewRenderer
465 // ---------------------------------------------------------
467 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
469 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
470 wxDataViewCellMode mode
,
472 wxDataViewRendererBase( varianttype
, mode
, align
)
479 wxDataViewRenderer::~wxDataViewRenderer()
485 wxDC
*wxDataViewRenderer::GetDC()
489 if (GetOwner() == NULL
)
491 if (GetOwner()->GetOwner() == NULL
)
493 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
499 // ---------------------------------------------------------
500 // wxDataViewCustomRenderer
501 // ---------------------------------------------------------
503 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
505 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
506 wxDataViewCellMode mode
, int align
) :
507 wxDataViewRenderer( varianttype
, mode
, align
)
511 // ---------------------------------------------------------
512 // wxDataViewTextRenderer
513 // ---------------------------------------------------------
515 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
517 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
518 wxDataViewCellMode mode
, int align
) :
519 wxDataViewCustomRenderer( varianttype
, mode
, align
)
523 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
525 m_text
= value
.GetString();
530 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
535 bool wxDataViewTextRenderer::HasEditorCtrl()
540 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
541 wxRect labelRect
, const wxVariant
&value
)
543 return new wxTextCtrl( parent
, wxID_ANY
, value
,
544 wxPoint(labelRect
.x
,labelRect
.y
),
545 wxSize(labelRect
.width
,labelRect
.height
) );
548 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
550 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
551 value
= text
->GetValue();
555 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
557 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
558 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
559 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
560 view
->GetForegroundColour();
562 dc
->SetTextForeground(col
);
563 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
568 wxSize
wxDataViewTextRenderer::GetSize() const
570 const wxDataViewCtrl
*view
= GetView();
574 view
->GetTextExtent( m_text
, &x
, &y
);
575 return wxSize( x
, y
);
577 return wxSize(80,20);
580 // ---------------------------------------------------------
581 // wxDataViewBitmapRenderer
582 // ---------------------------------------------------------
584 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
586 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
587 wxDataViewCellMode mode
, int align
) :
588 wxDataViewCustomRenderer( varianttype
, mode
, align
)
592 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
594 if (value
.GetType() == wxT("wxBitmap"))
596 if (value
.GetType() == wxT("wxIcon"))
602 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
607 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
610 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
611 else if (m_icon
.Ok())
612 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
617 wxSize
wxDataViewBitmapRenderer::GetSize() const
620 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
621 else if (m_icon
.Ok())
622 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
624 return wxSize(16,16);
627 // ---------------------------------------------------------
628 // wxDataViewToggleRenderer
629 // ---------------------------------------------------------
631 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
633 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
634 wxDataViewCellMode mode
, int align
) :
635 wxDataViewCustomRenderer( varianttype
, mode
, align
)
640 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
642 m_toggle
= value
.GetBool();
647 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
652 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
654 // User wxRenderer here
657 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
659 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
664 flags
|= wxCONTROL_CHECKED
;
665 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
666 flags
|= wxCONTROL_DISABLED
;
668 wxRendererNative::Get().DrawCheckBox(
669 GetOwner()->GetOwner(),
677 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
678 wxDataViewModel
*model
,
679 const wxDataViewItem
& item
, unsigned int col
)
681 bool value
= !m_toggle
;
682 wxVariant variant
= value
;
683 model
->SetValue( variant
, item
, col
);
684 model
->ValueChanged( item
, col
);
688 wxSize
wxDataViewToggleRenderer::GetSize() const
690 return wxSize(20,20);
693 // ---------------------------------------------------------
694 // wxDataViewProgressRenderer
695 // ---------------------------------------------------------
697 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
699 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
700 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
701 wxDataViewCustomRenderer( varianttype
, mode
, align
)
707 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
711 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
713 m_value
= (long) value
;
715 if (m_value
< 0) m_value
= 0;
716 if (m_value
> 100) m_value
= 100;
721 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
723 value
= (long) m_value
;
727 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
729 double pct
= (double)m_value
/ 100.0;
731 bar
.width
= (int)(cell
.width
* pct
);
732 dc
->SetPen( *wxTRANSPARENT_PEN
);
733 dc
->SetBrush( *wxBLUE_BRUSH
);
734 dc
->DrawRectangle( bar
);
736 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
737 dc
->SetPen( *wxBLACK_PEN
);
738 dc
->DrawRectangle( cell
);
743 wxSize
wxDataViewProgressRenderer::GetSize() const
745 return wxSize(40,12);
748 // ---------------------------------------------------------
749 // wxDataViewDateRenderer
750 // ---------------------------------------------------------
752 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
754 #if wxUSE_DATE_RENDERER_POPUP
756 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
759 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
760 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
761 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
766 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
767 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
768 sizer
->Add( m_cal
, 1, wxGROW
);
773 void OnCalendar( wxCalendarEvent
&event
);
775 wxCalendarCtrl
*m_cal
;
776 wxDataViewModel
*m_model
;
778 const wxDataViewItem
& m_item
;
781 virtual void OnDismiss()
786 DECLARE_EVENT_TABLE()
789 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
790 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
793 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
795 wxDateTime date
= event
.GetDate();
796 wxVariant value
= date
;
797 m_model
->SetValue( value
, m_item
, m_col
);
798 m_model
->ValueChanged( m_item
, m_col
);
802 #endif // wxUSE_DATE_RENDERER_POPUP
804 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
806 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
807 wxDataViewCellMode mode
, int align
) :
808 wxDataViewCustomRenderer( varianttype
, mode
, align
)
812 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
814 m_date
= value
.GetDateTime();
819 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
825 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
827 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
828 wxString tmp
= m_date
.FormatDate();
829 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
834 wxSize
wxDataViewDateRenderer::GetSize() const
836 const wxDataViewCtrl
* view
= GetView();
837 wxString tmp
= m_date
.FormatDate();
839 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
840 return wxSize(x
,y
+d
);
843 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
844 const wxDataViewItem
& item
, unsigned int col
)
847 model
->GetValue( variant
, item
, col
);
848 wxDateTime value
= variant
.GetDateTime();
850 #if wxUSE_DATE_RENDERER_POPUP
851 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
852 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
853 wxPoint pos
= wxGetMousePosition();
856 popup
->Popup( popup
->m_cal
);
857 #else // !wxUSE_DATE_RENDERER_POPUP
858 wxMessageBox(value
.Format());
859 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
863 // ---------------------------------------------------------
865 // ---------------------------------------------------------
867 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
869 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
870 unsigned int model_column
,
871 int width
, wxAlignment align
, int flags
) :
872 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
878 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
881 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
882 unsigned int model_column
,
883 int width
, wxAlignment align
, int flags
) :
884 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
889 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
892 wxDataViewColumn::~wxDataViewColumn()
896 void wxDataViewColumn::Init( int width
)
899 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
903 void wxDataViewColumn::SetResizeable( bool resizeable
)
906 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
908 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
911 void wxDataViewColumn::SetHidden( bool hidden
)
914 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
916 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
918 // tell our owner to e.g. update its scrollbars:
920 GetOwner()->OnColumnChange();
923 void wxDataViewColumn::SetSortable( bool sortable
)
926 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
928 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
930 // Update header button
932 GetOwner()->OnColumnChange();
935 void wxDataViewColumn::SetSortOrder( bool ascending
)
937 m_ascending
= ascending
;
939 // Update header button
941 GetOwner()->OnColumnChange();
944 bool wxDataViewColumn::IsSortOrderAscending() const
949 void wxDataViewColumn::SetInternalWidth( int width
)
953 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
954 if (m_owner
&& m_owner
->m_clientArea
)
955 m_owner
->m_clientArea
->RecalculateDisplay();
958 void wxDataViewColumn::SetWidth( int width
)
960 m_owner
->m_headerArea
->UpdateDisplay();
962 SetInternalWidth(width
);
966 //-----------------------------------------------------------------------------
967 // wxDataViewHeaderWindowBase
968 //-----------------------------------------------------------------------------
970 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
972 wxWindow
*parent
= GetParent();
973 wxDataViewEvent
le(type
, parent
->GetId());
975 le
.SetEventObject(parent
);
977 le
.SetDataViewColumn(GetColumn(n
));
978 le
.SetModel(GetOwner()->GetModel());
980 // for events created by wxDataViewHeaderWindow the
981 // row / value fields are not valid
983 parent
->GetEventHandler()->ProcessEvent(le
);
986 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
988 // implemented in msw/listctrl.cpp:
989 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
991 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
993 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
994 const wxPoint
&pos
, const wxSize
&size
,
995 const wxString
&name
)
999 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1002 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1003 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1004 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1005 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1007 // create the native WC_HEADER window:
1008 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1009 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1010 m_hWnd
= CreateWindowEx(0,
1021 wxLogLastError(_T("CreateWindowEx"));
1025 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1026 // to call wxDataViewHeaderWindow::MSWOnNotify
1027 SubclassWin(m_hWnd
);
1029 // the following is required to get the default win's font for
1030 // header windows and must be done befor sending the HDM_LAYOUT msg
1037 // Retrieve the bounding rectangle of the parent window's
1038 // client area, and then request size and position values
1039 // from the header control.
1040 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1042 hdl
.prc
= &rcParent
;
1044 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1046 wxLogLastError(_T("SendMessage"));
1050 // Set the size, position, and visibility of the header control.
1051 SetWindowPos((HWND
)m_hWnd
,
1055 wp
.flags
| SWP_SHOWWINDOW
);
1057 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1058 // a wxBoxSizer and in order to avoid super-big header windows,
1059 // we need to set our height as fixed
1060 SetMinSize(wxSize(-1, wp
.cy
));
1061 SetMaxSize(wxSize(-1, wp
.cy
));
1066 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1071 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1073 // remove old columns
1074 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1075 Header_DeleteItem((HWND
)m_hWnd
, 0);
1077 // add the updated array of columns to the header control
1078 unsigned int cols
= GetOwner()->GetColumnCount();
1079 unsigned int added
= 0;
1080 for (unsigned int i
= 0; i
< cols
; i
++)
1082 wxDataViewColumn
*col
= GetColumn( i
);
1083 if (col
->IsHidden())
1084 continue; // don't add it!
1087 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1088 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1089 hdi
.cxy
= col
->GetWidth();
1090 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1091 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1093 // lParam is reserved for application's use:
1094 // we store there the column index to use it later in MSWOnNotify
1095 // (since columns may have been hidden)
1096 hdi
.lParam
= (LPARAM
)i
;
1098 // the native wxMSW implementation of the header window
1099 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1100 // on the right: to correct this effect we make the column
1101 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1103 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1105 switch (col
->GetAlignment())
1108 hdi
.fmt
|= HDF_LEFT
;
1110 case wxALIGN_CENTER
:
1111 case wxALIGN_CENTER_HORIZONTAL
:
1112 hdi
.fmt
|= HDF_CENTER
;
1115 hdi
.fmt
|= HDF_RIGHT
;
1119 // such alignment is not allowed for the column header!
1123 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1124 (WPARAM
)added
, (LPARAM
)&hdi
);
1129 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1133 // NOTE: we don't just return nmHDR->iItem because when there are
1134 // hidden columns, nmHDR->iItem may be different from
1135 // nmHDR->pitem->lParam
1137 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1139 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1144 item
.mask
= HDI_LPARAM
;
1145 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1147 return (unsigned int)item
.lParam
;
1150 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1152 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1154 // is it a message from the header?
1155 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1156 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1158 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1159 switch ( nmhdr
->code
)
1161 case HDN_BEGINTRACK
:
1162 // user has started to resize a column:
1163 // do we need to veto it?
1164 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1172 // user has started to reorder a column
1175 case HDN_ITEMCHANGING
:
1176 if (nmHDR
->pitem
!= NULL
&&
1177 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1179 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1180 if (nmHDR
->pitem
->cxy
< minWidth
)
1182 // do not allow the user to resize this column under
1183 // its minimal width:
1189 case HDN_ITEMCHANGED
: // user is resizing a column
1190 case HDN_ENDTRACK
: // user has finished resizing a column
1191 case HDN_ENDDRAG
: // user has finished reordering a column
1193 // update the width of the modified column:
1194 if (nmHDR
->pitem
!= NULL
&&
1195 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1197 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1198 unsigned int w
= nmHDR
->pitem
->cxy
;
1199 wxDataViewColumn
*col
= GetColumn(idx
);
1201 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1202 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1203 w
-= COLUMN_WIDTH_OFFSET
;
1205 if (w
>= (unsigned)col
->GetMinWidth())
1206 col
->SetInternalWidth(w
);
1212 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1213 wxEventType evt
= nmHDR
->iButton
== 0 ?
1214 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1215 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1216 SendEvent(evt
, idx
);
1222 // NOTE: for some reason (i.e. for a bug in Windows)
1223 // the HDN_ITEMCLICK notification is not sent on
1224 // right clicks, so we need to handle NM_RCLICK
1227 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1228 if (column
!= wxNOT_FOUND
)
1231 item
.mask
= HDI_LPARAM
;
1232 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1234 // 'idx' may be different from 'column' if there are
1235 // hidden columns...
1236 unsigned int idx
= (unsigned int)item
.lParam
;
1237 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1243 case HDN_GETDISPINFOW
:
1244 // see wxListCtrl::MSWOnNotify for more info!
1247 case HDN_ITEMDBLCLICK
:
1249 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1250 int w
= GetOwner()->GetBestColumnWidth(idx
);
1252 // update the native control:
1254 ZeroMemory(&hd
, sizeof(hd
));
1255 hd
.mask
= HDI_WIDTH
;
1257 Header_SetItem(GetHwnd(),
1258 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1261 // update the wxDataViewColumn class:
1262 GetColumn(idx
)->SetInternalWidth(w
);
1267 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1273 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1274 const wxRect
*WXUNUSED(rect
))
1276 wxSize ourSz
= GetClientSize();
1277 wxSize ownerSz
= m_owner
->GetClientSize();
1279 // where should the (logical) origin of this window be placed?
1281 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1283 // put this window on top of our parent and
1284 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1285 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1289 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1290 int WXUNUSED(w
), int WXUNUSED(h
),
1293 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1294 // the wxDataViewCtrl window gets resized: the following dummy call
1295 // to ScrollWindow() is required in order to get this header window
1296 // correctly repainted when it's (horizontally) scrolled:
1301 #else // !defined(__WXMSW__)
1303 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1304 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1305 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1306 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1307 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1310 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1311 const wxPoint
&pos
, const wxSize
&size
,
1312 const wxString
&name
)
1316 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1319 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1320 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1321 SetOwnForegroundColour( attr
.colFg
);
1322 SetOwnBackgroundColour( attr
.colBg
);
1324 SetOwnFont( attr
.font
);
1326 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1327 // a wxBoxSizer and in order to avoid super-big header windows,
1328 // we need to set our height as fixed
1329 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1330 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1335 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1338 GetClientSize( &w
, &h
);
1340 wxAutoBufferedPaintDC
dc( this );
1342 dc
.SetBackground(GetBackgroundColour());
1346 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1349 m_owner
->GetViewStart( &x
, NULL
);
1351 // account for the horz scrollbar offset
1352 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1354 dc
.SetFont( GetFont() );
1356 unsigned int cols
= GetOwner()->GetColumnCount();
1359 for (i
= 0; i
< cols
; i
++)
1361 wxDataViewColumn
*col
= GetColumn( i
);
1362 if (col
->IsHidden())
1363 continue; // skip it!
1365 int cw
= col
->GetWidth();
1368 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1369 if (col
->IsSortable())
1371 if (col
->IsSortOrderAscending())
1372 sortArrow
= wxHDR_SORT_ICON_UP
;
1374 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1377 wxRendererNative::Get().DrawHeaderButton
1381 wxRect(xpos
, 0, cw
, ch
-1),
1382 m_parent
->IsEnabled() ? 0
1383 : (int)wxCONTROL_DISABLED
,
1387 // align as required the column title:
1389 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1390 switch (col
->GetAlignment())
1393 x
+= HEADER_HORIZ_BORDER
;
1395 case wxALIGN_CENTER
:
1396 case wxALIGN_CENTER_HORIZONTAL
:
1397 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1400 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1404 // always center the title vertically:
1405 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1407 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1409 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1410 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1411 dc
.DrawText( col
->GetTitle(), x
, y
);
1412 dc
.DestroyClippingRegion();
1418 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1420 GetParent()->SetFocus();
1424 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1426 // we want to work with logical coords
1428 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1429 int y
= event
.GetY();
1433 // we don't draw the line beyond our window,
1434 // but we allow dragging it there
1436 GetClientSize( &w
, NULL
);
1437 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1440 // erase the line if it was drawn
1444 if (event
.ButtonUp())
1446 m_isDragging
= false;
1452 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1455 GetOwner()->Refresh();
1459 m_currentX
= wxMax(m_minX
+ 7, x
);
1461 // draw in the new location
1462 if (m_currentX
< w
) DrawCurrent();
1466 else // not dragging
1469 m_column
= wxNOT_FOUND
;
1471 bool hit_border
= false;
1473 // end of the current column
1476 // find the column where this event occured
1477 int countCol
= m_owner
->GetColumnCount();
1478 for (int column
= 0; column
< countCol
; column
++)
1480 wxDataViewColumn
*p
= GetColumn(column
);
1483 continue; // skip if not shown
1485 xpos
+= p
->GetWidth();
1487 if ((abs(x
-xpos
) < 3) && (y
< 22))
1495 // inside the column
1502 if (m_column
== wxNOT_FOUND
)
1505 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1506 if (event
.LeftDClick() && resizeable
)
1508 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1511 else if (event
.LeftDown() || event
.RightUp())
1513 if (hit_border
&& event
.LeftDown() && resizeable
)
1515 m_isDragging
= true;
1520 else // click on a column
1522 wxEventType evt
= event
.LeftDown() ?
1523 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1524 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1525 SendEvent(evt
, m_column
);
1528 else if (event
.Moving())
1530 if (hit_border
&& resizeable
)
1531 m_currentCursor
= m_resizeCursor
;
1533 m_currentCursor
= wxSTANDARD_CURSOR
;
1535 SetCursor(*m_currentCursor
);
1540 void wxGenericDataViewHeaderWindow::DrawCurrent()
1542 int x1
= m_currentX
;
1544 ClientToScreen (&x1
, &y1
);
1546 int x2
= m_currentX
-1;
1548 ++x2
; // but why ????
1551 m_owner
->GetClientSize( NULL
, &y2
);
1552 m_owner
->ClientToScreen( &x2
, &y2
);
1555 dc
.SetLogicalFunction(wxINVERT
);
1556 dc
.SetPen(m_penCurrent
);
1557 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1559 dc
.DrawLine(x1
, y1
, x2
, y2
);
1562 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1566 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1567 m_owner
->GetViewStart( &x
, NULL
);
1569 // shift the DC origin to match the position of the main window horizontal
1570 // scrollbar: this allows us to always use logical coords
1571 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1574 #endif // defined(__WXMSW__)
1576 //-----------------------------------------------------------------------------
1577 // wxDataViewRenameTimer
1578 //-----------------------------------------------------------------------------
1580 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1585 void wxDataViewRenameTimer::Notify()
1587 m_owner
->OnRenameTimer();
1590 //-----------------------------------------------------------------------------
1591 // wxDataViewMainWindow
1592 //-----------------------------------------------------------------------------
1594 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1596 if (row1
> row2
) return 1;
1597 if (row1
== row2
) return 0;
1602 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1604 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1605 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1606 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1607 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1608 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1609 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1612 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1613 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1614 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1615 m_selection( wxDataViewSelectionCmp
)
1620 m_lastOnSame
= false;
1621 m_renameTimer
= new wxDataViewRenameTimer( this );
1623 // TODO: user better initial values/nothing selected
1624 m_currentCol
= NULL
;
1627 // TODO: we need to calculate this smartly
1634 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1637 m_dragStart
= wxPoint(0,0);
1638 m_lineLastClicked
= (unsigned int) -1;
1639 m_lineBeforeLastClicked
= (unsigned int) -1;
1640 m_lineSelectSingleOnUp
= (unsigned int) -1;
1644 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1645 SetBackgroundColour( *wxWHITE
);
1647 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1649 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1650 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1651 //Some new added code to deal with the tree structure
1652 m_root
= new wxDataViewTreeNode( NULL
);
1653 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1658 wxDataViewMainWindow::~wxDataViewMainWindow()
1661 delete m_renameTimer
;
1664 void wxDataViewMainWindow::OnRenameTimer()
1666 // We have to call this here because changes may just have
1667 // been made and no screen update taken place.
1672 unsigned int cols
= GetOwner()->GetColumnCount();
1674 for (i
= 0; i
< cols
; i
++)
1676 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1678 continue; // skip it!
1680 if (c
== m_currentCol
)
1682 xpos
+= c
->GetWidth();
1684 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1685 m_currentCol
->GetWidth(), m_lineHeight
);
1687 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1688 &labelRect
.x
, &labelRect
.y
);
1690 m_currentCol
->GetRenderer()->StartEditing( m_currentRow
, labelRect
);
1697 virtual ~DoJob() { }
1699 virtual bool operator() ( wxDataViewTreeNode
* node
) = 0 ;
1702 class ItemAddJob
: public DoJob
1705 ItemAddJob( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1706 { this->parent
= parent
; this->item
= item
; }
1707 virtual ~ItemAddJob() { }
1709 virtual bool operator() ( wxDataViewTreeNode
* node
)
1711 if( node
->GetItem() == parent
)
1713 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1714 newnode
->SetItem(item
);
1715 node
->AppendChild( newnode
);
1722 wxDataViewItem parent
, item
;
1725 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1727 if( node
==NULL
|| !node
->HasChildren())
1730 wxDataViewTreeNodes nodes
= node
->GetChildren();
1731 int len
= node
->GetChildrenNumber();
1733 for( ; i
< len
; i
++ )
1735 wxDataViewTreeNode
* n
= nodes
[i
];
1738 if( Walker( n
, func
) )
1745 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1747 ItemAddJob
job( parent
, item
);
1748 Walker( m_root
, job
);
1753 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& item
)
1759 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1765 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int WXUNUSED(col
) )
1767 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1768 /*#define MAX_VIRTUAL_WIDTH 100000
1770 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1771 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1772 Refresh( true, &rect );
1780 bool wxDataViewMainWindow::Cleared()
1786 void wxDataViewMainWindow::UpdateDisplay()
1791 void wxDataViewMainWindow::OnInternalIdle()
1793 wxWindow::OnInternalIdle();
1797 RecalculateDisplay();
1802 void wxDataViewMainWindow::RecalculateDisplay()
1804 wxDataViewModel
*model
= GetOwner()->GetModel();
1811 int width
= GetEndOfLastCol();
1812 int height
= GetRowCount() * m_lineHeight
;
1814 SetVirtualSize( width
, height
);
1815 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1820 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1822 wxWindow::ScrollWindow( dx
, dy
, rect
);
1824 if (GetOwner()->m_headerArea
)
1825 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1828 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1830 wxDataViewModel
*model
= GetOwner()->GetModel();
1831 wxAutoBufferedPaintDC
dc( this );
1834 dc
.SetBackground(GetBackgroundColour());
1836 GetOwner()->PrepareDC( dc
);
1837 dc
.SetFont( GetFont() );
1839 wxRect update
= GetUpdateRegion().GetBox();
1840 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1842 // compute which items needs to be redrawn
1843 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1844 unsigned int item_count
=
1845 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1846 (int)(GetRowCount( )- item_start
) );
1847 unsigned int item_last
= item_start
+ item_count
;
1849 // compute which columns needs to be redrawn
1850 unsigned int cols
= GetOwner()->GetColumnCount();
1851 unsigned int col_start
= 0;
1852 unsigned int x_start
= 0;
1853 for (x_start
= 0; col_start
< cols
; col_start
++)
1855 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
1856 if (col
->IsHidden())
1857 continue; // skip it!
1859 unsigned int w
= col
->GetWidth();
1860 if (x_start
+w
>= (unsigned int)update
.x
)
1866 unsigned int col_last
= col_start
;
1867 unsigned int x_last
= x_start
;
1868 for (; col_last
< cols
; col_last
++)
1870 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
1871 if (col
->IsHidden())
1872 continue; // skip it!
1874 if (x_last
> (unsigned int)update
.GetRight())
1877 x_last
+= col
->GetWidth();
1880 // Draw horizontal rules if required
1881 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1883 dc
.SetPen(m_penRule
);
1884 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1886 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1888 int y
= i
* m_lineHeight
;
1889 dc
.DrawLine(x_start
, y
, x_last
, y
);
1893 // Draw vertical rules if required
1894 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1896 dc
.SetPen(m_penRule
);
1897 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1900 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1902 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
1903 if (col
->IsHidden())
1904 continue; // skip it
1906 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1907 x
, item_last
* m_lineHeight
);
1909 x
+= col
->GetWidth();
1912 // Draw last vertical rule
1913 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1914 x
, item_last
* m_lineHeight
);
1917 // redraw the background for the items which are selected/current
1918 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1920 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1921 if (selected
|| item
== m_currentRow
)
1923 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1924 if (item
== m_currentRow
)
1925 flags
|= wxCONTROL_CURRENT
;
1927 flags
|= wxCONTROL_FOCUSED
;
1929 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
1930 wxRendererNative::Get().DrawItemSelectionRect
1940 // redraw all cells for all rows which must be repainted and for all columns
1942 cell_rect
.x
= x_start
;
1943 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
1944 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1946 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1947 wxDataViewRenderer
*cell
= col
->GetRenderer();
1948 cell_rect
.width
= col
->GetWidth();
1950 if (col
->IsHidden())
1951 continue; // skipt it!
1953 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1955 // get the cell value and set it into the renderer
1957 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
1961 wxDataViewItem dataitem
= node
->GetItem();
1962 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1963 cell
->SetValue( value
);
1965 // update the y offset
1966 cell_rect
.y
= item
* m_lineHeight
;
1968 //Draw the expander here. Please notice that I use const number for all pixel data. When the final API are determined
1969 //I will change this to the data member of the class wxDataViewCtrl
1970 int indent
= node
->GetIndentLevel();
1971 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
1973 //Calculate the indent first
1974 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1976 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1977 // change the cell_rect.x to the appropriate pos
1978 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
1979 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1980 dc
.SetPen( m_penExpander
);
1981 dc
.SetBrush( wxNullBrush
);
1982 if( node
->HasChildren() )
1984 dc
.DrawRoundedRectangle( expander_x
,expander_y
,expander_width
,expander_width
, 1.0);
1985 dc
.DrawLine( expander_x
+ 2 , expander_y
+ expander_width
/2, expander_x
+ expander_width
- 2, expander_y
+ expander_width
/2 );
1987 if( !node
->IsOpen() )
1988 dc
.DrawLine( expander_x
+ expander_width
/2, expander_y
+ 2, expander_x
+ expander_width
/2, expander_y
+ expander_width
-2 );
1992 // I am wandering whether we should draw dot lines between tree nodes
1995 //force the expander column to left-center align
1996 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2000 // cannot be bigger than allocated space
2001 wxSize size
= cell
->GetSize();
2002 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2003 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2004 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2006 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2007 int align
= cell
->GetAlignment();
2009 // horizontal alignment:
2010 item_rect
.x
= cell_rect
.x
;
2011 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2012 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2013 else if (align
& wxALIGN_RIGHT
)
2014 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2015 //else: wxALIGN_LEFT is the default
2017 // vertical alignment:
2018 item_rect
.y
= cell_rect
.y
;
2019 if (align
& wxALIGN_CENTER_VERTICAL
)
2020 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2021 else if (align
& wxALIGN_BOTTOM
)
2022 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2023 //else: wxALIGN_TOP is the default
2026 item_rect
.x
+= PADDING_RIGHTLEFT
;
2027 item_rect
.y
+= PADDING_TOPBOTTOM
;
2028 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2029 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2031 //Here we add the tree indent
2032 item_rect
.x
+= indent
;
2035 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2036 state
|= wxDATAVIEW_CELL_SELECTED
;
2038 // TODO: it would be much more efficient to create a clipping
2039 // region for the entire column being rendered (in the OnPaint
2040 // of wxDataViewMainWindow) instead of a single clip region for
2041 // each cell. However it would mean that each renderer should
2042 // respect the given wxRect's top & bottom coords, eventually
2043 // violating only the left & right coords - however the user can
2044 // make its own renderer and thus we cannot be sure of that.
2045 dc
.SetClippingRegion( item_rect
);
2046 cell
->Render( item_rect
, &dc
, state
);
2047 dc
.DestroyClippingRegion();
2050 cell_rect
.x
+= cell_rect
.width
;
2054 int wxDataViewMainWindow::GetCountPerPage() const
2056 wxSize size
= GetClientSize();
2057 return size
.y
/ m_lineHeight
;
2060 int wxDataViewMainWindow::GetEndOfLastCol() const
2064 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2066 const wxDataViewColumn
*c
=
2067 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2070 width
+= c
->GetWidth();
2075 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2079 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2081 return y
/ m_lineHeight
;
2084 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2086 wxSize client_size
= GetClientSize();
2087 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2088 &client_size
.x
, &client_size
.y
);
2090 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2093 unsigned int wxDataViewMainWindow::GetRowCount()
2095 if ( m_count
== -1 )
2097 m_count
= RecalculateCount();
2099 GetVirtualSize( &width
, &height
);
2100 height
= m_count
* m_lineHeight
;
2102 SetVirtualSize( width
, height
);
2107 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2114 void wxDataViewMainWindow::SelectAllRows( bool on
)
2121 m_selection
.Clear();
2122 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2123 m_selection
.Add( i
);
2128 unsigned int first_visible
= GetFirstVisibleRow();
2129 unsigned int last_visible
= GetLastVisibleRow();
2131 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2133 unsigned int row
= m_selection
[i
];
2134 if ((row
>= first_visible
) && (row
<= last_visible
))
2137 m_selection
.Clear();
2141 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2143 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2147 m_selection
.Add( row
);
2155 m_selection
.Remove( row
);
2161 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2165 unsigned int tmp
= from
;
2171 for (i
= from
; i
<= to
; i
++)
2173 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2176 m_selection
.Add( i
);
2181 m_selection
.Remove( i
);
2184 RefreshRows( from
, to
);
2187 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2189 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2191 int n
= aSelections
[i
];
2193 m_selection
.Add( n
);
2198 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2200 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2201 m_selection
.Add( row
);
2203 m_selection
.Remove( row
);
2207 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2209 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2212 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2214 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2215 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2217 wxSize client_size
= GetClientSize();
2218 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2219 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2220 if (intersect_rect
.width
> 0)
2221 Refresh( true, &intersect_rect
);
2224 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2228 unsigned int tmp
= to
;
2233 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2234 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2236 wxSize client_size
= GetClientSize();
2237 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2238 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2239 if (intersect_rect
.width
> 0)
2240 Refresh( true, &intersect_rect
);
2243 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2245 unsigned int count
= GetRowCount();
2246 if (firstRow
> count
)
2249 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2250 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2252 wxSize client_size
= GetClientSize();
2253 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2254 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2255 if (intersect_rect
.width
> 0)
2256 Refresh( true, &intersect_rect
);
2259 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2261 wxCHECK_RET( newCurrent
< GetRowCount(),
2262 _T("invalid item index in OnArrowChar()") );
2264 // if there is no selection, we cannot move it anywhere
2265 if (!HasCurrentRow())
2268 unsigned int oldCurrent
= m_currentRow
;
2270 // in single selection we just ignore Shift as we can't select several
2272 if ( event
.ShiftDown() && !IsSingleSel() )
2274 RefreshRow( oldCurrent
);
2276 ChangeCurrentRow( newCurrent
);
2278 // select all the items between the old and the new one
2279 if ( oldCurrent
> newCurrent
)
2281 newCurrent
= oldCurrent
;
2282 oldCurrent
= m_currentRow
;
2285 SelectRows( oldCurrent
, newCurrent
, true );
2289 RefreshRow( oldCurrent
);
2291 // all previously selected items are unselected unless ctrl is held
2292 if ( !event
.ControlDown() )
2293 SelectAllRows(false);
2295 ChangeCurrentRow( newCurrent
);
2297 if ( !event
.ControlDown() )
2298 SelectRow( m_currentRow
, true );
2300 RefreshRow( m_currentRow
);
2303 //EnsureVisible( m_currentRow );
2306 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2310 rect
.y
= m_lineHeight
* row
;
2311 rect
.width
= GetEndOfLastCol();
2312 rect
.height
= m_lineHeight
;
2317 class RowToItemJob
: public DoJob
2320 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2321 virtual ~RowToItemJob(){};
2323 virtual bool operator() ( wxDataViewTreeNode
* node
)
2325 if ( current
== row
)
2327 ret
= node
->GetItem() ;
2334 wxDataViewItem
GetResult() { return ret
; }
2342 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2344 RowToItemJob
job( row
, 0 );
2345 Walker( m_root
, job
);
2346 return job
.GetResult();
2349 class RowToTreeNodeJob
: public DoJob
2352 RowToTreeNodeJob( unsigned int row
, int current
)
2353 { this->row
= row
; this->current
= current
; ret
= NULL
; }
2354 virtual ~RowToTreeNodeJob() { }
2356 virtual int operator() ( wxDataViewTreeNode
* node
)
2364 if ( node
->IsOpen())
2370 wxDataViewTreeNode
* GetResult(){ return ret
; }
2374 wxDataViewTreeNode
* ret
;
2378 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2380 RowToTreeNodeJob
job( row
, 0 );
2381 Walker( m_root
, job
);
2382 return job
.GetResult();
2385 class CountJob
: public DoJob
2388 CountJob(){ count
= 0 ; }
2389 virtual ~CountJob() { }
2391 virtual int operator () ( wxDataViewTreeNode
* node
)
2400 unsigned int GetResult()
2408 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2410 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2413 if (node
->HasChildren())
2414 if (!node
->IsOpen())
2419 // RefreshRows(row,GetLastVisibleRow());
2424 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2426 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2429 if( node
->HasChildren() && node
->IsOpen() )
2434 //RefreshRows(row,GetLastVisibleRow());
2438 node
= node
->GetParent();
2441 int parent
= GetRowByItem( node
->GetItem()) ;
2442 SelectRow( row
, false);
2443 SelectRow(parent
, true );
2444 ChangeCurrentRow( parent
);
2450 int wxDataViewMainWindow::RecalculateCount()
2453 Walker( m_root
, job
);
2454 return job
.GetResult();
2457 class ItemToRowJob
: public DoJob
2460 ItemToRowJob(const wxDataViewItem
& item
){ this->item
= item
; ret
= 0 ; }
2461 virtual ~ItemToRowJob(){};
2463 virtual bool operator() ( wxDataViewTreeNode
* node
)
2466 if( node
->GetItem() == item
)
2471 //the row number is begin from zero
2472 int GetResult(){ return ret
-1 ; }
2474 wxDataViewItem item
;
2478 unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2480 ItemToRowJob
job( item
);
2481 Walker(m_root
, job
);
2482 return job
.GetResult();
2485 unsigned int BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2488 if( !model
->HasChildren( item
) )
2491 wxDataViewItem i
= model
->GetFirstChild( item
);
2494 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2496 node
->AppendChild(n
);
2497 int num
= BuildTreeHelper( model
, i
, n
) + 1;
2499 i
= model
->GetNextSibling( i
);
2504 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2506 //First we define a invalid item to fetch the top-level elements
2507 wxDataViewItem item
;
2508 BuildTreeHelper( model
, item
, m_root
);
2512 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2514 if( node
->HasChildren() )
2516 int len
= node
->GetChildrenNumber();
2518 wxDataViewTreeNodes nodes
= node
->GetChildren();
2519 for( ; i
< len
; i
++ )
2521 DestroyTreeHelper(nodes
[i
]);
2527 void wxDataViewMainWindow::DestroyTree()
2529 DestroyTreeHelper(m_root
);
2533 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2535 if (event
.GetKeyCode() == WXK_TAB
)
2537 wxNavigationKeyEvent nevent
;
2538 nevent
.SetWindowChange( event
.ControlDown() );
2539 nevent
.SetDirection( !event
.ShiftDown() );
2540 nevent
.SetEventObject( GetParent()->GetParent() );
2541 nevent
.SetCurrentFocus( m_parent
);
2542 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
2546 // no item -> nothing to do
2547 if (!HasCurrentRow())
2553 // don't use m_linesPerPage directly as it might not be computed yet
2554 const int pageSize
= GetCountPerPage();
2555 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2557 switch ( event
.GetKeyCode() )
2560 if ( m_currentRow
> 0 )
2561 OnArrowChar( m_currentRow
- 1, event
);
2565 if ( m_currentRow
< GetRowCount() - 1 )
2566 OnArrowChar( m_currentRow
+ 1, event
);
2568 //Add the process for tree expanding/collapsing
2570 OnCollapsing(m_currentRow
);
2573 OnExpanding( m_currentRow
);
2577 OnArrowChar( GetRowCount() - 1, event
);
2582 OnArrowChar( 0, event
);
2587 int steps
= pageSize
- 1;
2588 int index
= m_currentRow
- steps
;
2592 OnArrowChar( index
, event
);
2598 int steps
= pageSize
- 1;
2599 unsigned int index
= m_currentRow
+ steps
;
2600 unsigned int count
= GetRowCount();
2601 if ( index
>= count
)
2604 OnArrowChar( index
, event
);
2613 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2615 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2617 // let the base handle mouse wheel events.
2622 int x
= event
.GetX();
2623 int y
= event
.GetY();
2624 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2626 wxDataViewColumn
*col
= NULL
;
2629 unsigned int cols
= GetOwner()->GetColumnCount();
2631 for (i
= 0; i
< cols
; i
++)
2633 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2635 continue; // skip it!
2637 if (x
< xpos
+ c
->GetWidth())
2642 xpos
+= c
->GetWidth();
2646 wxDataViewRenderer
*cell
= col
->GetRenderer();
2648 unsigned int current
= y
/ m_lineHeight
;
2650 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
2652 // Unselect all if below the last row ?
2656 wxDataViewModel
*model
= GetOwner()->GetModel();
2658 if (event
.Dragging())
2660 if (m_dragCount
== 0)
2662 // we have to report the raw, physical coords as we want to be
2663 // able to call HitTest(event.m_pointDrag) from the user code to
2664 // get the item being dragged
2665 m_dragStart
= event
.GetPosition();
2670 if (m_dragCount
!= 3)
2673 if (event
.LeftIsDown())
2675 // Notify cell about drag
2684 bool forceClick
= false;
2686 if (event
.ButtonDClick())
2688 m_renameTimer
->Stop();
2689 m_lastOnSame
= false;
2692 if (event
.LeftDClick())
2694 if ( current
== m_lineLastClicked
)
2696 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
2699 model
->GetValue( value
, col
->GetModelColumn(), current
);
2700 cell
->SetValue( value
);
2701 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
2702 col
->GetWidth(), m_lineHeight
);
2703 wxDataViewItem dataitem
= GetItemByRow(current
);
2704 cell
->Activate( cell_rect
, model
, dataitem
, col
->GetModelColumn() );
2710 // The first click was on another item, so don't interpret this as
2711 // a double click, but as a simple click instead
2718 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
2720 // select single line
2721 SelectAllRows( false );
2722 SelectRow( m_lineSelectSingleOnUp
, true );
2725 //Process the event of user clicking the expander
2726 bool expander
= false;
2727 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2728 if( node
!=NULL
&& node
->HasChildren() )
2730 int indent
= node
->GetIndentLevel();
2731 indent
= GetOwner()->GetIndent()*indent
;
2732 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
2733 if( rect
.Contains( x
, y
) )
2737 m_count
= -1; //make the current row number fail
2740 //int last_row = GetLastVisibleRow();
2741 //RefreshRows( current, last_row );
2745 //If the user click the expander, we do not do editing even if the column with expander are editable
2746 if (m_lastOnSame
&& !expander
)
2748 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
2749 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
2751 m_renameTimer
->Start( 100, true );
2755 m_lastOnSame
= false;
2756 m_lineSelectSingleOnUp
= (unsigned int)-1;
2760 // This is necessary, because after a DnD operation in
2761 // from and to ourself, the up event is swallowed by the
2762 // DnD code. So on next non-up event (which means here and
2763 // now) m_lineSelectSingleOnUp should be reset.
2764 m_lineSelectSingleOnUp
= (unsigned int)-1;
2767 if (event
.RightDown())
2769 m_lineBeforeLastClicked
= m_lineLastClicked
;
2770 m_lineLastClicked
= current
;
2772 // If the item is already selected, do not update the selection.
2773 // Multi-selections should not be cleared if a selected item is clicked.
2774 if (!IsRowSelected(current
))
2776 SelectAllRows(false);
2777 ChangeCurrentRow(current
);
2778 SelectRow(m_currentRow
,true);
2781 // notify cell about right click
2784 // Allow generation of context menu event
2787 else if (event
.MiddleDown())
2789 // notify cell about middle click
2792 if (event
.LeftDown() || forceClick
)
2798 m_lineBeforeLastClicked
= m_lineLastClicked
;
2799 m_lineLastClicked
= current
;
2801 unsigned int oldCurrentRow
= m_currentRow
;
2802 bool oldWasSelected
= IsRowSelected(m_currentRow
);
2804 bool cmdModifierDown
= event
.CmdDown();
2805 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2807 if ( IsSingleSel() || !IsRowSelected(current
) )
2809 SelectAllRows( false );
2811 ChangeCurrentRow(current
);
2813 SelectRow(m_currentRow
,true);
2815 else // multi sel & current is highlighted & no mod keys
2817 m_lineSelectSingleOnUp
= current
;
2818 ChangeCurrentRow(current
); // change focus
2821 else // multi sel & either ctrl or shift is down
2823 if (cmdModifierDown
)
2825 ChangeCurrentRow(current
);
2827 ReverseRowSelection(m_currentRow
);
2829 else if (event
.ShiftDown())
2831 ChangeCurrentRow(current
);
2833 unsigned int lineFrom
= oldCurrentRow
,
2836 if ( lineTo
< lineFrom
)
2839 lineFrom
= m_currentRow
;
2842 SelectRows(lineFrom
, lineTo
, true);
2844 else // !ctrl, !shift
2846 // test in the enclosing if should make it impossible
2847 wxFAIL_MSG( _T("how did we get here?") );
2851 if (m_currentRow
!= oldCurrentRow
)
2852 RefreshRow( oldCurrentRow
);
2854 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
2856 // Update selection here...
2859 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
2860 (current
== oldCurrentRow
)) && oldWasSelected
;
2864 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
2868 if (HasCurrentRow())
2874 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
2878 if (HasCurrentRow())
2884 //-----------------------------------------------------------------------------
2886 //-----------------------------------------------------------------------------
2888 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2890 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
2891 EVT_SIZE(wxDataViewCtrl::OnSize
)
2894 wxDataViewCtrl::~wxDataViewCtrl()
2897 GetModel()->RemoveNotifier( m_notifier
);
2900 void wxDataViewCtrl::Init()
2905 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2906 const wxPoint
& pos
, const wxSize
& size
,
2907 long style
, const wxValidator
& validator
)
2909 if (!wxControl::Create( parent
, id
, pos
, size
,
2910 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
2916 MacSetClipChildren( true ) ;
2919 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
2921 if (HasFlag(wxDV_NO_HEADER
))
2922 m_headerArea
= NULL
;
2924 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
2926 SetTargetWindow( m_clientArea
);
2928 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
2930 sizer
->Add( m_headerArea
, 0, wxGROW
);
2931 sizer
->Add( m_clientArea
, 1, wxGROW
);
2938 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
2942 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
2945 // we need to process arrows ourselves for scrolling
2946 if ( nMsg
== WM_GETDLGCODE
)
2948 rc
|= DLGC_WANTARROWS
;
2956 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
2958 // We need to override OnSize so that our scrolled
2959 // window a) does call Layout() to use sizers for
2960 // positioning the controls but b) does not query
2961 // the sizer for their size and use that for setting
2962 // the scrollable area as set that ourselves by
2963 // calling SetScrollbar() further down.
2970 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2972 if (!wxDataViewCtrlBase::AssociateModel( model
))
2975 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
2977 model
->AddNotifier( m_notifier
);
2979 m_clientArea
->BuildTree(model
);
2981 m_clientArea
->UpdateDisplay();
2986 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2988 if (!wxDataViewCtrlBase::AppendColumn(col
))
2995 void wxDataViewCtrl::OnColumnChange()
2998 m_headerArea
->UpdateDisplay();
3000 m_clientArea
->UpdateDisplay();
3003 void wxDataViewCtrl::DoSetExpanderColumn()
3005 m_clientArea
->UpdateDisplay();
3008 void wxDataViewCtrl::DoSetIndent()
3010 m_clientArea
->UpdateDisplay();
3014 /********************************************************************
3015 void wxDataViewCtrl::SetSelection( int row )
3017 m_clientArea->SelectRow(row, true);
3020 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
3022 m_clientArea->SelectRows(from, to, true);
3025 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
3027 m_clientArea->Select(aSelections);
3030 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
3035 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
3042 int wxDataViewCtrl::GetSelection() const
3049 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
3055 *********************************************************************/
3057 // !wxUSE_GENERICDATAVIEWCTRL
3060 // wxUSE_DATAVIEWCTRL