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;
61 bool operator == ( const wxDataViewItem
& left
, const wxDataViewItem
& right
)
63 return left
.GetID() == right
.GetID();
66 //-----------------------------------------------------------------------------
67 // wxDataViewHeaderWindow
68 //-----------------------------------------------------------------------------
70 #define USE_NATIVE_HEADER_WINDOW 1
72 // NB: for some reason, this class must be dllexport'ed or we get warnings from
74 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
77 wxDataViewHeaderWindowBase()
80 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
81 const wxPoint
&pos
, const wxSize
&size
,
84 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
87 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
88 wxDataViewCtrl
*GetOwner() { return m_owner
; }
90 // called on column addition/removal
91 virtual void UpdateDisplay() { /* by default, do nothing */ }
93 // returns the n-th column
94 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
97 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
104 wxDataViewCtrl
*m_owner
;
106 // sends an event generated from the n-th wxDataViewColumn
107 void SendEvent(wxEventType type
, unsigned int n
);
110 // on wxMSW the header window (only that part however) can be made native!
111 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
113 #define COLUMN_WIDTH_OFFSET 2
114 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
116 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
120 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
122 const wxPoint
&pos
= wxDefaultPosition
,
123 const wxSize
&size
= wxDefaultSize
,
124 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
126 Create(parent
, id
, pos
, size
, name
);
129 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
130 const wxPoint
&pos
, const wxSize
&size
,
131 const wxString
&name
);
133 ~wxDataViewHeaderWindowMSW();
135 // called when any column setting is changed and/or changed
137 virtual void UpdateDisplay();
139 // called when the main window gets scrolled
140 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
143 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
144 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
146 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
148 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
149 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
152 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
155 #else // !defined(__WXMSW__)
157 #define HEADER_WINDOW_HEIGHT 25
158 #define HEADER_HORIZ_BORDER 5
159 #define HEADER_VERT_BORDER 3
160 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
162 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
165 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
167 const wxPoint
&pos
= wxDefaultPosition
,
168 const wxSize
&size
= wxDefaultSize
,
169 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
172 Create(parent
, id
, pos
, size
, name
);
175 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
176 const wxPoint
&pos
, const wxSize
&size
,
177 const wxString
&name
);
179 ~wxGenericDataViewHeaderWindow()
181 delete m_resizeCursor
;
184 virtual void UpdateDisplay() { Refresh(); }
188 void OnPaint( wxPaintEvent
&event
);
189 void OnMouse( wxMouseEvent
&event
);
190 void OnSetFocus( wxFocusEvent
&event
);
195 // vars used for column resizing:
197 wxCursor
*m_resizeCursor
;
198 const wxCursor
*m_currentCursor
;
201 bool m_dirty
; // needs refresh?
202 int m_column
; // index of the column being resized
203 int m_currentX
; // divider line position in logical (unscrolled) coords
204 int m_minX
; // minimal position beyond which the divider line
205 // can't be dragged in logical coords
207 // the pen used to draw the current column width drag line
208 // when resizing the columsn
212 // internal utilities:
216 m_currentCursor
= (wxCursor
*) NULL
;
217 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
219 m_isDragging
= false;
222 m_column
= wxNOT_FOUND
;
226 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
227 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
231 void AdjustDC(wxDC
& dc
);
234 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
235 DECLARE_EVENT_TABLE()
238 #endif // defined(__WXMSW__)
240 //-----------------------------------------------------------------------------
241 // wxDataViewRenameTimer
242 //-----------------------------------------------------------------------------
244 class wxDataViewRenameTimer
: public wxTimer
247 wxDataViewMainWindow
*m_owner
;
250 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
254 //-----------------------------------------------------------------------------
255 // wxDataViewTreeNode
256 //-----------------------------------------------------------------------------
257 class wxDataViewTreeNode
;
258 WX_DEFINE_ARRAY_PTR( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
260 class wxDataViewTreeNode
263 wxDataViewTreeNode( wxDataViewTreeNode
* parent
)
264 { this->parent
= parent
;
270 //I don't know what I need to do in the destructure
271 ~wxDataViewTreeNode()
274 wxDataViewTreeNode
* GetParent() { return parent
; }
275 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
276 wxDataViewTreeNodes
GetChildren() { return children
; }
277 void SetChildren( wxDataViewTreeNodes children
) { this->children
= children
; }
279 wxDataViewTreeNode
* GetChild( unsigned int n
) { return children
.Item( n
); }
280 void InsertChild( wxDataViewTreeNode
* child
, unsigned int n
) { children
.Insert( child
, n
); }
281 void AppendChild( wxDataViewTreeNode
* child
) { children
.Add( child
); }
283 wxDataViewItem
& GetItem() { return item
; }
284 void SetItem( wxDataViewItem
& item
) { this->item
= item
; }
286 unsigned int GetChildrenNumber() { return children
.GetCount(); }
288 bool IsOpen() { return open
; }
289 bool HasChildren() { return children
.GetCount() != 0; }
291 wxDataViewTreeNode
* parent
;
292 wxDataViewTreeNodes children
;
297 //-----------------------------------------------------------------------------
298 // wxDataViewMainWindow
299 //-----------------------------------------------------------------------------
301 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
304 class wxDataViewMainWindow
: public wxWindow
307 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
309 const wxPoint
&pos
= wxDefaultPosition
,
310 const wxSize
&size
= wxDefaultSize
,
311 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
312 virtual ~wxDataViewMainWindow();
314 // notifications from wxDataViewModel
315 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
316 bool ItemDeleted( const wxDataViewItem
&item
);
317 bool ItemChanged( const wxDataViewItem
&item
);
318 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
321 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
322 wxDataViewCtrl
*GetOwner() { return m_owner
; }
323 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
325 void OnPaint( wxPaintEvent
&event
);
326 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
327 void OnChar( wxKeyEvent
&event
);
328 void OnMouse( wxMouseEvent
&event
);
329 void OnSetFocus( wxFocusEvent
&event
);
330 void OnKillFocus( wxFocusEvent
&event
);
332 void UpdateDisplay();
333 void RecalculateDisplay();
334 void OnInternalIdle();
336 void OnRenameTimer();
338 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
340 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
341 void ChangeCurrentRow( unsigned int row
);
343 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
344 bool IsEmpty() { return GetRowCount() == 0; }
346 int GetCountPerPage() const;
347 int GetEndOfLastCol() const;
348 unsigned int GetFirstVisibleRow() const;
349 unsigned int GetLastVisibleRow() const;
350 unsigned int GetRowCount() const;
352 void Select( const wxArrayInt
& aSelections
);
353 void SelectAllRows( bool on
);
354 void SelectRow( unsigned int row
, bool on
);
355 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
356 void ReverseRowSelection( unsigned int row
);
357 bool IsRowSelected( unsigned int row
);
359 void RefreshRow( unsigned int row
);
360 void RefreshRows( unsigned int from
, unsigned int to
);
361 void RefreshRowsAfter( unsigned int firstRow
);
363 // returns the colour to be used for drawing the rules
364 wxColour
GetRuleColour() const
366 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
369 //void EnsureVisible( unsigned int row );
370 wxRect
GetLineRect( unsigned int row
) const;
372 //Some useful functions for row and item mapping
373 wxDataViewItem
GetItemByRow( unsigned int row
);
374 unsigned int GetRowByItem( const wxDataViewItem
& item
);
376 //Methods for building the mapping tree
377 void BuildTree( wxDataViewModel
* model
);
381 wxDataViewCtrl
*m_owner
;
385 wxDataViewColumn
*m_currentCol
;
386 unsigned int m_currentRow
;
387 wxDataViewSelection m_selection
;
389 wxDataViewRenameTimer
*m_renameTimer
;
397 // for double click logic
398 unsigned int m_lineLastClicked
,
399 m_lineBeforeLastClicked
,
400 m_lineSelectSingleOnUp
;
402 // the pen used to draw horiz/vertical rules
405 //This is the tree structure of the model
406 wxDataViewTreeNode
* m_root
;
407 unsigned int m_count
;
409 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
410 DECLARE_EVENT_TABLE()
413 // ---------------------------------------------------------
414 // wxGenericDataViewModelNotifier
415 // ---------------------------------------------------------
417 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
420 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
421 { m_mainWindow
= mainWindow
; }
423 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
424 { return m_mainWindow
->ItemAdded( parent
, item
); }
425 virtual bool ItemDeleted( const wxDataViewItem
& item
)
426 { return m_mainWindow
->ItemDeleted( item
); }
427 virtual bool ItemChanged( const wxDataViewItem
& item
)
428 { return m_mainWindow
->ItemChanged(item
); }
429 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
430 { return m_mainWindow
->ValueChanged( item
, col
); }
431 virtual bool Cleared()
432 { return m_mainWindow
->Cleared(); }
434 wxDataViewMainWindow
*m_mainWindow
;
437 // ---------------------------------------------------------
438 // wxDataViewRenderer
439 // ---------------------------------------------------------
441 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
443 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
444 wxDataViewCellMode mode
,
446 wxDataViewRendererBase( varianttype
, mode
, align
)
453 wxDataViewRenderer::~wxDataViewRenderer()
459 wxDC
*wxDataViewRenderer::GetDC()
463 if (GetOwner() == NULL
)
465 if (GetOwner()->GetOwner() == NULL
)
467 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
473 // ---------------------------------------------------------
474 // wxDataViewCustomRenderer
475 // ---------------------------------------------------------
477 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
479 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
480 wxDataViewCellMode mode
, int align
) :
481 wxDataViewRenderer( varianttype
, mode
, align
)
485 // ---------------------------------------------------------
486 // wxDataViewTextRenderer
487 // ---------------------------------------------------------
489 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
491 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
492 wxDataViewCellMode mode
, int align
) :
493 wxDataViewCustomRenderer( varianttype
, mode
, align
)
497 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
499 m_text
= value
.GetString();
504 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
509 bool wxDataViewTextRenderer::HasEditorCtrl()
514 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
515 wxRect labelRect
, const wxVariant
&value
)
517 return new wxTextCtrl( parent
, wxID_ANY
, value
,
518 wxPoint(labelRect
.x
,labelRect
.y
),
519 wxSize(labelRect
.width
,labelRect
.height
) );
522 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
524 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
525 value
= text
->GetValue();
529 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
531 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
532 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
533 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
534 view
->GetForegroundColour();
536 dc
->SetTextForeground(col
);
537 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
542 wxSize
wxDataViewTextRenderer::GetSize() const
544 const wxDataViewCtrl
*view
= GetView();
548 view
->GetTextExtent( m_text
, &x
, &y
);
549 return wxSize( x
, y
);
551 return wxSize(80,20);
554 // ---------------------------------------------------------
555 // wxDataViewBitmapRenderer
556 // ---------------------------------------------------------
558 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
560 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
561 wxDataViewCellMode mode
, int align
) :
562 wxDataViewCustomRenderer( varianttype
, mode
, align
)
566 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
568 if (value
.GetType() == wxT("wxBitmap"))
570 if (value
.GetType() == wxT("wxIcon"))
576 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
581 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
584 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
585 else if (m_icon
.Ok())
586 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
591 wxSize
wxDataViewBitmapRenderer::GetSize() const
594 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
595 else if (m_icon
.Ok())
596 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
598 return wxSize(16,16);
601 // ---------------------------------------------------------
602 // wxDataViewToggleRenderer
603 // ---------------------------------------------------------
605 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
607 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
608 wxDataViewCellMode mode
, int align
) :
609 wxDataViewCustomRenderer( varianttype
, mode
, align
)
614 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
616 m_toggle
= value
.GetBool();
621 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
626 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
628 // User wxRenderer here
631 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
633 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
638 flags
|= wxCONTROL_CHECKED
;
639 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
640 flags
|= wxCONTROL_DISABLED
;
642 wxRendererNative::Get().DrawCheckBox(
643 GetOwner()->GetOwner(),
651 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
652 wxDataViewModel
*model
,
653 const wxDataViewItem
& item
, unsigned int col
)
655 bool value
= !m_toggle
;
656 wxVariant variant
= value
;
657 model
->SetValue( variant
, item
, col
);
658 model
->ValueChanged( item
, col
);
662 wxSize
wxDataViewToggleRenderer::GetSize() const
664 return wxSize(20,20);
667 // ---------------------------------------------------------
668 // wxDataViewProgressRenderer
669 // ---------------------------------------------------------
671 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
673 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
674 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
675 wxDataViewCustomRenderer( varianttype
, mode
, align
)
681 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
685 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
687 m_value
= (long) value
;
689 if (m_value
< 0) m_value
= 0;
690 if (m_value
> 100) m_value
= 100;
695 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
697 value
= (long) m_value
;
701 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
703 double pct
= (double)m_value
/ 100.0;
705 bar
.width
= (int)(cell
.width
* pct
);
706 dc
->SetPen( *wxTRANSPARENT_PEN
);
707 dc
->SetBrush( *wxBLUE_BRUSH
);
708 dc
->DrawRectangle( bar
);
710 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
711 dc
->SetPen( *wxBLACK_PEN
);
712 dc
->DrawRectangle( cell
);
717 wxSize
wxDataViewProgressRenderer::GetSize() const
719 return wxSize(40,12);
722 // ---------------------------------------------------------
723 // wxDataViewDateRenderer
724 // ---------------------------------------------------------
726 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
728 #if wxUSE_DATE_RENDERER_POPUP
730 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
733 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
734 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
735 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
740 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
741 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
742 sizer
->Add( m_cal
, 1, wxGROW
);
747 void OnCalendar( wxCalendarEvent
&event
);
749 wxCalendarCtrl
*m_cal
;
750 wxDataViewModel
*m_model
;
752 const wxDataViewItem
& m_item
;
755 virtual void OnDismiss()
760 DECLARE_EVENT_TABLE()
763 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
764 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
767 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
769 wxDateTime date
= event
.GetDate();
770 wxVariant value
= date
;
771 m_model
->SetValue( value
, m_item
, m_col
);
772 m_model
->ValueChanged( m_item
, m_col
);
776 #endif // wxUSE_DATE_RENDERER_POPUP
778 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
780 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
781 wxDataViewCellMode mode
, int align
) :
782 wxDataViewCustomRenderer( varianttype
, mode
, align
)
786 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
788 m_date
= value
.GetDateTime();
793 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
799 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
801 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
802 wxString tmp
= m_date
.FormatDate();
803 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
808 wxSize
wxDataViewDateRenderer::GetSize() const
810 const wxDataViewCtrl
* view
= GetView();
811 wxString tmp
= m_date
.FormatDate();
813 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
814 return wxSize(x
,y
+d
);
817 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
818 const wxDataViewItem
& item
, unsigned int col
)
821 model
->GetValue( variant
, item
, col
);
822 wxDateTime value
= variant
.GetDateTime();
824 #if wxUSE_DATE_RENDERER_POPUP
825 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
826 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
827 wxPoint pos
= wxGetMousePosition();
830 popup
->Popup( popup
->m_cal
);
831 #else // !wxUSE_DATE_RENDERER_POPUP
832 wxMessageBox(value
.Format());
833 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
837 // ---------------------------------------------------------
839 // ---------------------------------------------------------
841 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
843 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
844 unsigned int model_column
,
845 int width
, wxAlignment align
, int flags
) :
846 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
852 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
855 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
856 unsigned int model_column
,
857 int width
, wxAlignment align
, int flags
) :
858 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
863 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
866 wxDataViewColumn::~wxDataViewColumn()
870 void wxDataViewColumn::Init( int width
)
873 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
877 void wxDataViewColumn::SetResizeable( bool resizeable
)
880 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
882 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
885 void wxDataViewColumn::SetHidden( bool hidden
)
888 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
890 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
892 // tell our owner to e.g. update its scrollbars:
894 GetOwner()->OnColumnChange();
897 void wxDataViewColumn::SetSortable( bool sortable
)
900 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
902 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
904 // Update header button
906 GetOwner()->OnColumnChange();
909 void wxDataViewColumn::SetSortOrder( bool ascending
)
911 m_ascending
= ascending
;
913 // Update header button
915 GetOwner()->OnColumnChange();
918 bool wxDataViewColumn::IsSortOrderAscending() const
923 void wxDataViewColumn::SetInternalWidth( int width
)
927 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
928 if (m_owner
&& m_owner
->m_clientArea
)
929 m_owner
->m_clientArea
->RecalculateDisplay();
932 void wxDataViewColumn::SetWidth( int width
)
934 m_owner
->m_headerArea
->UpdateDisplay();
936 SetInternalWidth(width
);
940 //-----------------------------------------------------------------------------
941 // wxDataViewHeaderWindowBase
942 //-----------------------------------------------------------------------------
944 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
946 wxWindow
*parent
= GetParent();
947 wxDataViewEvent
le(type
, parent
->GetId());
949 le
.SetEventObject(parent
);
951 le
.SetDataViewColumn(GetColumn(n
));
952 le
.SetModel(GetOwner()->GetModel());
954 // for events created by wxDataViewHeaderWindow the
955 // row / value fields are not valid
957 parent
->GetEventHandler()->ProcessEvent(le
);
960 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
962 // implemented in msw/listctrl.cpp:
963 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
965 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
967 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
968 const wxPoint
&pos
, const wxSize
&size
,
969 const wxString
&name
)
973 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
976 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
977 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
978 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
979 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
981 // create the native WC_HEADER window:
982 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
983 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
984 m_hWnd
= CreateWindowEx(0,
995 wxLogLastError(_T("CreateWindowEx"));
999 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1000 // to call wxDataViewHeaderWindow::MSWOnNotify
1001 SubclassWin(m_hWnd
);
1003 // the following is required to get the default win's font for
1004 // header windows and must be done befor sending the HDM_LAYOUT msg
1011 // Retrieve the bounding rectangle of the parent window's
1012 // client area, and then request size and position values
1013 // from the header control.
1014 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1016 hdl
.prc
= &rcParent
;
1018 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1020 wxLogLastError(_T("SendMessage"));
1024 // Set the size, position, and visibility of the header control.
1025 SetWindowPos((HWND
)m_hWnd
,
1029 wp
.flags
| SWP_SHOWWINDOW
);
1031 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1032 // a wxBoxSizer and in order to avoid super-big header windows,
1033 // we need to set our height as fixed
1034 SetMinSize(wxSize(-1, wp
.cy
));
1035 SetMaxSize(wxSize(-1, wp
.cy
));
1040 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1045 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1047 // remove old columns
1048 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1049 Header_DeleteItem((HWND
)m_hWnd
, 0);
1051 // add the updated array of columns to the header control
1052 unsigned int cols
= GetOwner()->GetColumnCount();
1053 unsigned int added
= 0;
1054 for (unsigned int i
= 0; i
< cols
; i
++)
1056 wxDataViewColumn
*col
= GetColumn( i
);
1057 if (col
->IsHidden())
1058 continue; // don't add it!
1061 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1062 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1063 hdi
.cxy
= col
->GetWidth();
1064 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1065 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1067 // lParam is reserved for application's use:
1068 // we store there the column index to use it later in MSWOnNotify
1069 // (since columns may have been hidden)
1070 hdi
.lParam
= (LPARAM
)i
;
1072 // the native wxMSW implementation of the header window
1073 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1074 // on the right: to correct this effect we make the column
1075 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1077 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1079 switch (col
->GetAlignment())
1082 hdi
.fmt
|= HDF_LEFT
;
1084 case wxALIGN_CENTER
:
1085 case wxALIGN_CENTER_HORIZONTAL
:
1086 hdi
.fmt
|= HDF_CENTER
;
1089 hdi
.fmt
|= HDF_RIGHT
;
1093 // such alignment is not allowed for the column header!
1097 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1098 (WPARAM
)added
, (LPARAM
)&hdi
);
1103 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1107 // NOTE: we don't just return nmHDR->iItem because when there are
1108 // hidden columns, nmHDR->iItem may be different from
1109 // nmHDR->pitem->lParam
1111 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1113 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1118 item
.mask
= HDI_LPARAM
;
1119 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1121 return (unsigned int)item
.lParam
;
1124 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1126 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1128 // is it a message from the header?
1129 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1130 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1132 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1133 switch ( nmhdr
->code
)
1135 case HDN_BEGINTRACK
:
1136 // user has started to resize a column:
1137 // do we need to veto it?
1138 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1146 // user has started to reorder a column
1149 case HDN_ITEMCHANGING
:
1150 if (nmHDR
->pitem
!= NULL
&&
1151 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1153 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1154 if (nmHDR
->pitem
->cxy
< minWidth
)
1156 // do not allow the user to resize this column under
1157 // its minimal width:
1163 case HDN_ITEMCHANGED
: // user is resizing a column
1164 case HDN_ENDTRACK
: // user has finished resizing a column
1165 case HDN_ENDDRAG
: // user has finished reordering a column
1167 // update the width of the modified column:
1168 if (nmHDR
->pitem
!= NULL
&&
1169 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1171 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1172 unsigned int w
= nmHDR
->pitem
->cxy
;
1173 wxDataViewColumn
*col
= GetColumn(idx
);
1175 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1176 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1177 w
-= COLUMN_WIDTH_OFFSET
;
1179 if (w
>= (unsigned)col
->GetMinWidth())
1180 col
->SetInternalWidth(w
);
1186 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1187 wxEventType evt
= nmHDR
->iButton
== 0 ?
1188 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1189 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1190 SendEvent(evt
, idx
);
1196 // NOTE: for some reason (i.e. for a bug in Windows)
1197 // the HDN_ITEMCLICK notification is not sent on
1198 // right clicks, so we need to handle NM_RCLICK
1201 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1202 if (column
!= wxNOT_FOUND
)
1205 item
.mask
= HDI_LPARAM
;
1206 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1208 // 'idx' may be different from 'column' if there are
1209 // hidden columns...
1210 unsigned int idx
= (unsigned int)item
.lParam
;
1211 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1217 case HDN_GETDISPINFOW
:
1218 // see wxListCtrl::MSWOnNotify for more info!
1221 case HDN_ITEMDBLCLICK
:
1223 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1224 int w
= GetOwner()->GetBestColumnWidth(idx
);
1226 // update the native control:
1228 ZeroMemory(&hd
, sizeof(hd
));
1229 hd
.mask
= HDI_WIDTH
;
1231 Header_SetItem(GetHwnd(),
1232 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1235 // update the wxDataViewColumn class:
1236 GetColumn(idx
)->SetInternalWidth(w
);
1241 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1247 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1248 const wxRect
*WXUNUSED(rect
))
1250 wxSize ourSz
= GetClientSize();
1251 wxSize ownerSz
= m_owner
->GetClientSize();
1253 // where should the (logical) origin of this window be placed?
1255 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1257 // put this window on top of our parent and
1258 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1259 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1263 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1264 int WXUNUSED(w
), int WXUNUSED(h
),
1267 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1268 // the wxDataViewCtrl window gets resized: the following dummy call
1269 // to ScrollWindow() is required in order to get this header window
1270 // correctly repainted when it's (horizontally) scrolled:
1275 #else // !defined(__WXMSW__)
1277 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1278 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1279 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1280 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1281 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1284 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1285 const wxPoint
&pos
, const wxSize
&size
,
1286 const wxString
&name
)
1290 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1293 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1294 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1295 SetOwnForegroundColour( attr
.colFg
);
1296 SetOwnBackgroundColour( attr
.colBg
);
1298 SetOwnFont( attr
.font
);
1300 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1301 // a wxBoxSizer and in order to avoid super-big header windows,
1302 // we need to set our height as fixed
1303 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1304 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1309 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1312 GetClientSize( &w
, &h
);
1314 wxAutoBufferedPaintDC
dc( this );
1316 dc
.SetBackground(GetBackgroundColour());
1320 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1323 m_owner
->GetViewStart( &x
, NULL
);
1325 // account for the horz scrollbar offset
1326 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1328 dc
.SetFont( GetFont() );
1330 unsigned int cols
= GetOwner()->GetColumnCount();
1333 for (i
= 0; i
< cols
; i
++)
1335 wxDataViewColumn
*col
= GetColumn( i
);
1336 if (col
->IsHidden())
1337 continue; // skip it!
1339 int cw
= col
->GetWidth();
1342 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1343 if (col
->IsSortable())
1345 if (col
->IsSortOrderAscending())
1346 sortArrow
= wxHDR_SORT_ICON_UP
;
1348 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1351 wxRendererNative::Get().DrawHeaderButton
1355 wxRect(xpos
, 0, cw
, ch
-1),
1356 m_parent
->IsEnabled() ? 0
1357 : (int)wxCONTROL_DISABLED
,
1361 // align as required the column title:
1363 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1364 switch (col
->GetAlignment())
1367 x
+= HEADER_HORIZ_BORDER
;
1369 case wxALIGN_CENTER
:
1370 case wxALIGN_CENTER_HORIZONTAL
:
1371 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1374 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1378 // always center the title vertically:
1379 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1381 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1383 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1384 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1385 dc
.DrawText( col
->GetTitle(), x
, y
);
1386 dc
.DestroyClippingRegion();
1392 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1394 GetParent()->SetFocus();
1398 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1400 // we want to work with logical coords
1402 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1403 int y
= event
.GetY();
1407 // we don't draw the line beyond our window,
1408 // but we allow dragging it there
1410 GetClientSize( &w
, NULL
);
1411 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1414 // erase the line if it was drawn
1418 if (event
.ButtonUp())
1420 m_isDragging
= false;
1426 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1429 GetOwner()->Refresh();
1433 m_currentX
= wxMax(m_minX
+ 7, x
);
1435 // draw in the new location
1436 if (m_currentX
< w
) DrawCurrent();
1440 else // not dragging
1443 m_column
= wxNOT_FOUND
;
1445 bool hit_border
= false;
1447 // end of the current column
1450 // find the column where this event occured
1451 int countCol
= m_owner
->GetColumnCount();
1452 for (int column
= 0; column
< countCol
; column
++)
1454 wxDataViewColumn
*p
= GetColumn(column
);
1457 continue; // skip if not shown
1459 xpos
+= p
->GetWidth();
1461 if ((abs(x
-xpos
) < 3) && (y
< 22))
1469 // inside the column
1476 if (m_column
== wxNOT_FOUND
)
1479 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1480 if (event
.LeftDClick() && resizeable
)
1482 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1485 else if (event
.LeftDown() || event
.RightUp())
1487 if (hit_border
&& event
.LeftDown() && resizeable
)
1489 m_isDragging
= true;
1494 else // click on a column
1496 wxEventType evt
= event
.LeftDown() ?
1497 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1498 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1499 SendEvent(evt
, m_column
);
1502 else if (event
.Moving())
1504 if (hit_border
&& resizeable
)
1505 m_currentCursor
= m_resizeCursor
;
1507 m_currentCursor
= wxSTANDARD_CURSOR
;
1509 SetCursor(*m_currentCursor
);
1514 void wxGenericDataViewHeaderWindow::DrawCurrent()
1516 int x1
= m_currentX
;
1518 ClientToScreen (&x1
, &y1
);
1520 int x2
= m_currentX
-1;
1522 ++x2
; // but why ????
1525 m_owner
->GetClientSize( NULL
, &y2
);
1526 m_owner
->ClientToScreen( &x2
, &y2
);
1529 dc
.SetLogicalFunction(wxINVERT
);
1530 dc
.SetPen(m_penCurrent
);
1531 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1533 dc
.DrawLine(x1
, y1
, x2
, y2
);
1536 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1540 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1541 m_owner
->GetViewStart( &x
, NULL
);
1543 // shift the DC origin to match the position of the main window horizontal
1544 // scrollbar: this allows us to always use logical coords
1545 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1548 #endif // defined(__WXMSW__)
1550 //-----------------------------------------------------------------------------
1551 // wxDataViewRenameTimer
1552 //-----------------------------------------------------------------------------
1554 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1559 void wxDataViewRenameTimer::Notify()
1561 m_owner
->OnRenameTimer();
1564 //-----------------------------------------------------------------------------
1565 // wxDataViewMainWindow
1566 //-----------------------------------------------------------------------------
1568 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1570 if (row1
> row2
) return 1;
1571 if (row1
== row2
) return 0;
1576 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1578 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1579 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1580 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1581 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1582 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1583 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1586 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1587 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1588 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1589 m_selection( wxDataViewSelectionCmp
)
1594 m_lastOnSame
= false;
1595 m_renameTimer
= new wxDataViewRenameTimer( this );
1597 // TODO: user better initial values/nothing selected
1598 m_currentCol
= NULL
;
1601 // TODO: we need to calculate this smartly
1608 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1611 m_dragStart
= wxPoint(0,0);
1612 m_lineLastClicked
= (unsigned int) -1;
1613 m_lineBeforeLastClicked
= (unsigned int) -1;
1614 m_lineSelectSingleOnUp
= (unsigned int) -1;
1618 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1619 SetBackgroundColour( *wxWHITE
);
1621 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1623 //Some new added code to deal with the tree structure
1624 m_root
= new wxDataViewTreeNode( NULL
);
1629 wxDataViewMainWindow::~wxDataViewMainWindow()
1632 delete m_renameTimer
;
1635 void wxDataViewMainWindow::OnRenameTimer()
1637 // We have to call this here because changes may just have
1638 // been made and no screen update taken place.
1643 unsigned int cols
= GetOwner()->GetColumnCount();
1645 for (i
= 0; i
< cols
; i
++)
1647 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1649 continue; // skip it!
1651 if (c
== m_currentCol
)
1653 xpos
+= c
->GetWidth();
1655 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1656 m_currentCol
->GetWidth(), m_lineHeight
);
1658 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1659 &labelRect
.x
, &labelRect
.y
);
1661 m_currentCol
->GetRenderer()->StartEditing( m_currentRow
, labelRect
);
1670 virtual bool operator() ( wxDataViewTreeNode
* node
) = 0 ;
1673 class ItemAddJob
: public DoJob
1676 ItemAddJob( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1677 { this->parent
= parent
; this->item
= item
; }
1678 virtual ~ItemAddJob(){};
1680 virtual bool operator() ( wxDataViewTreeNode
* node
)
1682 if( node
->GetItem() == parent
)
1684 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1685 newnode
->SetItem(item
);
1686 node
->AppendChild( newnode
);
1692 wxDataViewItem parent
, item
;
1695 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1697 if( !node
->HasChildren())
1700 wxDataViewTreeNodes nodes
= node
->GetChildren();
1701 int len
= node
->GetChildrenNumber();
1703 for( ; i
< len
; i
++ )
1705 wxDataViewTreeNode
* n
= nodes
[i
];
1708 if( Walker( n
, func
) )
1716 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1718 ItemAddJob
job( parent
, item
);
1719 Walker( m_root
, job
);
1724 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& item
)
1730 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1736 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int WXUNUSED(col
) )
1738 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1739 /*#define MAX_VIRTUAL_WIDTH 100000
1741 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1742 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1743 Refresh( true, &rect );
1751 bool wxDataViewMainWindow::Cleared()
1757 void wxDataViewMainWindow::UpdateDisplay()
1762 void wxDataViewMainWindow::OnInternalIdle()
1764 wxWindow::OnInternalIdle();
1768 RecalculateDisplay();
1773 void wxDataViewMainWindow::RecalculateDisplay()
1775 wxDataViewModel
*model
= GetOwner()->GetModel();
1782 int width
= GetEndOfLastCol();
1783 int height
= GetRowCount() * m_lineHeight
;
1785 SetVirtualSize( width
, height
);
1786 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1791 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1793 wxWindow::ScrollWindow( dx
, dy
, rect
);
1795 if (GetOwner()->m_headerArea
)
1796 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1799 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1801 wxDataViewModel
*model
= GetOwner()->GetModel();
1802 wxAutoBufferedPaintDC
dc( this );
1805 dc
.SetBackground(GetBackgroundColour());
1807 GetOwner()->PrepareDC( dc
);
1808 dc
.SetFont( GetFont() );
1810 wxRect update
= GetUpdateRegion().GetBox();
1811 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1813 // compute which items needs to be redrawn
1814 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1815 unsigned int item_count
=
1816 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1817 (int)(m_count
- item_start
) );
1818 unsigned int item_last
= item_start
+ item_count
;
1820 // compute which columns needs to be redrawn
1821 unsigned int cols
= GetOwner()->GetColumnCount();
1822 unsigned int col_start
= 0;
1823 unsigned int x_start
= 0;
1824 for (x_start
= 0; col_start
< cols
; col_start
++)
1826 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
1827 if (col
->IsHidden())
1828 continue; // skip it!
1830 unsigned int w
= col
->GetWidth();
1831 if (x_start
+w
>= (unsigned int)update
.x
)
1837 unsigned int col_last
= col_start
;
1838 unsigned int x_last
= x_start
;
1839 for (; col_last
< cols
; col_last
++)
1841 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
1842 if (col
->IsHidden())
1843 continue; // skip it!
1845 if (x_last
> (unsigned int)update
.GetRight())
1848 x_last
+= col
->GetWidth();
1851 // Draw horizontal rules if required
1852 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1854 dc
.SetPen(m_penRule
);
1855 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1857 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1859 int y
= i
* m_lineHeight
;
1860 dc
.DrawLine(x_start
, y
, x_last
, y
);
1864 // Draw vertical rules if required
1865 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1867 dc
.SetPen(m_penRule
);
1868 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1871 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1873 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
1874 if (col
->IsHidden())
1875 continue; // skip it
1877 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1878 x
, item_last
* m_lineHeight
);
1880 x
+= col
->GetWidth();
1883 // Draw last vertical rule
1884 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1885 x
, item_last
* m_lineHeight
);
1888 // redraw the background for the items which are selected/current
1889 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1891 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1892 if (selected
|| item
== m_currentRow
)
1894 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1895 if (item
== m_currentRow
)
1896 flags
|= wxCONTROL_CURRENT
;
1898 flags
|= wxCONTROL_FOCUSED
;
1900 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
1901 wxRendererNative::Get().DrawItemSelectionRect
1911 // redraw all cells for all rows which must be repainted and for all columns
1913 cell_rect
.x
= x_start
;
1914 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
1915 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1917 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1918 wxDataViewRenderer
*cell
= col
->GetRenderer();
1919 cell_rect
.width
= col
->GetWidth();
1921 if (col
->IsHidden())
1922 continue; // skipt it!
1924 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1926 // get the cell value and set it into the renderer
1928 wxDataViewItem dataitem
= GetItemByRow(item
);
1929 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1930 cell
->SetValue( value
);
1932 // update the y offset
1933 cell_rect
.y
= item
* m_lineHeight
;
1935 // cannot be bigger than allocated space
1936 wxSize size
= cell
->GetSize();
1937 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
);
1938 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
1940 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1941 int align
= cell
->GetAlignment();
1943 // horizontal alignment:
1944 item_rect
.x
= cell_rect
.x
;
1945 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1946 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1947 else if (align
& wxALIGN_RIGHT
)
1948 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1949 //else: wxALIGN_LEFT is the default
1951 // vertical alignment:
1952 item_rect
.y
= cell_rect
.y
;
1953 if (align
& wxALIGN_CENTER_VERTICAL
)
1954 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1955 else if (align
& wxALIGN_BOTTOM
)
1956 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1957 //else: wxALIGN_TOP is the default
1960 item_rect
.x
+= PADDING_RIGHTLEFT
;
1961 item_rect
.y
+= PADDING_TOPBOTTOM
;
1962 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1963 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
1966 if (m_selection
.Index(item
) != wxNOT_FOUND
)
1967 state
|= wxDATAVIEW_CELL_SELECTED
;
1969 // TODO: it would be much more efficient to create a clipping
1970 // region for the entire column being rendered (in the OnPaint
1971 // of wxDataViewMainWindow) instead of a single clip region for
1972 // each cell. However it would mean that each renderer should
1973 // respect the given wxRect's top & bottom coords, eventually
1974 // violating only the left & right coords - however the user can
1975 // make its own renderer and thus we cannot be sure of that.
1976 dc
.SetClippingRegion( item_rect
);
1977 cell
->Render( item_rect
, &dc
, state
);
1978 dc
.DestroyClippingRegion();
1981 cell_rect
.x
+= cell_rect
.width
;
1985 int wxDataViewMainWindow::GetCountPerPage() const
1987 wxSize size
= GetClientSize();
1988 return size
.y
/ m_lineHeight
;
1991 int wxDataViewMainWindow::GetEndOfLastCol() const
1995 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
1997 const wxDataViewColumn
*c
=
1998 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2001 width
+= c
->GetWidth();
2006 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2010 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2012 return y
/ m_lineHeight
;
2015 unsigned int wxDataViewMainWindow::GetLastVisibleRow() const
2017 wxSize client_size
= GetClientSize();
2018 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2019 &client_size
.x
, &client_size
.y
);
2021 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2024 unsigned int wxDataViewMainWindow::GetRowCount() const
2027 //return wx_const_cast(wxDataViewCtrl*, GetOwner())->GetModel()->GetRowCount();
2030 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2037 void wxDataViewMainWindow::SelectAllRows( bool on
)
2044 m_selection
.Clear();
2045 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2046 m_selection
.Add( i
);
2051 unsigned int first_visible
= GetFirstVisibleRow();
2052 unsigned int last_visible
= GetLastVisibleRow();
2054 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2056 unsigned int row
= m_selection
[i
];
2057 if ((row
>= first_visible
) && (row
<= last_visible
))
2060 m_selection
.Clear();
2064 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2066 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2070 m_selection
.Add( row
);
2078 m_selection
.Remove( row
);
2084 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2088 unsigned int tmp
= from
;
2094 for (i
= from
; i
<= to
; i
++)
2096 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2099 m_selection
.Add( i
);
2104 m_selection
.Remove( i
);
2107 RefreshRows( from
, to
);
2110 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2112 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2114 int n
= aSelections
[i
];
2116 m_selection
.Add( n
);
2121 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2123 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2124 m_selection
.Add( row
);
2126 m_selection
.Remove( row
);
2130 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2132 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2135 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2137 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2138 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2140 wxSize client_size
= GetClientSize();
2141 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2142 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2143 if (intersect_rect
.width
> 0)
2144 Refresh( true, &intersect_rect
);
2147 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2151 unsigned int tmp
= to
;
2156 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2157 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2159 wxSize client_size
= GetClientSize();
2160 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2161 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2162 if (intersect_rect
.width
> 0)
2163 Refresh( true, &intersect_rect
);
2166 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2168 unsigned int count
= GetRowCount();
2169 if (firstRow
> count
)
2172 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2173 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2175 wxSize client_size
= GetClientSize();
2176 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2177 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2178 if (intersect_rect
.width
> 0)
2179 Refresh( true, &intersect_rect
);
2182 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2184 wxCHECK_RET( newCurrent
< GetRowCount(),
2185 _T("invalid item index in OnArrowChar()") );
2187 // if there is no selection, we cannot move it anywhere
2188 if (!HasCurrentRow())
2191 unsigned int oldCurrent
= m_currentRow
;
2193 // in single selection we just ignore Shift as we can't select several
2195 if ( event
.ShiftDown() && !IsSingleSel() )
2197 RefreshRow( oldCurrent
);
2199 ChangeCurrentRow( newCurrent
);
2201 // select all the items between the old and the new one
2202 if ( oldCurrent
> newCurrent
)
2204 newCurrent
= oldCurrent
;
2205 oldCurrent
= m_currentRow
;
2208 SelectRows( oldCurrent
, newCurrent
, true );
2212 RefreshRow( oldCurrent
);
2214 // all previously selected items are unselected unless ctrl is held
2215 if ( !event
.ControlDown() )
2216 SelectAllRows(false);
2218 ChangeCurrentRow( newCurrent
);
2220 if ( !event
.ControlDown() )
2221 SelectRow( m_currentRow
, true );
2223 RefreshRow( m_currentRow
);
2226 //EnsureVisible( m_currentRow );
2229 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2233 rect
.y
= m_lineHeight
* row
;
2234 rect
.width
= GetEndOfLastCol();
2235 rect
.height
= m_lineHeight
;
2241 static int tree_walk_current ;
2242 wxDataViewTreeNode * TreeWalk( unsigned int row , wxDataViewTreeNode * node )
2244 wxDataViewTreeNode * ret ;
2245 if( tree_walk_current == row )
2248 if( node->HasChildren() && node->IsOpen())
2250 wxDataViewTreeNodes nodes = node->GetChildren();
2251 int len = nodes.GetCount();
2253 for( ; i < len; i ++)
2255 tree_walk_current ++;
2256 ret = TreeWalk( row, nodes[i] );
2265 class RowToItemJob
: public DoJob
2268 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2269 virtual ~RowToItemJob(){};
2271 virtual bool operator() ( wxDataViewTreeNode
* node
)
2275 ret
= node
->GetItem() ;
2282 wxDataViewItem
GetResult(){ return ret
; }
2289 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2291 RowToItemJob
job( row
, 0 );
2292 Walker( m_root
, job
);
2293 return job
.GetResult();
2296 class ItemToRowJob
: public DoJob
2299 ItemToRowJob(const wxDataViewItem
& item
){ this->item
= item
; }
2300 virtual ~ItemToRowJob(){};
2302 virtual bool operator() ( wxDataViewTreeNode
* node
)
2305 if( node
->GetItem() == item
)
2311 int GetResult(){ return ret
; }
2313 wxDataViewItem item
;
2317 unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2319 ItemToRowJob
job( item
);
2320 Walker(m_root
, job
);
2321 return job
.GetResult();
2324 unsigned int BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2327 if( !model
->HasChildren( item
) )
2330 wxDataViewItem i
= model
->GetFirstChild( item
);
2333 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2335 node
->AppendChild(n
);
2336 int num
= BuildTreeHelper( model
, i
, n
) + 1;
2338 i
= model
->GetNextSibling( i
);
2343 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2345 //First we define a invalid item to fetch the top-level elements
2346 wxDataViewItem item
;
2347 m_count
= BuildTreeHelper( model
, item
, m_root
);
2350 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2352 if( node
->HasChildren() )
2354 int len
= node
->GetChildrenNumber();
2356 wxDataViewTreeNodes nodes
= node
->GetChildren();
2357 for( ; i
< len
; i
++ )
2359 DestroyTreeHelper(nodes
[i
]);
2365 void wxDataViewMainWindow::DestroyTree()
2367 DestroyTreeHelper(m_root
);
2371 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2373 if (event
.GetKeyCode() == WXK_TAB
)
2375 wxNavigationKeyEvent nevent
;
2376 nevent
.SetWindowChange( event
.ControlDown() );
2377 nevent
.SetDirection( !event
.ShiftDown() );
2378 nevent
.SetEventObject( GetParent()->GetParent() );
2379 nevent
.SetCurrentFocus( m_parent
);
2380 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
2384 // no item -> nothing to do
2385 if (!HasCurrentRow())
2391 // don't use m_linesPerPage directly as it might not be computed yet
2392 const int pageSize
= GetCountPerPage();
2393 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2395 switch ( event
.GetKeyCode() )
2398 if ( m_currentRow
> 0 )
2399 OnArrowChar( m_currentRow
- 1, event
);
2403 if ( m_currentRow
< GetRowCount() - 1 )
2404 OnArrowChar( m_currentRow
+ 1, event
);
2409 OnArrowChar( GetRowCount() - 1, event
);
2414 OnArrowChar( 0, event
);
2419 int steps
= pageSize
- 1;
2420 int index
= m_currentRow
- steps
;
2424 OnArrowChar( index
, event
);
2430 int steps
= pageSize
- 1;
2431 unsigned int index
= m_currentRow
+ steps
;
2432 unsigned int count
= GetRowCount();
2433 if ( index
>= count
)
2436 OnArrowChar( index
, event
);
2445 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2447 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2449 // let the base handle mouse wheel events.
2454 int x
= event
.GetX();
2455 int y
= event
.GetY();
2456 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2458 wxDataViewColumn
*col
= NULL
;
2461 unsigned int cols
= GetOwner()->GetColumnCount();
2463 for (i
= 0; i
< cols
; i
++)
2465 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2467 continue; // skip it!
2469 if (x
< xpos
+ c
->GetWidth())
2474 xpos
+= c
->GetWidth();
2478 wxDataViewRenderer
*cell
= col
->GetRenderer();
2480 unsigned int current
= y
/ m_lineHeight
;
2482 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
2484 // Unselect all if below the last row ?
2488 wxDataViewModel
*model
= GetOwner()->GetModel();
2490 if (event
.Dragging())
2492 if (m_dragCount
== 0)
2494 // we have to report the raw, physical coords as we want to be
2495 // able to call HitTest(event.m_pointDrag) from the user code to
2496 // get the item being dragged
2497 m_dragStart
= event
.GetPosition();
2502 if (m_dragCount
!= 3)
2505 if (event
.LeftIsDown())
2507 // Notify cell about drag
2516 bool forceClick
= false;
2518 if (event
.ButtonDClick())
2520 m_renameTimer
->Stop();
2521 m_lastOnSame
= false;
2524 if (event
.LeftDClick())
2526 if ( current
== m_lineLastClicked
)
2528 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
2531 model
->GetValue( value
, col
->GetModelColumn(), current
);
2532 cell
->SetValue( value
);
2533 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
2534 col
->GetWidth(), m_lineHeight
);
2535 wxDataViewItem dataitem
= GetItemByRow(current
);
2536 cell
->Activate( cell_rect
, model
, dataitem
, col
->GetModelColumn() );
2542 // The first click was on another item, so don't interpret this as
2543 // a double click, but as a simple click instead
2550 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
2552 // select single line
2553 SelectAllRows( false );
2554 SelectRow( m_lineSelectSingleOnUp
, true );
2559 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
2560 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
2562 m_renameTimer
->Start( 100, true );
2566 m_lastOnSame
= false;
2567 m_lineSelectSingleOnUp
= (unsigned int)-1;
2571 // This is necessary, because after a DnD operation in
2572 // from and to ourself, the up event is swallowed by the
2573 // DnD code. So on next non-up event (which means here and
2574 // now) m_lineSelectSingleOnUp should be reset.
2575 m_lineSelectSingleOnUp
= (unsigned int)-1;
2578 if (event
.RightDown())
2580 m_lineBeforeLastClicked
= m_lineLastClicked
;
2581 m_lineLastClicked
= current
;
2583 // If the item is already selected, do not update the selection.
2584 // Multi-selections should not be cleared if a selected item is clicked.
2585 if (!IsRowSelected(current
))
2587 SelectAllRows(false);
2588 ChangeCurrentRow(current
);
2589 SelectRow(m_currentRow
,true);
2592 // notify cell about right click
2595 // Allow generation of context menu event
2598 else if (event
.MiddleDown())
2600 // notify cell about middle click
2603 if (event
.LeftDown() || forceClick
)
2609 m_lineBeforeLastClicked
= m_lineLastClicked
;
2610 m_lineLastClicked
= current
;
2612 unsigned int oldCurrentRow
= m_currentRow
;
2613 bool oldWasSelected
= IsRowSelected(m_currentRow
);
2615 bool cmdModifierDown
= event
.CmdDown();
2616 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2618 if ( IsSingleSel() || !IsRowSelected(current
) )
2620 SelectAllRows( false );
2622 ChangeCurrentRow(current
);
2624 SelectRow(m_currentRow
,true);
2626 else // multi sel & current is highlighted & no mod keys
2628 m_lineSelectSingleOnUp
= current
;
2629 ChangeCurrentRow(current
); // change focus
2632 else // multi sel & either ctrl or shift is down
2634 if (cmdModifierDown
)
2636 ChangeCurrentRow(current
);
2638 ReverseRowSelection(m_currentRow
);
2640 else if (event
.ShiftDown())
2642 ChangeCurrentRow(current
);
2644 unsigned int lineFrom
= oldCurrentRow
,
2647 if ( lineTo
< lineFrom
)
2650 lineFrom
= m_currentRow
;
2653 SelectRows(lineFrom
, lineTo
, true);
2655 else // !ctrl, !shift
2657 // test in the enclosing if should make it impossible
2658 wxFAIL_MSG( _T("how did we get here?") );
2662 if (m_currentRow
!= oldCurrentRow
)
2663 RefreshRow( oldCurrentRow
);
2665 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
2667 // Update selection here...
2670 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
2671 (current
== oldCurrentRow
)) && oldWasSelected
;
2675 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
2679 if (HasCurrentRow())
2685 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
2689 if (HasCurrentRow())
2695 //-----------------------------------------------------------------------------
2697 //-----------------------------------------------------------------------------
2699 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2701 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
2702 EVT_SIZE(wxDataViewCtrl::OnSize
)
2705 wxDataViewCtrl::~wxDataViewCtrl()
2708 GetModel()->RemoveNotifier( m_notifier
);
2711 void wxDataViewCtrl::Init()
2716 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2717 const wxPoint
& pos
, const wxSize
& size
,
2718 long style
, const wxValidator
& validator
)
2720 if (!wxControl::Create( parent
, id
, pos
, size
,
2721 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
2727 MacSetClipChildren( true ) ;
2730 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
2732 if (HasFlag(wxDV_NO_HEADER
))
2733 m_headerArea
= NULL
;
2735 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
2737 SetTargetWindow( m_clientArea
);
2739 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
2741 sizer
->Add( m_headerArea
, 0, wxGROW
);
2742 sizer
->Add( m_clientArea
, 1, wxGROW
);
2749 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
2753 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
2756 // we need to process arrows ourselves for scrolling
2757 if ( nMsg
== WM_GETDLGCODE
)
2759 rc
|= DLGC_WANTARROWS
;
2767 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
2769 // We need to override OnSize so that our scrolled
2770 // window a) does call Layout() to use sizers for
2771 // positioning the controls but b) does not query
2772 // the sizer for their size and use that for setting
2773 // the scrollable area as set that ourselves by
2774 // calling SetScrollbar() further down.
2781 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2783 if (!wxDataViewCtrlBase::AssociateModel( model
))
2786 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
2788 model
->AddNotifier( m_notifier
);
2790 m_clientArea
->BuildTree(model
);
2792 m_clientArea
->UpdateDisplay();
2797 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
2799 if (!wxDataViewCtrlBase::AppendColumn(col
))
2806 void wxDataViewCtrl::OnColumnChange()
2809 m_headerArea
->UpdateDisplay();
2811 m_clientArea
->UpdateDisplay();
2813 /********************************************************************
2814 void wxDataViewCtrl::SetSelection( int row )
2816 m_clientArea->SelectRow(row, true);
2819 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
2821 m_clientArea->SelectRows(from, to, true);
2824 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
2826 m_clientArea->Select(aSelections);
2829 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
2834 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
2841 int wxDataViewCtrl::GetSelection() const
2848 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
2854 *********************************************************************/
2856 // !wxUSE_GENERICDATAVIEWCTRL
2859 // wxUSE_DATAVIEWCTRL