blob: 364f22b0b2095c078c7a4fd8c2ca30a647c9cb6c [file] [log] [blame]
Barry Warsawfefbf791998-01-31 23:39:01 +00001"""Main Pynche (Pythonically Natural Color and Hue Editor) widget.
Barry Warsaweb61fbd1998-10-02 16:06:27 +00002
3This window provides the basic decorations, primarily including the menubar.
4It is used to bring up other windows.
Barry Warsawfefbf791998-01-31 23:39:01 +00005"""
6
Barry Warsawce0bbd21998-12-03 19:50:24 +00007import sys
8import os
Georg Brandlbf76ce12010-08-02 22:08:58 +00009from tkinter import *
10from tkinter import messagebox, filedialog
Barry Warsaw33699271999-04-27 19:51:55 +000011import ColorDB
Barry Warsawa69c1ba1998-09-28 23:38:44 +000012
13# Milliseconds between interrupt checks
14KEEPALIVE_TIMER = 500
Barry Warsawfefbf791998-01-31 23:39:01 +000015
Barry Warsaw552ac171998-02-17 22:25:23 +000016
17
Barry Warsawa69c1ba1998-09-28 23:38:44 +000018class PyncheWidget:
Barry Warsaw9af697b1999-04-27 18:55:48 +000019 def __init__(self, version, switchboard, master=None, extrapath=[]):
Barry Warsaw86daeb71998-10-01 16:46:16 +000020 self.__sb = switchboard
21 self.__version = version
22 self.__textwin = None
Barry Warsawbd36d6e1998-10-02 16:05:48 +000023 self.__listwin = None
Barry Warsaw9044b8e1998-10-05 21:14:46 +000024 self.__detailswin = None
Barry Warsawce0bbd21998-12-03 19:50:24 +000025 self.__helpwin = None
Barry Warsaw0604d721999-04-26 23:17:16 +000026 self.__dialogstate = {}
Barry Warsawca07ba01998-10-22 03:25:59 +000027 modal = self.__modal = not not master
28 # If a master was given, we are running as a modal dialog servant to
29 # some other application. We rearrange our UI in this case (there's
30 # no File menu and we get `Okay' and `Cancel' buttons), and we do a
31 # grab_set() to make ourselves modal
32 if modal:
33 self.__tkroot = tkroot = Toplevel(master, class_='Pynche')
34 tkroot.grab_set()
35 tkroot.withdraw()
36 else:
37 # Is there already a default root for Tk, say because we're
38 # running under Guido's IDE? :-) Two conditions say no, either the
39 # import fails or _default_root is None.
40 tkroot = None
41 try:
42 from Tkinter import _default_root
43 tkroot = self.__tkroot = _default_root
44 except ImportError:
45 pass
46 if not tkroot:
47 tkroot = self.__tkroot = Tk(className='Pynche')
48 # but this isn't our top level widget, so make it invisible
49 tkroot.withdraw()
Barry Warsawa69c1ba1998-09-28 23:38:44 +000050 # create the menubar
Barry Warsaw38365031998-10-06 19:39:34 +000051 menubar = self.__menubar = Menu(tkroot)
Barry Warsawa69c1ba1998-09-28 23:38:44 +000052 #
53 # File menu
54 #
Barry Warsaw0604d721999-04-26 23:17:16 +000055 filemenu = self.__filemenu = Menu(menubar, tearoff=0)
56 filemenu.add_command(label='Load palette...',
57 command=self.__load,
58 underline=0)
Barry Warsawca07ba01998-10-22 03:25:59 +000059 if not modal:
Barry Warsawca07ba01998-10-22 03:25:59 +000060 filemenu.add_command(label='Quit',
61 command=self.__quit,
62 accelerator='Alt-Q',
63 underline=0)
Barry Warsawa69c1ba1998-09-28 23:38:44 +000064 #
Barry Warsaw86daeb71998-10-01 16:46:16 +000065 # View menu
66 #
Barry Warsaw9af697b1999-04-27 18:55:48 +000067 views = make_view_popups(self.__sb, self.__tkroot, extrapath)
Barry Warsaw38365031998-10-06 19:39:34 +000068 viewmenu = Menu(menubar, tearoff=0)
Barry Warsaw9af697b1999-04-27 18:55:48 +000069 for v in views:
70 viewmenu.add_command(label=v.menutext(),
71 command=v.popup,
72 underline=v.underline())
Barry Warsaw38365031998-10-06 19:39:34 +000073 #
Barry Warsawa69c1ba1998-09-28 23:38:44 +000074 # Help menu
75 #
Barry Warsaw38365031998-10-06 19:39:34 +000076 helpmenu = Menu(menubar, name='help', tearoff=0)
Martin v. Löwis23b44a32003-10-24 20:09:23 +000077 helpmenu.add_command(label='About Pynche...',
Barry Warsawa69c1ba1998-09-28 23:38:44 +000078 command=self.__popup_about,
79 underline=0)
Barry Warsawce0bbd21998-12-03 19:50:24 +000080 helpmenu.add_command(label='Help...',
81 command=self.__popup_usage,
82 underline=0)
Barry Warsaw38365031998-10-06 19:39:34 +000083 #
84 # Tie them all together
85 #
Barry Warsaw6e7f6ea1999-04-26 23:36:47 +000086 menubar.add_cascade(label='File',
87 menu=filemenu,
88 underline=0)
Barry Warsaw38365031998-10-06 19:39:34 +000089 menubar.add_cascade(label='View',
90 menu=viewmenu,
91 underline=0)
92 menubar.add_cascade(label='Help',
93 menu=helpmenu,
94 underline=0)
95
96 # now create the top level window
97 root = self.__root = Toplevel(tkroot, class_='Pynche', menu=menubar)
Barry Warsawca07ba01998-10-22 03:25:59 +000098 root.protocol('WM_DELETE_WINDOW',
Barry Warsawd9e52141998-10-22 18:46:28 +000099 modal and self.__bell or self.__quit)
Barry Warsaw38365031998-10-06 19:39:34 +0000100 root.title('Pynche %s' % version)
101 root.iconname('Pynche')
Barry Warsawca07ba01998-10-22 03:25:59 +0000102 # Only bind accelerators for the File->Quit menu item if running as a
103 # standalone app
104 if not modal:
105 root.bind('<Alt-q>', self.__quit)
106 root.bind('<Alt-Q>', self.__quit)
107 else:
108 # We're a modal dialog so we have a new row of buttons
109 bframe = Frame(root, borderwidth=1, relief=RAISED)
110 bframe.grid(row=4, column=0, columnspan=2,
111 sticky='EW',
112 ipady=5)
113 okay = Button(bframe,
114 text='Okay',
115 command=self.__okay)
116 okay.pack(side=LEFT, expand=1)
117 cancel = Button(bframe,
118 text='Cancel',
119 command=self.__cancel)
120 cancel.pack(side=LEFT, expand=1)
Barry Warsawe7f4a471998-10-06 19:50:33 +0000121
122 def __quit(self, event=None):
Barry Warsawca07ba01998-10-22 03:25:59 +0000123 self.__tkroot.quit()
124
Barry Warsawd9e52141998-10-22 18:46:28 +0000125 def __bell(self, event=None):
126 self.__tkroot.bell()
Barry Warsawca07ba01998-10-22 03:25:59 +0000127
128 def __okay(self, event=None):
129 self.__sb.withdraw_views()
130 self.__tkroot.grab_release()
131 self.__quit()
132
133 def __cancel(self, event=None):
134 self.__sb.canceled()
135 self.__okay()
Barry Warsaw552ac171998-02-17 22:25:23 +0000136
Barry Warsawa69c1ba1998-09-28 23:38:44 +0000137 def __keepalive(self):
138 # Exercise the Python interpreter regularly so keyboard interrupts get
139 # through.
Barry Warsaw38365031998-10-06 19:39:34 +0000140 self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
Barry Warsaw552ac171998-02-17 22:25:23 +0000141
Barry Warsawa69c1ba1998-09-28 23:38:44 +0000142 def start(self):
Barry Warsawca07ba01998-10-22 03:25:59 +0000143 if not self.__modal:
144 self.__keepalive()
Barry Warsaw38365031998-10-06 19:39:34 +0000145 self.__tkroot.mainloop()
Barry Warsawa69c1ba1998-09-28 23:38:44 +0000146
Barry Warsawca07ba01998-10-22 03:25:59 +0000147 def window(self):
Barry Warsawa69c1ba1998-09-28 23:38:44 +0000148 return self.__root
Barry Warsawf67575d1998-03-10 00:17:01 +0000149
Barry Warsawa69c1ba1998-09-28 23:38:44 +0000150 def __popup_about(self, event=None):
Barry Warsaw0926dea1998-10-07 03:36:58 +0000151 from Main import __version__
Georg Brandlbf76ce12010-08-02 22:08:58 +0000152 messagebox.showinfo('About Pynche ' + __version__,
Barry Warsawa9b45581998-09-29 20:03:53 +0000153 '''\
Barry Warsawad3a67c1998-10-06 18:52:59 +0000154Pynche %s
155The PYthonically Natural
156Color and Hue Editor
Barry Warsawa69c1ba1998-09-28 23:38:44 +0000157
Barry Warsaw64039911998-12-15 01:02:51 +0000158For information
159contact: Barry A. Warsaw
160email: bwarsaw@python.org''' % __version__)
Barry Warsaw86daeb71998-10-01 16:46:16 +0000161
Barry Warsawce0bbd21998-12-03 19:50:24 +0000162 def __popup_usage(self, event=None):
163 if not self.__helpwin:
164 self.__helpwin = Helpwin(self.__root, self.__quit)
165 self.__helpwin.deiconify()
166
Barry Warsaw0604d721999-04-26 23:17:16 +0000167 def __load(self, event=None):
Barry Warsaw0604d721999-04-26 23:17:16 +0000168 while 1:
Barry Warsaw33699271999-04-27 19:51:55 +0000169 idir, ifile = os.path.split(self.__sb.colordb().filename())
Georg Brandlbf76ce12010-08-02 22:08:58 +0000170 file = filedialog.askopenfilename(
Barry Warsaw33699271999-04-27 19:51:55 +0000171 filetypes=[('Text files', '*.txt'),
172 ('All files', '*'),
173 ],
174 initialdir=idir,
175 initialfile=ifile)
176 if not file:
Barry Warsaw0604d721999-04-26 23:17:16 +0000177 # cancel button
178 return
179 try:
180 colordb = ColorDB.get_colordb(file)
181 except IOError:
Georg Brandlbf76ce12010-08-02 22:08:58 +0000182 messagebox.showerror('Read error', '''\
Barry Warsaw0604d721999-04-26 23:17:16 +0000183Could not open file for reading:
184%s''' % file)
185 continue
186 if colordb is None:
Georg Brandlbf76ce12010-08-02 22:08:58 +0000187 messagebox.showerror('Unrecognized color file type', '''\
Barry Warsaw0604d721999-04-26 23:17:16 +0000188Unrecognized color file type in file:
189%s''' % file)
190 continue
191 break
192 self.__sb.set_colordb(colordb)
Barry Warsaw0604d721999-04-26 23:17:16 +0000193
Barry Warsawca07ba01998-10-22 03:25:59 +0000194 def withdraw(self):
195 self.__root.withdraw()
Barry Warsawd9e52141998-10-22 18:46:28 +0000196
197 def deiconify(self):
198 self.__root.deiconify()
Barry Warsawce0bbd21998-12-03 19:50:24 +0000199
200
201
202class Helpwin:
203 def __init__(self, master, quitfunc):
Barry Warsaw6a552262001-04-18 03:50:07 +0000204 from Main import docstring
Barry Warsawce0bbd21998-12-03 19:50:24 +0000205 self.__root = root = Toplevel(master, class_='Pynche')
206 root.protocol('WM_DELETE_WINDOW', self.__withdraw)
207 root.title('Pynche Help Window')
208 root.iconname('Pynche Help Window')
209 root.bind('<Alt-q>', quitfunc)
210 root.bind('<Alt-Q>', quitfunc)
211 root.bind('<Alt-w>', self.__withdraw)
212 root.bind('<Alt-W>', self.__withdraw)
213
214 # more elaborate help is available in the README file
215 readmefile = os.path.join(sys.path[0], 'README')
216 try:
217 fp = None
218 try:
219 fp = open(readmefile)
220 contents = fp.read()
221 # wax the last page, it contains Emacs cruft
Barry Warsaw8e4fa072001-07-10 21:44:24 +0000222 i = contents.rfind('\f')
Barry Warsawce0bbd21998-12-03 19:50:24 +0000223 if i > 0:
Barry Warsaw8e4fa072001-07-10 21:44:24 +0000224 contents = contents[:i].rstrip()
Barry Warsawce0bbd21998-12-03 19:50:24 +0000225 finally:
226 if fp:
227 fp.close()
228 except IOError:
229 sys.stderr.write("Couldn't open Pynche's README, "
230 'using docstring instead.\n')
231 contents = docstring()
232
233 self.__text = text = Text(root, relief=SUNKEN,
234 width=80, height=24)
Barry Warsawecb1a651999-03-26 16:11:40 +0000235 self.__text.focus_set()
Barry Warsawce0bbd21998-12-03 19:50:24 +0000236 text.insert(0.0, contents)
237 scrollbar = Scrollbar(root)
238 scrollbar.pack(fill=Y, side=RIGHT)
239 text.pack(fill=BOTH, expand=YES)
240 text.configure(yscrollcommand=(scrollbar, 'set'))
241 scrollbar.configure(command=(text, 'yview'))
242
243 def __withdraw(self, event=None):
244 self.__root.withdraw()
245
246 def deiconify(self):
247 self.__root.deiconify()
Barry Warsaw9af697b1999-04-27 18:55:48 +0000248
249
250
Georg Brandlbf76ce12010-08-02 22:08:58 +0000251import functools
252@functools.total_ordering
Barry Warsaw9af697b1999-04-27 18:55:48 +0000253class PopupViewer:
254 def __init__(self, module, name, switchboard, root):
255 self.__m = module
256 self.__name = name
257 self.__sb = switchboard
258 self.__root = root
259 self.__menutext = module.ADDTOVIEW
260 # find the underline character
Barry Warsaw8e4fa072001-07-10 21:44:24 +0000261 underline = module.ADDTOVIEW.find('%')
Barry Warsaw9af697b1999-04-27 18:55:48 +0000262 if underline == -1:
263 underline = 0
264 else:
Barry Warsaw8e4fa072001-07-10 21:44:24 +0000265 self.__menutext = module.ADDTOVIEW.replace('%', '', 1)
Barry Warsaw9af697b1999-04-27 18:55:48 +0000266 self.__underline = underline
267 self.__window = None
268
269 def menutext(self):
270 return self.__menutext
271
272 def underline(self):
273 return self.__underline
274
275 def popup(self, event=None):
276 if not self.__window:
277 # class and module must have the same name
278 class_ = getattr(self.__m, self.__name)
279 self.__window = class_(self.__sb, self.__root)
280 self.__sb.add_view(self.__window)
281 self.__window.deiconify()
282
Georg Brandlbf76ce12010-08-02 22:08:58 +0000283 def __eq__(self, other):
284 return self.__menutext == other.__menutext
285
286 def __lt__(self, other):
287 return self.__menutext < other.__menutext
Barry Warsaw9af697b1999-04-27 18:55:48 +0000288
289
290def make_view_popups(switchboard, root, extrapath):
291 viewers = []
292 # where we are in the file system
293 dirs = [os.path.dirname(__file__)] + extrapath
294 for dir in dirs:
295 if dir == '':
296 dir = '.'
297 for file in os.listdir(dir):
298 if file[-9:] == 'Viewer.py':
299 name = file[:-3]
Barry Warsaw17a8b5d1999-07-06 22:00:52 +0000300 try:
301 module = __import__(name)
302 except ImportError:
303 # Pynche is running from inside a package, so get the
304 # module using the explicit path.
305 pkg = __import__('pynche.'+name)
306 module = getattr(pkg, name)
Barry Warsaw9af697b1999-04-27 18:55:48 +0000307 if hasattr(module, 'ADDTOVIEW') and module.ADDTOVIEW:
308 # this is an external viewer
309 v = PopupViewer(module, name, switchboard, root)
310 viewers.append(v)
311 # sort alphabetically
312 viewers.sort()
313 return viewers