KiCad PCB EDA Suite
action_toolbar.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) 2019 CERN
5  * Copyright (C) 2019 KiCad Developers, see CHANGELOG.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <bitmaps.h>
26 #include <eda_draw_frame.h>
27 #include <functional>
28 #include <memory>
29 #include <pgm_base.h>
31 #include <tool/action_toolbar.h>
32 #include <tool/actions.h>
33 #include <tool/tool_event.h>
34 #include <tool/tool_interactive.h>
35 #include <tool/tool_manager.h>
36 
37 
38 ACTION_TOOLBAR::ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id, const wxPoint& pos,
39  const wxSize& size, long style ) :
40  wxAuiToolBar( parent, id, pos, size, style ),
41  m_toolManager( parent->GetToolManager() )
42 {
43  Connect( wxEVT_COMMAND_TOOL_CLICKED, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolEvent ),
44  NULL, this );
45  Connect( wxEVT_AUITOOLBAR_RIGHT_CLICK, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolRightClick ),
46  NULL, this );
47 }
48 
49 
51 {
52  // Delete all the menus
53  for( auto it = m_toolMenus.begin(); it != m_toolMenus.end(); it++ )
54  delete it->second;
55 
56  m_toolMenus.clear();
57 }
58 
59 
60 void ACTION_TOOLBAR::Add( const TOOL_ACTION& aAction, bool aIsToggleEntry )
61 {
62  wxWindow* parent = dynamic_cast<wxWindow*>( m_toolManager->GetToolHolder() );
63  int toolId = aAction.GetId() + ACTION_ID;
64 
65  AddTool( toolId, wxEmptyString, KiScaledBitmap( aAction.GetIcon(), parent ),
66  aAction.GetDescription(), aIsToggleEntry ? wxITEM_CHECK : wxITEM_NORMAL );
67 
68  m_toolKinds[ toolId ] = aIsToggleEntry;
69  m_toolActions[ toolId ] = &aAction;
70 }
71 
72 
73 void ACTION_TOOLBAR::AddButton( const TOOL_ACTION& aAction )
74 {
75  wxWindow* parent = dynamic_cast<wxWindow*>( m_toolManager->GetToolHolder() );
76  int toolId = aAction.GetId() + ACTION_ID;
77 
78  AddTool( toolId, wxEmptyString, KiScaledBitmap( aAction.GetIcon(), parent ),
79  aAction.GetName(), wxITEM_NORMAL );
80 
81  m_toolKinds[ toolId ] = false;
82  m_toolActions[ toolId ] = &aAction;
83 }
84 
85 
86 void ACTION_TOOLBAR::AddScaledSeparator( wxWindow* aWindow )
87 {
88  int scale = Pgm().GetCommonSettings()->m_Appearance.icon_scale;
89 
90  if( scale == 0 )
91  scale = KiIconScale( aWindow );
92 
93  if( scale > 4 )
94  AddSpacer( 16 * ( scale - 4 ) / 4 );
95 
96  AddSeparator();
97 
98  if( scale > 4 )
99  AddSpacer( 16 * ( scale - 4 ) / 4 );
100 }
101 
102 
104 {
105  int toolId = aAction.GetId() + ACTION_ID;
106 
107  // If this is replacing an existing menu, delete the existing menu before adding the new one
108  const auto it = m_toolMenus.find( toolId );
109 
110  if( it != m_toolMenus.end() )
111  {
112  // Don't delete it if it is the same menu, just ignore this call
113  if( it->second == aMenu )
114  return;
115 
116  delete it->second;
117  }
118 
119  m_toolMenus[toolId] = aMenu;
120 }
121 
122 
124 {
125  // Delete all the menus
126  for( auto it = m_toolMenus.begin(); it != m_toolMenus.end(); it++ )
127  delete it->second;
128 
129  // Clear the menu items and the actual toolbar
130  m_toolMenus.clear();
131  Clear();
132 }
133 
134 
135 void ACTION_TOOLBAR::SetToolBitmap( const TOOL_ACTION& aAction, const wxBitmap& aBitmap )
136 {
137  int toolId = aAction.GetId() + ACTION_ID;
138  wxAuiToolBar::SetToolBitmap( toolId, aBitmap );
139 
140  // Set the disabled bitmap: we use the disabled bitmap version
141  // of aBitmap.
142  wxAuiToolBarItem* tb_item = wxAuiToolBar::FindTool( toolId );
143 
144  if( tb_item )
145  tb_item->SetDisabledBitmap( aBitmap.ConvertToDisabled() );
146 }
147 
148 
149 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aState )
150 {
151  int toolId = aAction.GetId() + ACTION_ID;
152 
153  if( m_toolKinds[ toolId ] )
154  ToggleTool( toolId, aState );
155  else
156  EnableTool( toolId, aState );
157 }
158 
159 
160 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aEnabled, bool aChecked )
161 {
162  int toolId = aAction.GetId() + ACTION_ID;
163 
164  EnableTool( toolId, aEnabled );
165  ToggleTool( toolId, aEnabled && aChecked );
166 }
167 
168 
169 void ACTION_TOOLBAR::onToolEvent( wxAuiToolBarEvent& aEvent )
170 {
171  OPT_TOOL_EVENT evt;
172  wxString menuText;
173 
174  wxEventType type = aEvent.GetEventType();
175 
176  if( type == wxEVT_COMMAND_TOOL_CLICKED && aEvent.GetId() >= ACTION_ID )
177  {
178  const auto it = m_toolActions.find( aEvent.GetId() );
179 
180  if( it != m_toolActions.end() )
181  evt = it->second->MakeEvent();
182  }
183 
184  // forward the action/update event to the TOOL_MANAGER
185  if( evt && m_toolManager )
186  {
187  evt->SetHasPosition( false );
188  m_toolManager->ProcessEvent( *evt );
189  }
190  else
191  {
192  aEvent.Skip();
193  }
194 }
195 
196 
197 void ACTION_TOOLBAR::onToolRightClick( wxAuiToolBarEvent& aEvent )
198 {
199  int toolId = aEvent.GetToolId();
200 
201  // This means the event was not on a button
202  if( toolId == -1 )
203  return;
204 
205  const auto it = m_toolMenus.find( aEvent.GetId() );
206 
207  if( it == m_toolMenus.end() )
208  return;
209 
210  // Update and show the menu
211  CONDITIONAL_MENU* menu = it->second;
212  SELECTION dummySel;
213 
214  menu->Evaluate( dummySel );
215  menu->UpdateAll();
216  PopupMenu( menu );
217 
218  // Remove hovered item when the menu closes, otherwise it remains hovered even if the
219  // mouse is not on the toolbar
220  SetHoverItem( nullptr );
221 }
std::map< int, CONDITIONAL_MENU * > m_toolMenus
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
void AddScaledSeparator(wxWindow *aWindow)
Add a separator that introduces space on either side to not squash the tools when scaled.
int KiIconScale(wxWindow *aWindow)
Return the automatic scale factor that would be used for a given window by KiScaledBitmap and KiScale...
Definition: bitmap.cpp:90
void onToolRightClick(wxAuiToolBarEvent &aEvent)
Handle a right-click on a menu item
void Add(const TOOL_ACTION &aAction, bool aIsToggleEntry=false)
Function Add() Adds a TOOL_ACTION-based button to the toolbar.
void UpdateAll()
Function UpdateAll() Runs update handlers for the menu and its submenus.
void onToolEvent(wxAuiToolBarEvent &aEvent)
The default tool event handler.
std::map< int, const TOOL_ACTION * > m_toolActions
wxBitmap KiScaledBitmap(BITMAP_DEF aBitmap, wxWindow *aWindow)
Construct a wxBitmap from a memory record, scaling it if device DPI demands it.
Definition: bitmap.cpp:116
const BITMAP_OPAQUE * GetIcon() const
Returns an icon associated with the action.
Definition: tool_action.h:149
wxString GetDescription() const
Definition: tool_action.cpp:83
void AddButton(const TOOL_ACTION &aAction)
Function AddButton() Adds a large button such as used in the Kicad Manager Frame's launch bar.
#define NULL
void AddToolContextMenu(const TOOL_ACTION &aAction, CONDITIONAL_MENU *aMenu)
Add a context menu to a specific tool item on the toolbar.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
void Evaluate(SELECTION &aSelection)
Function Evaluate()
ACTION_TOOLBAR(EDA_BASE_FRAME *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxAUI_TB_DEFAULT_STYLE)
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:295
TOOL_MANAGER * m_toolManager
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:100
const int scale
see class PGM_BASE
The base frame for deriving all KiCad main window classes.
TOOL_ACTION.
Definition: tool_action.h:46
void SetToolBitmap(const TOOL_ACTION &aAction, const wxBitmap &aBitmap)
Function SetToolBitmap() Updates the bitmap of a particular tool.
void Toggle(const TOOL_ACTION &aAction, bool aState)
Applies the default toggle action.
virtual ~ACTION_TOOLBAR()
static const int ACTION_ID
Tool items with ID higher than that are considered TOOL_ACTIONs
std::map< int, bool > m_toolKinds
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:556
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78
void ClearToolbar()
Clear the toolbar and remove all associated menus.