• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Dialog that allows user to specify a new config file section name.
3Used to get new highlight theme and keybinding set names.
4The 'return value' for the dialog, used two placed in configDialog.py,
5is the .result attribute set in the Ok and Cancel methods.
6"""
7from Tkinter import *
8import tkMessageBox
9class GetCfgSectionNameDialog(Toplevel):
10    def __init__(self, parent, title, message, used_names, _htest=False):
11        """
12        message - string, informational message to display
13        used_names - string collection, names already in use for validity check
14        _htest - bool, change box location when running htest
15        """
16        Toplevel.__init__(self, parent)
17        self.configure(borderwidth=5)
18        self.resizable(height=FALSE, width=FALSE)
19        self.title(title)
20        self.transient(parent)
21        self.grab_set()
22        self.protocol("WM_DELETE_WINDOW", self.Cancel)
23        self.parent = parent
24        self.message = message
25        self.used_names = used_names
26        self.create_widgets()
27        self.withdraw()  #hide while setting geometry
28        self.update_idletasks()
29        #needs to be done here so that the winfo_reqwidth is valid
30        self.messageInfo.config(width=self.frameMain.winfo_reqwidth())
31        self.geometry(
32                "+%d+%d" % (
33                    parent.winfo_rootx() +
34                    (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
35                    parent.winfo_rooty() +
36                    ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
37                    if not _htest else 100)
38                ) )  #centre dialog over parent (or below htest box)
39        self.deiconify()  #geometry set, unhide
40        self.wait_window()
41    def create_widgets(self):
42        self.name = StringVar(self.parent)
43        self.fontSize = StringVar(self.parent)
44        self.frameMain = Frame(self, borderwidth=2, relief=SUNKEN)
45        self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
46        self.messageInfo = Message(self.frameMain, anchor=W, justify=LEFT,
47                    padx=5, pady=5, text=self.message) #,aspect=200)
48        entryName = Entry(self.frameMain, textvariable=self.name, width=30)
49        entryName.focus_set()
50        self.messageInfo.pack(padx=5, pady=5) #, expand=TRUE, fill=BOTH)
51        entryName.pack(padx=5, pady=5)
52        frameButtons = Frame(self, pady=2)
53        frameButtons.pack(side=BOTTOM)
54        self.buttonOk = Button(frameButtons, text='Ok',
55                width=8, command=self.Ok)
56        self.buttonOk.pack(side=LEFT, padx=5)
57        self.buttonCancel = Button(frameButtons, text='Cancel',
58                width=8, command=self.Cancel)
59        self.buttonCancel.pack(side=RIGHT, padx=5)
60
61    def name_ok(self):
62        ''' After stripping entered name, check that it is a  sensible
63        ConfigParser file section name. Return it if it is, '' if not.
64        '''
65        name = self.name.get().strip()
66        if not name: #no name specified
67            tkMessageBox.showerror(title='Name Error',
68                    message='No name specified.', parent=self)
69        elif len(name)>30: #name too long
70            tkMessageBox.showerror(title='Name Error',
71                    message='Name too long. It should be no more than '+
72                    '30 characters.', parent=self)
73            name = ''
74        elif name in self.used_names:
75            tkMessageBox.showerror(title='Name Error',
76                    message='This name is already in use.', parent=self)
77            name = ''
78        return name
79    def Ok(self, event=None):
80        name = self.name_ok()
81        if name:
82            self.result = name
83            self.grab_release()
84            self.destroy()
85    def Cancel(self, event=None):
86        self.result = ''
87        self.grab_release()
88        self.destroy()
89
90if __name__ == '__main__':
91    import unittest
92    unittest.main('idlelib.idle_test.test_config_name', verbosity=2, exit=False)
93
94    from idlelib.idle_test.htest import run
95    run(GetCfgSectionNameDialog)
96