2015-04-30 09:08:18 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 CERN
|
2020-12-27 00:41:04 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2015-04-30 09:08:18 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONDITIONAL_MENU_H
|
|
|
|
#define CONDITIONAL_MENU_H
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
#include <tool/selection_conditions.h>
|
2019-05-14 11:14:00 +00:00
|
|
|
#include <tool/action_menu.h>
|
2016-05-28 16:46:29 +00:00
|
|
|
#include <list>
|
2015-04-30 09:08:18 +00:00
|
|
|
|
2020-12-16 13:31:32 +00:00
|
|
|
class PCB_SELECTION_TOOL;
|
2015-07-24 07:42:46 +00:00
|
|
|
class TOOL_ACTION;
|
2015-08-03 09:53:58 +00:00
|
|
|
class TOOL_INTERACTIVE;
|
2015-04-30 09:08:18 +00:00
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
enum class BITMAPS : unsigned int;
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
class CONDITIONAL_MENU : public ACTION_MENU
|
2015-04-30 09:08:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Constant to indicate that we do not care about an #ENTRY location in the menu.
|
2015-04-30 09:08:18 +00:00
|
|
|
static const int ANY_ORDER = -1;
|
|
|
|
|
2020-08-17 22:40:02 +00:00
|
|
|
CONDITIONAL_MENU( TOOL_INTERACTIVE* aTool );
|
2019-05-13 20:42:40 +00:00
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* create() const override;
|
2015-08-03 09:53:58 +00:00
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Add a menu entry to run a #TOOL_ACTION on selected items.
|
2020-08-17 22:40:02 +00:00
|
|
|
*
|
2015-04-30 09:08:18 +00:00
|
|
|
* @param aAction is a menu entry to be added.
|
2020-08-17 22:40:02 +00:00
|
|
|
* @param aCondition is a condition that has to be fulfilled to show the menu entry in the menu.
|
2015-04-30 09:08:18 +00:00
|
|
|
* @param aOrder determines location of the added item, higher numbers are put on the bottom.
|
2020-12-27 00:41:04 +00:00
|
|
|
* You may use ANY_ORDER here if you think it does not matter.
|
2015-04-30 09:08:18 +00:00
|
|
|
*/
|
2019-05-13 20:42:40 +00:00
|
|
|
void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
|
2015-04-30 09:08:18 +00:00
|
|
|
int aOrder = ANY_ORDER );
|
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
|
2019-05-14 19:21:10 +00:00
|
|
|
const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Add a checked menu entry to run a TOOL_ACTION on selected items.
|
2020-08-17 22:40:02 +00:00
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* The condition for checking the menu entry should be supplied through a #ACTION_CONDITION
|
|
|
|
* registered with the #ACTION_MANAGER.
|
2020-08-17 22:40:02 +00:00
|
|
|
*
|
2019-05-13 20:42:40 +00:00
|
|
|
* @param aAction is a menu entry to be added.
|
2020-08-17 22:40:02 +00:00
|
|
|
* @param aCondition is a condition that has to be fulfilled to show the menu entry in the menu.
|
2019-05-13 20:42:40 +00:00
|
|
|
* @param aOrder determines location of the added item, higher numbers are put on the bottom.
|
2020-12-27 00:41:04 +00:00
|
|
|
* You may use #ANY_ORDER here if you think it does not matter.
|
2019-05-13 20:42:40 +00:00
|
|
|
*/
|
|
|
|
void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
|
|
|
|
int aOrder = ANY_ORDER );
|
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
|
2019-05-14 19:21:10 +00:00
|
|
|
const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
|
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Add a submenu to the menu.
|
|
|
|
* CONDITIONAL_MENU takes ownership of the added menu, so it will be freed when the
|
|
|
|
* CONDITIONAL_MENU object is destroyed.
|
2020-08-17 22:40:02 +00:00
|
|
|
*
|
2015-04-30 09:08:18 +00:00
|
|
|
* @param aMenu is the submenu to be added.
|
|
|
|
* @param aExpand determines if the added submenu items should be added as individual items
|
2020-12-27 00:41:04 +00:00
|
|
|
* or as a submenu.
|
|
|
|
* @param aCondition is a condition that has to be fulfilled to show the submenu entry in
|
|
|
|
* the menu.
|
2015-04-30 09:08:18 +00:00
|
|
|
* @param aOrder determines location of the added menu, higher numbers are put on the bottom.
|
2020-12-27 00:41:04 +00:00
|
|
|
* You may use ANY_ORDER here if you think it does not matter.
|
2015-04-30 09:08:18 +00:00
|
|
|
*/
|
2019-05-14 11:14:00 +00:00
|
|
|
void AddMenu( ACTION_MENU* aMenu,
|
2015-04-30 09:08:18 +00:00
|
|
|
const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
|
|
|
|
int aOrder = ANY_ORDER );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Add a separator to the menu.
|
2020-08-17 22:40:02 +00:00
|
|
|
*
|
2019-06-15 16:40:14 +00:00
|
|
|
* @param aOrder determines location of the separator, higher numbers are put on the bottom.
|
2015-04-30 09:08:18 +00:00
|
|
|
*/
|
2019-06-15 16:40:14 +00:00
|
|
|
void AddSeparator( int aOrder = ANY_ORDER );
|
2015-04-30 09:08:18 +00:00
|
|
|
|
2023-10-17 07:27:59 +00:00
|
|
|
void AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
|
2022-05-30 13:07:28 +00:00
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Update the contents of the menu based on the supplied conditions.
|
2015-04-30 09:08:18 +00:00
|
|
|
*/
|
2019-05-13 20:42:40 +00:00
|
|
|
void Evaluate( SELECTION& aSelection );
|
2015-04-30 09:08:18 +00:00
|
|
|
|
2019-06-02 11:57:29 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Update the initial contents so that wxWidgets doesn't get its knickers tied in a knot
|
2019-06-02 11:57:29 +00:00
|
|
|
* over the menu being empty (mainly an issue on GTK, but also on OSX with the preferences
|
|
|
|
* and quit menu items).
|
|
|
|
*/
|
|
|
|
void Resolve();
|
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper class to organize menu entries.
|
2015-04-30 09:08:18 +00:00
|
|
|
class ENTRY
|
|
|
|
{
|
|
|
|
public:
|
2019-05-13 20:42:40 +00:00
|
|
|
ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
|
|
|
|
bool aCheckmark ) :
|
2022-09-03 18:29:02 +00:00
|
|
|
m_type( ACTION ),
|
|
|
|
m_icon( static_cast<BITMAPS>( 0 ) ),
|
2019-05-13 20:42:40 +00:00
|
|
|
m_condition( aCondition ),
|
|
|
|
m_order( aOrder ),
|
|
|
|
m_isCheckmarkEntry( aCheckmark )
|
2015-04-30 09:08:18 +00:00
|
|
|
{
|
|
|
|
m_data.action = aAction;
|
|
|
|
}
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
|
2022-09-03 18:29:02 +00:00
|
|
|
m_type( MENU ),
|
|
|
|
m_icon( static_cast<BITMAPS>( 0 ) ),
|
2019-05-13 20:42:40 +00:00
|
|
|
m_condition( aCondition ),
|
|
|
|
m_order( aOrder ),
|
|
|
|
m_isCheckmarkEntry( false )
|
2015-04-30 09:08:18 +00:00
|
|
|
{
|
|
|
|
m_data.menu = aMenu;
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
|
2020-06-04 19:08:59 +00:00
|
|
|
SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
|
2022-09-03 18:29:02 +00:00
|
|
|
m_type( WXITEM ),
|
|
|
|
m_icon( aBitmap ),
|
2019-05-14 19:21:10 +00:00
|
|
|
m_condition( aCondition ),
|
|
|
|
m_order( aOrder ),
|
|
|
|
m_isCheckmarkEntry( aCheckmark )
|
|
|
|
{
|
2020-06-04 19:08:59 +00:00
|
|
|
m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
|
|
|
|
aItem.GetHelp(), aItem.GetKind() );
|
2019-05-14 19:21:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
// Separator
|
2019-05-13 20:42:40 +00:00
|
|
|
ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
|
2022-09-03 18:29:02 +00:00
|
|
|
m_type( SEPARATOR ),
|
|
|
|
m_icon( static_cast<BITMAPS>( 0 ) ),
|
2021-05-30 23:47:57 +00:00
|
|
|
m_data(),
|
2019-05-13 20:42:40 +00:00
|
|
|
m_condition( aCondition ),
|
|
|
|
m_order( aOrder ),
|
|
|
|
m_isCheckmarkEntry( false )
|
2015-04-30 09:08:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-04 19:08:59 +00:00
|
|
|
ENTRY( const ENTRY& aEntry );
|
|
|
|
|
|
|
|
~ENTRY();
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Possible entry types.
|
2015-04-30 09:08:18 +00:00
|
|
|
enum ENTRY_TYPE {
|
|
|
|
ACTION,
|
|
|
|
MENU,
|
2019-05-14 19:21:10 +00:00
|
|
|
WXITEM,
|
2015-04-30 09:08:18 +00:00
|
|
|
SEPARATOR
|
|
|
|
};
|
|
|
|
|
|
|
|
inline ENTRY_TYPE Type() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
inline BITMAPS GetIcon() const
|
2019-05-21 15:50:05 +00:00
|
|
|
{
|
|
|
|
return m_icon;
|
|
|
|
}
|
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
inline const TOOL_ACTION* Action() const
|
|
|
|
{
|
|
|
|
assert( m_type == ACTION );
|
|
|
|
return m_data.action;
|
|
|
|
}
|
|
|
|
|
2019-05-14 11:14:00 +00:00
|
|
|
inline ACTION_MENU* Menu() const
|
2015-04-30 09:08:18 +00:00
|
|
|
{
|
|
|
|
assert( m_type == MENU );
|
|
|
|
return m_data.menu;
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:21:10 +00:00
|
|
|
inline wxMenuItem* wxItem() const
|
|
|
|
{
|
|
|
|
assert( m_type == WXITEM );
|
|
|
|
return m_data.wxItem;
|
|
|
|
}
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
inline bool IsCheckmarkEntry() const
|
2015-04-30 09:08:18 +00:00
|
|
|
{
|
2019-05-13 20:42:40 +00:00
|
|
|
return m_isCheckmarkEntry;
|
2015-04-30 09:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const SELECTION_CONDITION& Condition() const
|
|
|
|
{
|
|
|
|
return m_condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int Order() const
|
|
|
|
{
|
|
|
|
return m_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetOrder( int aOrder )
|
|
|
|
{
|
|
|
|
m_order = aOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ENTRY_TYPE m_type;
|
2021-03-08 02:59:07 +00:00
|
|
|
BITMAPS m_icon;
|
2015-04-30 09:08:18 +00:00
|
|
|
|
2020-06-04 19:08:59 +00:00
|
|
|
// This class owns the wxItem object and needs to create, copy and delete it accordingly
|
|
|
|
// But it does not own the action nor menu item
|
2015-04-30 09:08:18 +00:00
|
|
|
union {
|
|
|
|
const TOOL_ACTION* action;
|
2019-05-14 11:14:00 +00:00
|
|
|
ACTION_MENU* menu;
|
2019-05-14 19:21:10 +00:00
|
|
|
wxMenuItem* wxItem;
|
2015-04-30 09:08:18 +00:00
|
|
|
} m_data;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Condition to be fulfilled to show the entry in menu.
|
2015-04-30 09:08:18 +00:00
|
|
|
SELECTION_CONDITION m_condition;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Order number, the higher the number the lower position it takes it is in the menu.
|
2015-04-30 09:08:18 +00:00
|
|
|
int m_order;
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
bool m_isCheckmarkEntry;
|
2015-04-30 09:08:18 +00:00
|
|
|
};
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Inserts the entry, preserving the requested order.
|
2015-04-30 09:08:18 +00:00
|
|
|
void addEntry( ENTRY aEntry );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< List of all menu entries.
|
2015-04-30 09:08:18 +00:00
|
|
|
std::list<ENTRY> m_entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONDITIONAL_MENU_H */
|