KiCad PCB EDA Suite
action_toolbar.h
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-2021 KiCad Developers, see AUTHORS.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 #ifndef ACTION_TOOLBAR_H
25 #define ACTION_TOOLBAR_H
26 
27 #include <map>
28 #include <memory>
29 #include <vector>
30 #include <wx/bitmap.h> // Needed for the auibar include
31 #include <wx/aui/auibar.h>
32 #include <wx/aui/framemanager.h>
33 #include <wx/popupwin.h>
34 #include <wx/panel.h>
35 #include <tool/action_manager.h>
36 
37 class ACTION_MENU;
38 class BITMAP_BUTTON;
39 class EDA_BASE_FRAME;
40 class TOOL_ACTION;
41 class TOOL_MANAGER;
42 
47 {
48 public:
49  // Make the toolbar a friend so it can easily access everything inside here
50  friend class ACTION_TOOLBAR;
51 
52  ACTION_GROUP( const std::string& aName, const std::vector<const TOOL_ACTION*>& aActions );
53 
62  void SetDefaultAction( const TOOL_ACTION& aDefault );
63 
67  const TOOL_ACTION* GetDefaultAction() const { return m_defaultAction; }
68 
72  std::string GetName() const { return m_name; }
73 
77  int GetUIId() const;
78 
82  const std::vector<const TOOL_ACTION*>& GetActions() const { return m_actions; }
83 
84 protected:
86  int m_id;
87 
89  std::string m_name;
90 
93 
95  std::vector<const TOOL_ACTION*> m_actions;
96 };
97 
98 
102 class ACTION_TOOLBAR_PALETTE : public wxPopupTransientWindow
103 {
104 public:
112  ACTION_TOOLBAR_PALETTE( wxWindow* aParent, bool aVertical );
113 
119  void AddAction( const TOOL_ACTION& aAction );
120 
127  void EnableAction( const TOOL_ACTION& aAction, bool aEnable = true );
128 
135  void CheckAction( const TOOL_ACTION& aAction, bool aCheck = true );
136 
144  void SetButtonSize( wxRect& aSize ) { m_buttonSize = aSize; }
145 
151  void Popup( wxWindow* aFocus = nullptr ) override;
152 
156  void SetGroup( ACTION_GROUP* aGroup ) { m_group = aGroup; }
158 
159 protected:
160  void onCharHook( wxKeyEvent& aEvent );
161 
162  // The group that the buttons in the palette are part of
164 
166  wxRect m_buttonSize;
167 
170 
171  wxPanel* m_panel;
172  wxBoxSizer* m_mainSizer;
173  wxBoxSizer* m_buttonSizer;
174 
176  std::map<int, BITMAP_BUTTON*> m_buttons;
177 };
178 
179 
183 class ACTION_TOOLBAR : public wxAuiToolBar
184 {
185 public:
186  ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id = wxID_ANY,
187  const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
188  long style = wxAUI_TB_DEFAULT_STYLE );
189 
190  virtual ~ACTION_TOOLBAR();
191 
197  void SetAuiManager( wxAuiManager* aManager ) { m_auiManager = aManager; }
198 
208  void Add( const TOOL_ACTION& aAction, bool aIsToggleEntry = false,
209  bool aIsCancellable = false );
210 
216  void AddButton( const TOOL_ACTION& aAction );
217 
224  void AddScaledSeparator( wxWindow* aWindow );
225 
235  void AddToolContextMenu( const TOOL_ACTION& aAction, std::unique_ptr<ACTION_MENU> aMenu );
236 
245  void AddGroup( ACTION_GROUP* aGroup, bool aIsToggleEntry = false );
246 
253  void SelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aAction );
254 
260  void UpdateControlWidth( int aID );
261 
265  void ClearToolbar();
266 
272  void SetToolBitmap( const TOOL_ACTION& aAction, const wxBitmap& aBitmap );
273 
279  void Toggle( const TOOL_ACTION& aAction, bool aState );
280 
281  void Toggle( const TOOL_ACTION& aAction, bool aEnabled, bool aChecked );
282 
291  bool KiRealize();
292 
296  void RefreshBitmaps();
297 
298  static constexpr bool TOGGLE = true;
299  static constexpr bool CANCEL = true;
300 
301 protected:
307  void doSelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aAction );
308 
313  void popupPalette( wxAuiToolBarItem* aItem );
314 
316  void onMouseClick( wxMouseEvent& aEvent );
317 
319  void onItemDrag( wxAuiToolBarEvent& aEvent );
320 
322  void onToolEvent( wxAuiToolBarEvent& aEvent );
323 
325  void onToolRightClick( wxAuiToolBarEvent& aEvent );
326 
328  void onPaletteEvent( wxCommandEvent& aEvent );
329 
331  void onTimerDone( wxTimerEvent& aEvent );
332 
335  void OnCustomRender( wxDC& aDc, const wxAuiToolBarItem& aItem, const wxRect& aRect ) override;
336 
337 protected:
338  // Timer used to determine when the palette should be opened after a group item is pressed
339  wxTimer* m_paletteTimer;
340 
341  wxAuiManager* m_auiManager;
344 
345  std::map<int, bool> m_toolKinds;
346  std::map<int, bool> m_toolCancellable;
347  std::map<int, const TOOL_ACTION*> m_toolActions;
348  std::map<int, ACTION_GROUP*> m_actionGroups;
349 
350  std::map<int, std::unique_ptr<ACTION_MENU>> m_toolMenus;
351 };
352 
353 #endif
const TOOL_ACTION * m_defaultAction
The actions that compose the group.
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
Definition: bitmap_button.h:39
Define the structure of a toolbar with buttons that invoke ACTIONs.
void onItemDrag(wxAuiToolBarEvent &aEvent)
The default tool event handler.
void CheckAction(const TOOL_ACTION &aAction, bool aCheck=true)
Check/Toggle the button for an action on the palette.
std::string GetName() const
Get the name of the group.
ACTION_GROUP * GetGroup()
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
void SetButtonSize(wxRect &aSize)
Set the size all the buttons on this palette should be.
void AddToolContextMenu(const TOOL_ACTION &aAction, std::unique_ptr< ACTION_MENU > aMenu)
Add a context menu to a specific tool item on the toolbar.
static constexpr bool TOGGLE
void AddScaledSeparator(wxWindow *aWindow)
Add a separator that introduces space on either side to not squash the tools when scaled.
void popupPalette(wxAuiToolBarItem *aItem)
Popup the ACTION_TOOLBAR_PALETTE associated with the ACTION_GROUP of the given toolbar item.
void Add(const TOOL_ACTION &aAction, bool aIsToggleEntry=false, bool aIsCancellable=false)
Add a TOOL_ACTION-based button to the toolbar.
void onToolRightClick(wxAuiToolBarEvent &aEvent)
Handle the button select inside the palette.
void onTimerDone(wxTimerEvent &aEvent)
Render the triangle in the lower-right corner that represents that an action palette is available for...
void SetAuiManager(wxAuiManager *aManager)
Set the AUI manager that this toolbar belongs to.
void AddGroup(ACTION_GROUP *aGroup, bool aIsToggleEntry=false)
Add a set of actions to a toolbar as a group.
ACTION_TOOLBAR_PALETTE(wxWindow *aParent, bool aVertical)
Create the palette.
void onToolEvent(wxAuiToolBarEvent &aEvent)
Handle a right-click on a menu item.
std::map< int, const TOOL_ACTION * > m_toolActions
bool KiRealize()
Use this over Realize() to avoid a rendering glitch with fixed orientation toolbars.
void SetDefaultAction(const TOOL_ACTION &aDefault)
Set the default action to use when first creating the toolbar palette icon.
void onMouseClick(wxMouseEvent &aEvent)
Handler for when a drag event occurs on an item.
Master controller class:
Definition: tool_manager.h:54
A group of actions that will be displayed together on a toolbar palette.
static constexpr bool CANCEL
ACTION_GROUP(const std::string &aName, const std::vector< const TOOL_ACTION * > &aActions)
void AddAction(const TOOL_ACTION &aAction)
Add an action to the palette.
void AddButton(const TOOL_ACTION &aAction)
Add a large button such as used in the KiCad Manager Frame's launch bar.
void doSelectAction(ACTION_GROUP *aGroup, const TOOL_ACTION &aAction)
Update a group toolbar item to look like a specific action.
std::map< int, bool > m_toolCancellable
void UpdateControlWidth(int aID)
Update the toolbar item width of a control using its best size.
void SetGroup(ACTION_GROUP *aGroup)
Set the action group that this palette contains the actions for.
ACTION_GROUP * m_group
The size each button on the toolbar should be.
void OnCustomRender(wxDC &aDc, const wxAuiToolBarItem &aItem, const wxRect &aRect) override
ACTION_TOOLBAR_PALETTE * m_palette
std::vector< const TOOL_ACTION * > m_actions
ACTION_TOOLBAR(EDA_BASE_FRAME *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxAUI_TB_DEFAULT_STYLE)
std::map< int, std::unique_ptr< ACTION_MENU > > m_toolMenus
wxRect m_buttonSize
True if the palette uses vertical buttons, false for horizontal buttons.
wxAuiManager * m_auiManager
void RefreshBitmaps()
Reload all the bitmaps for the tools (e.g.
const TOOL_ACTION * GetDefaultAction() const
Get the default action to use when first creating this group's toolbar palette icon.
TOOL_MANAGER * m_toolManager
std::map< int, ACTION_GROUP * > m_actionGroups
std::map< int, BITMAP_BUTTON * > m_buttons
The base frame for deriving all KiCad main window classes.
Represent a single user action.
Definition: tool_action.h:67
void SetToolBitmap(const TOOL_ACTION &aAction, const wxBitmap &aBitmap)
Updates the bitmap of a particular tool.
void Toggle(const TOOL_ACTION &aAction, bool aState)
Apply the default toggle action.
int GetUIId() const
Get the ID used in the UI to reference this group.
wxBoxSizer * m_buttonSizer
The buttons that act as the toolbar on the palette.
virtual ~ACTION_TOOLBAR()
void SelectAction(ACTION_GROUP *aGroup, const TOOL_ACTION &aAction)
Select an action inside a group.
int m_id
< The action ID for this action group
void Popup(wxWindow *aFocus=nullptr) override
Popup this window.
void EnableAction(const TOOL_ACTION &aAction, bool aEnable=true)
Enable the button for an action on the palette.
void onCharHook(wxKeyEvent &aEvent)
std::map< int, bool > m_toolKinds
wxTimer * m_paletteTimer
const std::vector< const TOOL_ACTION * > & GetActions() const
Get a vector of all the actions contained inside this group.
A popup window that contains a row of toolbar-like buttons for the user to choose from.
void onPaletteEvent(wxCommandEvent &aEvent)
Handle the palette timer triggering.
std::string m_name
The default action to display on the toolbar item.
void ClearToolbar()
Clear the toolbar and remove all associated menus.