X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c741d33febd42cc9933b764dd686fdab2a8d4461..6f3f9b50aa275243ade3541ecccb95de22cf57a9:/src/generic/datavgen.cpp diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index cdd2e939a9..f47731e5e4 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -33,6 +33,7 @@ #include "wx/timer.h" #include "wx/settings.h" #include "wx/msgdlg.h" + #include "wx/dcscreen.h" #endif #include "wx/stockitem.h" @@ -41,6 +42,8 @@ #include "wx/renderer.h" #include "wx/dcbuffer.h" #include "wx/icon.h" +#include "wx/list.h" +#include "wx/listimpl.cpp" //----------------------------------------------------------------------------- // classes @@ -56,6 +59,8 @@ static const int PADDING_RIGHTLEFT = 3; // the cell padding on the top/bottom static const int PADDING_TOPBOTTOM = 1; +// the expander space margin +static const int EXPANDER_MARGIN = 4; //----------------------------------------------------------------------------- // wxDataViewHeaderWindow @@ -175,6 +180,8 @@ public: delete m_resizeCursor; } + virtual void UpdateDisplay() { Refresh(); } + // event handlers: void OnPaint( wxPaintEvent &event ); @@ -244,50 +251,161 @@ public: }; //----------------------------------------------------------------------------- -// wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing +// wxDataViewTreeNode //----------------------------------------------------------------------------- +class wxDataViewTreeNode; +WX_DEFINE_SORTED_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes ); +WX_DEFINE_SORTED_ARRAY( void* , wxDataViewTreeLeaves); + +int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode * node1, wxDataViewTreeNode * node2); +int LINKAGEMODE wxGenericTreeModelItemCmp( void * id1, void * id2); -class wxDataViewTextCtrlWrapper : public wxEvtHandler +class wxDataViewTreeNode { public: - // NB: text must be a valid object but not Create()d yet - wxDataViewTextCtrlWrapper( wxDataViewMainWindow *owner, - wxTextCtrl *text, - wxDataViewListModel *model, - unsigned int col, unsigned int row, - wxRect cellLabel ); + wxDataViewTreeNode( wxDataViewTreeNode * parent ) + :leaves( wxGenericTreeModelItemCmp ), + nodes(wxGenericTreeModelNodeCmp) + { this->parent = parent; + if( parent == NULL ) + open = true; + else + open = false; + hasChildren = false; + subTreeCount = 0; + } + //I don't know what I need to do in the destructure + ~wxDataViewTreeNode() + { - wxTextCtrl *GetText() const { return m_text; } + } - void AcceptChangesAndFinish(); + wxDataViewTreeNode * GetParent() { return parent; } + void SetParent( wxDataViewTreeNode * parent ) { this->parent = parent; } + wxDataViewTreeNodes & GetNodes() { return nodes; } + wxDataViewTreeLeaves & GetChildren() { return leaves; } -protected: - void OnChar( wxKeyEvent &event ); - void OnKeyUp( wxKeyEvent &event ); - void OnKillFocus( wxFocusEvent &event ); + void AddNode( wxDataViewTreeNode * node ) + { + nodes.Add( node ); + leaves.Add( node->GetItem().GetID() ); + } + void AddLeaf( void * leaf ) { leaves.Add( leaf ); } - bool AcceptChanges(); - void Finish(); + wxDataViewItem & GetItem() { return item; } + void SetItem( const wxDataViewItem & item ) { this->item = item; } -private: - wxDataViewMainWindow *m_owner; - wxTextCtrl *m_text; - wxString m_startValue; - wxDataViewListModel *m_model; - unsigned int m_col; - unsigned int m_row; - bool m_finished; - bool m_aboutToFinish; + unsigned int GetChildrenNumber() { return leaves.GetCount(); } + unsigned int GetNodeNumber() { return nodes.GetCount(); } + int GetIndentLevel() + { + int ret = 0 ; + wxDataViewTreeNode * node = this; + while( node->GetParent()->GetParent() != NULL ) + { + node = node->GetParent(); + ret ++; + } + return ret; + } - DECLARE_EVENT_TABLE() + bool IsOpen() + { + return open ; + } + + void ToggleOpen() + { + int len = nodes.GetCount(); + int sum = 0; + for ( int i = 0 ;i < len ; i ++) + sum += nodes[i]->GetSubTreeCount(); + + sum += leaves.GetCount(); + if( open ) + { + ChangeSubTreeCount(-sum); + open = !open; + } + else + { + open = !open; + ChangeSubTreeCount(sum); + } + } + bool HasChildren() { return hasChildren; } + void SetHasChildren( bool has ){ hasChildren = has; } + + void SetSubTreeCount( int num ) { subTreeCount = num; } + int GetSubTreeCount() { return subTreeCount; } + void ChangeSubTreeCount( int num ) + { + if( !open ) + return ; + subTreeCount += num; + if( parent ) + parent->ChangeSubTreeCount(num); + } + + void Resort() + { + wxDataViewTreeNodes nds = nodes; + wxDataViewTreeLeaves lvs = leaves; + nodes.Empty(); + leaves.Empty(); + + int len = nds.GetCount(); + if(len > 0) + { + int i; + for(i = 0; i < len; i ++) + nodes.Add(nds[i]); + for(i = 0; i < len; i ++) + nodes[i]->Resort(); + } + + len = lvs.GetCount(); + if(len > 0) + { + for(int i = 0; i < len; i++) + leaves.Add(lvs[i]); + } + } + +private: + wxDataViewTreeNode * parent; + wxDataViewTreeNodes nodes; + wxDataViewTreeLeaves leaves; + wxDataViewItem item; + bool open; + bool hasChildren; + int subTreeCount; }; +//Below is the compare stuff +//For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed +static wxDataViewModel * g_model; + +int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode * node1, wxDataViewTreeNode * node2) +{ + return g_model->Compare( node1->GetItem(), node2->GetItem() ); +} + +int LINKAGEMODE wxGenericTreeModelItemCmp( void * id1, void * id2) +{ + return g_model->Compare( id1, id2 ); +} + + + //----------------------------------------------------------------------------- // wxDataViewMainWindow //----------------------------------------------------------------------------- WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection, WXDLLIMPEXP_ADV); +WX_DECLARE_LIST(wxDataViewItem, ItemList); +WX_DEFINE_LIST(ItemList); class wxDataViewMainWindow: public wxWindow { @@ -299,15 +417,14 @@ public: const wxString &name = wxT("wxdataviewctrlmainwindow") ); virtual ~wxDataViewMainWindow(); - // notifications from wxDataViewListModel - bool RowAppended(); - bool RowPrepended(); - bool RowInserted( unsigned int before ); - bool RowDeleted( unsigned int row ); - bool RowChanged( unsigned int row ); - bool ValueChanged( unsigned int col, unsigned int row ); - bool RowsReordered( unsigned int *new_order ); + // notifications from wxDataViewModel + bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); + bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); + bool ItemChanged( const wxDataViewItem &item ); + bool ValueChanged( const wxDataViewItem &item, unsigned int col ); bool Cleared(); + void Resort() + { g_model = GetOwner()->GetModel(); m_root->Resort(); UpdateDisplay(); } void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } wxDataViewCtrl *GetOwner() { return m_owner; } @@ -325,21 +442,23 @@ public: void OnInternalIdle(); void OnRenameTimer(); - void FinishEditing( wxTextCtrl *text ); void ScrollWindow( int dx, int dy, const wxRect *rect = NULL ); bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; } void ChangeCurrentRow( unsigned int row ); - bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); }; + bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); } bool IsEmpty() { return GetRowCount() == 0; } int GetCountPerPage() const; int GetEndOfLastCol() const; unsigned int GetFirstVisibleRow() const; - unsigned int GetLastVisibleRow() const; - unsigned int GetRowCount() const; + //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 + unsigned int GetLastVisibleRow(); + unsigned int GetRowCount() ; + + wxDataViewItem GetSelection(); void Select( const wxArrayInt& aSelections ); void SelectAllRows( bool on ); @@ -361,17 +480,35 @@ public: //void EnsureVisible( unsigned int row ); wxRect GetLineRect( unsigned int row ) const; + //Some useful functions for row and item mapping + wxDataViewItem GetItemByRow( unsigned int row ); + unsigned int GetRowByItem( const wxDataViewItem & item ); + + //Methods for building the mapping tree + void BuildTree( wxDataViewModel * model ); + void DestroyTree(); +private: + wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ); + //We did not need this temporarily + //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item ); + + int RecalculateCount() ; + + void OnExpanding( unsigned int row ); + void OnCollapsing( unsigned int row ); + + wxDataViewTreeNode * FindNode( const wxDataViewItem & item ); + private: wxDataViewCtrl *m_owner; int m_lineHeight; bool m_dirty; wxDataViewColumn *m_currentCol; - unsigned int m_currentRow; + unsigned int m_currentRow; wxDataViewSelection m_selection; wxDataViewRenameTimer *m_renameTimer; - wxDataViewTextCtrlWrapper *m_textctrlWrapper; bool m_lastOnSame; bool m_hasFocus; @@ -387,37 +524,39 @@ private: // the pen used to draw horiz/vertical rules wxPen m_penRule; + // the pen used to draw the expander and the lines + wxPen m_penExpander; + + //This is the tree structure of the model + wxDataViewTreeNode * m_root; + int m_count; private: DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) DECLARE_EVENT_TABLE() }; // --------------------------------------------------------- -// wxGenericDataViewListModelNotifier +// wxGenericDataViewModelNotifier // --------------------------------------------------------- -class wxGenericDataViewListModelNotifier: public wxDataViewListModelNotifier +class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier { public: - wxGenericDataViewListModelNotifier( wxDataViewMainWindow *mainWindow ) + wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow ) { m_mainWindow = mainWindow; } - virtual bool RowAppended() - { return m_mainWindow->RowAppended(); } - virtual bool RowPrepended() - { return m_mainWindow->RowPrepended(); } - virtual bool RowInserted( unsigned int before ) - { return m_mainWindow->RowInserted( before ); } - virtual bool RowDeleted( unsigned int row ) - { return m_mainWindow->RowDeleted( row ); } - virtual bool RowChanged( unsigned int row ) - { return m_mainWindow->RowChanged( row ); } - virtual bool ValueChanged( unsigned int col, unsigned int row ) - { return m_mainWindow->ValueChanged( col, row ); } - virtual bool RowsReordered( unsigned int *new_order ) - { return m_mainWindow->RowsReordered( new_order ); } + virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item ) + { return m_mainWindow->ItemAdded( parent , item ); } + virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) + { return m_mainWindow->ItemDeleted( parent, item ); } + virtual bool ItemChanged( const wxDataViewItem & item ) + { return m_mainWindow->ItemChanged(item); } + virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col ) + { return m_mainWindow->ValueChanged( item, col ); } virtual bool Cleared() { return m_mainWindow->Cleared(); } + virtual void Resort() + { m_mainWindow->Resort(); } wxDataViewMainWindow *m_mainWindow; }; @@ -458,7 +597,6 @@ wxDC *wxDataViewRenderer::GetDC() return m_dc; } - // --------------------------------------------------------- // wxDataViewCustomRenderer // --------------------------------------------------------- @@ -495,6 +633,26 @@ bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const return false; } +bool wxDataViewTextRenderer::HasEditorCtrl() +{ + return true; +} + +wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent, + wxRect labelRect, const wxVariant &value ) +{ + return new wxTextCtrl( parent, wxID_ANY, value, + wxPoint(labelRect.x,labelRect.y), + wxSize(labelRect.width,labelRect.height) ); +} + +bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant &value ) +{ + wxTextCtrl *text = (wxTextCtrl*) editor; + value = text->GetValue(); + return true; +} + bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state ) { wxDataViewCtrl *view = GetOwner()->GetOwner(); @@ -618,13 +776,13 @@ bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state } bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell), - wxDataViewListModel *model, - unsigned int col, unsigned int row ) + wxDataViewModel *model, + const wxDataViewItem & item, unsigned int col) { bool value = !m_toggle; wxVariant variant = value; - model->SetValue( variant, col, row ); - model->ValueChanged( col, row ); + model->SetValue( variant, item, col); + model->ValueChanged( item, col ); return true; } @@ -700,12 +858,12 @@ class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow { public: wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, - wxDataViewListModel *model, unsigned int col, unsigned int row ) : - wxPopupTransientWindow( parent, wxBORDER_SIMPLE ) + wxDataViewModel *model, const wxDataViewItem & item, unsigned int col) : + wxPopupTransientWindow( parent, wxBORDER_SIMPLE ), + m_item( item ) { m_model = model; m_col = col; - m_row = row; m_cal = new wxCalendarCtrl( this, wxID_ANY, *value ); wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); sizer->Add( m_cal, 1, wxGROW ); @@ -716,9 +874,9 @@ public: void OnCalendar( wxCalendarEvent &event ); wxCalendarCtrl *m_cal; - wxDataViewListModel *m_model; + wxDataViewModel *m_model; unsigned int m_col; - unsigned int m_row; + const wxDataViewItem & m_item; protected: virtual void OnDismiss() @@ -737,8 +895,8 @@ void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) { wxDateTime date = event.GetDate(); wxVariant value = date; - m_model->SetValue( value, m_col, m_row ); - m_model->ValueChanged( m_col, m_row ); + m_model->SetValue( value, m_item, m_col ); + m_model->ValueChanged( m_item, m_col ); DismissAndNotify(); } @@ -783,16 +941,16 @@ wxSize wxDataViewDateRenderer::GetSize() const return wxSize(x,y+d); } -bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model, - unsigned int col, unsigned int row ) +bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model, + const wxDataViewItem & item, unsigned int col ) { wxVariant variant; - model->GetValue( variant, col, row ); + model->GetValue( variant, item, col ); wxDateTime value = variant.GetDateTime(); #if wxUSE_DATE_RENDERER_POPUP wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( - GetOwner()->GetOwner()->GetParent(), &value, model, col, row ); + GetOwner()->GetOwner()->GetParent(), &value, model, item, col); wxPoint pos = wxGetMousePosition(); popup->Move( pos ); popup->Layout(); @@ -840,6 +998,7 @@ void wxDataViewColumn::Init( int width ) { m_width = width; m_minWidth = wxDVC_DEFAULT_MINWIDTH; + m_ascending = true; } void wxDataViewColumn::SetResizeable( bool resizeable ) @@ -868,17 +1027,24 @@ void wxDataViewColumn::SetSortable( bool sortable ) m_flags |= wxDATAVIEW_COL_SORTABLE; else m_flags &= ~wxDATAVIEW_COL_SORTABLE; + + // Update header button + if (GetOwner()) + GetOwner()->OnColumnChange(); } -void wxDataViewColumn::SetSortOrder( bool WXUNUSED(ascending) ) +void wxDataViewColumn::SetSortOrder( bool ascending ) { - // TODO + m_ascending = ascending; + + // Update header button + if (GetOwner()) + GetOwner()->OnColumnChange(); } bool wxDataViewColumn::IsSortOrderAscending() const { - // TODO - return true; + return m_ascending; } void wxDataViewColumn::SetInternalWidth( int width ) @@ -1012,6 +1178,7 @@ void wxDataViewHeaderWindowMSW::UpdateDisplay() // add the updated array of columns to the header control unsigned int cols = GetOwner()->GetColumnCount(); unsigned int added = 0; + wxDataViewModel * model = GetOwner()->GetModel(); for (unsigned int i = 0; i < cols; i++) { wxDataViewColumn *col = GetColumn( i ); @@ -1020,7 +1187,7 @@ void wxDataViewHeaderWindowMSW::UpdateDisplay() HDITEM hdi; hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH; - hdi.pszText = (wxChar *) col->GetTitle().c_str(); + hdi.pszText = (wxChar *) col->GetTitle().wx_str(); hdi.cxy = col->GetWidth(); hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]); hdi.fmt = HDF_LEFT | HDF_STRING; @@ -1145,6 +1312,30 @@ bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARA case HDN_ITEMCLICK: { unsigned int idx = GetColumnIdxFromHeader(nmHDR); + wxDataViewModel * model = GetOwner()->GetModel(); + + if(nmHDR->iButton == 0) + { + wxDataViewColumn *col = GetColumn(idx); + if(col->IsSortable()) + { + if(model && model->GetSortingColumn() == idx) + { + bool order = col->IsSortOrderAscending(); + col->SetSortOrder(!order); + model->SetSortOrderAscending(!order); + } + else if(model) + { + model->SetSortingColumn(idx); + model->SetSortOrderAscending(true); + } + } + UpdateDisplay(); + if(model) + model->Resort(); + } + wxEventType evt = nmHDR->iButton == 0 ? wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK : wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK; @@ -1300,13 +1491,23 @@ void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) int cw = col->GetWidth(); int ch = h; + wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE; + if (col->IsSortable()) + { + if (col->IsSortOrderAscending()) + sortArrow = wxHDR_SORT_ICON_UP; + else + sortArrow = wxHDR_SORT_ICON_DOWN; + } + wxRendererNative::Get().DrawHeaderButton ( this, dc, wxRect(xpos, 0, cw, ch-1), m_parent->IsEnabled() ? 0 - : (int)wxCONTROL_DISABLED + : (int)wxCONTROL_DISABLED, + sortArrow ); // align as required the column title: @@ -1512,153 +1713,13 @@ void wxDataViewRenameTimer::Notify() m_owner->OnRenameTimer(); } -//----------------------------------------------------------------------------- -// wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing -//----------------------------------------------------------------------------- - -BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper, wxEvtHandler) - EVT_CHAR (wxDataViewTextCtrlWrapper::OnChar) - EVT_KEY_UP (wxDataViewTextCtrlWrapper::OnKeyUp) - EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper::OnKillFocus) -END_EVENT_TABLE() - -wxDataViewTextCtrlWrapper::wxDataViewTextCtrlWrapper( - wxDataViewMainWindow *owner, - wxTextCtrl *text, - wxDataViewListModel *model, - unsigned int col, unsigned int row, - wxRect rectLabel ) -{ - m_owner = owner; - m_model = model; - m_row = row; - m_col = col; - m_text = text; - - m_finished = false; - m_aboutToFinish = false; - - wxVariant value; - model->GetValue( value, col, row ); - m_startValue = value.GetString(); - - m_owner->GetOwner()->CalcScrolledPosition( - rectLabel.x, rectLabel.y, &rectLabel.x, &rectLabel.y ); - - m_text->Create( owner, wxID_ANY, m_startValue, - wxPoint(rectLabel.x-2,rectLabel.y-2), - wxSize(rectLabel.width+7,rectLabel.height+4) ); - m_text->SetFocus(); - - m_text->PushEventHandler(this); -} - -void wxDataViewTextCtrlWrapper::AcceptChangesAndFinish() -{ - m_aboutToFinish = true; - - // Notify the owner about the changes - AcceptChanges(); - - // Even if vetoed, close the control (consistent with MSW) - Finish(); -} - -void wxDataViewTextCtrlWrapper::OnChar( wxKeyEvent &event ) -{ - switch ( event.m_keyCode ) - { - case WXK_RETURN: - AcceptChangesAndFinish(); - break; - - case WXK_ESCAPE: - // m_owner->OnRenameCancelled( m_itemEdited ); - Finish(); - break; - - default: - event.Skip(); - } -} - -void wxDataViewTextCtrlWrapper::OnKeyUp( wxKeyEvent &event ) -{ - if (m_finished) - { - event.Skip(); - return; - } - - // auto-grow the textctrl - wxSize parentSize = m_owner->GetSize(); - wxPoint myPos = m_text->GetPosition(); - wxSize mySize = m_text->GetSize(); - int sx, sy; - m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy); - if (myPos.x + sx > parentSize.x) - sx = parentSize.x - myPos.x; - if (mySize.x > sx) - sx = mySize.x; - m_text->SetSize(sx, wxDefaultCoord); - - event.Skip(); -} - -void wxDataViewTextCtrlWrapper::OnKillFocus( wxFocusEvent &event ) -{ - if ( !m_finished && !m_aboutToFinish ) - { - AcceptChanges(); - //if ( !AcceptChanges() ) - // m_owner->OnRenameCancelled( m_itemEdited ); - - Finish(); - } - - // We must let the native text control handle focus - event.Skip(); -} - -bool wxDataViewTextCtrlWrapper::AcceptChanges() -{ - const wxString value = m_text->GetValue(); - - if ( value == m_startValue ) - // nothing changed, always accept - return true; - -// if ( !m_owner->OnRenameAccept(m_itemEdited, value) ) - // vetoed by the user -// return false; - - // accepted, do rename the item - wxVariant variant; - variant = value; - m_model->SetValue( variant, m_col, m_row ); - m_model->ValueChanged( m_col, m_row ); - - return true; -} - -void wxDataViewTextCtrlWrapper::Finish() -{ - if ( !m_finished ) - { - m_finished = true; - - m_text->RemoveEventHandler(this); - m_owner->FinishEditing(m_text); - - // delete later - wxPendingDelete.Append( this ); - } -} - //----------------------------------------------------------------------------- // wxDataViewMainWindow //----------------------------------------------------------------------------- +//The tree building helper, declared firstly +void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node); + int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 ) { if (row1 > row2) return 1; @@ -1687,7 +1748,6 @@ wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID i m_lastOnSame = false; m_renameTimer = new wxDataViewRenameTimer( this ); - m_textctrlWrapper = NULL; // TODO: user better initial values/nothing selected m_currentCol = NULL; @@ -1715,11 +1775,20 @@ wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID i m_penRule = wxPen(GetRuleColour(), 1, wxSOLID); + //Here I compose a pen can draw black lines, maybe there are something system colour to use + m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID ); + //Some new added code to deal with the tree structure + m_root = new wxDataViewTreeNode( NULL ); + m_root->SetHasChildren(true); + + //Make m_count = -1 will cause the class recaculate the real displaying number of rows. + m_count = -1 ; UpdateDisplay(); } wxDataViewMainWindow::~wxDataViewMainWindow() { + DestroyTree(); delete m_renameTimer; } @@ -1730,7 +1799,6 @@ void wxDataViewMainWindow::OnRenameTimer() if ( m_dirty ) wxSafeYield(); - int xpos = 0; unsigned int cols = GetOwner()->GetColumnCount(); unsigned int i; @@ -1747,68 +1815,247 @@ void wxDataViewMainWindow::OnRenameTimer() wxRect labelRect( xpos, m_currentRow * m_lineHeight, m_currentCol->GetWidth(), m_lineHeight ); - wxClassInfo *textControlClass = CLASSINFO(wxTextCtrl); + GetOwner()->CalcScrolledPosition( labelRect.x, labelRect.y, + &labelRect.x, &labelRect.y); - wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject(); - m_textctrlWrapper = new wxDataViewTextCtrlWrapper(this, text, GetOwner()->GetModel(), - m_currentCol->GetModelColumn(), m_currentRow, labelRect ); + wxDataViewItem item = GetItemByRow( m_currentRow ); + m_currentCol->GetRenderer()->StartEditing( item, labelRect ); } -void wxDataViewMainWindow::FinishEditing( wxTextCtrl *text ) +//------------------------------------------------------------------ +// Helper class for do operation on the tree node +//------------------------------------------------------------------ +class DoJob { - delete text; - m_textctrlWrapper = NULL; - SetFocus(); - // SetFocusIgnoringChildren(); -} +public: + DoJob(){}; + virtual ~DoJob(){}; + + //The return value control how the tree-walker tranverse the tree + // 0: Job done, stop tranverse and return + // 1: Ignore the current node's subtree and continue + // 2: Job not done, continue + enum { OK = 0 , IGR = 1, CONT = 2 }; + virtual int operator() ( wxDataViewTreeNode * node ) = 0 ; + virtual int operator() ( void * n ) = 0; +}; -bool wxDataViewMainWindow::RowAppended() +#if 0 +class ItemAddJob: public DoJob { - return false; -} +public: + ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item, int * count ) + { this->parent = parent ; this->item = item ; m_count = count; } + virtual ~ItemAddJob(){}; + + virtual int operator() ( wxDataViewTreeNode * node ) + { + if( node->GetItem() == parent ) + { + node->SetHasChildren( true ); + wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); + newnode->SetItem(item); + node->AppendChild( newnode); + *m_count = -1; + return OK; + } + return CONT; + } + +private: + int * m_count; + wxDataViewItem parent, item; +}; +#endif -bool wxDataViewMainWindow::RowPrepended() +bool Walker( wxDataViewTreeNode * node, DoJob & func ) { + if( node==NULL || !node->HasChildren()) + return false; + + wxDataViewTreeNodes nodes = node->GetNodes(); + wxDataViewTreeLeaves leaves = node->GetChildren(); + + int len_nodes = nodes.GetCount(); + int len = leaves.GetCount(); + int i = 0, nodes_i = 0; + for( ; i < len ; i ++ ) + { + void * n = leaves[i]; + if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() ) + { + wxDataViewTreeNode * nd = nodes[nodes_i]; + nodes_i++; + + switch( func( nd ) ) + { + case DoJob::OK : + return true ; + case DoJob::IGR: + continue; + case DoJob::CONT: + default: + ; + } + + if( Walker( nd , func ) ) + return true; + + } + else + switch( func( n ) ) + { + case DoJob::OK : + return true ; + case DoJob::IGR: + continue; + case DoJob::CONT: + default: + ; + } + } return false; } -bool wxDataViewMainWindow::RowInserted( unsigned int WXUNUSED(before) ) +bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) { - return false; + g_model = GetOwner()->GetModel(); + + wxDataViewTreeNode * node; + node = FindNode(parent); + + if( node == NULL ) + return false; + + node->SetHasChildren( true ); + + if( g_model->IsContainer( item ) ) + { + wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); + newnode->SetItem(item); + node->AddNode( newnode); + } + else + node->AddLeaf( item.GetID() ); + + node->ChangeSubTreeCount(1); + + m_count = -1; + UpdateDisplay(); + return true; } -bool wxDataViewMainWindow::RowDeleted( unsigned int WXUNUSED(row) ) +#if 0 +class ItemDeleteJob: public DoJob { - return false; +public: + ItemDeleteJob( const wxDataViewItem & item, int * count ) { m_item = item; m_count = count; } + virtual ~ItemDeleteJob(){} + virtual int operator() ( wxDataViewTreeNode * node ) + { + if( node->GetItem() == m_item ) + { + node->GetParent()->GetChildren().Remove( node ); + delete node; + *m_count = -1; + return DoJob::OK; + } + return DoJob::CONT; + } + +private: + int * m_count; + wxDataViewItem m_item; +}; +#endif + +void DestroyTreeHelper( wxDataViewTreeNode * node); + +bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, + const wxDataViewItem& item) +{ + g_model = GetOwner()->GetModel(); + + wxDataViewTreeNode * node; + node = FindNode(parent); + + if( node == NULL || node->GetChildren().Index( item.GetID() ) == wxNOT_FOUND ) + { + return false; + } + + int sub = -1; + node->GetChildren().Remove( item.GetID() ); + if( GetOwner()->GetModel()->IsContainer( item ) ) + { + wxDataViewTreeNode * n = NULL; + wxDataViewTreeNodes nodes = node->GetNodes(); + int len = nodes.GetCount(); + for( int i = 0 ; i < len; i ++) + { + if( nodes[i]->GetItem() == item ) + { + n = nodes[i]; + break; + } + } + + if (!n) + return false; + + node->GetNodes().Remove( n ); + sub -= n->GetSubTreeCount(); + DestroyTreeHelper(n); + } + + if( node->GetChildrenNumber() == 0) + node->SetHasChildren( false ); + + //Make the row number invalid and get a new valid one when user call GetRowCount + m_count = -1; + node->ChangeSubTreeCount(sub); + //Change the current row to the last row if the current exceed the max row number + if( m_currentRow > GetRowCount() ) + m_currentRow = m_count - 1; + + UpdateDisplay(); + return true; } -bool wxDataViewMainWindow::RowChanged( unsigned int WXUNUSED(row) ) +bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) { - return false; + g_model = GetOwner()->GetModel(); + + unsigned int row = GetRowByItem(item); + RefreshRow( row ); + return true; } -bool wxDataViewMainWindow::ValueChanged( unsigned int WXUNUSED(col), unsigned int row ) +bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int WXUNUSED(col) ) { // NOTE: to be valid, we cannot use e.g. INT_MAX - 1 -#define MAX_VIRTUAL_WIDTH 100000 +/*#define MAX_VIRTUAL_WIDTH 100000 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight ); m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); Refresh( true, &rect ); return true; -} - -bool wxDataViewMainWindow::RowsReordered( unsigned int *WXUNUSED(new_order) ) -{ - Refresh(); +*/ + g_model = GetOwner()->GetModel(); + unsigned int row = GetRowByItem(item); + RefreshRow( row ); return true; } bool wxDataViewMainWindow::Cleared() { - return false; + g_model = GetOwner()->GetModel(); + + DestroyTree(); + UpdateDisplay(); + return true; } void wxDataViewMainWindow::UpdateDisplay() @@ -1829,7 +2076,7 @@ void wxDataViewMainWindow::OnInternalIdle() void wxDataViewMainWindow::RecalculateDisplay() { - wxDataViewListModel *model = GetOwner()->GetModel(); + wxDataViewModel *model = GetOwner()->GetModel(); if (!model) { Refresh(); @@ -1837,7 +2084,7 @@ void wxDataViewMainWindow::RecalculateDisplay() } int width = GetEndOfLastCol(); - int height = model->GetRowCount() * m_lineHeight; + int height = GetRowCount() * m_lineHeight; SetVirtualSize( width, height ); GetOwner()->SetScrollRate( 10, m_lineHeight ); @@ -1855,7 +2102,7 @@ void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) { - wxDataViewListModel *model = GetOwner()->GetModel(); + wxDataViewModel *model = GetOwner()->GetModel(); wxAutoBufferedPaintDC dc( this ); // prepare the DC @@ -1871,7 +2118,7 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) ); unsigned int item_count = wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1), - (int)(model->GetRowCount() - item_start) ); + (int)(GetRowCount( )- item_start) ); unsigned int item_last = item_start + item_count; // compute which columns needs to be redrawn @@ -1982,15 +2229,58 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) { // get the cell value and set it into the renderer wxVariant value; - model->GetValue( value, col->GetModelColumn(), item ); + wxDataViewTreeNode * node = GetTreeNodeByRow(item); + if( node == NULL ) + { + continue; + } + + wxDataViewItem dataitem = node->GetItem(); + model->GetValue( value, dataitem, col->GetModelColumn()); cell->SetValue( value ); // update the y offset cell_rect.y = item * m_lineHeight; + //Draw the expander here. + int indent = node->GetIndentLevel(); + if( col->GetModelColumn() == GetOwner()->GetExpanderColumn() ) + { + //Calculate the indent first + indent = cell_rect.x + GetOwner()->GetIndent() * indent; + + int expander_width = m_lineHeight - 2*EXPANDER_MARGIN; + // change the cell_rect.x to the appropriate pos + int expander_x = indent + EXPANDER_MARGIN , expander_y = cell_rect.y + EXPANDER_MARGIN ; + indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space + dc.SetPen( m_penExpander ); + dc.SetBrush( wxNullBrush ); + if( node->HasChildren() ) + { + //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0); + //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 ); + wxRect rect( expander_x , expander_y, expander_width, expander_width); + if( node->IsOpen() ) + wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, wxCONTROL_EXPANDED ); + else + wxRendererNative::Get().DrawTreeItemButton( this, dc, rect ); + } + else + { + // I am wandering whether we should draw dot lines between tree nodes + delete node; + //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow + } + + //force the expander column to left-center align + cell->SetAlignment( wxALIGN_CENTER_VERTICAL ); + } + + // cannot be bigger than allocated space wxSize size = cell->GetSize(); - size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width ); + // Because of the tree structure indent, here we should minus the width of the cell for drawing + size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent ); size.y = wxMin( size.y + 2*PADDING_TOPBOTTOM, cell_rect.height ); wxRect item_rect(cell_rect.GetTopLeft(), size); @@ -2018,6 +2308,9 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) item_rect.width = size.x - 2 * PADDING_RIGHTLEFT; item_rect.height = size.y - 2 * PADDING_TOPBOTTOM; + //Here we add the tree indent + item_rect.x += indent; + int state = 0; if (m_selection.Index(item) != wxNOT_FOUND) state |= wxDATAVIEW_CELL_SELECTED; @@ -2068,7 +2361,7 @@ unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const return y / m_lineHeight; } -unsigned int wxDataViewMainWindow::GetLastVisibleRow() const +unsigned int wxDataViewMainWindow::GetLastVisibleRow() { wxSize client_size = GetClientSize(); m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, @@ -2077,9 +2370,18 @@ unsigned int wxDataViewMainWindow::GetLastVisibleRow() const return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 ); } -unsigned int wxDataViewMainWindow::GetRowCount() const +unsigned int wxDataViewMainWindow::GetRowCount() { - return wx_const_cast(wxDataViewCtrl*, GetOwner())->GetModel()->GetRowCount(); + if ( m_count == -1 ) + { + m_count = RecalculateCount(); + int width, height; + GetVirtualSize( &width, &height ); + height = m_count * m_lineHeight; + + SetVirtualSize( width, height ); + } + return m_count; } void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row ) @@ -2292,6 +2594,382 @@ wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const return rect; } +class RowToItemJob: public DoJob +{ +public: + RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;} + virtual ~RowToItemJob(){}; + + virtual int operator() ( wxDataViewTreeNode * node ) + { + current ++; + if( current == static_cast(row)) + { + ret = node->GetItem() ; + return DoJob::OK; + } + + if( node->GetSubTreeCount() + current < static_cast(row) ) + { + current += node->GetSubTreeCount(); + return DoJob::IGR; + } + else + return DoJob::CONT; + } + + virtual int operator() ( void * n ) + { + current ++; + if( current == static_cast(row)) + { + ret = wxDataViewItem( n ) ; + return DoJob::OK; + } + return DoJob::CONT; + } + wxDataViewItem GetResult(){ return ret; } +private: + unsigned int row; + int current ; + wxDataViewItem ret; +}; + +wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) +{ + RowToItemJob job( row, -1 ); + Walker( m_root , job ); + return job.GetResult(); +} + +class RowToTreeNodeJob: public DoJob +{ +public: + RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node ) + { + this->row = row; + this->current = current ; + ret = NULL ; + parent = node; + } + virtual ~RowToTreeNodeJob(){}; + + virtual int operator() ( wxDataViewTreeNode * node ) + { + current ++; + if( current == static_cast(row)) + { + ret = node ; + return DoJob::OK; + } + + if( node->GetSubTreeCount() + current < static_cast(row) ) + { + current += node->GetSubTreeCount(); + return DoJob::IGR; + } + else + { + parent = node; + return DoJob::CONT; + } + + } + + virtual int operator() ( void * n ) + { + current ++; + if( current == static_cast(row)) + { + ret = new wxDataViewTreeNode( parent ) ; + ret->SetItem( wxDataViewItem( n )); + ret->SetHasChildren(false); + return DoJob::OK; + } + + return DoJob::CONT; + } + wxDataViewTreeNode * GetResult(){ return ret; } +private: + unsigned int row; + int current ; + wxDataViewTreeNode * ret; + wxDataViewTreeNode * parent ; +}; + + +wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) +{ + RowToTreeNodeJob job( row , -1, m_root ); + Walker( m_root , job ); + return job.GetResult(); +} + +#if 0 +class CountJob : public DoJob +{ +public: + CountJob(){ count = 0 ; } + virtual ~CountJob(){}; + + virtual int operator () ( wxDataViewTreeNode * node ) + { + count ++; + if ( node->IsOpen()) + return DoJob::CONT; + else + return DoJob::IGR; + } + + unsigned int GetResult() + { + return count ; + } +private: + unsigned int count; +}; +#endif + +void wxDataViewMainWindow::OnExpanding( unsigned int row ) +{ + wxDataViewTreeNode * node = GetTreeNodeByRow(row); + if( node != NULL ) + { + if( node->HasChildren()) + if( !node->IsOpen()) + { + node->ToggleOpen(); + //Here I build the children of current node + if( node->GetChildrenNumber() == 0 ) + BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node); + m_count = -1; + UpdateDisplay(); + } + else + { + SelectRow( row, false ); + SelectRow( row + 1, true ); + ChangeCurrentRow( row + 1 ); + } + else + delete node; + } +} + +void wxDataViewMainWindow::OnCollapsing(unsigned int row) +{ + wxDataViewTreeNode * node = GetTreeNodeByRow(row); + if( node != NULL ) + { + wxDataViewTreeNode * nd = node; + + if( node->HasChildren() && node->IsOpen() ) + { + node->ToggleOpen(); + m_count = -1; + UpdateDisplay(); + //RefreshRows(row,GetLastVisibleRow()); + } + else + { + node = node->GetParent(); + if( node != NULL ) + { + int parent = GetRowByItem( node->GetItem()) ; + SelectRow( row, false); + SelectRow(parent , true ); + ChangeCurrentRow( parent ); + } + } + if( !nd->HasChildren()) + delete nd; + } +} + +wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item ) +{ + wxDataViewModel * model = GetOwner()->GetModel(); + if( model == NULL ) + return NULL; + + //Compose the a parent-chain of the finding item + ItemList list; + list.DeleteContents( true ); + wxDataViewItem it( item ); + while( it.IsOk() ) + { + wxDataViewItem * pItem = new wxDataViewItem( it ); + list.Insert( pItem ); + it = model->GetParent( it ); + } + + //Find the item along the parent-chain. + //This algorithm is designed to speed up the node-finding method + bool found = true; + wxDataViewTreeNode * node = m_root; + for( ItemList::Node * n = list.GetFirst(); n; n = n->GetNext() ) + { + if( node->HasChildren() ) + { + if( node->GetChildrenNumber() == 0 ) + BuildTreeHelper(model, node->GetItem(), node); + + int len = node->GetNodeNumber(); + wxDataViewTreeNodes nodes = node->GetNodes(); + int j = 0; + for( ; j < len; j ++) + { + if( nodes[j]->GetItem() == *(n->GetData())) + { + node = nodes[j]; + break; + } + } + // Whenever we can't find the node in any level, return NULL to indicate the item can't be found + if( j == len ) + { + found = false; + return NULL; + } + } + else + return NULL; + } + return node; +} + +int wxDataViewMainWindow::RecalculateCount() +{ + return m_root->GetSubTreeCount(); +} + +class ItemToRowJob : public DoJob +{ +public: + ItemToRowJob(const wxDataViewItem & item, ItemList::Node * node ) + { this->item = item ; ret = 0 ; nd = node ; } + virtual ~ItemToRowJob(){}; + + virtual int operator() ( wxDataViewTreeNode * node) + { + ret ++; + if( node->GetItem() == item ) + { + return DoJob::OK; + } + + if( nd && node->GetItem() == *(nd->GetData())) + { + nd = nd->GetNext(); + return DoJob::CONT; + } + else + { + ret += node->GetSubTreeCount(); + return DoJob::IGR; + } + + } + + virtual int operator() ( void * n ) + { + ret ++; + if( n == item.GetID() ) + return DoJob::OK; + return DoJob::CONT; + } + //the row number is begin from zero + int GetResult(){ return ret -1 ; } +private: + ItemList::Node * nd; + wxDataViewItem item; + int ret; + +}; + +unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) +{ + wxDataViewModel * model = GetOwner()->GetModel(); + if( model == NULL ) + return 0; + + //Compose the a parent-chain of the finding item + ItemList list; + list.DeleteContents( true ); + wxDataViewItem it( item ); + while( it.IsOk() ) + { + wxDataViewItem * pItem = new wxDataViewItem( it ); + list.Insert( pItem ); + it = model->GetParent( it ); + } + + ItemToRowJob job( item, list.GetFirst() ); + Walker(m_root , job ); + return job.GetResult(); +} + +void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node) +{ + if( !model->IsContainer( item ) ) + return ; + + wxDataViewItem i = model->GetFirstChild( item ); + int num = 0; + while( i.IsOk() ) + { + num ++; + if( model->IsContainer( i ) ) + { + wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); + n->SetItem(i); + n->SetHasChildren( true ) ; + node->AddNode( n ); + } + else + { + node->AddLeaf( i.GetID() ); + } + i = model->GetNextSibling( i ); + } + node->SetSubTreeCount( num ); + wxDataViewTreeNode * n = node->GetParent(); + if( n != NULL) + n->ChangeSubTreeCount(num); + +} + +void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) +{ + //First we define a invalid item to fetch the top-level elements + wxDataViewItem item; + g_model = GetOwner()->GetModel(); + BuildTreeHelper( model, item, m_root); + m_count = -1 ; +} + +void DestroyTreeHelper( wxDataViewTreeNode * node ) +{ + if( node->GetNodeNumber() != 0 ) + { + int len = node->GetNodeNumber(); + int i = 0 ; + wxDataViewTreeNodes nodes = node->GetNodes(); + for( ; i < len; i ++ ) + { + DestroyTreeHelper(nodes[i]); + } + } + delete node; +} + +void wxDataViewMainWindow::DestroyTree() +{ + DestroyTreeHelper(m_root); + m_root->SetSubTreeCount(0); + m_count = 0 ; +} + void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) { if (event.GetKeyCode() == WXK_TAB) @@ -2327,7 +3005,13 @@ void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) if ( m_currentRow < GetRowCount() - 1 ) OnArrowChar( m_currentRow + 1, event ); break; - + //Add the process for tree expanding/collapsing + case WXK_LEFT: + OnCollapsing(m_currentRow); + break; + case WXK_RIGHT: + OnExpanding( m_currentRow); + break; case WXK_END: if (!IsEmpty()) OnArrowChar( GetRowCount() - 1, event ); @@ -2409,7 +3093,7 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) return; } - wxDataViewListModel *model = GetOwner()->GetModel(); + wxDataViewModel *model = GetOwner()->GetModel(); if (event.Dragging()) { @@ -2451,12 +3135,13 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) { if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) { + wxDataViewItem item = GetItemByRow(current); wxVariant value; - model->GetValue( value, col->GetModelColumn(), current ); + model->GetValue( value, item, col->GetModelColumn() ); cell->SetValue( value ); wxRect cell_rect( xpos, current * m_lineHeight, col->GetWidth(), m_lineHeight ); - cell->Activate( cell_rect, model, col->GetModelColumn(), current ); + cell->Activate( cell_rect, model, item, col->GetModelColumn() ); } return; } @@ -2477,7 +3162,26 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) SelectRow( m_lineSelectSingleOnUp, true ); } - if (m_lastOnSame) + //Process the event of user clicking the expander + bool expander = false; + wxDataViewTreeNode * node = GetTreeNodeByRow(current); + if( node!=NULL && node->HasChildren() ) + { + int indent = node->GetIndentLevel(); + indent = GetOwner()->GetIndent()*indent; + wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN); + if( rect.Contains( x, y) ) + { + expander = true; + if( node->IsOpen() ) + OnCollapsing(current); + else + OnExpanding( current ); + } + } + + //If the user click the expander, we do not do editing even if the column with expander are editable + if (m_lastOnSame && !expander ) { if ((col == m_currentCol) && (current == m_currentRow) && (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) ) @@ -2615,6 +3319,14 @@ void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event ) event.Skip(); } +wxDataViewItem wxDataViewMainWindow::GetSelection() +{ + if( m_selection.GetCount() != 1 ) + return wxDataViewItem(); + + return GetItemByRow( m_selection.Item(0)); +} + //----------------------------------------------------------------------------- // wxDataViewCtrl //----------------------------------------------------------------------------- @@ -2701,15 +3413,17 @@ void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) ) AdjustScrollbars(); } -bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model ) +bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model ) { if (!wxDataViewCtrlBase::AssociateModel( model )) return false; - m_notifier = new wxGenericDataViewListModelNotifier( m_clientArea ); + m_notifier = new wxGenericDataViewModelNotifier( m_clientArea ); model->AddNotifier( m_notifier ); + m_clientArea->BuildTree(model); + m_clientArea->UpdateDisplay(); return true; @@ -2732,6 +3446,22 @@ void wxDataViewCtrl::OnColumnChange() m_clientArea->UpdateDisplay(); } +void wxDataViewCtrl::DoSetExpanderColumn() +{ + m_clientArea->UpdateDisplay(); +} + +void wxDataViewCtrl::DoSetIndent() +{ + m_clientArea->UpdateDisplay(); +} + +wxDataViewItem wxDataViewCtrl::GetSelection() +{ + return m_clientArea->GetSelection(); +} + +/******************************************************************** void wxDataViewCtrl::SetSelection( int row ) { m_clientArea->SelectRow(row, true); @@ -2772,7 +3502,7 @@ int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const return 0; } - +*********************************************************************/ #endif // !wxUSE_GENERICDATAVIEWCTRL