Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 1 | """Main Pynche (Pythonically Natural Color and Hue Editor) widget. |
Barry Warsaw | eb61fbd | 1998-10-02 16:06:27 +0000 | [diff] [blame] | 2 | |
| 3 | This window provides the basic decorations, primarily including the menubar. |
| 4 | It is used to bring up other windows. |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 5 | """ |
| 6 | |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 7 | import sys |
| 8 | import os |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 9 | from tkinter import * |
| 10 | from tkinter import messagebox, filedialog |
Barry Warsaw | 3369927 | 1999-04-27 19:51:55 +0000 | [diff] [blame] | 11 | import ColorDB |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 12 | |
| 13 | # Milliseconds between interrupt checks |
| 14 | KEEPALIVE_TIMER = 500 |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 15 | |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 16 | |
| 17 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 18 | class PyncheWidget: |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 19 | def __init__(self, version, switchboard, master=None, extrapath=[]): |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 20 | self.__sb = switchboard |
| 21 | self.__version = version |
| 22 | self.__textwin = None |
Barry Warsaw | bd36d6e | 1998-10-02 16:05:48 +0000 | [diff] [blame] | 23 | self.__listwin = None |
Barry Warsaw | 9044b8e | 1998-10-05 21:14:46 +0000 | [diff] [blame] | 24 | self.__detailswin = None |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 25 | self.__helpwin = None |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 26 | self.__dialogstate = {} |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 27 | 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 Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 50 | # create the menubar |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 51 | menubar = self.__menubar = Menu(tkroot) |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 52 | # |
| 53 | # File menu |
| 54 | # |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 55 | filemenu = self.__filemenu = Menu(menubar, tearoff=0) |
| 56 | filemenu.add_command(label='Load palette...', |
| 57 | command=self.__load, |
| 58 | underline=0) |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 59 | if not modal: |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 60 | filemenu.add_command(label='Quit', |
| 61 | command=self.__quit, |
| 62 | accelerator='Alt-Q', |
| 63 | underline=0) |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 64 | # |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 65 | # View menu |
| 66 | # |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 67 | views = make_view_popups(self.__sb, self.__tkroot, extrapath) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 68 | viewmenu = Menu(menubar, tearoff=0) |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 69 | for v in views: |
| 70 | viewmenu.add_command(label=v.menutext(), |
| 71 | command=v.popup, |
| 72 | underline=v.underline()) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 73 | # |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 74 | # Help menu |
| 75 | # |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 76 | helpmenu = Menu(menubar, name='help', tearoff=0) |
Martin v. Löwis | 23b44a3 | 2003-10-24 20:09:23 +0000 | [diff] [blame] | 77 | helpmenu.add_command(label='About Pynche...', |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 78 | command=self.__popup_about, |
| 79 | underline=0) |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 80 | helpmenu.add_command(label='Help...', |
| 81 | command=self.__popup_usage, |
| 82 | underline=0) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 83 | # |
| 84 | # Tie them all together |
| 85 | # |
Barry Warsaw | 6e7f6ea | 1999-04-26 23:36:47 +0000 | [diff] [blame] | 86 | menubar.add_cascade(label='File', |
| 87 | menu=filemenu, |
| 88 | underline=0) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 89 | 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 Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 98 | root.protocol('WM_DELETE_WINDOW', |
Barry Warsaw | d9e5214 | 1998-10-22 18:46:28 +0000 | [diff] [blame] | 99 | modal and self.__bell or self.__quit) |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 100 | root.title('Pynche %s' % version) |
| 101 | root.iconname('Pynche') |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 102 | # 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 Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame] | 121 | |
| 122 | def __quit(self, event=None): |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 123 | self.__tkroot.quit() |
| 124 | |
Barry Warsaw | d9e5214 | 1998-10-22 18:46:28 +0000 | [diff] [blame] | 125 | def __bell(self, event=None): |
| 126 | self.__tkroot.bell() |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 127 | |
| 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 Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 136 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 137 | def __keepalive(self): |
| 138 | # Exercise the Python interpreter regularly so keyboard interrupts get |
| 139 | # through. |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 140 | self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive) |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 141 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 142 | def start(self): |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 143 | if not self.__modal: |
| 144 | self.__keepalive() |
Barry Warsaw | 3836503 | 1998-10-06 19:39:34 +0000 | [diff] [blame] | 145 | self.__tkroot.mainloop() |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 146 | |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 147 | def window(self): |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 148 | return self.__root |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 149 | |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 150 | def __popup_about(self, event=None): |
Barry Warsaw | 0926dea | 1998-10-07 03:36:58 +0000 | [diff] [blame] | 151 | from Main import __version__ |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 152 | messagebox.showinfo('About Pynche ' + __version__, |
Barry Warsaw | a9b4558 | 1998-09-29 20:03:53 +0000 | [diff] [blame] | 153 | '''\ |
Barry Warsaw | ad3a67c | 1998-10-06 18:52:59 +0000 | [diff] [blame] | 154 | Pynche %s |
| 155 | The PYthonically Natural |
| 156 | Color and Hue Editor |
Barry Warsaw | a69c1ba | 1998-09-28 23:38:44 +0000 | [diff] [blame] | 157 | |
Barry Warsaw | 6403991 | 1998-12-15 01:02:51 +0000 | [diff] [blame] | 158 | For information |
| 159 | contact: Barry A. Warsaw |
| 160 | email: bwarsaw@python.org''' % __version__) |
Barry Warsaw | 86daeb7 | 1998-10-01 16:46:16 +0000 | [diff] [blame] | 161 | |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 162 | 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 Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 167 | def __load(self, event=None): |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 168 | while 1: |
Barry Warsaw | 3369927 | 1999-04-27 19:51:55 +0000 | [diff] [blame] | 169 | idir, ifile = os.path.split(self.__sb.colordb().filename()) |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 170 | file = filedialog.askopenfilename( |
Barry Warsaw | 3369927 | 1999-04-27 19:51:55 +0000 | [diff] [blame] | 171 | filetypes=[('Text files', '*.txt'), |
| 172 | ('All files', '*'), |
| 173 | ], |
| 174 | initialdir=idir, |
| 175 | initialfile=ifile) |
| 176 | if not file: |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 177 | # cancel button |
| 178 | return |
| 179 | try: |
| 180 | colordb = ColorDB.get_colordb(file) |
| 181 | except IOError: |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 182 | messagebox.showerror('Read error', '''\ |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 183 | Could not open file for reading: |
| 184 | %s''' % file) |
| 185 | continue |
| 186 | if colordb is None: |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 187 | messagebox.showerror('Unrecognized color file type', '''\ |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 188 | Unrecognized color file type in file: |
| 189 | %s''' % file) |
| 190 | continue |
| 191 | break |
| 192 | self.__sb.set_colordb(colordb) |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 193 | |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 194 | def withdraw(self): |
| 195 | self.__root.withdraw() |
Barry Warsaw | d9e5214 | 1998-10-22 18:46:28 +0000 | [diff] [blame] | 196 | |
| 197 | def deiconify(self): |
| 198 | self.__root.deiconify() |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | |
| 202 | class Helpwin: |
| 203 | def __init__(self, master, quitfunc): |
Barry Warsaw | 6a55226 | 2001-04-18 03:50:07 +0000 | [diff] [blame] | 204 | from Main import docstring |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 205 | 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 Warsaw | 8e4fa07 | 2001-07-10 21:44:24 +0000 | [diff] [blame] | 222 | i = contents.rfind('\f') |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 223 | if i > 0: |
Barry Warsaw | 8e4fa07 | 2001-07-10 21:44:24 +0000 | [diff] [blame] | 224 | contents = contents[:i].rstrip() |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 225 | 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 Warsaw | ecb1a65 | 1999-03-26 16:11:40 +0000 | [diff] [blame] | 235 | self.__text.focus_set() |
Barry Warsaw | ce0bbd2 | 1998-12-03 19:50:24 +0000 | [diff] [blame] | 236 | 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 Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 251 | import functools |
| 252 | @functools.total_ordering |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 253 | class 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 Warsaw | 8e4fa07 | 2001-07-10 21:44:24 +0000 | [diff] [blame] | 261 | underline = module.ADDTOVIEW.find('%') |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 262 | if underline == -1: |
| 263 | underline = 0 |
| 264 | else: |
Barry Warsaw | 8e4fa07 | 2001-07-10 21:44:24 +0000 | [diff] [blame] | 265 | self.__menutext = module.ADDTOVIEW.replace('%', '', 1) |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 266 | 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 Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 283 | def __eq__(self, other): |
| 284 | return self.__menutext == other.__menutext |
| 285 | |
| 286 | def __lt__(self, other): |
| 287 | return self.__menutext < other.__menutext |
Barry Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 288 | |
| 289 | |
| 290 | def 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 Warsaw | 17a8b5d | 1999-07-06 22:00:52 +0000 | [diff] [blame] | 300 | 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 Warsaw | 9af697b | 1999-04-27 18:55:48 +0000 | [diff] [blame] | 307 | 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 |