| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  | "Dialog to specify or edit the parameters for a user configured help source." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2004-07-15 04:54:57 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  | from Tkinter import * | 
					
						
							|  |  |  | import tkMessageBox | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  | import tkFileDialog | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class GetHelpSourceDialog(Toplevel): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |     def __init__(self, parent, title, menuItem='', filePath=''): | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |         """Get menu entry and url/ local file location for Additional Help
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         User selects a name for the Help resource and provides a web url | 
					
						
							|  |  |  |         or a local file as its source.  The user can enter a url or browse | 
					
						
							|  |  |  |         for the file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Toplevel.__init__(self, parent) | 
					
						
							|  |  |  |         self.configure(borderwidth=5) | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.resizable(height=FALSE, width=FALSE) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.title(title) | 
					
						
							|  |  |  |         self.transient(parent) | 
					
						
							|  |  |  |         self.grab_set() | 
					
						
							|  |  |  |         self.protocol("WM_DELETE_WINDOW", self.Cancel) | 
					
						
							|  |  |  |         self.parent = parent | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.result = None | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.CreateWidgets() | 
					
						
							| 
									
										
										
										
											2002-03-27 02:25:44 +00:00
										 |  |  |         self.menu.set(menuItem) | 
					
						
							|  |  |  |         self.path.set(filePath) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.withdraw() #hide while setting geometry | 
					
						
							|  |  |  |         #needs to be done here so that the winfo_reqwidth is valid | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.update_idletasks() | 
					
						
							|  |  |  |         #centre dialog over parent: | 
					
						
							| 
									
										
										
										
											2002-12-31 16:03:23 +00:00
										 |  |  |         self.geometry("+%d+%d" % | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |                       ((parent.winfo_rootx() + ((parent.winfo_width()/2) | 
					
						
							|  |  |  |                                                 -(self.winfo_reqwidth()/2)), | 
					
						
							|  |  |  |                         parent.winfo_rooty() + ((parent.winfo_height()/2) | 
					
						
							|  |  |  |                                                 -(self.winfo_reqheight()/2))))) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.deiconify() #geometry set, unhide | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.bind('<Return>', self.Ok) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.wait_window() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def CreateWidgets(self): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.menu = StringVar(self) | 
					
						
							|  |  |  |         self.path = StringVar(self) | 
					
						
							|  |  |  |         self.fontSize = StringVar(self) | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |         self.frameMain = Frame(self, borderwidth=2, relief=GROOVE) | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH) | 
					
						
							|  |  |  |         labelMenu = Label(self.frameMain, anchor=W, justify=LEFT, | 
					
						
							|  |  |  |                           text='Menu Item:') | 
					
						
							|  |  |  |         self.entryMenu = Entry(self.frameMain, textvariable=self.menu, | 
					
						
							|  |  |  |                                width=30) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.entryMenu.focus_set() | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         labelPath = Label(self.frameMain, anchor=W, justify=LEFT, | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |                           text='Help File Path: Enter URL or browse for file') | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.entryPath = Entry(self.frameMain, textvariable=self.path, | 
					
						
							|  |  |  |                                width=40) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.entryMenu.focus_set() | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         labelMenu.pack(anchor=W, padx=5, pady=3) | 
					
						
							|  |  |  |         self.entryMenu.pack(anchor=W, padx=5, pady=3) | 
					
						
							|  |  |  |         labelPath.pack(anchor=W, padx=5, pady=3) | 
					
						
							|  |  |  |         self.entryPath.pack(anchor=W, padx=5, pady=3) | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |         browseButton = Button(self.frameMain, text='Browse', width=8, | 
					
						
							|  |  |  |                               command=self.browseFile) | 
					
						
							|  |  |  |         browseButton.pack(pady=3) | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         frameButtons = Frame(self) | 
					
						
							|  |  |  |         frameButtons.pack(side=BOTTOM, fill=X) | 
					
						
							|  |  |  |         self.buttonOk = Button(frameButtons, text='OK', | 
					
						
							|  |  |  |                                width=8, default=ACTIVE,  command=self.Ok) | 
					
						
							|  |  |  |         self.buttonOk.grid(row=0, column=0, padx=5,pady=5) | 
					
						
							|  |  |  |         self.buttonCancel = Button(frameButtons, text='Cancel', | 
					
						
							|  |  |  |                                    width=8, command=self.Cancel) | 
					
						
							|  |  |  |         self.buttonCancel.grid(row=0, column=1, padx=5, pady=5) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |     def browseFile(self): | 
					
						
							|  |  |  |         filetypes = [ | 
					
						
							|  |  |  |             ("HTML Files", "*.htm *.html", "TEXT"), | 
					
						
							|  |  |  |             ("PDF Files", "*.pdf", "TEXT"), | 
					
						
							|  |  |  |             ("Windows Help Files", "*.chm"), | 
					
						
							|  |  |  |             ("Text Files", "*.txt", "TEXT"), | 
					
						
							|  |  |  |             ("All Files", "*")] | 
					
						
							|  |  |  |         path = self.path.get() | 
					
						
							|  |  |  |         if path: | 
					
						
							|  |  |  |             dir, base = os.path.split(path) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             base = None | 
					
						
							| 
									
										
										
										
											2004-07-15 04:54:57 +00:00
										 |  |  |             if sys.platform[:3] == 'win': | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |                 dir = os.path.join(os.path.dirname(sys.executable), 'Doc') | 
					
						
							|  |  |  |                 if not os.path.isdir(dir): | 
					
						
							|  |  |  |                     dir = os.getcwd() | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 dir = os.getcwd() | 
					
						
							|  |  |  |         opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes) | 
					
						
							|  |  |  |         file = opendialog.show(initialdir=dir, initialfile=base) | 
					
						
							|  |  |  |         if file: | 
					
						
							|  |  |  |             self.path.set(file) | 
					
						
							| 
									
										
										
										
											2003-01-27 02:36:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |     def MenuOk(self): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         "Simple validity check for a sensible menu item name" | 
					
						
							|  |  |  |         menuOk = True | 
					
						
							|  |  |  |         menu = self.menu.get() | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         menu.strip() | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         if not menu: | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |             tkMessageBox.showerror(title='Menu Item Error', | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |                                    message='No menu item specified', | 
					
						
							|  |  |  |                                    parent=self) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |             self.entryMenu.focus_set() | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |             menuOk = False | 
					
						
							|  |  |  |         elif len(menu) > 30: | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |             tkMessageBox.showerror(title='Menu Item Error', | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |                                    message='Menu item too long:' | 
					
						
							|  |  |  |                                            '\nLimit 30 characters.', | 
					
						
							|  |  |  |                                    parent=self) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |             self.entryMenu.focus_set() | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |             menuOk = False | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         return menuOk | 
					
						
							| 
									
										
										
										
											2002-12-31 16:03:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |     def PathOk(self): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         "Simple validity check for menu file path" | 
					
						
							|  |  |  |         pathOk = True | 
					
						
							|  |  |  |         path = self.path.get() | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         path.strip() | 
					
						
							|  |  |  |         if not path: #no path specified | 
					
						
							|  |  |  |             tkMessageBox.showerror(title='File Path Error', | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |                                    message='No help file path specified.', | 
					
						
							|  |  |  |                                    parent=self) | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |             self.entryPath.focus_set() | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |             pathOk = False | 
					
						
							| 
									
										
										
										
											2003-01-14 22:03:31 +00:00
										 |  |  |         elif path.startswith('www.') or path.startswith('http'): | 
					
						
							| 
									
										
										
										
											2004-07-15 04:54:57 +00:00
										 |  |  |             pass | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if path[:5] == 'file:': | 
					
						
							|  |  |  |                 path = path[5:] | 
					
						
							|  |  |  |             if not os.path.exists(path): | 
					
						
							|  |  |  |                 tkMessageBox.showerror(title='File Path Error', | 
					
						
							|  |  |  |                                        message='Help file path does not exist.', | 
					
						
							|  |  |  |                                        parent=self) | 
					
						
							|  |  |  |                 self.entryPath.focus_set() | 
					
						
							|  |  |  |                 pathOk = False | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         return pathOk | 
					
						
							| 
									
										
										
										
											2002-12-31 16:03:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |     def Ok(self, event=None): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         if self.MenuOk() and self.PathOk(): | 
					
						
							|  |  |  |             self.result = (self.menu.get().strip(), | 
					
						
							|  |  |  |                            self.path.get().strip()) | 
					
						
							| 
									
										
										
										
											2004-07-15 04:54:57 +00:00
										 |  |  |             if sys.platform == 'darwin': | 
					
						
							|  |  |  |                 path = self.result[1] | 
					
						
							|  |  |  |                 if (path.startswith('www') or path.startswith('file:') | 
					
						
							|  |  |  |                     or path.startswith('http:')): | 
					
						
							|  |  |  |                     pass | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     # Mac Safari insists on using the URI form for local files | 
					
						
							| 
									
										
										
										
											2006-05-26 08:41:25 +00:00
										 |  |  |                     self.result = list(self.result) | 
					
						
							| 
									
										
										
										
											2004-07-15 04:54:57 +00:00
										 |  |  |                     self.result[1] = "file://" + path | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |             self.destroy() | 
					
						
							| 
									
										
										
										
											2002-12-31 16:03:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |     def Cancel(self, event=None): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         self.result = None | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         self.destroy() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     #test the dialog | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |     root = Tk() | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |     def run(): | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |         keySeq = '' | 
					
						
							|  |  |  |         dlg = GetHelpSourceDialog(root, 'Get Help Source') | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |         print dlg.result | 
					
						
							| 
									
										
										
										
											2003-01-10 20:13:57 +00:00
										 |  |  |     Button(root,text='Dialog', command=run).pack() | 
					
						
							| 
									
										
										
										
											2002-02-05 04:52:32 +00:00
										 |  |  |     root.mainloop() |