1999-01-02 22:28:54 +01:00
|
|
|
from Tkinter import *
|
|
|
|
|
|
|
|
class WindowList:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.dict = {}
|
1999-02-17 18:19:37 +01:00
|
|
|
self.callbacks = []
|
1999-01-02 22:28:54 +01:00
|
|
|
|
|
|
|
def add(self, window):
|
1999-02-17 18:19:37 +01:00
|
|
|
window.after_idle(self.call_callbacks)
|
1999-01-02 22:28:54 +01:00
|
|
|
self.dict[str(window)] = window
|
|
|
|
|
|
|
|
def delete(self, window):
|
|
|
|
try:
|
|
|
|
del self.dict[str(window)]
|
|
|
|
except KeyError:
|
|
|
|
# Sometimes, destroy() is called twice
|
|
|
|
pass
|
1999-02-17 18:34:25 +01:00
|
|
|
self.call_callbacks()
|
1999-01-02 22:28:54 +01:00
|
|
|
|
|
|
|
def add_windows_to_menu(self, menu):
|
|
|
|
list = []
|
|
|
|
for key in self.dict.keys():
|
|
|
|
window = self.dict[key]
|
1999-02-08 23:27:49 +01:00
|
|
|
try:
|
|
|
|
title = window.get_title()
|
|
|
|
except TclError:
|
|
|
|
continue
|
1999-01-02 22:28:54 +01:00
|
|
|
list.append((title, window))
|
|
|
|
list.sort()
|
|
|
|
for title, window in list:
|
|
|
|
if title == "Python Shell":
|
|
|
|
# Hack -- until we have a better way to this
|
|
|
|
continue
|
|
|
|
menu.add_command(label=title, command=window.wakeup)
|
|
|
|
|
1999-02-17 18:19:37 +01:00
|
|
|
def register_callback(self, callback):
|
|
|
|
self.callbacks.append(callback)
|
|
|
|
|
|
|
|
def unregister_callback(self, callback):
|
|
|
|
try:
|
2001-01-17 09:48:39 +01:00
|
|
|
self.callbacks.remove(callback)
|
2000-02-15 19:03:01 +01:00
|
|
|
except ValueError:
|
1999-02-17 18:19:37 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def call_callbacks(self):
|
|
|
|
for callback in self.callbacks:
|
|
|
|
try:
|
|
|
|
callback()
|
|
|
|
except:
|
|
|
|
print "warning: callback failed in WindowList", \
|
|
|
|
sys.exc_type, ":", sys.exc_value
|
|
|
|
|
1999-01-02 22:28:54 +01:00
|
|
|
registry = WindowList()
|
|
|
|
|
1999-02-17 18:19:37 +01:00
|
|
|
add_windows_to_menu = registry.add_windows_to_menu
|
|
|
|
register_callback = registry.register_callback
|
|
|
|
unregister_callback = registry.unregister_callback
|
|
|
|
|
1999-01-02 22:28:54 +01:00
|
|
|
|
|
|
|
class ListedToplevel(Toplevel):
|
|
|
|
|
|
|
|
def __init__(self, master, **kw):
|
|
|
|
Toplevel.__init__(self, master, kw)
|
|
|
|
registry.add(self)
|
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
registry.delete(self)
|
|
|
|
Toplevel.destroy(self)
|
|
|
|
|
|
|
|
def get_title(self):
|
|
|
|
# Subclass can override
|
|
|
|
return self.wm_title()
|
|
|
|
|
|
|
|
def wakeup(self):
|
1999-01-09 23:01:33 +01:00
|
|
|
try:
|
1999-01-18 16:18:57 +01:00
|
|
|
if self.wm_state() == "iconic":
|
|
|
|
self.wm_deiconify()
|
|
|
|
else:
|
|
|
|
self.tkraise()
|
1999-01-09 23:01:33 +01:00
|
|
|
self.focus_set()
|
|
|
|
except TclError:
|
|
|
|
# This can happen when the window menu was torn off.
|
|
|
|
# Simply ignore it.
|
|
|
|
pass
|