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-2019 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 <filehistory.h>
27 #include <kiface_i.h>
28 #include <menus_helpers.h>
29 #include <pgm_base.h>
30 #include <tool/conditional_menu.h>
31 #include <tool/tool_manager.h>
32 #include <tool/selection.h>
33 #include <tools/pl_actions.h>
35 #include "pl_editor_frame.h"
36 #include "pl_editor_id.h"
37 #include <widgets/wx_menubar.h>
38 
39 
41 {
43  // wxWidgets handles the Mac Application menu behind the scenes, but that means
44  // we always have to start from scratch with a new wxMenuBar.
45  wxMenuBar* oldMenuBar = GetMenuBar();
46  WX_MENUBAR* menuBar = new WX_MENUBAR();
47 
48  auto modifiedDocumentCondition = [ this ] ( const SELECTION& sel ) {
49  return IsContentModified();
50  };
51 
52  static ACTION_MENU* openRecentMenu; // Open Recent submenu, static to remember this menu
53  FILE_HISTORY& recentFiles = GetFileHistory();
54 
55  // Create the menu if it does not exist. Adding a file to/from the history
56  // will automatically refresh the menu.
57  if( !openRecentMenu )
58  {
59  openRecentMenu = new ACTION_MENU( false );
60  openRecentMenu->SetTool( selTool );
61  openRecentMenu->SetTitle( _( "Open Recent" ) );
62  openRecentMenu->SetIcon( recent_xpm );
63 
64  recentFiles.UseMenu( openRecentMenu );
65  recentFiles.AddFilesToMenu();
66  }
67 
68  //-- File menu -------------------------------------------------------
69  //
70  CONDITIONAL_MENU* fileMenu = new CONDITIONAL_MENU( false, selTool );
71 
74  fileMenu->AddMenu( openRecentMenu, FILE_HISTORY::FileHistoryNotEmpty( recentFiles ) );
75 
76  fileMenu->AddSeparator();
77  fileMenu->AddItem( ACTIONS::save, modifiedDocumentCondition );
79 
80  fileMenu->AddSeparator();
82 
83  fileMenu->AddSeparator();
84  fileMenu->AddQuitOrClose( &Kiface(), _( "Page Layout Editor" ) );
85 
86  fileMenu->Resolve();
87 
88  //-- Edit menu -------------------------------------------------------
89  //
90  CONDITIONAL_MENU* editMenu = new CONDITIONAL_MENU( false, selTool );
91 
92  auto enableUndoCondition = [ this ] ( const SELECTION& sel ) {
93  return GetUndoCommandCount() != 0;
94  };
95  auto enableRedoCondition = [ this ] ( const SELECTION& sel ) {
96  return GetRedoCommandCount() != 0;
97  };
98  auto idleCondition = [] ( const SELECTION& sel ) {
99  return !sel.Front() || sel.Front()->GetEditFlags() == 0;
100  };
101 
102  editMenu->AddItem( ACTIONS::undo, enableUndoCondition );
103  editMenu->AddItem( ACTIONS::redo, enableRedoCondition );
104 
105  editMenu->AddSeparator();
108  editMenu->AddItem( ACTIONS::paste, idleCondition );
110 
111  editMenu->Resolve();
112 
113  //-- View menu -------------------------------------------------------
114  //
115  CONDITIONAL_MENU* viewMenu = new CONDITIONAL_MENU( false, selTool );
116 
117  auto whiteBackgroundCondition = [ this ] ( const SELECTION& aSel ) {
118  return GetDrawBgColor() == WHITE;
119  };
120  auto gridShownCondition = [ this ] ( const SELECTION& aSel ) {
121  return IsGridVisible();
122  };
123  auto fullCrosshairCondition = [ this ] ( const SELECTION& aSel ) {
125  };
126 
127  viewMenu->AddSeparator();
133 
134  viewMenu->AddSeparator();
135  viewMenu->AddCheckItem( PL_ACTIONS::toggleBackground, whiteBackgroundCondition );
136  viewMenu->AddCheckItem( ACTIONS::toggleGrid, gridShownCondition );
137  viewMenu->AddCheckItem( ACTIONS::toggleCursorStyle, fullCrosshairCondition );
138 
139  viewMenu->AddSeparator();
141 
142 #ifdef __APPLE__
143  viewMenu->AddSeparator();
144 #endif
145 
146  viewMenu->Resolve();
147 
148  //-- Place menu -------------------------------------------------------
149  //
150  CONDITIONAL_MENU* placeMenu = new CONDITIONAL_MENU( false, selTool );
151 
156 
157  placeMenu->AddSeparator();
159 
160  placeMenu->Resolve();
161 
162  //-- Inspector menu -------------------------------------------------------
163  //
164  CONDITIONAL_MENU* inspectorMenu = new CONDITIONAL_MENU( false, selTool );
166 
167  inspectorMenu->Resolve();
168 
169  //-- Preferences menu --------------------------------------------------
170  //
171  CONDITIONAL_MENU* preferencesMenu = new CONDITIONAL_MENU( false, selTool );
172 
173  preferencesMenu->AddItem( wxID_PREFERENCES,
174  _( "Preferences...\tCTRL+," ),
175  _( "Show preferences for all open tools" ),
177 
178  // Language submenu
179  AddMenuLanguageList( preferencesMenu, selTool );
180 
181  preferencesMenu->Resolve();
182 
183  //-- Menubar -----------------------------------------------------------
184  //
185  menuBar->Append( fileMenu, _( "&File" ) );
186  menuBar->Append( editMenu, _( "&Edit" ) );
187  menuBar->Append( viewMenu, _( "&View" ) );
188  menuBar->Append( placeMenu, _( "&Place" ) );
189  menuBar->Append( inspectorMenu, _( "&Inspect" ) );
190  menuBar->Append( preferencesMenu, _( "P&references" ) );
191  AddStandardHelpMenu( menuBar );
192 
193  SetMenuBar( menuBar );
194  delete oldMenuBar;
195 }
void AddFilesToMenu() override
Add the files to all registered menus.
Definition: filehistory.h:98
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()
bool IsContentModified() override
Get if the page layout has been modified but not saved.
static TOOL_ACTION showInspector
Definition: pl_actions.h:76
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.
ACTION_MENU.
Definition: action_menu.h:44
static TOOL_ACTION doDelete
Definition: actions.h:74
static TOOL_ACTION drawLine
Definition: pl_actions.h:67
static SELECTION_CONDITION MoreThan(int aNumber)
Function MoreThan Creates a functor that tests if the number of selected items is greater than the va...
int GetUndoCommandCount() const
static TOOL_ACTION zoomTool
Definition: actions.h:95
static TOOL_ACTION placeImage
Definition: pl_actions.h:65
void ReCreateMenuBar() override
Recreates the menu bar.
int GetRedoCommandCount() const
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
void AddCheckItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddCheckItem()
static TOOL_ACTION placeText
Definition: pl_actions.h:64
static TOOL_ACTION print
Definition: actions.h:60
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:91
const BITMAP_OPAQUE recent_xpm[1]
Definition: recent.cpp:68
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
static TOOL_ACTION copy
Definition: actions.h:70
bool IsGridVisible() const
static TOOL_ACTION toggleGrid
Definition: actions.h:136
static TOOL_ACTION open
Definition: actions.h:53
static TOOL_ACTION save
Definition: actions.h:54
static TOOL_ACTION doNew
Definition: actions.h:47
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: action_menu.cpp:70
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:77
static TOOL_ACTION cut
Definition: actions.h:69
static TOOL_ACTION drawRectangle
Definition: pl_actions.h:66
void Resolve()
Function Resolve()
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
Definition: color4d.h:48
static TOOL_ACTION toggleBackground
Definition: pl_actions.h:75
void AddQuitOrClose(KIFACE_I *aKiface, wxString aAppname="")
Functions AddQuitOrClose()
static TOOL_ACTION redo
Definition: actions.h:68
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
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...
void AddMenuLanguageList(CONDITIONAL_MENU *aMasterMenu, TOOL_INTERACTIVE *aControlTool)
Function AddMenuLanguageList creates a menu list for language choice, and add it as submenu to Master...
static TOOL_ACTION saveAs
Definition: actions.h:55
#define _(s)
Definition: 3d_actions.cpp:33
static TOOL_ACTION zoomRedraw
Definition: actions.h:87
static TOOL_ACTION appendImportedWorksheet
Definition: pl_actions.h:68
virtual COLOR4D GetDrawBgColor() const
static TOOL_ACTION undo
Definition: actions.h:67
static TOOL_ACTION toggleCursorStyle
Definition: actions.h:98
const BITMAP_OPAQUE preference_xpm[1]
Definition: preference.cpp:66
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