File: dock_window_shell.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 (199 lines) | stat: -rw-r--r-- 6,929 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# (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!


""" Defines the DockWindowShell class used to house drag and drag DockWindow
    items that are dropped on the desktop or on the DockWindowShell window.
"""


import wx

# Fixme: Hack to force 'image_slice' to be added via Category to Theme class:
import traitsui.wx  # noqa: F401
from traits.api import HasPrivateTraits, Instance

from pyface.api import SystemMetrics
from pyface.image_resource import ImageResource
from .dock_window import DockWindow
from .dock_sizer import (
    DockSizer,
    DockSection,
    DockRegion,
    DockControl,
    DOCK_RIGHT,
)

# -------------------------------------------------------------------------------
#  Constants:
# -------------------------------------------------------------------------------

# DockWindowShell frame icon:
FrameIcon = ImageResource("shell.ico")

# -------------------------------------------------------------------------------
#  'DockWindowShell' class:
# -------------------------------------------------------------------------------


class DockWindowShell(HasPrivateTraits):

    # ---------------------------------------------------------------------------
    #  Trait definitions:
    # ---------------------------------------------------------------------------

    # The wx.Frame window which is the actual shell:
    control = Instance(wx.Frame)

    # ---------------------------------------------------------------------------
    #  Initializes the object:
    # ---------------------------------------------------------------------------

    def __init__(self, dock_control, use_mouse=False, **traits):
        super().__init__(**traits)

        old_control = dock_control.control
        parent = wx.GetTopLevelParent(old_control)
        while True:
            next_parent = parent.GetParent()
            if next_parent is None:
                break
            parent = next_parent

        self.control = shell = wx.Frame(
            parent,
            -1,
            dock_control.name,
            style=wx.DEFAULT_FRAME_STYLE
            | wx.FRAME_FLOAT_ON_PARENT
            | wx.FRAME_NO_TASKBAR,
        )
        shell.SetIcon(FrameIcon.create_icon())
        shell.SetBackgroundColour(SystemMetrics().dialog_background_color)
        shell.Bind(wx.EVT_CLOSE, self._on_close)

        theme = dock_control.theme
        dw = DockWindow(shell, auto_close=True, theme=theme)
        dw.trait_set(style="tab")
        self._dock_window = dw
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(dw.control, 1, wx.EXPAND)
        shell.SetSizer(sizer)

        if use_mouse:
            x, y = wx.GetMousePosition()
        else:
            x, y = old_control.Sizer.GetPosition().Get()
            x, y = old_control.GetParent().Window.ClientToScreen(x, y)

        dx, dy = old_control.GetSize().Get()
        tis = theme.tab.image_slice
        tc = theme.tab.content
        tdy = theme.tab_active.image_slice.dy
        dx += tis.xleft + tc.left + tis.xright + tc.right
        dy += tis.xtop + tc.top + tis.xbottom + tc.bottom + tdy

        self.add_control(dock_control)

        # Set the correct window size and position, accounting for the tab size
        # and window borders:
        shell.SetSize(x, y, dx, dy)
        cdx, cdy = shell.GetClientSize().Get()
        ex_dx = dx - cdx
        ex_dy = dy - cdy
        shell.SetSize(
            x - (ex_dx // 2) - tis.xleft - tc.left,
            y - ex_dy + (ex_dx // 2) - tdy - tis.xtop - tc.top,
            dx + ex_dx,
            dy + ex_dy,
        )
        shell.Show()

    # ---------------------------------------------------------------------------
    #  Adds a new DockControl to the shell window:
    # ---------------------------------------------------------------------------

    def add_control(self, dock_control):
        """ Adds a new DockControl to the shell window.
        """
        dw = self._dock_window.control
        dockable = dock_control.dockable

        # If the current DockControl should be closed, then do it:
        close = dockable.dockable_should_close()
        if close:
            dock_control.close(force=True)

        # Create the new control:
        control = dockable.dockable_get_control(dw)

        # If the DockControl was closed, then reset it to point to the new
        # control:
        if close:
            dock_control.trait_set(control=control, style="tab")
        else:
            # Create a DockControl to describe the new control:
            dock_control = DockControl(
                control=control,
                name=dock_control.name,
                export=dock_control.export,
                style="tab",
                image=dock_control.image,
                closeable=True,
            )

        # Finish initializing the DockControl:
        dockable.dockable_init_dockcontrol(dock_control)

        # Get the current DockSizer:
        sizer = dw.GetSizer()
        if sizer is None:
            # Create the initial sizer:
            dw.SetSizer(
                DockSizer(
                    DockSection(contents=[DockRegion(contents=[dock_control])])
                )
            )
        else:
            # Sizer exists already, try to add the DockControl as a new
            # notebook tab. If the user has reorganized the layout, then just
            # dock it on the right side somewhere:
            section = sizer.GetContents()
            region = section.contents[0]
            if isinstance(region, DockRegion):
                region.add(dock_control)
            else:
                section.add(dock_control, region, DOCK_RIGHT)

            # Force the control to update:
            dw.Layout()
            dw.Refresh()

    # ---------------------------------------------------------------------------
    #  Handles the user attempting to close the window:
    # ---------------------------------------------------------------------------

    def _on_close(self, event):
        """ Handles the user attempting to close the window.
        """
        window = self._dock_window.control
        section = window.GetSizer().GetContents()
        n = len(section.contents)

        # Try to close each individual control:
        for control in section.get_controls():
            control.close(layout=False)

        # If some, but not all, were closed, make sure the window gets updated:
        if 0 < len(section.contents) < n:
            window.Layout()
            window.Refresh()

        self.control.Unbind(wx.EVT_CLOSE)