diff --git a/eeschema/cmp_tree_model.cpp b/eeschema/cmp_tree_model.cpp index 994e4aa691..20c6f70c1b 100644 --- a/eeschema/cmp_tree_model.cpp +++ b/eeschema/cmp_tree_model.cpp @@ -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(); diff --git a/eeschema/cmp_tree_model_adapter.cpp b/eeschema/cmp_tree_model_adapter.cpp index ac9cd5e6a7..e70a4c0c81 100644 --- a/eeschema/cmp_tree_model_adapter.cpp +++ b/eeschema/cmp_tree_model_adapter.cpp @@ -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; diff --git a/eeschema/cmp_tree_model_adapter.h b/eeschema/cmp_tree_model_adapter.h index 9cd67ae9bb..c878ae72f9 100644 --- a/eeschema/cmp_tree_model_adapter.h +++ b/eeschema/cmp_tree_model_adapter.h @@ -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. */ diff --git a/eeschema/widgets/component_tree.cpp b/eeschema/widgets/component_tree.cpp index fe2e245756..7a286a732e 100644 --- a/eeschema/widgets/component_tree.cpp +++ b/eeschema/widgets/component_tree.cpp @@ -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 ); diff --git a/eeschema/widgets/component_tree.h b/eeschema/widgets/component_tree.h index 4f9084d010..04ca143b8b 100644 --- a/eeschema/widgets/component_tree.h +++ b/eeschema/widgets/component_tree.h @@ -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 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> m_menus; + + ///> Flag indicating whether a right-click context menu is active + bool m_menuActive; }; ///> Custom event sent when a new component is preselected