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
|
|
|
|
* @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-07-24 07:42:46 +00:00
|
|
|
#include <wx/wx.h>
|
2015-04-30 09:08:18 +00:00
|
|
|
|
|
|
|
class SELECTION_TOOL;
|
2015-07-24 07:42:46 +00:00
|
|
|
class TOOL_ACTION;
|
2015-08-03 09:53:58 +00:00
|
|
|
class TOOL_INTERACTIVE;
|
2019-08-02 18:36:14 +00:00
|
|
|
class KIFACE_I;
|
2015-04-30 09:08:18 +00:00
|
|
|
|
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:
|
|
|
|
///> Constant to indicate that we do not care about an ENTRY location in the menu.
|
|
|
|
static const int ANY_ORDER = -1;
|
|
|
|
|
2019-05-17 14:35:50 +00:00
|
|
|
CONDITIONAL_MENU( bool isContextMenu, 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
|
|
|
/**
|
|
|
|
* Function AddItem()
|
|
|
|
*
|
|
|
|
* Adds a menu entry to run a TOOL_ACTION on selected items.
|
|
|
|
* @param aAction is a menu entry to be added.
|
|
|
|
* @param aCondition is a condition that has to be fulfilled to enable the menu entry.
|
|
|
|
* @param aOrder determines location of the added item, higher numbers are put on the bottom.
|
|
|
|
* You may use ANY_ORDER here if you think it does not matter.
|
|
|
|
*/
|
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 );
|
|
|
|
|
2019-05-14 19:21:10 +00:00
|
|
|
void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
|
|
|
|
const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
/**
|
|
|
|
* Function AddCheckItem()
|
|
|
|
*
|
|
|
|
* Adds a checked menu entry to run a TOOL_ACTION on selected items.
|
|
|
|
* @param aAction is a menu entry to be added.
|
|
|
|
* @param aCondition is a condition that has to be fulfilled to check the menu entry.
|
|
|
|
* @param aOrder determines location of the added item, higher numbers are put on the bottom.
|
|
|
|
* 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 );
|
|
|
|
|
2019-05-14 19:21:10 +00:00
|
|
|
void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
|
|
|
|
const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
|
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
/**
|
|
|
|
* Function AddMenu()
|
|
|
|
*
|
2015-07-22 08:46:57 +00:00
|
|
|
* Adds 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
|
|
|
|
* or as a submenu.
|
|
|
|
* @param aCondition is a condition that has to be fulfilled to enable the submenu entry.
|
|
|
|
* @param aOrder determines location of the added menu, higher numbers are put on the bottom.
|
|
|
|
* You may use ANY_ORDER here if you think it does not matter.
|
|
|
|
*/
|
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 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function AddSeparator()
|
|
|
|
*
|
|
|
|
* Adds a separator to the menu.
|
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
|
|
|
|
2019-08-13 23:34:05 +00:00
|
|
|
/**
|
|
|
|
* Function AddClose()
|
|
|
|
*
|
|
|
|
* Add a standard close item to the menu with the accelerator key CTRL-W.
|
|
|
|
* Emits the wxID_CLOSE event.
|
|
|
|
*
|
|
|
|
* @param aAppname is the application name to append to the tooltip
|
|
|
|
*/
|
|
|
|
void AddClose( wxString aAppname = "" );
|
|
|
|
|
2019-08-02 18:36:14 +00:00
|
|
|
/**
|
|
|
|
* Functions AddQuitOrClose()
|
|
|
|
*
|
2019-08-13 23:34:05 +00:00
|
|
|
* Adds either a standard Quit or Close item to the menu. If aKiface is NULL or in
|
|
|
|
* single-instance then Quite (wxID_QUIT) is used, otherwise Close (wxID_CLOSE) is used.
|
|
|
|
*
|
|
|
|
* @param aAppname is the application name to append to the tooltip
|
2019-08-02 18:36:14 +00:00
|
|
|
*/
|
2019-08-13 23:34:05 +00:00
|
|
|
void AddQuitOrClose( KIFACE_I* aKiface, wxString aAppname = "" );
|
2019-08-02 18:36:14 +00:00
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
/**
|
2019-05-13 20:42:40 +00:00
|
|
|
* Function Evaluate()
|
2015-04-30 09:08:18 +00:00
|
|
|
*
|
2019-05-13 20:42:40 +00:00
|
|
|
* Updates 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
|
|
|
/**
|
|
|
|
* Function Resolve()
|
|
|
|
*
|
|
|
|
* Updates 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:
|
|
|
|
///> Helper class to organize menu entries.
|
|
|
|
class ENTRY
|
|
|
|
{
|
|
|
|
public:
|
2019-05-13 20:42:40 +00:00
|
|
|
ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
|
|
|
|
bool aCheckmark ) :
|
2019-05-21 15:50:05 +00:00
|
|
|
m_type( ACTION ), m_icon(nullptr),
|
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 ) :
|
2019-05-21 15:50:05 +00:00
|
|
|
m_type( MENU ), m_icon(nullptr),
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-04 19:08:59 +00:00
|
|
|
ENTRY( const wxMenuItem& aItem, const BITMAP_OPAQUE* aWxMenuBitmap,
|
|
|
|
SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
|
2019-05-21 15:50:05 +00:00
|
|
|
m_type( WXITEM ), m_icon( aWxMenuBitmap ),
|
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 ) :
|
2019-05-21 15:50:05 +00:00
|
|
|
m_type( SEPARATOR ), m_icon(nullptr),
|
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();
|
|
|
|
|
2015-04-30 09:08:18 +00:00
|
|
|
///> Possible entry types.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-21 15:50:05 +00:00
|
|
|
inline const BITMAP_OPAQUE* 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;
|
|
|
|
}
|
|
|
|
|
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;
|
2019-05-21 15:50:05 +00:00
|
|
|
const BITMAP_OPAQUE* 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;
|
|
|
|
|
|
|
|
///> Condition to be fulfilled to show the entry in menu.
|
|
|
|
SELECTION_CONDITION m_condition;
|
|
|
|
|
|
|
|
///> Order number, the higher the number the lower position it takes it is in the menu.
|
|
|
|
int m_order;
|
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
bool m_isCheckmarkEntry;
|
2015-04-30 09:08:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///> Inserts the entry, preserving the requested order.
|
|
|
|
void addEntry( ENTRY aEntry );
|
|
|
|
|
|
|
|
///> List of all menu entries.
|
|
|
|
std::list<ENTRY> m_entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONDITIONAL_MENU_H */
|