1# (C) Copyright 2005-2020 Enthought, Inc., Austin, TX 2# All rights reserved. 3# 4# This software is provided without warranty under the terms of the BSD 5# license included in LICENSE.txt and may be redistributed only under 6# the conditions described in the aforementioned license. The license 7# is also available online at http://www.enthought.com/licenses/BSD.txt 8# 9# Thanks for using Enthought open source! 10 11 12""" A tool bar manager realizes itself in a tool palette control. 13""" 14 15 16import wx 17 18 19from traits.api import Any, Bool, Enum, Instance, Tuple 20 21 22from pyface.image_cache import ImageCache 23from pyface.action.action_manager import ActionManager 24from .tool_palette import ToolPalette 25 26 27class ToolPaletteManager(ActionManager): 28 """ A tool bar manager realizes itself in a tool palette bar control. """ 29 30 # 'ToolPaletteManager' interface --------------------------------------- 31 32 # The size of tool images (width, height). 33 image_size = Tuple((16, 16)) 34 35 # Should we display the name of each tool bar tool under its image? 36 show_tool_names = Bool(True) 37 38 # Private interface ---------------------------------------------------- 39 40 # Cache of tool images (scaled to the appropriate size). 41 _image_cache = Instance(ImageCache) 42 43 # ------------------------------------------------------------------------ 44 # 'object' interface. 45 # ------------------------------------------------------------------------ 46 47 def __init__(self, *args, **traits): 48 """ Creates a new tool bar manager. """ 49 50 # Base class contructor. 51 super(ToolPaletteManager, self).__init__(*args, **traits) 52 53 # An image cache to make sure that we only load each image used in the 54 # tool bar exactly once. 55 self._image_cache = ImageCache(self.image_size[0], self.image_size[1]) 56 57 return 58 59 # ------------------------------------------------------------------------ 60 # 'ToolPaletteManager' interface. 61 # ------------------------------------------------------------------------ 62 63 def create_tool_palette(self, parent, controller=None): 64 """ Creates a tool bar. """ 65 66 # Create the control. 67 tool_palette = ToolPalette(parent) 68 69 # Add all of items in the manager's groups to the tool bar. 70 self._add_tools(tool_palette, self.groups) 71 72 self._set_initial_tool_state(tool_palette, self.groups) 73 74 return tool_palette 75 76 # ------------------------------------------------------------------------ 77 # Private interface. 78 # ------------------------------------------------------------------------ 79 80 def _add_tools(self, tool_palette, groups): 81 """ Adds tools for all items in a list of groups. """ 82 83 previous_non_empty_group = None 84 for group in self.groups: 85 if len(group.items) > 0: 86 # Is a separator required? 87 ## FIXME : Does the palette need the notion of a separator? 88 ## if previous_non_empty_group is not None and group.separator: 89 ## tool_bar.AddSeparator() 90 ## 91 ## previous_non_empty_group = group 92 93 # Create a tool bar tool for each item in the group. 94 for item in group.items: 95 control_id = item.add_to_palette( 96 tool_palette, self._image_cache, self.show_tool_names 97 ) 98 item.control_id = control_id 99 100 tool_palette.realize() 101 102 def _set_initial_tool_state(self, tool_palette, groups): 103 """ Workaround for the wxPython tool bar bug. 104 105 Without this, only the first item in a radio group can be selected 106 when the tool bar is first realised 8^() 107 108 """ 109 110 for group in groups: 111 checked = False 112 for item in group.items: 113 # If the group is a radio group, set the initial checked state 114 # of every tool in it. 115 if item.action.style == "radio": 116 tool_palette.toggle_tool( 117 item.control_id, item.action.checked 118 ) 119 checked = checked or item.action.checked 120 121 # Every item in a radio group MUST be 'radio' style, so we 122 # can just skip to the next group. 123 else: 124 break 125 126 # We get here if the group is a radio group. 127 else: 128 # If none of the actions in the group is specified as 'checked' 129 # we will check the first one. 130 if not checked and len(group.items) > 0: 131 group.items[0].action.checked = True 132 133 return 134