| 
									
										
										
										
											2006-06-11 19:43:49 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | A number of function that enhance IDLE on MacOSX when it used as a normal | 
					
						
							|  |  |  | GUI application (as opposed to an X11 application). | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2008-05-20 07:13:37 +00:00
										 |  |  | import Tkinter | 
					
						
							| 
									
										
										
										
											2006-06-11 19:43:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def runningAsOSXApp(): | 
					
						
							| 
									
										
										
										
											2009-03-04 21:35:05 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Returns True if Python is running from within an app on OSX. | 
					
						
							|  |  |  |     If so, assume that Python was built with Aqua Tcl/Tk rather than | 
					
						
							| 
									
										
										
										
											2009-08-05 23:48:26 +00:00
										 |  |  |     X11 Tcl/Tk. | 
					
						
							| 
									
										
										
										
											2009-03-04 21:35:05 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     return (sys.platform == 'darwin' and '.app' in sys.executable) | 
					
						
							| 
									
										
										
										
											2006-06-11 19:43:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def addOpenEventSupport(root, flist): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     This ensures that the application will respont to open AppleEvents, which | 
					
						
							|  |  |  |     makes is feaseable to use IDLE as the default application for python files. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     def doOpenFile(*args): | 
					
						
							|  |  |  |         for fn in args: | 
					
						
							|  |  |  |             flist.open(fn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # The command below is a hook in aquatk that is called whenever the app | 
					
						
							|  |  |  |     # receives a file open event. The callback can have multiple arguments, | 
					
						
							|  |  |  |     # one for every file that should be opened. | 
					
						
							|  |  |  |     root.createcommand("::tk::mac::OpenDocument", doOpenFile) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def hideTkConsole(root): | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         root.tk.call('console', 'hide') | 
					
						
							| 
									
										
										
										
											2008-05-20 07:13:37 +00:00
										 |  |  |     except Tkinter.TclError: | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  |         # Some versions of the Tk framework don't have a console object | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2006-06-11 19:43:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  | def overrideRootMenu(root, flist): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Replace the Tk root menu by something that's more appropriate for | 
					
						
							|  |  |  |     IDLE. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2006-07-24 21:02:15 +00:00
										 |  |  |     # The menu that is attached to the Tk root (".") is also used by AquaTk for | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |     # all windows that don't specify a menu of their own. The default menubar | 
					
						
							|  |  |  |     # contains a number of menus, none of which are appropriate for IDLE. The | 
					
						
							|  |  |  |     # Most annoying of those is an 'About Tck/Tk...' menu in the application | 
					
						
							|  |  |  |     # menu. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # This function replaces the default menubar by a mostly empty one, it | 
					
						
							|  |  |  |     # should only contain the correct application menu and the window menu. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Due to a (mis-)feature of TkAqua the user will also see an empty Help | 
					
						
							|  |  |  |     # menu. | 
					
						
							| 
									
										
										
										
											2008-05-20 07:13:37 +00:00
										 |  |  |     from Tkinter import Menu, Text, Text | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |     from EditorWindow import prepstr, get_accelerator | 
					
						
							|  |  |  |     import Bindings | 
					
						
							|  |  |  |     import WindowList | 
					
						
							|  |  |  |     from MultiCall import MultiCallCreator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     menubar = Menu(root) | 
					
						
							|  |  |  |     root.configure(menu=menubar) | 
					
						
							|  |  |  |     menudict = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     menudict['windows'] = menu = Menu(menubar, name='windows') | 
					
						
							|  |  |  |     menubar.add_cascade(label='Window', menu=menu, underline=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def postwindowsmenu(menu=menu): | 
					
						
							|  |  |  |         end = menu.index('end') | 
					
						
							|  |  |  |         if end is None: | 
					
						
							|  |  |  |             end = -1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if end > 0: | 
					
						
							|  |  |  |             menu.delete(0, end) | 
					
						
							|  |  |  |         WindowList.add_windows_to_menu(menu) | 
					
						
							|  |  |  |     WindowList.register_callback(postwindowsmenu) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     menudict['application'] = menu = Menu(menubar, name='apple') | 
					
						
							|  |  |  |     menubar.add_cascade(label='IDLE', menu=menu) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def about_dialog(event=None): | 
					
						
							|  |  |  |         import aboutDialog | 
					
						
							|  |  |  |         aboutDialog.AboutDialog(root, 'About IDLE') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def config_dialog(event=None): | 
					
						
							|  |  |  |         import configDialog | 
					
						
							| 
									
										
										
										
											2009-05-26 18:44:48 +00:00
										 |  |  |         root.instance_dict = flist.inversedict | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |         configDialog.ConfigDialog(root, 'Settings') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |     root.bind('<<about-idle>>', about_dialog) | 
					
						
							|  |  |  |     root.bind('<<open-config-dialog>>', config_dialog) | 
					
						
							|  |  |  |     if flist: | 
					
						
							|  |  |  |         root.bind('<<close-all-windows>>', flist.close_all_callback) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding | 
					
						
							|  |  |  |     tkversion = root.tk.eval('info patchlevel') | 
					
						
							| 
									
										
										
										
											2009-01-02 12:59:32 +00:00
										 |  |  |     # Note: we cannot check if the string tkversion >= '8.4.14', because | 
					
						
							|  |  |  |     # the string '8.4.7' is greater than the string '8.4.14'. | 
					
						
							| 
									
										
										
										
											2009-05-26 18:44:48 +00:00
										 |  |  |     if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14): | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  |         Bindings.menudefs[0] =  ('application', [ | 
					
						
							|  |  |  |                 ('About IDLE', '<<about-idle>>'), | 
					
						
							|  |  |  |                 None, | 
					
						
							|  |  |  |             ]) | 
					
						
							|  |  |  |         root.createcommand('::tk::mac::ShowPreferences', config_dialog) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         for mname, entrylist in Bindings.menudefs: | 
					
						
							|  |  |  |             menu = menudict.get(mname) | 
					
						
							|  |  |  |             if not menu: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  |                 for entry in entrylist: | 
					
						
							|  |  |  |                     if not entry: | 
					
						
							|  |  |  |                         menu.add_separator() | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         label, eventname = entry | 
					
						
							|  |  |  |                         underline, label = prepstr(label) | 
					
						
							|  |  |  |                         accelerator = get_accelerator(Bindings.default_keydefs, | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |                         eventname) | 
					
						
							| 
									
										
										
										
											2007-07-09 06:02:21 +00:00
										 |  |  |                         def command(text=root, eventname=eventname): | 
					
						
							|  |  |  |                             text.event_generate(eventname) | 
					
						
							|  |  |  |                         menu.add_command(label=label, underline=underline, | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |                         command=command, accelerator=accelerator) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-11 19:43:49 +00:00
										 |  |  | def setupApp(root, flist): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Perform setup for the OSX application bundle. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not runningAsOSXApp(): return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hideTkConsole(root) | 
					
						
							| 
									
										
										
										
											2006-07-23 09:46:11 +00:00
										 |  |  |     overrideRootMenu(root, flist) | 
					
						
							| 
									
										
										
										
											2006-06-11 19:43:49 +00:00
										 |  |  |     addOpenEventSupport(root, flist) |