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 <functional>
26 #include <eda_draw_frame.h>
27 #include <tool/actions.h>
28 #include <tool/tool_event.h>
29 #include <tool/tool_manager.h>
30 #include <tool/tool_interactive.h>
31 #include <tool/action_toolbar.h>
32 
33 
34 ACTION_TOOLBAR::ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id, const wxPoint& pos,
35  const wxSize& size, long style ) :
36  wxAuiToolBar( parent, id, pos, size, style ),
37  m_toolManager( parent->GetToolManager() )
38 {
39  Connect( wxEVT_COMMAND_TOOL_CLICKED, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolEvent ), NULL, this );
40 }
41 
42 
43 void ACTION_TOOLBAR::Add( const TOOL_ACTION& aAction, bool aIsToggleEntry )
44 {
46  int toolId = aAction.GetId() + ACTION_ID;
47 
48  AddTool( toolId, wxEmptyString, KiScaledBitmap( aAction.GetIcon(), editFrame ),
49  aAction.GetDescription(), aIsToggleEntry ? wxITEM_CHECK : wxITEM_NORMAL );
50 
51  m_toolKinds[ toolId ] = aIsToggleEntry;
52  m_toolActions[ toolId ] = &aAction;
53 }
54 
55 
56 void ACTION_TOOLBAR::AddButton( const TOOL_ACTION& aAction )
57 {
59  int toolId = aAction.GetId() + ACTION_ID;
60 
61  AddTool( toolId, wxEmptyString, KiScaledBitmap( aAction.GetIcon(), editFrame ),
62  aAction.GetName(), wxITEM_NORMAL );
63 
64  m_toolKinds[ toolId ] = false;
65  m_toolActions[ toolId ] = &aAction;
66 }
67 
68 
69 void ACTION_TOOLBAR::SetToolBitmap( const TOOL_ACTION& aAction, const wxBitmap& aBitmap )
70 {
71  int toolId = aAction.GetId() + ACTION_ID;
72  wxAuiToolBar::SetToolBitmap( toolId, aBitmap );
73 
74  // Set the disabled bitmap: we use the disabled bitmap version
75  // of aBitmap.
76  wxAuiToolBarItem* tb_item = wxAuiToolBar::FindTool( toolId );
77 
78  if( tb_item )
79  tb_item->SetDisabledBitmap( aBitmap.ConvertToDisabled() );
80 }
81 
82 
83 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aState )
84 {
85  int toolId = aAction.GetId() + ACTION_ID;
86 
87  if( m_toolKinds[ toolId ] )
88  ToggleTool( toolId, aState );
89  else
90  EnableTool( toolId, aState );
91 }
92 
93 
94 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aEnabled, bool aChecked )
95 {
96  int toolId = aAction.GetId() + ACTION_ID;
97 
98  EnableTool( toolId, aEnabled );
99  ToggleTool( toolId, aEnabled && aChecked );
100 }
101 
102 
103 void ACTION_TOOLBAR::onToolEvent( wxAuiToolBarEvent& aEvent )
104 {
105  OPT_TOOL_EVENT evt;
106  wxString menuText;
107 
108  wxEventType type = aEvent.GetEventType();
109 
110  if( type == wxEVT_COMMAND_TOOL_CLICKED && aEvent.GetId() >= ACTION_ID )
111  {
112  const auto it = m_toolActions.find( aEvent.GetId() );
113 
114  if( it != m_toolActions.end() )
115  evt = it->second->MakeEvent();
116  }
117 
118  // forward the action/update event to the TOOL_MANAGER
119  if( evt && m_toolManager )
120  {
121  evt->SetHasPosition( false );
122  m_toolManager->ProcessEvent( *evt );
123  }
124  else
125  {
126  aEvent.Skip();
127  }
128 }
129 
EDA_BASE_FRAME * GetEditFrame() const
Definition: tool_manager.h:256
void Add(const TOOL_ACTION &aAction, bool aIsToggleEntry=false)
Function Add() Adds a TOOL_ACTION-based button to the toolbar.
void onToolEvent(wxAuiToolBarEvent &aEvent)
The default tool event handler.
wxBitmap KiScaledBitmap(BITMAP_DEF aBitmap, EDA_BASE_FRAME *aWindow)
Construct a wxBitmap from a memory record, scaling it if device DPI demands it.
Definition: bitmap.cpp:116
std::map< int, const TOOL_ACTION * > m_toolActions
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.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
ACTION_TOOLBAR(EDA_BASE_FRAME *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxAUI_TB_DEFAULT_STYLE)
TOOL_MANAGER * m_toolManager
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:100
The base frame for deriving all KiCad main window classes.
Class 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.
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:548
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78