-
-
Notifications
You must be signed in to change notification settings - Fork 653
/
window.py
171 lines (140 loc) · 5.8 KB
/
window.py
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
import toga
from toga import GROUP_BREAK, SECTION_BREAK
from travertino.layout import Viewport
from .libs import WinForms, Size, add_handler
class WinFormsViewport:
def __init__(self, native, frame):
self.native = native
self.frame = frame
self.dpi = 96 # FIXME This is almost certainly wrong...
@property
def width(self):
return self.native.ClientSize.Width
@property
def height(self):
# Subtract any vertical shift of the frame. This is to allow
# for toolbars, or any other viewport-level decoration.
return self.native.ClientSize.Height - self.frame.vertical_shift
class Window:
def __init__(self, interface):
self.interface = interface
self.interface._impl = self
self.create()
def create(self):
self.native = WinForms.Form(self)
self.native.ClientSize = Size(self.interface._size[0], self.interface._size[1])
self.native.interface = self.interface
self.native.Resize += self.on_resize
self.toolbar_native = None
self.toolbar_items = None
def create_toolbar(self):
self.toolbar_native = WinForms.MenuStrip()
for cmd in self.interface.toolbar:
if cmd == GROUP_BREAK:
item = WinForms.ToolStripSeparator()
elif cmd == SECTION_BREAK:
item = WinForms.ToolStripSeparator()
else:
cmd.native = cmd.bind(self.interface.factory)
native_icon = cmd.icon.bind(self.interface.factory).native
if cmd.icon is not None:
item = WinForms.ToolStripMenuItem(cmd.label, native_icon.ToBitmap())
else:
item = WinForms.ToolStripMenuItem(cmd.label)
item.Click += add_handler(cmd)
self.toolbar_native.Items.Add(item)
def create_menus(self):
toga.Group.FILE.order = 0
# Only create the menu if the menu item index has been created.
if hasattr(self, '_menu_items'):
self._menu_items = {}
menubar = WinForms.MenuStrip()
submenu = None
for cmd in self.interface._app.commands:
if cmd == GROUP_BREAK:
menubar.Items.Add(submenu)
submenu = None
elif cmd == SECTION_BREAK:
submenu.DropDownItems.Add('-')
else:
if submenu is None:
submenu = WinForms.ToolStripMenuItem(cmd.group.label)
item = WinForms.ToolStripMenuItem(cmd.label)
item.Click += add_handler(cmd)
cmd._widgets.append(item)
self._menu_items[item] = cmd
# This line may appear redundant, but it triggers the logic
# to force the enabled status on the underlying widgets.
cmd.enabled = cmd.enabled
submenu.DropDownItems.Add(item)
if submenu:
menubar.Items.Add(submenu)
# Set the menu for the app.
self.native.MainMenuStrip = menubar
self.native.Controls.Add(menubar)
def set_position(self, position):
pass
def set_size(self, size):
self.native.ClientSize = Size(self.interface._size[0], self.interface._size[1])
def set_app(self, app):
pass
@property
def vertical_shift(self):
# vertical shift is the toolbar height or 0
try:
return self.native.interface._impl.toolbar_native.Height
except AttributeError:
return 0
def set_content(self, widget):
if self.toolbar_native:
self.native.Controls.Add(self.toolbar_native)
# Create the lookup table of menu items,
# then force the creation of the menus.
self._menu_items = {}
if self.interface is self.interface.app._main_window:
self.create_menus()
self.native.Controls.Add(widget.native)
# Set the widget's viewport to be based on the window's content.
widget.viewport = WinFormsViewport(self.native, self)
widget.frame = self
# Add all children to the content widget.
for child in widget.interface.children:
child._impl.container = widget
def set_title(self, title):
self.native.Text = title
def show(self):
# The first render of the content will establish the
# minimum possible content size; use that to enforce
# a minimum window size.
TITLEBAR_HEIGHT = WinForms.SystemInformation.CaptionHeight
# Now that the content is visible, we can do our initial hinting,
# and use that as the basis for setting the minimum window size.
self.interface.content._impl.rehint()
self.interface.content.style.layout(self.interface.content, Viewport(0, 0))
self.native.MinimumSize = Size(
int(self.interface.content.layout.width),
int(self.interface.content.layout.height) + TITLEBAR_HEIGHT
)
self.interface.content.refresh()
if self.interface is not self.interface.app._main_window:
self.native.Show()
def on_close(self):
pass
def close(self):
self.native.Close()
def on_resize(self, sender, args):
if self.interface.content:
# Re-layout the content
self.interface.content.refresh()
def info_dialog(self, title, message):
pass
def question_dialog(self, title, message):
pass
def confirm_dialog(self, title, message):
pass
def error_dialog(self, title, message):
pass
def stack_trace_dialog(self, title, message, content, retry=False):
pass
def save_file_dialog(self, title, suggested_filename, file_types):
pass