kicad/include/tool/conditional_menu.h

258 lines
8.3 KiB
C
Raw Normal View History

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
#include <tool/selection_conditions.h>
#include <tool/action_menu.h>
#include <list>
2015-04-30 09:08:18 +00:00
class PCB_SELECTION_TOOL;
class TOOL_ACTION;
class TOOL_INTERACTIVE;
2015-04-30 09:08:18 +00:00
enum class BITMAPS : unsigned int;
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;
CONDITIONAL_MENU( TOOL_INTERACTIVE* aTool );
ACTION_MENU* create() const override;
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.
*
2015-04-30 09:08:18 +00:00
* @param aAction is a menu entry to be added.
* @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
*/
void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
2015-04-30 09:08:18 +00:00
int aOrder = ANY_ORDER );
void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
/**
2020-12-27 00:41:04 +00:00
* Add a checked menu entry to run a TOOL_ACTION on selected items.
*
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.
*
* @param aAction is a menu entry to be added.
* @param aCondition is a condition that has to be fulfilled to show the menu entry in the menu.
* @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.
*/
void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
int aOrder = ANY_ORDER );
void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
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.
*
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
*/
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.
*
* @param aOrder determines location of the separator, higher numbers are put on the bottom.
2015-04-30 09:08:18 +00:00
*/
void AddSeparator( int aOrder = ANY_ORDER );
2015-04-30 09:08:18 +00:00
void AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
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
*/
void Evaluate( SELECTION& aSelection );
2015-04-30 09:08:18 +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
* 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:
ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
bool aCheckmark ) :
m_type( ACTION ),
m_icon( static_cast<BITMAPS>( 0 ) ),
2023-12-18 17:19:15 +00:00
m_condition( std::move( aCondition ) ),
m_order( aOrder ),
m_isCheckmarkEntry( aCheckmark )
2015-04-30 09:08:18 +00:00
{
m_data.action = aAction;
}
ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
m_type( MENU ),
m_icon( static_cast<BITMAPS>( 0 ) ),
2023-12-18 17:19:15 +00:00
m_condition( std::move( aCondition ) ),
m_order( aOrder ),
m_isCheckmarkEntry( false )
2015-04-30 09:08:18 +00:00
{
m_data.menu = aMenu;
}
ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
m_type( WXITEM ),
m_icon( aBitmap ),
2023-12-18 17:19:15 +00:00
m_condition( std::move( aCondition ) ),
m_order( aOrder ),
m_isCheckmarkEntry( aCheckmark )
{
m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
aItem.GetHelp(), aItem.GetKind() );
}
2015-04-30 09:08:18 +00:00
// Separator
ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
m_type( SEPARATOR ),
m_icon( static_cast<BITMAPS>( 0 ) ),
m_data(),
2023-12-18 17:19:15 +00:00
m_condition( std::move( aCondition ) ),
m_order( aOrder ),
m_isCheckmarkEntry( false )
2015-04-30 09:08:18 +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,
WXITEM,
2015-04-30 09:08:18 +00:00
SEPARATOR
};
inline ENTRY_TYPE Type() const
{
return m_type;
}
inline BITMAPS GetIcon() const
{
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;
}
inline ACTION_MENU* Menu() const
2015-04-30 09:08:18 +00:00
{
assert( m_type == MENU );
return m_data.menu;
}
inline wxMenuItem* wxItem() const
{
assert( m_type == WXITEM );
return m_data.wxItem;
}
inline bool IsCheckmarkEntry() const
2015-04-30 09:08:18 +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;
BITMAPS m_icon;
2015-04-30 09:08:18 +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;
ACTION_MENU* menu;
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;
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 */