Add wxDataViewTreeStore with sample
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@49168 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/log.h"
|
||||
#include "wx/icon.h"
|
||||
#endif
|
||||
|
||||
const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl");
|
||||
@@ -830,5 +831,392 @@ DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
|
||||
DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
|
||||
DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// wxDataViewTreeStore
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
|
||||
wxDataViewTreeStoreNode *parent,
|
||||
const wxString &text, const wxIcon &icon, wxClientData *data )
|
||||
{
|
||||
m_parent = parent;
|
||||
m_text = text;
|
||||
m_icon = icon;
|
||||
m_data = data;
|
||||
}
|
||||
|
||||
wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
|
||||
{
|
||||
if (m_data)
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
#include "wx/listimpl.cpp"
|
||||
WX_DEFINE_LIST(wxDataViewTreeStoreNodeList);
|
||||
|
||||
wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
|
||||
wxDataViewTreeStoreNode *parent, const wxString &text,
|
||||
const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
|
||||
wxDataViewTreeStoreNode( parent, text, icon, data )
|
||||
{
|
||||
m_iconExpanded = expanded;
|
||||
m_children.DeleteContents(true);
|
||||
}
|
||||
|
||||
wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
wxDataViewTreeStore::wxDataViewTreeStore()
|
||||
{
|
||||
m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
|
||||
}
|
||||
|
||||
wxDataViewTreeStore::~wxDataViewTreeStore()
|
||||
{
|
||||
delete m_root;
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
|
||||
const wxString &text, const wxIcon &icon, wxClientData *data )
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
|
||||
if (!parent_node) return wxDataViewItem(0);
|
||||
|
||||
wxDataViewTreeStoreNode *node =
|
||||
new wxDataViewTreeStoreNode( parent_node, text, icon, data );
|
||||
parent_node->GetChildren().Append( node );
|
||||
|
||||
// notify control
|
||||
ItemAdded( parent, node->GetItem() );
|
||||
|
||||
return node->GetItem();
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
|
||||
const wxString &text, const wxIcon &icon, wxClientData *data )
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
|
||||
if (!parent_node) return wxDataViewItem(0);
|
||||
|
||||
wxDataViewTreeStoreNode *node =
|
||||
new wxDataViewTreeStoreNode( parent_node, text, icon, data );
|
||||
parent_node->GetChildren().Insert( node );
|
||||
|
||||
// notify control
|
||||
ItemAdded( parent, node->GetItem() );
|
||||
|
||||
return node->GetItem();
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
|
||||
const wxString &text, const wxIcon &icon, wxClientData *data )
|
||||
{
|
||||
return wxDataViewItem(0);
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
|
||||
const wxString &text, const wxIcon &icon, const wxIcon &expanded,
|
||||
wxClientData *data )
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
|
||||
if (!parent_node) return wxDataViewItem(0);
|
||||
|
||||
wxDataViewTreeStoreContainerNode *node =
|
||||
new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
|
||||
parent_node->GetChildren().Insert( node );
|
||||
|
||||
// notify control
|
||||
ItemAdded( parent, node->GetItem() );
|
||||
|
||||
return node->GetItem();
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::AppendContainer( const wxDataViewItem& parent,
|
||||
const wxString &text, const wxIcon &icon, const wxIcon &expanded,
|
||||
wxClientData *data )
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
|
||||
if (!parent_node) return wxDataViewItem(0);
|
||||
|
||||
wxDataViewTreeStoreContainerNode *node =
|
||||
new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
|
||||
parent_node->GetChildren().Append( node );
|
||||
|
||||
// notify control
|
||||
ItemAdded( parent, node->GetItem() );
|
||||
|
||||
return node->GetItem();
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
|
||||
const wxString &text, const wxIcon &icon, const wxIcon &expanded,
|
||||
wxClientData *data )
|
||||
{
|
||||
return wxDataViewItem(0);
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
|
||||
if (!parent_node) return wxDataViewItem(0);
|
||||
|
||||
wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
|
||||
if (node)
|
||||
return node->GetData();
|
||||
|
||||
return wxDataViewItem(0);
|
||||
}
|
||||
|
||||
int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( parent );
|
||||
if (!node) return -1;
|
||||
|
||||
if (!node->IsContainer())
|
||||
return 0;
|
||||
|
||||
wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
|
||||
return (int) container_node->GetChildren().GetCount();
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return;
|
||||
|
||||
node->SetText( text );
|
||||
|
||||
// notify control
|
||||
ValueChanged( item, 0 );
|
||||
}
|
||||
|
||||
wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return wxEmptyString;
|
||||
|
||||
return node->GetText();
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return;
|
||||
|
||||
node->SetIcon( icon );
|
||||
|
||||
// notify control
|
||||
ValueChanged( item, 0 );
|
||||
}
|
||||
|
||||
const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return wxNullIcon;
|
||||
|
||||
return node->GetIcon();
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
|
||||
if (!node) return;
|
||||
|
||||
node->SetExpandedIcon( icon );
|
||||
|
||||
// notify control
|
||||
ValueChanged( item, 0 );
|
||||
}
|
||||
|
||||
const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
|
||||
if (!node) return wxNullIcon;
|
||||
|
||||
return node->GetExpandedIcon();
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return;
|
||||
|
||||
node->SetData( data );
|
||||
|
||||
// notify control? only sensible when sorting on client data
|
||||
// ValueChanged( item, 0 );
|
||||
}
|
||||
|
||||
wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return NULL;
|
||||
|
||||
return node->GetData();
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
|
||||
{
|
||||
if (!item.IsOk()) return;
|
||||
|
||||
wxDataViewItem parent_item = GetParent( item );
|
||||
|
||||
wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
|
||||
if (!parent_node) return;
|
||||
|
||||
wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
|
||||
if (!node) return;
|
||||
|
||||
parent_node->GetChildren().DeleteObject( node );
|
||||
|
||||
// notify control
|
||||
ItemDeleted( parent_item, item );
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
|
||||
if (!node) return;
|
||||
|
||||
wxDataViewItemArray array;
|
||||
wxDataViewTreeStoreNodeList::iterator iter;
|
||||
for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
|
||||
{
|
||||
wxDataViewTreeStoreNode* child = *iter;
|
||||
array.Add( child->GetItem() );
|
||||
}
|
||||
|
||||
node->GetChildren().clear();
|
||||
|
||||
// notify control
|
||||
ItemsDeleted( item, array );
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::DeleteAllItems()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
void wxDataViewTreeStore::GetValue( wxVariant &variant,
|
||||
const wxDataViewItem &item, unsigned int col ) const
|
||||
{
|
||||
// if (col != 0) return;
|
||||
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return;
|
||||
|
||||
wxDataViewIconText data( node->GetText(), node->GetIcon() );
|
||||
|
||||
variant << data;
|
||||
}
|
||||
|
||||
bool wxDataViewTreeStore::SetValue( const wxVariant &variant,
|
||||
const wxDataViewItem &item, unsigned int col )
|
||||
{
|
||||
// if (col != 0) return false;
|
||||
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return false;
|
||||
|
||||
wxDataViewIconText data;
|
||||
|
||||
data << variant;
|
||||
|
||||
node->SetText( data.GetText() );
|
||||
node->SetIcon( data.GetIcon() );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return wxDataViewItem(0);
|
||||
|
||||
wxDataViewTreeStoreNode *parent = node->GetParent();
|
||||
if (!parent) return wxDataViewItem(0);
|
||||
|
||||
if (parent == m_root)
|
||||
return wxDataViewItem(0);
|
||||
|
||||
return parent->GetItem();
|
||||
}
|
||||
|
||||
bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
|
||||
{
|
||||
wxDataViewTreeStoreNode *node = FindNode( item );
|
||||
if (!node) return false;
|
||||
|
||||
return node->IsContainer();
|
||||
}
|
||||
|
||||
unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
|
||||
{
|
||||
wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
|
||||
if (!node) return 0;
|
||||
|
||||
wxDataViewTreeStoreNodeList::iterator iter;
|
||||
for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
|
||||
{
|
||||
wxDataViewTreeStoreNode* child = *iter;
|
||||
children.Add( child->GetItem() );
|
||||
}
|
||||
|
||||
return node->GetChildren().GetCount();
|
||||
}
|
||||
|
||||
int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
|
||||
unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
|
||||
{
|
||||
wxDataViewTreeStoreNode *node1 = FindNode( item1 );
|
||||
wxDataViewTreeStoreNode *node2 = FindNode( item2 );
|
||||
|
||||
if (!node1 || !node2)
|
||||
return 0;
|
||||
|
||||
wxDataViewTreeStoreContainerNode* parent1 =
|
||||
(wxDataViewTreeStoreContainerNode*) node1->GetParent();
|
||||
wxDataViewTreeStoreContainerNode* parent2 =
|
||||
(wxDataViewTreeStoreContainerNode*) node2->GetParent();
|
||||
|
||||
if (parent1 != parent2)
|
||||
{
|
||||
wxLogError( wxT("Comparing items with different parent.") );
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (node1->IsContainer() && !!node2->IsContainer())
|
||||
return 1;
|
||||
|
||||
if (node2->IsContainer() && !!node1->IsContainer())
|
||||
return -1;
|
||||
|
||||
return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
|
||||
}
|
||||
|
||||
wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
|
||||
{
|
||||
if (!item.IsOk())
|
||||
return m_root;
|
||||
|
||||
return (wxDataViewTreeStoreNode*) item.GetID();
|
||||
}
|
||||
|
||||
wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
|
||||
{
|
||||
if (!item.IsOk())
|
||||
return (wxDataViewTreeStoreContainerNode*) m_root;
|
||||
|
||||
wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
|
||||
|
||||
if (!node->IsContainer())
|
||||
return NULL;
|
||||
|
||||
return (wxDataViewTreeStoreContainerNode*) node;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user