forked from roseman/idle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.py
More file actions
271 lines (226 loc) · 9.13 KB
/
container.py
File metadata and controls
271 lines (226 loc) · 9.13 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Object that holds a Component, e.g. editor
For now, a toplevel, but later a frame too...
"""
import platform
from tkinter import *
from idlelib.statusbar import Statusbar
from idlelib.uitabs import UITabs, UITabsObserver
_py_version = ' (%s)' % platform.python_version()
class Container(object):
def __init__(self, flist):
self.flist = flist
self.component = None # caller sets via add_component
self.w = self.make_widget() # w: widget component packs into
self.top = self.w.winfo_toplevel() # top: toplevel for this container
self.statusbar = None
if self.flist:
self.flist.add_container(self)
def make_widget(self):
t = Toplevel(self.flist.root)
t.protocol('WM_DELETE_WINDOW', self._close)
t.bind("<<close-window>>", lambda e: self._close())
return t
def add_component(self, component):
self.component = component
self.w.after_idle(self.title_changed)
def _close(self):
if self.component is not None:
try:
self.component.close()
self.w.destroy()
self.w = None
self.component = None
if self.flist:
self.flist.delete_container(self)
except Exception: # if file needs saving, user may abort
pass
def title_changed(self):
if self.component is not None:
short = self.component.short_title()
long = self.component.long_title()
if short and long:
title = short + " - " + long + _py_version
elif short:
title = short
elif long:
title = long
else:
title = "Untitled"
icon = short or long or title
if not self.component.get_saved():
title = "*%s*" % title
icon = "*%s" % icon
self.w.wm_title(title)
self.w.wm_iconname(icon)
self.flist.filenames_changed() # to update window list
def get_title(self):
return self.w.wm_title()
def set_menubar(self, menu):
self.w['menu'] = menu
def setup_statusbar(self):
self.create_statusbar()
self.statusbar.observe(self.component)
def create_statusbar(self):
self.statusbar = Statusbar(self.w)
self.statusbar.pack(side=BOTTOM, fill=X)
def ping_statusbar(self): # later - 'metadata_changed'?
if self.statusbar is not None:
self.statusbar.update()
def move_to_front(self, component):
"Adjust the container so the given component is brought forward."
# we can ignore component parameter for now, as base container
# supports only a single component
try:
if self.w.wm_state() == "iconic":
self.w.wm_withdraw()
self.w.wm_deiconify()
self.w.tkraise()
except TclError:
# This can happen when the window menu was torn off.
# Simply ignore it.
pass
class TabbedContainer(Container, UITabsObserver):
def __init__(self, flist):
Container.__init__(self, flist)
self.containers = {} # map tab to container
self.active = None
self.w.bind('<Activate>', lambda e: self._activate())
self.w.bind('<FocusIn>', lambda e: self._activate())
def add_component(self, component):
id = self.tabs.add(title='Shell***')
self.containers[id] = component.top
def make_widget(self):
t = Container.make_widget(self)
self.tabs = UITabs(t, self)
self.tabs.pack(side='top', fill='x')
self.main = Frame(t)
self.main.pack(side='top', fill='both')
return t
def move_to_front(self, component):
Container.move_to_front(self, component)
tabid = self.get_tabid(component.top)
self.tabs.select(tabid)
def handle_addtab(self, tabs):
self.flist.new()
def remove(self, container):
tabid = self.get_tabid(container)
if tabid is not None:
if container == self.active:
self.tab_deselected(self.tabs, tabid)
self.active = None
self.component = None
del(self.containers[tabid])
self.tabs.remove(tabid)
if len(self.containers) == 0:
self._close()
def _activate(self):
# Called when we get moved to the front
self.flist.tab_container = self
def _close(self):
"Close button on window - try to close all tabs"
# loop through all dirty containers, if nobody aborts, we're good
# to close the whole window
for t, c in self.containers.items():
if c.component is not None and not c.component.get_saved():
# NOTE: while maybesave() does move the tab to the front,
# because of deferred drawing plus the 'after_idle' in
# our tab_selected routine, the changes don't appear
# onscreen immediately.
#
# This is normally not a problem, but because this may
# be immediately followed by a modal dialog call which
# blocks the update events, we ensure everything is
# onscreen before calling maybesave()
self.move_to_front(c.component)
self.w.update()
self.w.update_idletasks()
if c.component.maybesave() == 'cancel':
return # user aborted the close
for t, c in self.containers.items():
if c.component is not None:
c.component.close(without_save=True) # already asked
self.w.destroy()
self.w = None
self.component = None
if self.flist:
self.flist.delete_container(self)
def handle_closetab(self, tabs, tabid):
self.containers[tabid]._close()
def tab_deselected(self, tabs, tabid):
if len(self.containers) == 0:
return
self.w.pack_propagate(False)
self.containers[tabid].w.pack_forget()
self.w['menu'] = None
def setup_statusbar(self):
if self.statusbar is None:
self.create_statusbar()
def get_tabid(self, container):
for t in self.containers:
if self.containers[t] == container:
return t
return None
def container_title_changed(self, container):
tabid = self.get_tabid(container)
if tabid is not None:
self.tabs.set_title(tabid, container.short_title)
if container.title is not None:
self.tabs.set_tooltip(tabid, container.title)
self.tabs.set_dirty(tabid, not container.saved)
if container == self.active:
self.w.wm_title(container.title if container.title else container.short_title)
self.w.wm_iconname(container.short_title)
self.flist.filenames_changed() # to update window list
def tab_selected(self, tabs, tabid):
self.w.after_idle(lambda: self._tab_selected(tabid))
def _tab_selected(self, tabid):
if self.active != self.containers[tabid]:
self.active = self.containers[tabid]
self.component = self.active.component
self.active.w.pack(side='top', fill='both')
self.w['menu'] = self.active.menubar
self.statusbar.observe(self.active.component)
self.container_title_changed(self.active)
self.w.pack_propagate(True)
class ProxyContainer(Container):
def __init__(self, top_container):
self.top_container = top_container
Container.__init__(self, top_container.flist)
self.top_container.flist.delete_container(self)
self.short_title = ''
self.title = ''
self.saved = ''
self.menubar = None
def _close(self):
if self.component is not None:
try:
self.component.close()
self.top_container.remove(self)
except Exception: # if file needs saving, user may abort
pass
def add_component(self, component):
Container.add_component(self, component)
self.top_container.add_component(component)
def make_widget(self):
return Frame(self.top_container.w)
def active(self):
return self.top_container.active == self
def title_changed(self):
self.short_title = self.component.short_title()
self.long_title = self.component.long_title()
self.saved = self.component.get_saved()
self.top_container.container_title_changed(self)
def get_title(self):
return self.title
def set_menubar(self, menu):
self.menubar = menu
if self.active():
self.top_container.set_menubar(menu)
def setup_statusbar(self):
self.top_container.setup_statusbar()
def ping_statusbar(self):
if self.active():
self.top_container.ping_statusbar()
def move_to_front(self, component):
self.top_container.move_to_front(component)