Right-click context menus for COMPONENT_TREE widget

This commit is contained in:
Maciej Suminski 2017-10-28 18:59:49 +02:00
parent 42220579df
commit 3288a5f8b8
5 changed files with 63 additions and 0 deletions

View File

@ -133,6 +133,7 @@ CMP_TREE_NODE_LIB_ID::CMP_TREE_NODE_LIB_ID( CMP_TREE_NODE* aParent, LIB_ALIAS* a
Type = LIBID;
Parent = aParent;
Type = ALIAS;
Name = aAlias->GetName();
Desc = aAlias->GetDescription();

View File

@ -261,6 +261,13 @@ int CMP_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
}
CMP_TREE_NODE::TYPE CMP_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
{
auto node = ToNode( aSelection );
return node ? node->Type : CMP_TREE_NODE::INVALID;
}
int CMP_TREE_MODEL_ADAPTER::GetComponentsCount() const
{
int n = 0;

View File

@ -225,6 +225,16 @@ public:
*/
int GetUnitFor( const wxDataViewItem& aSelection ) const;
/**
* Return node type for the given item.
*
* @param aSelection item from the wxDataViewCtrl
* (see wxDataViewCtrl::GetSelection())
*
* @return Type of the selected node, might be INVALID.
*/
CMP_TREE_NODE::TYPE GetTypeFor( const wxDataViewItem& aSelection ) const;
/**
* Return the number of components loaded in the tree.
*/

View File

@ -43,6 +43,9 @@ COMPONENT_TREE::COMPONENT_TREE( wxWindow* aParent, SYMBOL_LIB_TABLE* aSymLibTabl
m_query_ctrl( nullptr ),
m_details_ctrl( nullptr )
{
// create space for context menu pointers, INVALID is the max value
m_menus.resize( CMP_TREE_NODE::TYPE::INVALID + 1 );
auto sizer = new wxBoxSizer( wxVERTICAL );
// Search text control
@ -89,6 +92,7 @@ COMPONENT_TREE::COMPONENT_TREE( wxWindow* aParent, SYMBOL_LIB_TABLE* aSymLibTabl
m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &COMPONENT_TREE::onTreeActivate, this );
m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &COMPONENT_TREE::onTreeSelect, this );
m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &COMPONENT_TREE::onContextMenu, this );
Bind( COMPONENT_PRESELECTED, &COMPONENT_TREE::onPreselect, this );
@ -236,5 +240,19 @@ void COMPONENT_TREE::onPreselect( wxCommandEvent& aEvent )
}
void COMPONENT_TREE::onContextMenu( wxDataViewEvent& aEvent )
{
auto const sel = m_tree_ctrl->GetSelection();
auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
if( m_menus[type] )
{
m_menuActive = true;
PopupMenu( m_menus[type].get() );
m_menuActive = false;
}
}
wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
wxDEFINE_EVENT( COMPONENT_SELECTED, wxCommandEvent );

View File

@ -60,6 +60,25 @@ public:
*/
LIB_ID GetSelectedLibId( int* aUnit = nullptr ) const;
/**
* Associates a right click context menu for a specific node type.
* @param aType is the node type to have a menu associated.
* @param aMenu is the associated menu.
*/
void SetMenu( CMP_TREE_NODE::TYPE aType, std::unique_ptr<wxMenu> aMenu )
{
m_menus[aType] = std::move( aMenu );
}
/**
* Returns the status of right-click context menu.
* @return True in case a right-click context menu is active.
*/
bool IsMenuActive() const
{
return m_menuActive;
}
protected:
/**
* If a wxDataViewitem is valid, select it and post a selection event.
@ -86,12 +105,20 @@ protected:
void onDetailsLink( wxHtmlLinkEvent& aEvent );
void onPreselect( wxCommandEvent& aEvent );
void onContextMenu( wxDataViewEvent& aEvent );
SYMBOL_LIB_TABLE* m_sym_lib_table;
CMP_TREE_MODEL_ADAPTER::PTR m_adapter;
wxTextCtrl* m_query_ctrl;
wxDataViewCtrl* m_tree_ctrl;
wxHtmlWindow* m_details_ctrl;
///> Right click context menus for each tree level
std::vector<std::unique_ptr<wxMenu>> m_menus;
///> Flag indicating whether a right-click context menu is active
bool m_menuActive;
};
///> Custom event sent when a new component is preselected