KiCad PCB EDA Suite
pagelayout_editor/menubar.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) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  * Copyright (C) 2013-2019 CERN
6  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
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 <bitmaps.h>
27 #include <filehistory.h>
28 #include <kiface_i.h>
29 #include <menus_helpers.h>
30 #include <tool/action_manager.h>
31 #include <tool/action_menu.h>
32 #include <tool/tool_manager.h>
33 #include <widgets/wx_menubar.h>
34 
35 #include "pl_editor_frame.h"
36 #include "pl_editor_id.h"
37 #include "tools/pl_actions.h"
39 
40 
42 {
44  // wxWidgets handles the Mac Application menu behind the scenes, but that means
45  // we always have to start from scratch with a new wxMenuBar.
46  wxMenuBar* oldMenuBar = GetMenuBar();
47  WX_MENUBAR* menuBar = new WX_MENUBAR();
48 
49  static ACTION_MENU* openRecentMenu; // Open Recent submenu, static to remember this menu
50  FILE_HISTORY& recentFiles = GetFileHistory();
51 
52  // Create the menu if it does not exist. Adding a file to/from the history
53  // will automatically refresh the menu.
54  if( !openRecentMenu )
55  {
56  openRecentMenu = new ACTION_MENU( false, selTool );
57  openRecentMenu->SetTitle( _( "Open Recent" ) );
58  openRecentMenu->SetIcon( BITMAPS::recent );
59 
60  recentFiles.UseMenu( openRecentMenu );
61  recentFiles.AddFilesToMenu();
62  }
63 
64  //-- File menu -------------------------------------------------------
65  //
66  ACTION_MENU* fileMenu = new ACTION_MENU( false, selTool );
67 
68  fileMenu->Add( ACTIONS::doNew );
69  fileMenu->Add( ACTIONS::open );
70 
71  wxMenuItem* item = fileMenu->Add( openRecentMenu );
72 
73  // Add the file menu condition here since it needs the item ID for the submenu
74  ACTION_CONDITIONS cond;
75  cond.Enable( FILE_HISTORY::FileHistoryNotEmpty( recentFiles ) );
76  RegisterUIUpdateHandler( item->GetId(), cond );
77 
78  fileMenu->AppendSeparator();
79  fileMenu->Add( ACTIONS::save );
80  fileMenu->Add( ACTIONS::saveAs );
81 
82  fileMenu->AppendSeparator();
83  fileMenu->Add( ACTIONS::print );
84 
85  fileMenu->AppendSeparator();
86  fileMenu->AddQuitOrClose( &Kiface(), _( "Drawing Sheet Editor" ) );
87 
88  //-- Edit menu -------------------------------------------------------
89  //
90  ACTION_MENU* editMenu = new ACTION_MENU( false, selTool );
91 
92  editMenu->Add( ACTIONS::undo );
93  editMenu->Add( ACTIONS::redo );
94 
95  editMenu->AppendSeparator();
96  editMenu->Add( ACTIONS::cut );
97  editMenu->Add( ACTIONS::copy );
98  editMenu->Add( ACTIONS::paste );
99  editMenu->Add( ACTIONS::doDelete );
100 
101  //-- View menu -------------------------------------------------------
102  //
103  ACTION_MENU* viewMenu = new ACTION_MENU( false, selTool );
104 
105  viewMenu->Add( ACTIONS::zoomInCenter );
106  viewMenu->Add( ACTIONS::zoomOutCenter );
107  viewMenu->Add( ACTIONS::zoomFitScreen );
108  viewMenu->Add( ACTIONS::zoomTool );
109  viewMenu->Add( ACTIONS::zoomRedraw );
110 
111  viewMenu->AppendSeparator();
113 
114  // Units submenu
115  ACTION_MENU* unitsSubMenu = new ACTION_MENU( false, selTool );
116  unitsSubMenu->SetTitle( _( "&Units" ) );
117  unitsSubMenu->SetIcon( BITMAPS::unit_mm );
118  unitsSubMenu->Add( ACTIONS::inchesUnits, ACTION_MENU::CHECK );
119  unitsSubMenu->Add( ACTIONS::milsUnits, ACTION_MENU::CHECK );
121  viewMenu->Add( unitsSubMenu );
122 
124 
125  viewMenu->AppendSeparator();
126  viewMenu->Add( PL_ACTIONS::previewSettings );
127 
128 #ifdef __APPLE__
129  // Add a separator only on macOS because the OS adds menu items to the view menu after ours
130  viewMenu->AppendSeparator();
131 #endif
132 
133  //-- Place menu -------------------------------------------------------
134  //
135  ACTION_MENU* placeMenu = new ACTION_MENU( false, selTool );
136 
137  placeMenu->Add( PL_ACTIONS::drawLine );
138  placeMenu->Add( PL_ACTIONS::drawRectangle );
139  placeMenu->Add( PL_ACTIONS::placeText );
140  placeMenu->Add( PL_ACTIONS::placeImage );
141 
142  placeMenu->AppendSeparator();
144 
145  //-- Inspector menu -------------------------------------------------------
146  //
147  ACTION_MENU* inspectorMenu = new ACTION_MENU( false, selTool );
148  inspectorMenu->Add( PL_ACTIONS::showInspector );
149 
150  //-- Preferences menu --------------------------------------------------
151  //
152  ACTION_MENU* preferencesMenu = new ACTION_MENU( false, selTool );
153 
154  // We can't use ACTIONS::showPreferences yet because wxWidgets moves this on
155  // Mac, and it needs the wxID_PREFERENCES id to find it.
156  preferencesMenu->Add( _( "Preferences..." ) + "\tCtrl+,",
157  _( "Show preferences for all open tools" ),
158  wxID_PREFERENCES,
160 
161  // Language submenu
162  AddMenuLanguageList( preferencesMenu, selTool );
163 
164  //-- Menubar -----------------------------------------------------------
165  //
166  menuBar->Append( fileMenu, _( "&File" ) );
167  menuBar->Append( editMenu, _( "&Edit" ) );
168  menuBar->Append( viewMenu, _( "&View" ) );
169  menuBar->Append( placeMenu, _( "&Place" ) );
170  menuBar->Append( inspectorMenu, _( "&Inspect" ) );
171  menuBar->Append( preferencesMenu, _( "P&references" ) );
172  AddStandardHelpMenu( menuBar );
173 
174  SetMenuBar( menuBar );
175  delete oldMenuBar;
176 }
void AddFilesToMenu() override
Add the files to all registered menus.
Definition: filehistory.h:98
static TOOL_ACTION showInspector
Definition: pl_actions.h:72
static TOOL_ACTION zoomInCenter
Definition: actions.h:93
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
static TOOL_ACTION doDelete
Definition: actions.h:72
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
Definition: action_menu.cpp:73
static TOOL_ACTION drawLine
Definition: pl_actions.h:61
static TOOL_ACTION zoomTool
Definition: actions.h:99
static TOOL_ACTION placeImage
Definition: pl_actions.h:59
void ReCreateMenuBar() override
Recreates the menu bar.
static TOOL_ACTION zoomFitScreen
Definition: actions.h:96
static TOOL_ACTION millimetersUnits
Definition: actions.h:146
virtual void RegisterUIUpdateHandler(int aID, const ACTION_CONDITIONS &aConditions) override
Register a UI update handler for the control with ID aID.
static TOOL_ACTION placeText
Definition: pl_actions.h:58
static TOOL_ACTION print
Definition: actions.h:57
Wrapper around a wxMenuBar object that prevents the accelerator table from being used.
Definition: wx_menubar.h:45
This class implements a file history object to store a list of files, that can then be added to a men...
Definition: filehistory.h:42
static TOOL_ACTION zoomOutCenter
Definition: actions.h:94
ACTION_CONDITIONS & Enable(const SELECTION_CONDITION &aCondition)
static TOOL_ACTION copy
Definition: actions.h:67
static TOOL_ACTION toggleGrid
Definition: actions.h:140
static TOOL_ACTION open
Definition: actions.h:50
static TOOL_ACTION inchesUnits
Definition: actions.h:144
static TOOL_ACTION save
Definition: actions.h:51
static TOOL_ACTION doNew
Definition: actions.h:44
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
void AddStandardHelpMenu(wxMenuBar *aMenuBar)
Adds the standard KiCad help menu to the menubar.
static TOOL_ACTION previewSettings
Definition: pl_actions.h:73
static TOOL_ACTION cut
Definition: actions.h:66
#define _(s)
static TOOL_ACTION drawRectangle
Definition: pl_actions.h:60
void AddMenuLanguageList(ACTION_MENU *aMasterMenu, TOOL_INTERACTIVE *aControlTool)
Function AddMenuLanguageList creates a menu list for language choice, and add it as submenu to Master...
static TOOL_ACTION milsUnits
Definition: actions.h:145
static TOOL_ACTION redo
Definition: actions.h:65
static constexpr bool CHECK
Definition: action_menu.h:183
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:158
void SetTitle(const wxString &aTitle) override
Set title for the menu.
Definition: action_menu.cpp:87
FILE_HISTORY & GetFileHistory()
Get the frame's main file history.
static SELECTION_CONDITION FileHistoryNotEmpty(const FILE_HISTORY &aHistory)
Create a SELECTION_CONDITION that can be used to enable a menu item when the file history has items i...
static TOOL_ACTION saveAs
Definition: actions.h:52
void AddQuitOrClose(KIFACE_I *aKiface, wxString aAppname="")
Add either a standard Quit or Close item to the menu.
static TOOL_ACTION appendImportedDrawingSheet
Definition: pl_actions.h:62
static TOOL_ACTION zoomRedraw
Definition: actions.h:90
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
static TOOL_ACTION undo
Definition: actions.h:64
static TOOL_ACTION toggleCursorStyle
Definition: actions.h:102
static TOOL_ACTION paste
Definition: actions.h:68