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 KiCad Developers, see CHANGELOG.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <functional>
25 #include <draw_frame.h>
26 #include <tool/actions.h>
27 #include <tool/tool_event.h>
28 #include <tool/tool_manager.h>
29 #include <tool/tool_interactive.h>
30 #include <tool/action_toolbar.h>
31 
32 
33 ACTION_TOOLBAR::ACTION_TOOLBAR( EDA_DRAW_FRAME* parent, wxWindowID id, const wxPoint& pos,
34  const wxSize& size, long style ) :
35  wxAuiToolBar( parent, id, pos, size, style ),
36  m_toolManager( parent->GetToolManager() )
37 {
38  Connect( wxEVT_COMMAND_TOOL_CLICKED, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolEvent ), NULL, this );
39 }
40 
41 
42 void ACTION_TOOLBAR::Add( const TOOL_ACTION& aAction, bool aIsToggleEntry )
43 {
44  int toolId = aAction.GetId() + ACTION_ID;
45 
46  AddTool( toolId, wxEmptyString, KiBitmap( aAction.GetIcon() ), aAction.GetDescription(),
47  aIsToggleEntry ? wxITEM_CHECK : wxITEM_NORMAL );
48 
49  m_toolActions[ toolId ] = &aAction;
50 }
51 
52 
53 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aState )
54 {
55  ToggleTool( aAction.GetId() + ACTION_ID, aState );
56 }
57 
58 
59 void ACTION_TOOLBAR::onToolEvent( wxAuiToolBarEvent& aEvent )
60 {
61  OPT_TOOL_EVENT evt;
62  wxString menuText;
63 
64  wxEventType type = aEvent.GetEventType();
65 
66  if( type == wxEVT_COMMAND_TOOL_CLICKED && aEvent.GetId() >= ACTION_ID )
67  {
68  const auto it = m_toolActions.find( aEvent.GetId() );
69 
70  if( it != m_toolActions.end() )
71  evt = it->second->MakeEvent();
72  }
73 
74  // forward the action/update event to the TOOL_MANAGER
75  if( evt && m_toolManager )
76  {
77  //aEvent.StopPropagation();
78  m_toolManager->ProcessEvent( *evt );
79  }
80  else
81  {
82  aEvent.Skip();
83  }
84 }
85 
ACTION_TOOLBAR(EDA_DRAW_FRAME *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxAUI_TB_DEFAULT_STYLE)
const wxString & GetDescription() const
Definition: tool_action.h:115
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.
The base class for create windows for drawing purpose.
Definition: draw_frame.h:82
std::map< int, const TOOL_ACTION * > m_toolActions
const BITMAP_OPAQUE * GetIcon() const
Returns an icon associated with the action.
Definition: tool_action.h:156
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
TOOL_MANAGER * m_toolManager
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
Class TOOL_ACTION.
Definition: tool_action.h:46
void Toggle(const TOOL_ACTION &aAction, bool aState)
static const int ACTION_ID
Tool items with ID higher than that are considered TOOL_ACTIONs
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486