KiCad PCB EDA Suite
menubar_libedit.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2009 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <menus_helpers.h>
27 #include <pgm_base.h>
28 #include <tool/conditional_menu.h>
29 #include <tool/tool_manager.h>
30 #include <tools/ee_actions.h>
32 #include <lib_manager.h>
33 #include "lib_edit_frame.h"
34 #include <widgets/wx_menubar.h>
35 
36 
38 {
40  // wxWidgets handles the Mac Application menu behind the scenes, but that means
41  // we always have to start from scratch with a new wxMenuBar.
42  wxMenuBar* oldMenuBar = GetMenuBar();
43  WX_MENUBAR* menuBar = new WX_MENUBAR();
44 
45  auto modifiedDocumentCondition = [ this ] ( const SELECTION& sel ) {
46  LIB_ID libId = getTargetLibId();
47  const wxString& libName = libId.GetLibNickname();
48  const wxString& partName = libId.GetLibItemName();
49  bool readOnly = libName.IsEmpty() || m_libMgr->IsLibraryReadOnly( libName );
50 
51  if( partName.IsEmpty() )
52  return ( !readOnly && m_libMgr->IsLibraryModified( libName ) );
53  else
54  return ( !readOnly && m_libMgr->IsPartModified( partName, libName ) );
55  };
56 
57  auto saveAllEnableCondition = [this] ( const SELECTION& sel ) {
58  return m_libMgr->HasModifications();
59  };
60 
61  //-- File menu -----------------------------------------------
62  //
63  CONDITIONAL_MENU* fileMenu = new CONDITIONAL_MENU( false, selTool );
64 
68 
69  fileMenu->AddSeparator();
70  fileMenu->AddItem( ACTIONS::save, modifiedDocumentCondition );
72  fileMenu->AddItem( ACTIONS::saveAll, saveAllEnableCondition );
73  fileMenu->AddItem( ACTIONS::revert, modifiedDocumentCondition );
74 
75  fileMenu->AddSeparator();
77 
78  // Export submenu
79  ACTION_MENU* submenuExport = new ACTION_MENU( false );
80  submenuExport->SetTool( selTool );
81  submenuExport->SetTitle( _( "Export" ) );
82  submenuExport->SetIcon( export_xpm );
83  submenuExport->Add( EE_ACTIONS::exportSymbol );
84  submenuExport->Add( EE_ACTIONS::exportSymbolView );
85  submenuExport->Add( EE_ACTIONS::exportSymbolAsSVG );
86  fileMenu->AddMenu( submenuExport, EE_CONDITIONS::ShowAlways );
87 
88  fileMenu->AddSeparator();
89  fileMenu->AddClose( _( "Library Editor" ) );
90 
91  fileMenu->Resolve();
92 
93  //-- Edit menu -----------------------------------------------
94  //
95  CONDITIONAL_MENU* editMenu = new CONDITIONAL_MENU( false, selTool );
96 
97  auto enableUndoCondition = [ this ] ( const SELECTION& sel ) {
98  return m_my_part && GetScreen() && GetScreen()->GetUndoCommandCount() != 0;
99  };
100  auto enableRedoCondition = [ this ] ( const SELECTION& sel ) {
101  return m_my_part && GetScreen() && GetScreen()->GetRedoCommandCount() != 0;
102  };
103  auto haveSymbolCondition = [ this ] ( const SELECTION& sel ) {
104  return m_my_part != nullptr;
105  };
106  auto isRootSymbolCondition = [ this ] ( const SELECTION& sel ) {
107  return m_my_part != nullptr && m_my_part->IsRoot();
108  };
109 
110  editMenu->AddItem( ACTIONS::undo, enableUndoCondition );
111  editMenu->AddItem( ACTIONS::redo, enableRedoCondition );
112 
113  editMenu->AddSeparator();
119 
120  editMenu->AddSeparator();
121  editMenu->AddItem( EE_ACTIONS::symbolProperties, haveSymbolCondition );
122  editMenu->AddItem( EE_ACTIONS::pinTable, isRootSymbolCondition );
123 
124  editMenu->Resolve();
125 
126  //-- View menu -----------------------------------------------
127  //
128  CONDITIONAL_MENU* viewMenu = new CONDITIONAL_MENU( false, selTool );
129 
130  auto gridShownCondition = [ this ] ( const SELECTION& aSel ) {
131  return IsGridVisible();
132  };
133  auto imperialUnitsCondition = [this]( const SELECTION& aSel ) {
134  return GetUserUnits() == EDA_UNITS::INCHES;
135  };
136  auto metricUnitsCondition = [this]( const SELECTION& aSel ) {
138  };
139  auto fullCrosshairCondition = [ this ] ( const SELECTION& aSel ) {
141  };
142  auto compTreeShownCondition = [ this ] ( const SELECTION& aSel ) {
143  return IsSearchTreeShown();
144  };
145 
147 
148  viewMenu->AddSeparator();
154 
155  viewMenu->AddSeparator();
156  viewMenu->AddCheckItem( ACTIONS::toggleGrid, gridShownCondition );
158 
159  // Units submenu
160  CONDITIONAL_MENU* unitsSubMenu = new CONDITIONAL_MENU( false, selTool );
161  unitsSubMenu->SetTitle( _( "&Units" ) );
162  unitsSubMenu->SetIcon( unit_mm_xpm );
163  unitsSubMenu->AddCheckItem( ACTIONS::imperialUnits, imperialUnitsCondition );
164  unitsSubMenu->AddCheckItem( ACTIONS::metricUnits, metricUnitsCondition );
165  viewMenu->AddMenu( unitsSubMenu );
166 
167  viewMenu->AddCheckItem( ACTIONS::toggleCursorStyle, fullCrosshairCondition );
168 
169  viewMenu->AddSeparator();
170  viewMenu->AddCheckItem( EE_ACTIONS::showComponentTree, compTreeShownCondition );
171 
172  viewMenu->Resolve();
173 
174  //-- Place menu -----------------------------------------------
175  //
176  CONDITIONAL_MENU* placeMenu = new CONDITIONAL_MENU( false, selTool );
177 
178  placeMenu->AddItem( EE_ACTIONS::placeSymbolPin, isRootSymbolCondition );
179  placeMenu->AddItem( EE_ACTIONS::placeSymbolText, isRootSymbolCondition );
180  placeMenu->AddItem( EE_ACTIONS::drawSymbolRectangle, isRootSymbolCondition );
181  placeMenu->AddItem( EE_ACTIONS::drawSymbolCircle, isRootSymbolCondition );
182  placeMenu->AddItem( EE_ACTIONS::drawSymbolArc, isRootSymbolCondition );
183  placeMenu->AddItem( EE_ACTIONS::drawSymbolLines, isRootSymbolCondition );
184 
185  placeMenu->Resolve();
186 
187  //-- Inspect menu -----------------------------------------------
188  //
189  CONDITIONAL_MENU* inspectMenu = new CONDITIONAL_MENU( false, selTool );
190 
191  inspectMenu->AddItem( EE_ACTIONS::showDatasheet, haveSymbolCondition );
192  inspectMenu->AddItem( EE_ACTIONS::runERC, isRootSymbolCondition );
193 
194  inspectMenu->Resolve();
195 
196  //-- Preferences menu -----------------------------------------------
197  //
198  CONDITIONAL_MENU* prefsMenu = new CONDITIONAL_MENU( false, selTool );
199 
200  auto acceleratedGraphicsCondition = [ this ] ( const SELECTION& aSel ) {
202  };
203  auto standardGraphicsCondition = [ this ] ( const SELECTION& aSel ) {
205  };
206 
209  prefsMenu->AddItem( wxID_PREFERENCES,
210  _( "Preferences...\tCTRL+," ),
211  _( "Show preferences for all open tools" ),
213 
214  prefsMenu->AddSeparator();
215  AddMenuLanguageList( prefsMenu, selTool );
216 
217  prefsMenu->AddSeparator();
218  prefsMenu->AddCheckItem( ACTIONS::acceleratedGraphics, acceleratedGraphicsCondition );
219  prefsMenu->AddCheckItem( ACTIONS::standardGraphics, standardGraphicsCondition );
220 
221  prefsMenu->Resolve();
222 
223  //-- Menubar -------------------------------------------------------------
224  //
225  menuBar->Append( fileMenu, _( "&File" ) );
226  menuBar->Append( editMenu, _( "&Edit" ) );
227  menuBar->Append( viewMenu, _( "&View" ) );
228  menuBar->Append( placeMenu, _( "&Place" ) );
229  menuBar->Append( inspectMenu, _( "&Inspect" ) );
230  menuBar->Append( prefsMenu, _( "P&references" ) );
231  AddStandardHelpMenu( menuBar );
232  menuBar->SetDoubleBuffered( true );
233 
234  SetMenuBar( menuBar );
235  delete oldMenuBar;
236 }
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddMenu()
static TOOL_ACTION pinTable
Definition: ee_actions.h:145
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Function Add() Adds a wxWidgets-style entry to the menu.
static TOOL_ACTION showDatasheet
Inspection and Editing.
Definition: ee_actions.h:139
static TOOL_ACTION placeSymbolText
Definition: ee_actions.h:102
static TOOL_ACTION drawSymbolLines
Definition: ee_actions.h:106
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
static TOOL_ACTION zoomInCenter
Definition: actions.h:90
KIGFX::GAL_DISPLAY_OPTIONS & GetGalDisplayOptions()
Return a reference to the gal rendering options used by GAL for rendering.
static TOOL_ACTION runERC
Definition: ee_actions.h:140
ACTION_MENU.
Definition: action_menu.h:44
static TOOL_ACTION doDelete
Definition: actions.h:74
static TOOL_ACTION zoomTool
Definition: actions.h:95
int GetUndoCommandCount() const
Definition: base_screen.h:160
LIB_MANAGER * m_libMgr
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
static TOOL_ACTION showSymbolLibTable
Definition: actions.h:168
static TOOL_ACTION standardGraphics
Definition: actions.h:160
void AddCheckItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddCheckItem()
static TOOL_ACTION showComponentTree
Definition: ee_actions.h:194
static SELECTION_CONDITION Idle
Wrapper around a wxMenuBar object that prevents the accelerator table from being used.
Definition: wx_menubar.h:45
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
static TOOL_ACTION imperialUnits
Definition: actions.h:140
static bool NotEmpty(const SELECTION &aSelection)
Function NotEmpty Tests if there are any items selected.
LIB_PART * m_my_part
static TOOL_ACTION zoomOutCenter
Definition: actions.h:91
static TOOL_ACTION saveCopyAs
Definition: actions.h:56
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
static TOOL_ACTION drawSymbolRectangle
Definition: ee_actions.h:103
static TOOL_ACTION copy
Definition: actions.h:70
static TOOL_ACTION placeSymbolPin
Definition: ee_actions.h:101
void ReCreateMenuBar() override
Recreates the menu bar.
bool IsGridVisible() const
static TOOL_ACTION toggleGrid
Definition: actions.h:136
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.
static TOOL_ACTION save
Definition: actions.h:54
static TOOL_ACTION symbolProperties
Definition: ee_actions.h:144
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: action_menu.cpp:70
void AddStandardHelpMenu(wxMenuBar *aMenuBar)
Adds the standard KiCad help menu to the menubar.
const BITMAP_OPAQUE export_xpm[1]
Definition: export.cpp:75
static TOOL_ACTION cut
Definition: actions.h:69
static TOOL_ACTION drawSymbolCircle
Definition: ee_actions.h:104
static TOOL_ACTION addLibrary
Definition: actions.h:52
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
static TOOL_ACTION importSymbol
Definition: ee_actions.h:172
static TOOL_ACTION newLibrary
Definition: actions.h:51
void Resolve()
Function Resolve()
static TOOL_ACTION exportSymbolView
Definition: ee_actions.h:197
const BITMAP_OPAQUE unit_mm_xpm[1]
Definition: unit_mm.cpp:28
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
static TOOL_ACTION newSymbol
Definition: ee_actions.h:165
static TOOL_ACTION exportSymbol
Definition: ee_actions.h:173
static TOOL_ACTION metricUnits
Definition: actions.h:141
static TOOL_ACTION redo
Definition: actions.h:68
void AddClose(wxString aAppname="")
Function AddClose()
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the menu.
Definition: action_menu.cpp:88
see class PGM_BASE
void AddMenuLanguageList(CONDITIONAL_MENU *aMasterMenu, TOOL_INTERACTIVE *aControlTool)
Function AddMenuLanguageList creates a menu list for language choice, and add it as submenu to Master...
#define _(s)
Definition: 3d_actions.cpp:33
static TOOL_ACTION zoomRedraw
Definition: actions.h:87
static TOOL_ACTION acceleratedGraphics
Definition: actions.h:159
LIB_ID getTargetLibId() const
Returns either the part selected in the component tree, if context menu is active or the currently mo...
static TOOL_ACTION drawSymbolArc
Definition: ee_actions.h:105
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
bool HasModifications() const
Definition: lib_manager.cpp:66
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.
static TOOL_ACTION gridProperties
Definition: actions.h:137
static TOOL_ACTION undo
Definition: actions.h:67
GAL_TYPE GetBackend() const
Function GetBackend Returns the type of backend currently used by GAL canvas.
static TOOL_ACTION toggleCursorStyle
Definition: actions.h:98
static TOOL_ACTION saveAll
Definition: actions.h:57
static TOOL_ACTION showSymbolBrowser
Definition: actions.h:153
const BITMAP_OPAQUE preference_xpm[1]
Definition: preference.cpp:66
static TOOL_ACTION configurePaths
Definition: actions.h:167
int GetRedoCommandCount() const
Definition: base_screen.h:165
static TOOL_ACTION exportSymbolAsSVG
Definition: ee_actions.h:198
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
bool m_fullscreenCursor
Fullscreen crosshair or small cross
static TOOL_ACTION paste
Definition: actions.h:71
bool IsLibraryReadOnly(const wxString &aLibrary) const
Returns true if the library is stored in a read-only file.
static TOOL_ACTION duplicate
Definition: actions.h:73
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
static TOOL_ACTION revert
Definition: actions.h:58