File: multi_toolbar_window.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (145 lines) | stat: -rw-r--r-- 5,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" A top-level application window that supports multiple toolbars. """


import wx


from pyface.action.api import ToolBarManager
from traits.api import Dict, Enum, Instance, List


from .application_window import ApplicationWindow


class MultiToolbarWindow(ApplicationWindow):
    """ A top-level application window that supports multiple toolbars.

    The multi-toolbar window has support for a menu bar, status bar, and
    multiple toolbars (all of which are optional).

    """

    # The toolbars in the order they were added to the window.
    _tool_bar_managers = List(Instance(ToolBarManager))

    # Map of toolbar to screen location.
    _tool_bar_locations = Dict(
        Instance(ToolBarManager), Enum("top", "bottom", "left", "right")
    )

    # ------------------------------------------------------------------------
    # Protected 'Window' interface.
    # ------------------------------------------------------------------------
    def _create_contents(self, parent):
        panel = super()._create_contents(parent)
        self._create_trim_widgets(parent)

        return panel

    def _create_trim_widgets(self, parent):

        # The frame's icon.
        self._set_window_icon()

        # Add the (optional) menu bar.
        self._create_menu_bar(parent)

        # Add the (optional) status bar.
        self._create_status_bar(parent)

        # Add the (optional) tool bars.
        self.sizer = self._create_tool_bars(parent)

    def _create_tool_bars(self, parent):
        """ Create the tool bars for this window. """

        if len(self._tool_bar_managers) > 0:
            # Create a top level sizer to handle to main layout and attach
            # it to the parent frame.
            self.main_sizer = sizer = wx.BoxSizer(wx.VERTICAL)
            parent.SetSizer(sizer)
            parent.SetAutoLayout(True)

            for tool_bar_manager in self._tool_bar_managers:
                location = self._tool_bar_locations[tool_bar_manager]
                sizer = self._create_tool_bar(
                    parent, sizer, tool_bar_manager, location
                )

            return sizer

        return None

    def _create_tool_bar(self, parent, sizer, tool_bar_manager, location):
        """ Create and add the toolbar to the parent window at the specified
        location.

        Returns the sizer where the remaining content should be added.  For
        'top' and 'left' toolbars, we can return the same sizer that contains
        the toolbar, because subsequent additions will be added below or to
        the right of those toolbars.  For 'right' and 'bottom' toolbars, we
        create a spacer toolbar to hold the content.
        """

        tool_bar = tool_bar_manager.create_tool_bar(parent)

        if location == "top":
            child_sizer = wx.BoxSizer(wx.VERTICAL)
            child_sizer.Add(tool_bar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
            sizer.Add(child_sizer, 1, wx.ALL | wx.EXPAND)

        if location == "bottom":
            toolbar_sizer = wx.BoxSizer(wx.VERTICAL)

            # Add the placeholder for the content before adding the toolbar.
            child_sizer = self._create_content_spacer(toolbar_sizer)

            # Add the tool bar.
            toolbar_sizer.Add(tool_bar, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
            sizer.Add(toolbar_sizer, 1, wx.ALL | wx.EXPAND)

        if location == "left":
            child_sizer = wx.BoxSizer(wx.HORIZONTAL)
            child_sizer.Add(tool_bar, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
            sizer.Add(child_sizer, 1, wx.ALL | wx.EXPAND)

        if location == "right":
            toolbar_sizer = wx.BoxSizer(wx.HORIZONTAL)

            # Add the placeholder for the content before adding the toolbar.
            child_sizer = self._create_content_spacer(toolbar_sizer)

            # Add the tool bar.
            toolbar_sizer.Add(tool_bar, 0, wx.ALL | wx.ALIGN_TOP | wx.EXPAND)
            sizer.Add(toolbar_sizer, 1, wx.ALL | wx.EXPAND)

        return child_sizer

    def _create_content_spacer(self, sizer):
        spacer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(spacer, 1, wx.ALL | wx.EXPAND)

        return spacer

    # ------------------------------------------------------------------------
    # Public MultiToolbarWindow interface
    # ------------------------------------------------------------------------

    def add_tool_bar(self, tool_bar_manager, location="top"):
        """ Add a toolbar in the specified location.

        Valid locations are 'top', 'bottom', 'left', and 'right'
        """

        self._tool_bar_managers.append(tool_bar_manager)
        self._tool_bar_locations[tool_bar_manager] = location