You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
11 KiB

  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. #ifndef ACTION_TOOLBAR_H
  24. #define ACTION_TOOLBAR_H
  25. #include <map>
  26. #include <memory>
  27. #include <vector>
  28. #include <wx/bitmap.h> // Needed for the auibar include
  29. #include <wx/aui/auibar.h>
  30. #include <wx/aui/framemanager.h>
  31. #include <wx/popupwin.h>
  32. #include <wx/panel.h>
  33. #include <tool/action_manager.h>
  34. class ACTION_MENU;
  35. class BITMAP_BUTTON;
  36. class EDA_BASE_FRAME;
  37. class TOOL_ACTION;
  38. class TOOL_MANAGER;
  39. /**
  40. * A group of actions that will be displayed together on a toolbar palette.
  41. */
  42. class ACTION_GROUP
  43. {
  44. public:
  45. // Make the toolbar a friend so it can easily access everything inside here
  46. friend class ACTION_TOOLBAR;
  47. ACTION_GROUP( const std::string& aName, const std::vector<const TOOL_ACTION*>& aActions );
  48. /**
  49. * Set the default action to use when first creating the toolbar palette icon.
  50. *
  51. * If no default action is provided, the default will be the first action in the
  52. * vector.
  53. *
  54. * @param aDefault is the default action.
  55. */
  56. void SetDefaultAction( const TOOL_ACTION& aDefault );
  57. /**
  58. * Get the default action to use when first creating this group's toolbar palette icon.
  59. */
  60. const TOOL_ACTION* GetDefaultAction() const { return m_defaultAction; }
  61. /**
  62. * Get the name of the group.
  63. */
  64. std::string GetName() const { return m_name; }
  65. /**
  66. * Get the ID used in the UI to reference this group
  67. */
  68. int GetUIId() const;
  69. /**
  70. * Get a vector of all the actions contained inside this group.
  71. */
  72. const std::vector<const TOOL_ACTION*>& GetActions() const { return m_actions; }
  73. protected:
  74. ///< The action ID for this action group
  75. int m_id;
  76. ///< The name of this action group
  77. std::string m_name;
  78. ///< The default action to display on the toolbar item
  79. const TOOL_ACTION* m_defaultAction;
  80. ///< The actions that compose the group
  81. std::vector<const TOOL_ACTION*> m_actions;
  82. };
  83. /**
  84. * A popup window that contains a row of toolbar-like buttons for the user to choose from.
  85. */
  86. class ACTION_TOOLBAR_PALETTE : public wxPopupTransientWindow
  87. {
  88. public:
  89. /**
  90. * Create the palette.
  91. *
  92. * @param aParent is the parent window
  93. * @param aVertical is true if the palette should make the buttons a vertical line,
  94. * false for a horizontal line.
  95. */
  96. ACTION_TOOLBAR_PALETTE( wxWindow* aParent, bool aVertical );
  97. /**
  98. * Add an action to the palette.
  99. *
  100. * @param aAction is the action to add
  101. */
  102. void AddAction( const TOOL_ACTION& aAction );
  103. /**
  104. * Enable the button for an action on the palette.
  105. *
  106. * @param aAction is the action who's button should be enabled
  107. * @param aEnable is true to enable the button, false to disable
  108. */
  109. void EnableAction( const TOOL_ACTION& aAction, bool aEnable = true );
  110. /**
  111. * Check/Toggle the button for an action on the palette.
  112. *
  113. * @param aAction is the action who's button should be checked
  114. * @param aCheck is true to check the button, false to uncheck
  115. */
  116. void CheckAction( const TOOL_ACTION& aAction, bool aCheck = true );
  117. /**
  118. * Set the size all the buttons on this palette should be.
  119. * This function will automatically pad all button bitmaps to ensure this
  120. * size is met.
  121. *
  122. * @param aSize is the requested size of the buttons
  123. */
  124. void SetButtonSize( wxRect& aSize ) { m_buttonSize = aSize; }
  125. /**
  126. * Popup this window
  127. *
  128. * @param aFocus is the window to keep focus on (if supported)
  129. */
  130. void Popup( wxWindow* aFocus = nullptr ) override;
  131. /**
  132. * Set the action group that this palette contains the actions for
  133. */
  134. void SetGroup( ACTION_GROUP* aGroup ) { m_group = aGroup; }
  135. ACTION_GROUP* GetGroup() { return m_group; }
  136. protected:
  137. void onCharHook( wxKeyEvent& aEvent );
  138. // The group that the buttons in the palette are part of
  139. ACTION_GROUP* m_group;
  140. ///< The size each button on the toolbar should be
  141. wxRect m_buttonSize;
  142. ///< True if the palette uses vertical buttons, false for horizontal buttons
  143. bool m_isVertical;
  144. wxPanel* m_panel;
  145. wxBoxSizer* m_mainSizer;
  146. wxBoxSizer* m_buttonSizer;
  147. ///< The buttons that act as the toolbar on the palette
  148. std::map<int, BITMAP_BUTTON*> m_buttons;
  149. };
  150. /**
  151. * Define the structure of a toolbar with buttons that invoke ACTIONs.
  152. */
  153. class ACTION_TOOLBAR : public wxAuiToolBar
  154. {
  155. public:
  156. ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id = wxID_ANY,
  157. const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
  158. long style = wxAUI_TB_DEFAULT_STYLE );
  159. virtual ~ACTION_TOOLBAR();
  160. /**
  161. * Set the AUI manager that this toolbar belongs to.
  162. *
  163. * @param aManager is the AUI manager
  164. */
  165. void SetAuiManager( wxAuiManager* aManager ) { m_auiManager = aManager; }
  166. /**
  167. * Add a TOOL_ACTION-based button to the toolbar.
  168. *
  169. * After selecting the entry, a #TOOL_EVENT command containing name of the action is sent.
  170. *
  171. * @param aAction is the action to add.
  172. * @param aIsToggleEntry makes the toolbar item a toggle entry when true.
  173. * @param aIsCancellable when true, cancels the tool if clicked when tool is active.
  174. */
  175. void Add( const TOOL_ACTION& aAction, bool aIsToggleEntry = false,
  176. bool aIsCancellable = false );
  177. /**
  178. * Add a large button such as used in the KiCad Manager Frame's launch bar.
  179. *
  180. * @param aAction
  181. */
  182. void AddButton( const TOOL_ACTION& aAction );
  183. /**
  184. * Add a separator that introduces space on either side to not squash the tools
  185. * when scaled.
  186. *
  187. * @param aWindow is the window to get the scaling factor of
  188. */
  189. void AddScaledSeparator( wxWindow* aWindow );
  190. /**
  191. * Add a context menu to a specific tool item on the toolbar.
  192. *
  193. * This toolbar gets ownership of the menu object, and will delete it when the
  194. * ClearToolbar() function is called.
  195. *
  196. * @param aAction is the action to get the menu
  197. * @param aMenu is the context menu
  198. */
  199. void AddToolContextMenu( const TOOL_ACTION& aAction, std::unique_ptr<ACTION_MENU> aMenu );
  200. /**
  201. * Add a set of actions to a toolbar as a group. One action from the group will be displayed
  202. * at a time.
  203. *
  204. * @param aGroup is the group to add. The first action in the group will be the first shown
  205. * on the toolbar.
  206. * @param aIsToggleEntry makes the toolbar item a toggle entry when true
  207. */
  208. void AddGroup( ACTION_GROUP* aGroup, bool aIsToggleEntry = false );
  209. /**
  210. * Select an action inside a group
  211. *
  212. * @param aGroup is the group that contains the action
  213. * @param aAction is the action inside the group
  214. */
  215. void SelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aAction );
  216. /**
  217. * Update the toolbar item width of a control using its best size.
  218. *
  219. * @param aID is the ID of the toolbar item to update the width for
  220. */
  221. void UpdateControlWidth( int aID );
  222. /**
  223. * Clear the toolbar and remove all associated menus.
  224. */
  225. void ClearToolbar();
  226. /**
  227. * Updates the bitmap of a particular tool.
  228. *
  229. * Not icon-based because we use it for the custom-drawn layer pair bitmap.
  230. */
  231. void SetToolBitmap( const TOOL_ACTION& aAction, const wxBitmap& aBitmap );
  232. /**
  233. * Apply the default toggle action.
  234. *
  235. * For checked items this is check/uncheck; for non-checked items it's enable/disable.
  236. */
  237. void Toggle( const TOOL_ACTION& aAction, bool aState );
  238. void Toggle( const TOOL_ACTION& aAction, bool aEnabled, bool aChecked );
  239. /**
  240. * Use this over Realize() to avoid a rendering glitch with fixed orientation toolbars
  241. *
  242. * The standard Realize() draws both horizontal and vertical to determine sizing
  243. * However with many icons, potato PCs, etc, you can actually see that double draw
  244. * This custom function avoids the double draw if the HORIZONTAL or VERTICAL toolbar
  245. * properties are set.
  246. */
  247. bool KiRealize();
  248. /**
  249. * Reload all the bitmaps for the tools (e.g. when switching icon themes)
  250. */
  251. void RefreshBitmaps();
  252. static constexpr bool TOGGLE = true;
  253. static constexpr bool CANCEL = true;
  254. protected:
  255. /**
  256. * Update a group toolbar item to look like a specific action.
  257. *
  258. * Note: This function does not verify that the action is inside the group.
  259. */
  260. void doSelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aAction );
  261. /**
  262. * Popup the #ACTION_TOOLBAR_PALETTE associated with the ACTION_GROUP of the
  263. * given toolbar item.
  264. */
  265. void popupPalette( wxAuiToolBarItem* aItem );
  266. ///< Handler for a mouse up/down event
  267. void onMouseClick( wxMouseEvent& aEvent );
  268. ///< Handler for when a drag event occurs on an item
  269. void onItemDrag( wxAuiToolBarEvent& aEvent );
  270. ///< The default tool event handler
  271. void onToolEvent( wxAuiToolBarEvent& aEvent );
  272. ///< Handle a right-click on a menu item
  273. void onToolRightClick( wxAuiToolBarEvent& aEvent );
  274. ///< Handle the button select inside the palette
  275. void onPaletteEvent( wxCommandEvent& aEvent );
  276. ///< Handle the palette timer triggering
  277. void onTimerDone( wxTimerEvent& aEvent );
  278. void onThemeChanged( wxSysColourChangedEvent &aEvent );
  279. ///< Render the triangle in the lower-right corner that represents that an action palette
  280. ///< is available for an item
  281. void OnCustomRender( wxDC& aDc, const wxAuiToolBarItem& aItem, const wxRect& aRect ) override;
  282. protected:
  283. // Timer used to determine when the palette should be opened after a group item is pressed
  284. wxTimer* m_paletteTimer;
  285. wxAuiManager* m_auiManager;
  286. TOOL_MANAGER* m_toolManager;
  287. ACTION_TOOLBAR_PALETTE* m_palette;
  288. std::map<int, bool> m_toolKinds;
  289. std::map<int, bool> m_toolCancellable;
  290. std::map<int, const TOOL_ACTION*> m_toolActions;
  291. std::map<int, ACTION_GROUP*> m_actionGroups;
  292. std::map<int, std::unique_ptr<ACTION_MENU>> m_toolMenus;
  293. };
  294. #endif