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 //-----------------------------------------------------------------------------
64 // wxDataViewHeaderWindow
65 //-----------------------------------------------------------------------------
67 #define USE_NATIVE_HEADER_WINDOW 1
69 // NB: for some reason, this class must be dllexport'ed or we get warnings from
71 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
74 wxDataViewHeaderWindowBase()
77 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
78 const wxPoint
&pos
, const wxSize
&size
,
81 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
84 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
85 wxDataViewCtrl
*GetOwner() { return m_owner
; }
87 // called on column addition/removal
88 virtual void UpdateDisplay() { /* by default, do nothing */ }
90 // returns the n-th column
91 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
94 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
101 wxDataViewCtrl
*m_owner
;
103 // sends an event generated from the n-th wxDataViewColumn
104 void SendEvent(wxEventType type
, unsigned int n
);
107 // on wxMSW the header window (only that part however) can be made native!
108 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
110 #define COLUMN_WIDTH_OFFSET 2
111 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
113 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
117 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
119 const wxPoint
&pos
= wxDefaultPosition
,
120 const wxSize
&size
= wxDefaultSize
,
121 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
123 Create(parent
, id
, pos
, size
, name
);
126 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
127 const wxPoint
&pos
, const wxSize
&size
,
128 const wxString
&name
);
130 ~wxDataViewHeaderWindowMSW();
132 // called when any column setting is changed and/or changed
134 virtual void UpdateDisplay();
136 // called when the main window gets scrolled
137 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
140 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
141 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
143 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
145 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
146 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
149 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
152 #else // !defined(__WXMSW__)
154 #define HEADER_WINDOW_HEIGHT 25
155 #define HEADER_HORIZ_BORDER 5
156 #define HEADER_VERT_BORDER 3
157 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
159 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
162 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
164 const wxPoint
&pos
= wxDefaultPosition
,
165 const wxSize
&size
= wxDefaultSize
,
166 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
169 Create(parent
, id
, pos
, size
, name
);
172 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
,
174 const wxString
&name
);
176 ~wxGenericDataViewHeaderWindow()
178 delete m_resizeCursor
;
181 virtual void UpdateDisplay() { Refresh(); }
185 void OnPaint( wxPaintEvent
&event
);
186 void OnMouse( wxMouseEvent
&event
);
187 void OnSetFocus( wxFocusEvent
&event
);
192 // vars used for column resizing:
194 wxCursor
*m_resizeCursor
;
195 const wxCursor
*m_currentCursor
;
198 bool m_dirty
; // needs refresh?
199 int m_column
; // index of the column being resized
200 int m_currentX
; // divider line position in logical (unscrolled) coords
201 int m_minX
; // minimal position beyond which the divider line
202 // can't be dragged in logical coords
204 // the pen used to draw the current column width drag line
205 // when resizing the columsn
209 // internal utilities:
213 m_currentCursor
= (wxCursor
*) NULL
;
214 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
216 m_isDragging
= false;
219 m_column
= wxNOT_FOUND
;
223 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
224 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
228 void AdjustDC(wxDC
& dc
);
231 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
232 DECLARE_EVENT_TABLE()
235 #endif // defined(__WXMSW__)
237 //-----------------------------------------------------------------------------
238 // wxDataViewRenameTimer
239 //-----------------------------------------------------------------------------
241 class wxDataViewRenameTimer
: public wxTimer
244 wxDataViewMainWindow
*m_owner
;
247 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
251 //-----------------------------------------------------------------------------
252 // wxDataViewTreeNode
253 //-----------------------------------------------------------------------------
254 class wxDataViewTreeNode
;
255 WX_DEFINE_ARRAY_PTR( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
257 class wxDataViewTreeNode
260 wxDataViewTreeNode( wxDataViewTreeNode
* parent
)
261 { this->parent
= parent
;
267 //I don't know what I need to do in the destructure
268 ~wxDataViewTreeNode()
271 wxDataViewTreeNode
* GetParent() { return parent
; }
272 void SetParent( wxDataViewTreeNode
* parent
) { this->parent
= parent
; }
273 wxDataViewTreeNodes
GetChildren() { return children
; }
274 void SetChildren( wxDataViewTreeNodes children
) { this->children
= children
; }
276 wxDataViewTreeNode
* GetChild( unsigned int n
) { return children
.Item( n
); }
277 void InsertChild( wxDataViewTreeNode
* child
, unsigned int n
) { children
.Insert( child
, n
); }
278 void AppendChild( wxDataViewTreeNode
* child
) { children
.Add( child
); }
280 wxDataViewItem
& GetItem() { return item
; }
281 void SetItem( wxDataViewItem
& item
) { this->item
= item
; }
283 unsigned int GetChildrenNumber() { return children
.GetCount(); }
287 wxDataViewTreeNode
* node
= this;
288 while( node
->GetParent()->GetParent() != NULL
)
290 node
= node
->GetParent();
296 bool IsOpen() { return open
; }
297 void ToggleOpen(){ open
= !open
; }
298 bool HasChildren() { return children
.GetCount() != 0; }
300 wxDataViewTreeNode
* parent
;
301 wxDataViewTreeNodes children
;
306 //-----------------------------------------------------------------------------
307 // wxDataViewMainWindow
308 //-----------------------------------------------------------------------------
310 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
313 class wxDataViewMainWindow
: public wxWindow
316 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
318 const wxPoint
&pos
= wxDefaultPosition
,
319 const wxSize
&size
= wxDefaultSize
,
320 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
321 virtual ~wxDataViewMainWindow();
323 // notifications from wxDataViewModel
324 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
325 bool ItemDeleted( const wxDataViewItem
&item
);
326 bool ItemChanged( const wxDataViewItem
&item
);
327 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
330 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
331 wxDataViewCtrl
*GetOwner() { return m_owner
; }
332 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
334 void OnPaint( wxPaintEvent
&event
);
335 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
336 void OnChar( wxKeyEvent
&event
);
337 void OnMouse( wxMouseEvent
&event
);
338 void OnSetFocus( wxFocusEvent
&event
);
339 void OnKillFocus( wxFocusEvent
&event
);
341 void UpdateDisplay();
342 void RecalculateDisplay();
343 void OnInternalIdle();
345 void OnRenameTimer();
347 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
349 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
350 void ChangeCurrentRow( unsigned int row
);
352 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
353 bool IsEmpty() { return GetRowCount() == 0; }
355 int GetCountPerPage() const;
356 int GetEndOfLastCol() const;
357 unsigned int GetFirstVisibleRow() const;
358 //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
359 unsigned int GetLastVisibleRow();
360 unsigned int GetRowCount() ;
362 void Select( const wxArrayInt
& aSelections
);
363 void SelectAllRows( bool on
);
364 void SelectRow( unsigned int row
, bool on
);
365 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
366 void ReverseRowSelection( unsigned int row
);
367 bool IsRowSelected( unsigned int row
);
369 void RefreshRow( unsigned int row
);
370 void RefreshRows( unsigned int from
, unsigned int to
);
371 void RefreshRowsAfter( unsigned int firstRow
);
373 // returns the colour to be used for drawing the rules
374 wxColour
GetRuleColour() const
376 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
379 //void EnsureVisible( unsigned int row );
380 wxRect
GetLineRect( unsigned int row
) const;
382 //Some useful functions for row and item mapping
383 wxDataViewItem
GetItemByRow( unsigned int row
);
384 unsigned int GetRowByItem( const wxDataViewItem
& item
);
386 //Methods for building the mapping tree
387 void BuildTree( wxDataViewModel
* model
);
390 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
391 wxDataViewTreeNode
* GetTreeNodeByItem( const wxDataViewItem
& item
){};
393 int RecalculateCount() ;
395 void OnExpanding( unsigned int row
);
396 void OnCollapsing( unsigned int row
);
399 wxDataViewCtrl
*m_owner
;
403 wxDataViewColumn
*m_currentCol
;
404 unsigned int m_currentRow
;
405 wxDataViewSelection m_selection
;
407 wxDataViewRenameTimer
*m_renameTimer
;
415 // for double click logic
416 unsigned int m_lineLastClicked
,
417 m_lineBeforeLastClicked
,
418 m_lineSelectSingleOnUp
;
420 // the pen used to draw horiz/vertical rules
423 // the pen used to draw the expander and the lines
426 //This is the tree structure of the model
427 wxDataViewTreeNode
* m_root
;
430 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
431 DECLARE_EVENT_TABLE()
434 // ---------------------------------------------------------
435 // wxGenericDataViewModelNotifier
436 // ---------------------------------------------------------
438 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
441 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
442 { m_mainWindow
= mainWindow
; }
444 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
445 { return m_mainWindow
->ItemAdded( parent
, item
); }
446 virtual bool ItemDeleted( const wxDataViewItem
& item
)
447 { return m_mainWindow
->ItemDeleted( item
); }
448 virtual bool ItemChanged( const wxDataViewItem
& item
)
449 { return m_mainWindow
->ItemChanged(item
); }
450 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
451 { return m_mainWindow
->ValueChanged( item
, col
); }
452 virtual bool Cleared()
453 { return m_mainWindow
->Cleared(); }
455 wxDataViewMainWindow
*m_mainWindow
;
458 // ---------------------------------------------------------
459 // wxDataViewRenderer
460 // ---------------------------------------------------------
462 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
464 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
465 wxDataViewCellMode mode
,
467 wxDataViewRendererBase( varianttype
, mode
, align
)
474 wxDataViewRenderer::~wxDataViewRenderer()
480 wxDC
*wxDataViewRenderer::GetDC()
484 if (GetOwner() == NULL
)
486 if (GetOwner()->GetOwner() == NULL
)
488 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
494 // ---------------------------------------------------------
495 // wxDataViewCustomRenderer
496 // ---------------------------------------------------------
498 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
500 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
501 wxDataViewCellMode mode
, int align
) :
502 wxDataViewRenderer( varianttype
, mode
, align
)
506 // ---------------------------------------------------------
507 // wxDataViewTextRenderer
508 // ---------------------------------------------------------
510 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
512 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
513 wxDataViewCellMode mode
, int align
) :
514 wxDataViewCustomRenderer( varianttype
, mode
, align
)
518 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
520 m_text
= value
.GetString();
525 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
530 bool wxDataViewTextRenderer::HasEditorCtrl()
535 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
536 wxRect labelRect
, const wxVariant
&value
)
538 return new wxTextCtrl( parent
, wxID_ANY
, value
,
539 wxPoint(labelRect
.x
,labelRect
.y
),
540 wxSize(labelRect
.width
,labelRect
.height
) );
543 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
545 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
546 value
= text
->GetValue();
550 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
552 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
553 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
554 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
555 view
->GetForegroundColour();
557 dc
->SetTextForeground(col
);
558 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
563 wxSize
wxDataViewTextRenderer::GetSize() const
565 const wxDataViewCtrl
*view
= GetView();
569 view
->GetTextExtent( m_text
, &x
, &y
);
570 return wxSize( x
, y
);
572 return wxSize(80,20);
575 // ---------------------------------------------------------
576 // wxDataViewBitmapRenderer
577 // ---------------------------------------------------------
579 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
581 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
582 wxDataViewCellMode mode
, int align
) :
583 wxDataViewCustomRenderer( varianttype
, mode
, align
)
587 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
589 if (value
.GetType() == wxT("wxBitmap"))
591 if (value
.GetType() == wxT("wxIcon"))
597 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
602 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
605 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
606 else if (m_icon
.Ok())
607 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
612 wxSize
wxDataViewBitmapRenderer::GetSize() const
615 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
616 else if (m_icon
.Ok())
617 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
619 return wxSize(16,16);
622 // ---------------------------------------------------------
623 // wxDataViewToggleRenderer
624 // ---------------------------------------------------------
626 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
628 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
629 wxDataViewCellMode mode
, int align
) :
630 wxDataViewCustomRenderer( varianttype
, mode
, align
)
635 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
637 m_toggle
= value
.GetBool();
642 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
647 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
649 // User wxRenderer here
652 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
654 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
659 flags
|= wxCONTROL_CHECKED
;
660 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
661 flags
|= wxCONTROL_DISABLED
;
663 wxRendererNative::Get().DrawCheckBox(
664 GetOwner()->GetOwner(),
672 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
673 wxDataViewModel
*model
,
674 const wxDataViewItem
& item
, unsigned int col
)
676 bool value
= !m_toggle
;
677 wxVariant variant
= value
;
678 model
->SetValue( variant
, item
, col
);
679 model
->ValueChanged( item
, col
);
683 wxSize
wxDataViewToggleRenderer::GetSize() const
685 return wxSize(20,20);
688 // ---------------------------------------------------------
689 // wxDataViewProgressRenderer
690 // ---------------------------------------------------------
692 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
694 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
695 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
696 wxDataViewCustomRenderer( varianttype
, mode
, align
)
702 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
706 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
708 m_value
= (long) value
;
710 if (m_value
< 0) m_value
= 0;
711 if (m_value
> 100) m_value
= 100;
716 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
718 value
= (long) m_value
;
722 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
724 double pct
= (double)m_value
/ 100.0;
726 bar
.width
= (int)(cell
.width
* pct
);
727 dc
->SetPen( *wxTRANSPARENT_PEN
);
728 dc
->SetBrush( *wxBLUE_BRUSH
);
729 dc
->DrawRectangle( bar
);
731 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
732 dc
->SetPen( *wxBLACK_PEN
);
733 dc
->DrawRectangle( cell
);
738 wxSize
wxDataViewProgressRenderer::GetSize() const
740 return wxSize(40,12);
743 // ---------------------------------------------------------
744 // wxDataViewDateRenderer
745 // ---------------------------------------------------------
747 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
749 #if wxUSE_DATE_RENDERER_POPUP
751 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
754 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
755 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
756 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
761 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
762 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
763 sizer
->Add( m_cal
, 1, wxGROW
);
768 void OnCalendar( wxCalendarEvent
&event
);
770 wxCalendarCtrl
*m_cal
;
771 wxDataViewModel
*m_model
;
773 const wxDataViewItem
& m_item
;
776 virtual void OnDismiss()
781 DECLARE_EVENT_TABLE()
784 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
785 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
788 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
790 wxDateTime date
= event
.GetDate();
791 wxVariant value
= date
;
792 m_model
->SetValue( value
, m_item
, m_col
);
793 m_model
->ValueChanged( m_item
, m_col
);
797 #endif // wxUSE_DATE_RENDERER_POPUP
799 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
801 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
802 wxDataViewCellMode mode
, int align
) :
803 wxDataViewCustomRenderer( varianttype
, mode
, align
)
807 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
809 m_date
= value
.GetDateTime();
814 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
820 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
822 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
823 wxString tmp
= m_date
.FormatDate();
824 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
829 wxSize
wxDataViewDateRenderer::GetSize() const
831 const wxDataViewCtrl
* view
= GetView();
832 wxString tmp
= m_date
.FormatDate();
834 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
835 return wxSize(x
,y
+d
);
838 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
839 const wxDataViewItem
& item
, unsigned int col
)
842 model
->GetValue( variant
, item
, col
);
843 wxDateTime value
= variant
.GetDateTime();
845 #if wxUSE_DATE_RENDERER_POPUP
846 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
847 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
848 wxPoint pos
= wxGetMousePosition();
851 popup
->Popup( popup
->m_cal
);
852 #else // !wxUSE_DATE_RENDERER_POPUP
853 wxMessageBox(value
.Format());
854 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
858 // ---------------------------------------------------------
860 // ---------------------------------------------------------
862 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
864 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
865 unsigned int model_column
,
866 int width
, wxAlignment align
, int flags
) :
867 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
873 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
876 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
877 unsigned int model_column
,
878 int width
, wxAlignment align
, int flags
) :
879 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
884 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
887 wxDataViewColumn::~wxDataViewColumn()
891 void wxDataViewColumn::Init( int width
)
894 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
898 void wxDataViewColumn::SetResizeable( bool resizeable
)
901 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
903 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
906 void wxDataViewColumn::SetHidden( bool hidden
)
909 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
911 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
913 // tell our owner to e.g. update its scrollbars:
915 GetOwner()->OnColumnChange();
918 void wxDataViewColumn::SetSortable( bool sortable
)
921 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
923 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
925 // Update header button
927 GetOwner()->OnColumnChange();
930 void wxDataViewColumn::SetSortOrder( bool ascending
)
932 m_ascending
= ascending
;
934 // Update header button
936 GetOwner()->OnColumnChange();
939 bool wxDataViewColumn::IsSortOrderAscending() const
944 void wxDataViewColumn::SetInternalWidth( int width
)
948 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
949 if (m_owner
&& m_owner
->m_clientArea
)
950 m_owner
->m_clientArea
->RecalculateDisplay();
953 void wxDataViewColumn::SetWidth( int width
)
955 m_owner
->m_headerArea
->UpdateDisplay();
957 SetInternalWidth(width
);
961 //-----------------------------------------------------------------------------
962 // wxDataViewHeaderWindowBase
963 //-----------------------------------------------------------------------------
965 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
967 wxWindow
*parent
= GetParent();
968 wxDataViewEvent
le(type
, parent
->GetId());
970 le
.SetEventObject(parent
);
972 le
.SetDataViewColumn(GetColumn(n
));
973 le
.SetModel(GetOwner()->GetModel());
975 // for events created by wxDataViewHeaderWindow the
976 // row / value fields are not valid
978 parent
->GetEventHandler()->ProcessEvent(le
);
981 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
983 // implemented in msw/listctrl.cpp:
984 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
986 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
988 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
989 const wxPoint
&pos
, const wxSize
&size
,
990 const wxString
&name
)
994 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
997 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
998 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
999 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1000 h
= size
.y
== wxDefaultCoord
? 22 : size
.y
;
1002 // create the native WC_HEADER window:
1003 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1004 WXDWORD msStyle
= WS_CHILD
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1005 m_hWnd
= CreateWindowEx(0,
1016 wxLogLastError(_T("CreateWindowEx"));
1020 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1021 // to call wxDataViewHeaderWindow::MSWOnNotify
1022 SubclassWin(m_hWnd
);
1024 // the following is required to get the default win's font for
1025 // header windows and must be done befor sending the HDM_LAYOUT msg
1032 // Retrieve the bounding rectangle of the parent window's
1033 // client area, and then request size and position values
1034 // from the header control.
1035 ::GetClientRect((HWND
)hwndParent
, &rcParent
);
1037 hdl
.prc
= &rcParent
;
1039 if (!SendMessage((HWND
)m_hWnd
, HDM_LAYOUT
, 0, (LPARAM
) &hdl
))
1041 wxLogLastError(_T("SendMessage"));
1045 // Set the size, position, and visibility of the header control.
1046 SetWindowPos((HWND
)m_hWnd
,
1050 wp
.flags
| SWP_SHOWWINDOW
);
1052 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1053 // a wxBoxSizer and in order to avoid super-big header windows,
1054 // we need to set our height as fixed
1055 SetMinSize(wxSize(-1, wp
.cy
));
1056 SetMaxSize(wxSize(-1, wp
.cy
));
1061 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1066 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1068 // remove old columns
1069 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1070 Header_DeleteItem((HWND
)m_hWnd
, 0);
1072 // add the updated array of columns to the header control
1073 unsigned int cols
= GetOwner()->GetColumnCount();
1074 unsigned int added
= 0;
1075 for (unsigned int i
= 0; i
< cols
; i
++)
1077 wxDataViewColumn
*col
= GetColumn( i
);
1078 if (col
->IsHidden())
1079 continue; // don't add it!
1082 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1083 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1084 hdi
.cxy
= col
->GetWidth();
1085 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1086 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1088 // lParam is reserved for application's use:
1089 // we store there the column index to use it later in MSWOnNotify
1090 // (since columns may have been hidden)
1091 hdi
.lParam
= (LPARAM
)i
;
1093 // the native wxMSW implementation of the header window
1094 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1095 // on the right: to correct this effect we make the column
1096 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1098 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1100 switch (col
->GetAlignment())
1103 hdi
.fmt
|= HDF_LEFT
;
1105 case wxALIGN_CENTER
:
1106 case wxALIGN_CENTER_HORIZONTAL
:
1107 hdi
.fmt
|= HDF_CENTER
;
1110 hdi
.fmt
|= HDF_RIGHT
;
1114 // such alignment is not allowed for the column header!
1118 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1119 (WPARAM
)added
, (LPARAM
)&hdi
);
1124 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1128 // NOTE: we don't just return nmHDR->iItem because when there are
1129 // hidden columns, nmHDR->iItem may be different from
1130 // nmHDR->pitem->lParam
1132 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1134 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1139 item
.mask
= HDI_LPARAM
;
1140 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1142 return (unsigned int)item
.lParam
;
1145 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1147 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1149 // is it a message from the header?
1150 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1151 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1153 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1154 switch ( nmhdr
->code
)
1156 case HDN_BEGINTRACK
:
1157 // user has started to resize a column:
1158 // do we need to veto it?
1159 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1167 // user has started to reorder a column
1170 case HDN_ITEMCHANGING
:
1171 if (nmHDR
->pitem
!= NULL
&&
1172 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1174 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1175 if (nmHDR
->pitem
->cxy
< minWidth
)
1177 // do not allow the user to resize this column under
1178 // its minimal width:
1184 case HDN_ITEMCHANGED
: // user is resizing a column
1185 case HDN_ENDTRACK
: // user has finished resizing a column
1186 case HDN_ENDDRAG
: // user has finished reordering a column
1188 // update the width of the modified column:
1189 if (nmHDR
->pitem
!= NULL
&&
1190 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1192 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1193 unsigned int w
= nmHDR
->pitem
->cxy
;
1194 wxDataViewColumn
*col
= GetColumn(idx
);
1196 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1197 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1198 w
-= COLUMN_WIDTH_OFFSET
;
1200 if (w
>= (unsigned)col
->GetMinWidth())
1201 col
->SetInternalWidth(w
);
1207 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1208 wxEventType evt
= nmHDR
->iButton
== 0 ?
1209 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1210 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1211 SendEvent(evt
, idx
);
1217 // NOTE: for some reason (i.e. for a bug in Windows)
1218 // the HDN_ITEMCLICK notification is not sent on
1219 // right clicks, so we need to handle NM_RCLICK
1222 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1223 if (column
!= wxNOT_FOUND
)
1226 item
.mask
= HDI_LPARAM
;
1227 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1229 // 'idx' may be different from 'column' if there are
1230 // hidden columns...
1231 unsigned int idx
= (unsigned int)item
.lParam
;
1232 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1238 case HDN_GETDISPINFOW
:
1239 // see wxListCtrl::MSWOnNotify for more info!
1242 case HDN_ITEMDBLCLICK
:
1244 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1245 int w
= GetOwner()->GetBestColumnWidth(idx
);
1247 // update the native control:
1249 ZeroMemory(&hd
, sizeof(hd
));
1250 hd
.mask
= HDI_WIDTH
;
1252 Header_SetItem(GetHwnd(),
1253 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1256 // update the wxDataViewColumn class:
1257 GetColumn(idx
)->SetInternalWidth(w
);
1262 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1268 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx
), int WXUNUSED(dy
),
1269 const wxRect
*WXUNUSED(rect
))
1271 wxSize ourSz
= GetClientSize();
1272 wxSize ownerSz
= m_owner
->GetClientSize();
1274 // where should the (logical) origin of this window be placed?
1276 m_owner
->CalcUnscrolledPosition(0, 0, &x1
, &y1
);
1278 // put this window on top of our parent and
1279 SetWindowPos((HWND
)m_hWnd
, HWND_TOP
, -x1
, 0,
1280 ownerSz
.GetWidth() + x1
, ourSz
.GetHeight(),
1284 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
1285 int WXUNUSED(w
), int WXUNUSED(h
),
1288 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1289 // the wxDataViewCtrl window gets resized: the following dummy call
1290 // to ScrollWindow() is required in order to get this header window
1291 // correctly repainted when it's (horizontally) scrolled:
1296 #else // !defined(__WXMSW__)
1298 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1299 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1300 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1301 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1302 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1305 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1306 const wxPoint
&pos
, const wxSize
&size
,
1307 const wxString
&name
)
1311 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1314 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1315 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1316 SetOwnForegroundColour( attr
.colFg
);
1317 SetOwnBackgroundColour( attr
.colBg
);
1319 SetOwnFont( attr
.font
);
1321 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1322 // a wxBoxSizer and in order to avoid super-big header windows,
1323 // we need to set our height as fixed
1324 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1325 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1330 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1333 GetClientSize( &w
, &h
);
1335 wxAutoBufferedPaintDC
dc( this );
1337 dc
.SetBackground(GetBackgroundColour());
1341 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1344 m_owner
->GetViewStart( &x
, NULL
);
1346 // account for the horz scrollbar offset
1347 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1349 dc
.SetFont( GetFont() );
1351 unsigned int cols
= GetOwner()->GetColumnCount();
1354 for (i
= 0; i
< cols
; i
++)
1356 wxDataViewColumn
*col
= GetColumn( i
);
1357 if (col
->IsHidden())
1358 continue; // skip it!
1360 int cw
= col
->GetWidth();
1363 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1364 if (col
->IsSortable())
1366 if (col
->IsSortOrderAscending())
1367 sortArrow
= wxHDR_SORT_ICON_UP
;
1369 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1372 wxRendererNative::Get().DrawHeaderButton
1376 wxRect(xpos
, 0, cw
, ch
-1),
1377 m_parent
->IsEnabled() ? 0
1378 : (int)wxCONTROL_DISABLED
,
1382 // align as required the column title:
1384 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1385 switch (col
->GetAlignment())
1388 x
+= HEADER_HORIZ_BORDER
;
1390 case wxALIGN_CENTER
:
1391 case wxALIGN_CENTER_HORIZONTAL
:
1392 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1395 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1399 // always center the title vertically:
1400 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1402 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1404 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1405 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1406 dc
.DrawText( col
->GetTitle(), x
, y
);
1407 dc
.DestroyClippingRegion();
1413 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1415 GetParent()->SetFocus();
1419 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1421 // we want to work with logical coords
1423 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1424 int y
= event
.GetY();
1428 // we don't draw the line beyond our window,
1429 // but we allow dragging it there
1431 GetClientSize( &w
, NULL
);
1432 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1435 // erase the line if it was drawn
1439 if (event
.ButtonUp())
1441 m_isDragging
= false;
1447 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1450 GetOwner()->Refresh();
1454 m_currentX
= wxMax(m_minX
+ 7, x
);
1456 // draw in the new location
1457 if (m_currentX
< w
) DrawCurrent();
1461 else // not dragging
1464 m_column
= wxNOT_FOUND
;
1466 bool hit_border
= false;
1468 // end of the current column
1471 // find the column where this event occured
1472 int countCol
= m_owner
->GetColumnCount();
1473 for (int column
= 0; column
< countCol
; column
++)
1475 wxDataViewColumn
*p
= GetColumn(column
);
1478 continue; // skip if not shown
1480 xpos
+= p
->GetWidth();
1482 if ((abs(x
-xpos
) < 3) && (y
< 22))
1490 // inside the column
1497 if (m_column
== wxNOT_FOUND
)
1500 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1501 if (event
.LeftDClick() && resizeable
)
1503 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1506 else if (event
.LeftDown() || event
.RightUp())
1508 if (hit_border
&& event
.LeftDown() && resizeable
)
1510 m_isDragging
= true;
1515 else // click on a column
1517 wxEventType evt
= event
.LeftDown() ?
1518 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1519 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1520 SendEvent(evt
, m_column
);
1523 else if (event
.Moving())
1525 if (hit_border
&& resizeable
)
1526 m_currentCursor
= m_resizeCursor
;
1528 m_currentCursor
= wxSTANDARD_CURSOR
;
1530 SetCursor(*m_currentCursor
);
1535 void wxGenericDataViewHeaderWindow::DrawCurrent()
1537 int x1
= m_currentX
;
1539 ClientToScreen (&x1
, &y1
);
1541 int x2
= m_currentX
-1;
1543 ++x2
; // but why ????
1546 m_owner
->GetClientSize( NULL
, &y2
);
1547 m_owner
->ClientToScreen( &x2
, &y2
);
1550 dc
.SetLogicalFunction(wxINVERT
);
1551 dc
.SetPen(m_penCurrent
);
1552 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1554 dc
.DrawLine(x1
, y1
, x2
, y2
);
1557 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1561 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1562 m_owner
->GetViewStart( &x
, NULL
);
1564 // shift the DC origin to match the position of the main window horizontal
1565 // scrollbar: this allows us to always use logical coords
1566 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1569 #endif // defined(__WXMSW__)
1571 //-----------------------------------------------------------------------------
1572 // wxDataViewRenameTimer
1573 //-----------------------------------------------------------------------------
1575 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1580 void wxDataViewRenameTimer::Notify()
1582 m_owner
->OnRenameTimer();
1585 //-----------------------------------------------------------------------------
1586 // wxDataViewMainWindow
1587 //-----------------------------------------------------------------------------
1589 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1591 if (row1
> row2
) return 1;
1592 if (row1
== row2
) return 0;
1597 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1599 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1600 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1601 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1602 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1603 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1604 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1607 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1608 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1609 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
1610 m_selection( wxDataViewSelectionCmp
)
1615 m_lastOnSame
= false;
1616 m_renameTimer
= new wxDataViewRenameTimer( this );
1618 // TODO: user better initial values/nothing selected
1619 m_currentCol
= NULL
;
1622 // TODO: we need to calculate this smartly
1629 wxASSERT(m_lineHeight
> 2*PADDING_TOPBOTTOM
);
1632 m_dragStart
= wxPoint(0,0);
1633 m_lineLastClicked
= (unsigned int) -1;
1634 m_lineBeforeLastClicked
= (unsigned int) -1;
1635 m_lineSelectSingleOnUp
= (unsigned int) -1;
1639 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1640 SetBackgroundColour( *wxWHITE
);
1642 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1644 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1645 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1646 //Some new added code to deal with the tree structure
1647 m_root
= new wxDataViewTreeNode( NULL
);
1648 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1653 wxDataViewMainWindow::~wxDataViewMainWindow()
1656 delete m_renameTimer
;
1659 void wxDataViewMainWindow::OnRenameTimer()
1661 // We have to call this here because changes may just have
1662 // been made and no screen update taken place.
1667 unsigned int cols
= GetOwner()->GetColumnCount();
1669 for (i
= 0; i
< cols
; i
++)
1671 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1673 continue; // skip it!
1675 if (c
== m_currentCol
)
1677 xpos
+= c
->GetWidth();
1679 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1680 m_currentCol
->GetWidth(), m_lineHeight
);
1682 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1683 &labelRect
.x
, &labelRect
.y
);
1685 m_currentCol
->GetRenderer()->StartEditing( m_currentRow
, labelRect
);
1688 //------------------------------------------------------------------
1689 // Helper class for do operation on the tree node
1690 //------------------------------------------------------------------
1697 //The return value control how the tree-walker tranverse the tree
1698 // 0: Job done, stop tranverse and return
1699 // 1: Ignore the current node's subtree and continue
1700 // 2: Job not done, continue
1701 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1702 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1705 class ItemAddJob
: public DoJob
1708 ItemAddJob( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1709 { this->parent
= parent
; this->item
= item
; }
1710 virtual ~ItemAddJob(){};
1712 virtual int operator() ( wxDataViewTreeNode
* node
)
1714 if( node
->GetItem() == parent
)
1716 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1717 newnode
->SetItem(item
);
1718 node
->AppendChild( newnode
);
1724 wxDataViewItem parent
, item
;
1727 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1729 if( node
==NULL
|| !node
->HasChildren())
1732 wxDataViewTreeNodes nodes
= node
->GetChildren();
1733 int len
= node
->GetChildrenNumber();
1735 for( ; i
< len
; i
++ )
1737 wxDataViewTreeNode
* n
= nodes
[i
];
1749 if( Walker( n
, func
) )
1757 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1759 ItemAddJob
job( parent
, item
);
1760 Walker( m_root
, job
);
1765 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& item
)
1771 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
1777 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int WXUNUSED(col
) )
1779 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1780 /*#define MAX_VIRTUAL_WIDTH 100000
1782 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1783 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1784 Refresh( true, &rect );
1792 bool wxDataViewMainWindow::Cleared()
1798 void wxDataViewMainWindow::UpdateDisplay()
1803 void wxDataViewMainWindow::OnInternalIdle()
1805 wxWindow::OnInternalIdle();
1809 RecalculateDisplay();
1814 void wxDataViewMainWindow::RecalculateDisplay()
1816 wxDataViewModel
*model
= GetOwner()->GetModel();
1823 int width
= GetEndOfLastCol();
1824 int height
= GetRowCount() * m_lineHeight
;
1826 SetVirtualSize( width
, height
);
1827 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1832 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1834 wxWindow::ScrollWindow( dx
, dy
, rect
);
1836 if (GetOwner()->m_headerArea
)
1837 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1840 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1842 wxDataViewModel
*model
= GetOwner()->GetModel();
1843 wxAutoBufferedPaintDC
dc( this );
1846 dc
.SetBackground(GetBackgroundColour());
1848 GetOwner()->PrepareDC( dc
);
1849 dc
.SetFont( GetFont() );
1851 wxRect update
= GetUpdateRegion().GetBox();
1852 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1854 // compute which items needs to be redrawn
1855 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1856 unsigned int item_count
=
1857 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1858 (int)(GetRowCount( )- item_start
) );
1859 unsigned int item_last
= item_start
+ item_count
;
1861 // compute which columns needs to be redrawn
1862 unsigned int cols
= GetOwner()->GetColumnCount();
1863 unsigned int col_start
= 0;
1864 unsigned int x_start
= 0;
1865 for (x_start
= 0; col_start
< cols
; col_start
++)
1867 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
1868 if (col
->IsHidden())
1869 continue; // skip it!
1871 unsigned int w
= col
->GetWidth();
1872 if (x_start
+w
>= (unsigned int)update
.x
)
1878 unsigned int col_last
= col_start
;
1879 unsigned int x_last
= x_start
;
1880 for (; col_last
< cols
; col_last
++)
1882 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
1883 if (col
->IsHidden())
1884 continue; // skip it!
1886 if (x_last
> (unsigned int)update
.GetRight())
1889 x_last
+= col
->GetWidth();
1892 // Draw horizontal rules if required
1893 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1895 dc
.SetPen(m_penRule
);
1896 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1898 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1900 int y
= i
* m_lineHeight
;
1901 dc
.DrawLine(x_start
, y
, x_last
, y
);
1905 // Draw vertical rules if required
1906 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1908 dc
.SetPen(m_penRule
);
1909 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1912 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1914 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
1915 if (col
->IsHidden())
1916 continue; // skip it
1918 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1919 x
, item_last
* m_lineHeight
);
1921 x
+= col
->GetWidth();
1924 // Draw last vertical rule
1925 dc
.DrawLine(x
, item_start
* m_lineHeight
,
1926 x
, item_last
* m_lineHeight
);
1929 // redraw the background for the items which are selected/current
1930 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1932 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1933 if (selected
|| item
== m_currentRow
)
1935 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1936 if (item
== m_currentRow
)
1937 flags
|= wxCONTROL_CURRENT
;
1939 flags
|= wxCONTROL_FOCUSED
;
1941 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
1942 wxRendererNative::Get().DrawItemSelectionRect
1952 // redraw all cells for all rows which must be repainted and for all columns
1954 cell_rect
.x
= x_start
;
1955 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
1956 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1958 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1959 wxDataViewRenderer
*cell
= col
->GetRenderer();
1960 cell_rect
.width
= col
->GetWidth();
1962 if (col
->IsHidden())
1963 continue; // skipt it!
1965 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1967 // get the cell value and set it into the renderer
1969 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
1973 wxDataViewItem dataitem
= node
->GetItem();
1974 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1975 cell
->SetValue( value
);
1977 // update the y offset
1978 cell_rect
.y
= item
* m_lineHeight
;
1980 //Draw the expander here. Please notice that I use const number for all pixel data. When the final API are determined
1981 //I will change this to the data member of the class wxDataViewCtrl
1982 int indent
= node
->GetIndentLevel();
1983 if( col
->GetModelColumn() == GetOwner()->GetExpanderColumn() )
1985 //Calculate the indent first
1986 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1988 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1989 // change the cell_rect.x to the appropriate pos
1990 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
1991 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1992 dc
.SetPen( m_penExpander
);
1993 dc
.SetBrush( wxNullBrush
);
1994 if( node
->HasChildren() )
1996 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
1997 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
1998 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1999 if( node
->IsOpen() )
2000 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, wxCONTROL_EXPANDED
);
2002 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
);
2006 // I am wandering whether we should draw dot lines between tree nodes
2009 //force the expander column to left-center align
2010 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2014 // cannot be bigger than allocated space
2015 wxSize size
= cell
->GetSize();
2016 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2017 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2018 size
.y
= wxMin( size
.y
+ 2*PADDING_TOPBOTTOM
, cell_rect
.height
);
2020 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2021 int align
= cell
->GetAlignment();
2023 // horizontal alignment:
2024 item_rect
.x
= cell_rect
.x
;
2025 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2026 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2027 else if (align
& wxALIGN_RIGHT
)
2028 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2029 //else: wxALIGN_LEFT is the default
2031 // vertical alignment:
2032 item_rect
.y
= cell_rect
.y
;
2033 if (align
& wxALIGN_CENTER_VERTICAL
)
2034 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2035 else if (align
& wxALIGN_BOTTOM
)
2036 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2037 //else: wxALIGN_TOP is the default
2040 item_rect
.x
+= PADDING_RIGHTLEFT
;
2041 item_rect
.y
+= PADDING_TOPBOTTOM
;
2042 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2043 item_rect
.height
= size
.y
- 2 * PADDING_TOPBOTTOM
;
2045 //Here we add the tree indent
2046 item_rect
.x
+= indent
;
2049 if (m_selection
.Index(item
) != wxNOT_FOUND
)
2050 state
|= wxDATAVIEW_CELL_SELECTED
;
2052 // TODO: it would be much more efficient to create a clipping
2053 // region for the entire column being rendered (in the OnPaint
2054 // of wxDataViewMainWindow) instead of a single clip region for
2055 // each cell. However it would mean that each renderer should
2056 // respect the given wxRect's top & bottom coords, eventually
2057 // violating only the left & right coords - however the user can
2058 // make its own renderer and thus we cannot be sure of that.
2059 dc
.SetClippingRegion( item_rect
);
2060 cell
->Render( item_rect
, &dc
, state
);
2061 dc
.DestroyClippingRegion();
2064 cell_rect
.x
+= cell_rect
.width
;
2068 int wxDataViewMainWindow::GetCountPerPage() const
2070 wxSize size
= GetClientSize();
2071 return size
.y
/ m_lineHeight
;
2074 int wxDataViewMainWindow::GetEndOfLastCol() const
2078 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2080 const wxDataViewColumn
*c
=
2081 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2084 width
+= c
->GetWidth();
2089 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2093 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2095 return y
/ m_lineHeight
;
2098 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2100 wxSize client_size
= GetClientSize();
2101 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2102 &client_size
.x
, &client_size
.y
);
2104 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
2107 unsigned int wxDataViewMainWindow::GetRowCount()
2109 if ( m_count
== -1 )
2111 m_count
= RecalculateCount();
2113 GetVirtualSize( &width
, &height
);
2114 height
= m_count
* m_lineHeight
;
2116 SetVirtualSize( width
, height
);
2121 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2128 void wxDataViewMainWindow::SelectAllRows( bool on
)
2135 m_selection
.Clear();
2136 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2137 m_selection
.Add( i
);
2142 unsigned int first_visible
= GetFirstVisibleRow();
2143 unsigned int last_visible
= GetLastVisibleRow();
2145 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2147 unsigned int row
= m_selection
[i
];
2148 if ((row
>= first_visible
) && (row
<= last_visible
))
2151 m_selection
.Clear();
2155 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2157 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2161 m_selection
.Add( row
);
2169 m_selection
.Remove( row
);
2175 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2179 unsigned int tmp
= from
;
2185 for (i
= from
; i
<= to
; i
++)
2187 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2190 m_selection
.Add( i
);
2195 m_selection
.Remove( i
);
2198 RefreshRows( from
, to
);
2201 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2203 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2205 int n
= aSelections
[i
];
2207 m_selection
.Add( n
);
2212 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2214 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2215 m_selection
.Add( row
);
2217 m_selection
.Remove( row
);
2221 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2223 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2226 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2228 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2229 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2231 wxSize client_size
= GetClientSize();
2232 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2233 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2234 if (intersect_rect
.width
> 0)
2235 Refresh( true, &intersect_rect
);
2238 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2242 unsigned int tmp
= to
;
2247 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2248 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2250 wxSize client_size
= GetClientSize();
2251 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2252 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2253 if (intersect_rect
.width
> 0)
2254 Refresh( true, &intersect_rect
);
2257 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2259 unsigned int count
= GetRowCount();
2260 if (firstRow
> count
)
2263 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2264 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2266 wxSize client_size
= GetClientSize();
2267 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2268 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2269 if (intersect_rect
.width
> 0)
2270 Refresh( true, &intersect_rect
);
2273 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2275 wxCHECK_RET( newCurrent
< GetRowCount(),
2276 _T("invalid item index in OnArrowChar()") );
2278 // if there is no selection, we cannot move it anywhere
2279 if (!HasCurrentRow())
2282 unsigned int oldCurrent
= m_currentRow
;
2284 // in single selection we just ignore Shift as we can't select several
2286 if ( event
.ShiftDown() && !IsSingleSel() )
2288 RefreshRow( oldCurrent
);
2290 ChangeCurrentRow( newCurrent
);
2292 // select all the items between the old and the new one
2293 if ( oldCurrent
> newCurrent
)
2295 newCurrent
= oldCurrent
;
2296 oldCurrent
= m_currentRow
;
2299 SelectRows( oldCurrent
, newCurrent
, true );
2303 RefreshRow( oldCurrent
);
2305 // all previously selected items are unselected unless ctrl is held
2306 if ( !event
.ControlDown() )
2307 SelectAllRows(false);
2309 ChangeCurrentRow( newCurrent
);
2311 if ( !event
.ControlDown() )
2312 SelectRow( m_currentRow
, true );
2314 RefreshRow( m_currentRow
);
2317 //EnsureVisible( m_currentRow );
2320 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2324 rect
.y
= m_lineHeight
* row
;
2325 rect
.width
= GetEndOfLastCol();
2326 rect
.height
= m_lineHeight
;
2331 class RowToItemJob
: public DoJob
2334 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2335 virtual ~RowToItemJob(){};
2337 virtual int operator() ( wxDataViewTreeNode
* node
)
2341 ret
= node
->GetItem() ;
2345 if ( node
->IsOpen())
2351 wxDataViewItem
GetResult(){ return ret
; }
2358 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
)
2360 RowToItemJob
job( row
, 0 );
2361 Walker( m_root
, job
);
2362 return job
.GetResult();
2365 class RowToTreeNodeJob
: public DoJob
2368 RowToTreeNodeJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
; ret
= NULL
; }
2369 virtual ~RowToTreeNodeJob(){};
2371 virtual int operator() ( wxDataViewTreeNode
* node
)
2379 if ( node
->IsOpen())
2385 wxDataViewTreeNode
* GetResult(){ return ret
; }
2389 wxDataViewTreeNode
* ret
;
2393 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2395 RowToTreeNodeJob
job( row
, 0 );
2396 Walker( m_root
, job
);
2397 return job
.GetResult();
2400 class CountJob
: public DoJob
2403 CountJob(){ count
= 0 ; }
2404 virtual ~CountJob(){};
2406 virtual int operator () ( wxDataViewTreeNode
* node
)
2409 if ( node
->IsOpen())
2415 unsigned int GetResult()
2423 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2425 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2428 if( node
->HasChildren())
2429 if( !node
->IsOpen())
2434 //RefreshRows(row,GetLastVisibleRow());
2439 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2441 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2444 if( node
->HasChildren() && node
->IsOpen() )
2449 //RefreshRows(row,GetLastVisibleRow());
2453 node
= node
->GetParent();
2456 int parent
= GetRowByItem( node
->GetItem()) ;
2457 SelectRow( row
, false);
2458 SelectRow(parent
, true );
2459 ChangeCurrentRow( parent
);
2465 int wxDataViewMainWindow::RecalculateCount()
2468 Walker( m_root
, job
);
2469 return job
.GetResult();
2472 class ItemToRowJob
: public DoJob
2475 ItemToRowJob(const wxDataViewItem
& item
){ this->item
= item
; ret
= 0 ; }
2476 virtual ~ItemToRowJob(){};
2478 virtual int operator() ( wxDataViewTreeNode
* node
)
2481 if( node
->GetItem() == item
)
2490 //the row number is begin from zero
2491 int GetResult(){ return ret
-1 ; }
2493 wxDataViewItem item
;
2497 unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
2499 ItemToRowJob
job( item
);
2500 Walker(m_root
, job
);
2501 return job
.GetResult();
2504 unsigned int BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
2507 if( !model
->HasChildren( item
) )
2510 wxDataViewItem i
= model
->GetFirstChild( item
);
2513 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
2515 node
->AppendChild(n
);
2516 int num
= BuildTreeHelper( model
, i
, n
) + 1;
2518 i
= model
->GetNextSibling( i
);
2523 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
2525 //First we define a invalid item to fetch the top-level elements
2526 wxDataViewItem item
;
2527 BuildTreeHelper( model
, item
, m_root
);
2531 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
2533 if( node
->HasChildren() )
2535 int len
= node
->GetChildrenNumber();
2537 wxDataViewTreeNodes nodes
= node
->GetChildren();
2538 for( ; i
< len
; i
++ )
2540 DestroyTreeHelper(nodes
[i
]);
2546 void wxDataViewMainWindow::DestroyTree()
2548 DestroyTreeHelper(m_root
);
2552 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
2554 if (event
.GetKeyCode() == WXK_TAB
)
2556 wxNavigationKeyEvent nevent
;
2557 nevent
.SetWindowChange( event
.ControlDown() );
2558 nevent
.SetDirection( !event
.ShiftDown() );
2559 nevent
.SetEventObject( GetParent()->GetParent() );
2560 nevent
.SetCurrentFocus( m_parent
);
2561 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
2565 // no item -> nothing to do
2566 if (!HasCurrentRow())
2572 // don't use m_linesPerPage directly as it might not be computed yet
2573 const int pageSize
= GetCountPerPage();
2574 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2576 switch ( event
.GetKeyCode() )
2579 if ( m_currentRow
> 0 )
2580 OnArrowChar( m_currentRow
- 1, event
);
2584 if ( m_currentRow
< GetRowCount() - 1 )
2585 OnArrowChar( m_currentRow
+ 1, event
);
2587 //Add the process for tree expanding/collapsing
2589 OnCollapsing(m_currentRow
);
2592 OnExpanding( m_currentRow
);
2596 OnArrowChar( GetRowCount() - 1, event
);
2601 OnArrowChar( 0, event
);
2606 int steps
= pageSize
- 1;
2607 int index
= m_currentRow
- steps
;
2611 OnArrowChar( index
, event
);
2617 int steps
= pageSize
- 1;
2618 unsigned int index
= m_currentRow
+ steps
;
2619 unsigned int count
= GetRowCount();
2620 if ( index
>= count
)
2623 OnArrowChar( index
, event
);
2632 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
2634 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2636 // let the base handle mouse wheel events.
2641 int x
= event
.GetX();
2642 int y
= event
.GetY();
2643 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2645 wxDataViewColumn
*col
= NULL
;
2648 unsigned int cols
= GetOwner()->GetColumnCount();
2650 for (i
= 0; i
< cols
; i
++)
2652 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
2654 continue; // skip it!
2656 if (x
< xpos
+ c
->GetWidth())
2661 xpos
+= c
->GetWidth();
2665 wxDataViewRenderer
*cell
= col
->GetRenderer();
2667 unsigned int current
= y
/ m_lineHeight
;
2669 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
2671 // Unselect all if below the last row ?
2675 wxDataViewModel
*model
= GetOwner()->GetModel();
2677 if (event
.Dragging())
2679 if (m_dragCount
== 0)
2681 // we have to report the raw, physical coords as we want to be
2682 // able to call HitTest(event.m_pointDrag) from the user code to
2683 // get the item being dragged
2684 m_dragStart
= event
.GetPosition();
2689 if (m_dragCount
!= 3)
2692 if (event
.LeftIsDown())
2694 // Notify cell about drag
2703 bool forceClick
= false;
2705 if (event
.ButtonDClick())
2707 m_renameTimer
->Stop();
2708 m_lastOnSame
= false;
2711 if (event
.LeftDClick())
2713 if ( current
== m_lineLastClicked
)
2715 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
2718 model
->GetValue( value
, col
->GetModelColumn(), current
);
2719 cell
->SetValue( value
);
2720 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
2721 col
->GetWidth(), m_lineHeight
);
2722 wxDataViewItem dataitem
= GetItemByRow(current
);
2723 cell
->Activate( cell_rect
, model
, dataitem
, col
->GetModelColumn() );
2729 // The first click was on another item, so don't interpret this as
2730 // a double click, but as a simple click instead
2737 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
2739 // select single line
2740 SelectAllRows( false );
2741 SelectRow( m_lineSelectSingleOnUp
, true );
2744 //Process the event of user clicking the expander
2745 bool expander
= false;
2746 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
2747 if( node
!=NULL
&& node
->HasChildren() )
2749 int indent
= node
->GetIndentLevel();
2750 indent
= GetOwner()->GetIndent()*indent
;
2751 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
2752 if( rect
.Contains( x
, y
) )
2756 m_count
= -1; //make the current row number fail
2759 //int last_row = GetLastVisibleRow();
2760 //RefreshRows( current, last_row );
2764 //If the user click the expander, we do not do editing even if the column with expander are editable
2765 if (m_lastOnSame
&& !expander
)
2767 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
2768 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
2770 m_renameTimer
->Start( 100, true );
2774 m_lastOnSame
= false;
2775 m_lineSelectSingleOnUp
= (unsigned int)-1;
2779 // This is necessary, because after a DnD operation in
2780 // from and to ourself, the up event is swallowed by the
2781 // DnD code. So on next non-up event (which means here and
2782 // now) m_lineSelectSingleOnUp should be reset.
2783 m_lineSelectSingleOnUp
= (unsigned int)-1;
2786 if (event
.RightDown())
2788 m_lineBeforeLastClicked
= m_lineLastClicked
;
2789 m_lineLastClicked
= current
;
2791 // If the item is already selected, do not update the selection.
2792 // Multi-selections should not be cleared if a selected item is clicked.
2793 if (!IsRowSelected(current
))
2795 SelectAllRows(false);
2796 ChangeCurrentRow(current
);
2797 SelectRow(m_currentRow
,true);
2800 // notify cell about right click
2803 // Allow generation of context menu event
2806 else if (event
.MiddleDown())
2808 // notify cell about middle click
2811 if (event
.LeftDown() || forceClick
)
2817 m_lineBeforeLastClicked
= m_lineLastClicked
;
2818 m_lineLastClicked
= current
;
2820 unsigned int oldCurrentRow
= m_currentRow
;
2821 bool oldWasSelected
= IsRowSelected(m_currentRow
);
2823 bool cmdModifierDown
= event
.CmdDown();
2824 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2826 if ( IsSingleSel() || !IsRowSelected(current
) )
2828 SelectAllRows( false );
2830 ChangeCurrentRow(current
);
2832 SelectRow(m_currentRow
,true);
2834 else // multi sel & current is highlighted & no mod keys
2836 m_lineSelectSingleOnUp
= current
;
2837 ChangeCurrentRow(current
); // change focus
2840 else // multi sel & either ctrl or shift is down
2842 if (cmdModifierDown
)
2844 ChangeCurrentRow(current
);
2846 ReverseRowSelection(m_currentRow
);
2848 else if (event
.ShiftDown())
2850 ChangeCurrentRow(current
);
2852 unsigned int lineFrom
= oldCurrentRow
,
2855 if ( lineTo
< lineFrom
)
2858 lineFrom
= m_currentRow
;
2861 SelectRows(lineFrom
, lineTo
, true);
2863 else // !ctrl, !shift
2865 // test in the enclosing if should make it impossible
2866 wxFAIL_MSG( _T("how did we get here?") );
2870 if (m_currentRow
!= oldCurrentRow
)
2871 RefreshRow( oldCurrentRow
);
2873 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
2875 // Update selection here...
2878 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
2879 (current
== oldCurrentRow
)) && oldWasSelected
;
2883 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
2887 if (HasCurrentRow())
2893 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
2897 if (HasCurrentRow())
2903 //-----------------------------------------------------------------------------
2905 //-----------------------------------------------------------------------------
2907 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
2909 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
2910 EVT_SIZE(wxDataViewCtrl::OnSize
)
2913 wxDataViewCtrl::~wxDataViewCtrl()
2916 GetModel()->RemoveNotifier( m_notifier
);
2919 void wxDataViewCtrl::Init()
2924 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
2925 const wxPoint
& pos
, const wxSize
& size
,
2926 long style
, const wxValidator
& validator
)
2928 if (!wxControl::Create( parent
, id
, pos
, size
,
2929 style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
2935 MacSetClipChildren( true ) ;
2938 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
2940 if (HasFlag(wxDV_NO_HEADER
))
2941 m_headerArea
= NULL
;
2943 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
2945 SetTargetWindow( m_clientArea
);
2947 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
2949 sizer
->Add( m_headerArea
, 0, wxGROW
);
2950 sizer
->Add( m_clientArea
, 1, wxGROW
);
2957 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
2961 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
2964 // we need to process arrows ourselves for scrolling
2965 if ( nMsg
== WM_GETDLGCODE
)
2967 rc
|= DLGC_WANTARROWS
;
2975 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
2977 // We need to override OnSize so that our scrolled
2978 // window a) does call Layout() to use sizers for
2979 // positioning the controls but b) does not query
2980 // the sizer for their size and use that for setting
2981 // the scrollable area as set that ourselves by
2982 // calling SetScrollbar() further down.
2989 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
2991 if (!wxDataViewCtrlBase::AssociateModel( model
))
2994 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
2996 model
->AddNotifier( m_notifier
);
2998 m_clientArea
->BuildTree(model
);
3000 m_clientArea
->UpdateDisplay();
3005 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3007 if (!wxDataViewCtrlBase::AppendColumn(col
))
3014 void wxDataViewCtrl::OnColumnChange()
3017 m_headerArea
->UpdateDisplay();
3019 m_clientArea
->UpdateDisplay();
3022 void wxDataViewCtrl::DoSetExpanderColumn()
3024 m_clientArea
->UpdateDisplay();
3027 void wxDataViewCtrl::DoSetIndent()
3029 m_clientArea
->UpdateDisplay();
3033 /********************************************************************
3034 void wxDataViewCtrl::SetSelection( int row )
3036 m_clientArea->SelectRow(row, true);
3039 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
3041 m_clientArea->SelectRows(from, to, true);
3044 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
3046 m_clientArea->Select(aSelections);
3049 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
3054 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
3061 int wxDataViewCtrl::GetSelection() const
3068 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
3074 *********************************************************************/
3076 // !wxUSE_GENERICDATAVIEWCTRL
3079 // wxUSE_DATAVIEWCTRL