blob: 41edd7e08b6c8f05982a11e67d9a6a187b2f123f [file] [log] [blame]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001"""Easy to use dialogs.
2
3Message(msg) -- display a message and an OK button.
4AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
5AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
6
7More documentation in each function.
8This module uses DLOG resources 256, 257 and 258.
9Based upon STDWIN dialogs with the same names and functions.
10"""
11
12from Dlg import GetNewDialog, SetIText, GetIText, ModalDialog
13
14
15def Message(msg):
16 """Display a MESSAGE string.
17
18 Return when the user clicks the OK button or presses Return.
19
20 The MESSAGE string can be at most 255 characters long.
21 """
22
23 id = 256
24 d = GetNewDialog(id, -1)
25 if not d:
26 print "Can't get DLOG resource with id =", id
27 return
28 tp, h, rect = d.GetDItem(2)
29 SetIText(h, msg)
30 while 1:
31 n = ModalDialog(None)
32 if n == 1:
33 return
34
35
36def AskString(prompt, default = ""):
37 """Display a PROMPT string and a text entry field with a DEFAULT string.
38
39 Return the contents of the text entry field when the user clicks the
40 OK button or presses Return.
41 Return None when the user clicks the Cancel button.
42
43 If omitted, DEFAULT is empty.
44
45 The PROMPT and DEFAULT strings, as well as the return value,
46 can be at most 255 characters long.
47 """
48
49 id = 257
50 d = GetNewDialog(id, -1)
51 if not d:
52 print "Can't get DLOG resource with id =", id
53 return
54 tp, h, rect = d.GetDItem(3)
55 SetIText(h, prompt)
56 tp, h, rect = d.GetDItem(4)
57 SetIText(h, default)
58 d.SelIText(4, 0, 255)
59 while 1:
60 n = ModalDialog(None)
61 if n == 1:
62 tp, h, rect = d.GetDItem(4)
63 return GetIText(h)
64 if n == 2: return None
65
66
67def AskYesNoCancel(question, default = 0):
68## """Display a QUESTION string which can be answered with Yes or No.
69##
70## Return 1 when the user clicks the Yes button.
71## Return 0 when the user clicks the No button.
72## Return -1 when the user clicks the Cancel button.
73##
74## When the user presses Return, the DEFAULT value is returned.
75## If omitted, this is 0 (No).
76##
77## The QUESTION strign ca be at most 255 characters.
78## """
79
80 id = 258
81 d = GetNewDialog(id, -1)
82 if not d:
83 print "Can't get DLOG resource with id =", id
84 return
85 # Button assignments:
86 # 1 = default (invisible)
87 # 2 = Yes
88 # 3 = No
89 # 4 = Cancel
90 # The question string is item 5
91 tp, h, rect = d.GetDItem(5)
92 SetIText(h, question)
93 while 1:
94 n = ModalDialog(None)
95 if n == 1: return default
96 if n == 2: return 1
97 if n == 3: return 0
98 if n == 4: return -1
99
100
101def test():
102 Message("Testing EasyDialogs.")
103 ok = AskYesNoCancel("Do you want to proceed?")
104 if ok > 0:
105 s = AskString("Enter your first name")
106 Message("Thank you,\015%s" % `s`)
107
108
109if __name__ == '__main__':
110 test()