Home | History | Annotate | Download | only in idlelib
      1 "Dialog to specify or edit the parameters for a user configured help source."
      2 
      3 import os
      4 import sys
      5 
      6 from Tkinter import *
      7 import tkMessageBox
      8 import tkFileDialog
      9 
     10 class GetHelpSourceDialog(Toplevel):
     11     def __init__(self, parent, title, menuItem='', filePath='', _htest=False):
     12         """Get menu entry and url/ local file location for Additional Help
     13 
     14         User selects a name for the Help resource and provides a web url
     15         or a local file as its source.  The user can enter a url or browse
     16         for the file.
     17 
     18         _htest - bool, change box location when running htest
     19         """
     20         Toplevel.__init__(self, parent)
     21         self.configure(borderwidth=5)
     22         self.resizable(height=FALSE, width=FALSE)
     23         self.title(title)
     24         self.transient(parent)
     25         self.grab_set()
     26         self.protocol("WM_DELETE_WINDOW", self.Cancel)
     27         self.parent = parent
     28         self.result = None
     29         self.CreateWidgets()
     30         self.menu.set(menuItem)
     31         self.path.set(filePath)
     32         self.withdraw() #hide while setting geometry
     33         #needs to be done here so that the winfo_reqwidth is valid
     34         self.update_idletasks()
     35         #centre dialog over parent. below parent if running htest.
     36         self.geometry(
     37                 "+%d+%d" % (
     38                     parent.winfo_rootx() +
     39                     (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
     40                     parent.winfo_rooty() +
     41                     ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
     42                     if not _htest else 150)))
     43         self.deiconify() #geometry set, unhide
     44         self.bind('<Return>', self.Ok)
     45         self.wait_window()
     46 
     47     def CreateWidgets(self):
     48         self.menu = StringVar(self)
     49         self.path = StringVar(self)
     50         self.fontSize = StringVar(self)
     51         self.frameMain = Frame(self, borderwidth=2, relief=GROOVE)
     52         self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
     53         labelMenu = Label(self.frameMain, anchor=W, justify=LEFT,
     54                           text='Menu Item:')
     55         self.entryMenu = Entry(self.frameMain, textvariable=self.menu,
     56                                width=30)
     57         self.entryMenu.focus_set()
     58         labelPath = Label(self.frameMain, anchor=W, justify=LEFT,
     59                           text='Help File Path: Enter URL or browse for file')
     60         self.entryPath = Entry(self.frameMain, textvariable=self.path,
     61                                width=40)
     62         self.entryMenu.focus_set()
     63         labelMenu.pack(anchor=W, padx=5, pady=3)
     64         self.entryMenu.pack(anchor=W, padx=5, pady=3)
     65         labelPath.pack(anchor=W, padx=5, pady=3)
     66         self.entryPath.pack(anchor=W, padx=5, pady=3)
     67         browseButton = Button(self.frameMain, text='Browse', width=8,
     68                               command=self.browseFile)
     69         browseButton.pack(pady=3)
     70         frameButtons = Frame(self)
     71         frameButtons.pack(side=BOTTOM, fill=X)
     72         self.buttonOk = Button(frameButtons, text='OK',
     73                                width=8, default=ACTIVE,  command=self.Ok)
     74         self.buttonOk.grid(row=0, column=0, padx=5,pady=5)
     75         self.buttonCancel = Button(frameButtons, text='Cancel',
     76                                    width=8, command=self.Cancel)
     77         self.buttonCancel.grid(row=0, column=1, padx=5, pady=5)
     78 
     79     def browseFile(self):
     80         filetypes = [
     81             ("HTML Files", "*.htm *.html", "TEXT"),
     82             ("PDF Files", "*.pdf", "TEXT"),
     83             ("Windows Help Files", "*.chm"),
     84             ("Text Files", "*.txt", "TEXT"),
     85             ("All Files", "*")]
     86         path = self.path.get()
     87         if path:
     88             dir, base = os.path.split(path)
     89         else:
     90             base = None
     91             if sys.platform[:3] == 'win':
     92                 dir = os.path.join(os.path.dirname(sys.executable), 'Doc')
     93                 if not os.path.isdir(dir):
     94                     dir = os.getcwd()
     95             else:
     96                 dir = os.getcwd()
     97         opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes)
     98         file = opendialog.show(initialdir=dir, initialfile=base)
     99         if file:
    100             self.path.set(file)
    101 
    102     def MenuOk(self):
    103         "Simple validity check for a sensible menu item name"
    104         menuOk = True
    105         menu = self.menu.get()
    106         menu.strip()
    107         if not menu:
    108             tkMessageBox.showerror(title='Menu Item Error',
    109                                    message='No menu item specified',
    110                                    parent=self)
    111             self.entryMenu.focus_set()
    112             menuOk = False
    113         elif len(menu) > 30:
    114             tkMessageBox.showerror(title='Menu Item Error',
    115                                    message='Menu item too long:'
    116                                            '\nLimit 30 characters.',
    117                                    parent=self)
    118             self.entryMenu.focus_set()
    119             menuOk = False
    120         return menuOk
    121 
    122     def PathOk(self):
    123         "Simple validity check for menu file path"
    124         pathOk = True
    125         path = self.path.get()
    126         path.strip()
    127         if not path: #no path specified
    128             tkMessageBox.showerror(title='File Path Error',
    129                                    message='No help file path specified.',
    130                                    parent=self)
    131             self.entryPath.focus_set()
    132             pathOk = False
    133         elif path.startswith(('www.', 'http')):
    134             pass
    135         else:
    136             if path[:5] == 'file:':
    137                 path = path[5:]
    138             if not os.path.exists(path):
    139                 tkMessageBox.showerror(title='File Path Error',
    140                                        message='Help file path does not exist.',
    141                                        parent=self)
    142                 self.entryPath.focus_set()
    143                 pathOk = False
    144         return pathOk
    145 
    146     def Ok(self, event=None):
    147         if self.MenuOk() and self.PathOk():
    148             self.result = (self.menu.get().strip(),
    149                            self.path.get().strip())
    150             if sys.platform == 'darwin':
    151                 path = self.result[1]
    152                 if path.startswith(('www', 'file:', 'http:')):
    153                     pass
    154                 else:
    155                     # Mac Safari insists on using the URI form for local files
    156                     self.result = list(self.result)
    157                     self.result[1] = "file://" + path
    158             self.destroy()
    159 
    160     def Cancel(self, event=None):
    161         self.result = None
    162         self.destroy()
    163 
    164 if __name__ == '__main__':
    165     from idlelib.idle_test.htest import run
    166     run(GetHelpSourceDialog)
    167