blob: f0c6081ee606a0e50e0792076ef31e63e6e39445 [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.
Jack Jansen3a87f5b1995-11-14 10:13:49 +00006bar = Progress(label, maxvalue) -- Display a progress bar
7bar.set(value) -- Set value
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00008
9More documentation in each function.
10This module uses DLOG resources 256, 257 and 258.
11Based upon STDWIN dialogs with the same names and functions.
12"""
13
Jack Jansene4b40381995-07-17 13:25:15 +000014from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
Jack Jansen3a87f5b1995-11-14 10:13:49 +000015import Qd
Jack Jansen3a87f5b1995-11-14 10:13:49 +000016import QuickDraw
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000017
18
19def Message(msg):
20 """Display a MESSAGE string.
21
22 Return when the user clicks the OK button or presses Return.
23
24 The MESSAGE string can be at most 255 characters long.
25 """
26
27 id = 256
28 d = GetNewDialog(id, -1)
29 if not d:
30 print "Can't get DLOG resource with id =", id
31 return
Jack Jansene4b40381995-07-17 13:25:15 +000032 tp, h, rect = d.GetDialogItem(2)
33 SetDialogItemText(h, msg)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000034 d.SetDialogDefaultItem(1)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000035 while 1:
36 n = ModalDialog(None)
37 if n == 1:
38 return
39
40
41def AskString(prompt, default = ""):
42 """Display a PROMPT string and a text entry field with a DEFAULT string.
43
44 Return the contents of the text entry field when the user clicks the
45 OK button or presses Return.
46 Return None when the user clicks the Cancel button.
47
48 If omitted, DEFAULT is empty.
49
50 The PROMPT and DEFAULT strings, as well as the return value,
51 can be at most 255 characters long.
52 """
53
54 id = 257
55 d = GetNewDialog(id, -1)
56 if not d:
57 print "Can't get DLOG resource with id =", id
58 return
Jack Jansene4b40381995-07-17 13:25:15 +000059 tp, h, rect = d.GetDialogItem(3)
60 SetDialogItemText(h, prompt)
61 tp, h, rect = d.GetDialogItem(4)
62 SetDialogItemText(h, default)
63# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000064 d.SetDialogDefaultItem(1)
65 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000066 while 1:
67 n = ModalDialog(None)
68 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000069 tp, h, rect = d.GetDialogItem(4)
70 return GetDialogItemText(h)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000071 if n == 2: return None
72
73
74def AskYesNoCancel(question, default = 0):
75## """Display a QUESTION string which can be answered with Yes or No.
76##
77## Return 1 when the user clicks the Yes button.
78## Return 0 when the user clicks the No button.
79## Return -1 when the user clicks the Cancel button.
80##
81## When the user presses Return, the DEFAULT value is returned.
82## If omitted, this is 0 (No).
83##
84## The QUESTION strign ca be at most 255 characters.
85## """
86
87 id = 258
88 d = GetNewDialog(id, -1)
89 if not d:
90 print "Can't get DLOG resource with id =", id
91 return
92 # Button assignments:
93 # 1 = default (invisible)
94 # 2 = Yes
95 # 3 = No
96 # 4 = Cancel
97 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +000098 tp, h, rect = d.GetDialogItem(5)
99 SetDialogItemText(h, question)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000100 d.SetDialogCancelItem(4)
101 if default in (2, 3, 4):
102 d.SetDialogDefaultItem(default)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000103 while 1:
104 n = ModalDialog(None)
105 if n == 1: return default
106 if n == 2: return 1
107 if n == 3: return 0
108 if n == 4: return -1
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000109
110class ProgressBar:
111 def __init__(self, label="Working...", maxval=100):
112 self.label = label
113 self.maxval = maxval
114 self.curval = -1
115 self.d = GetNewDialog(259, -1)
116 tp, text_h, rect = self.d.GetDialogItem(2)
117 SetDialogItemText(text_h, "Progress...")
118 self._update(0)
119
120 def _update(self, value):
121 tp, h, bar_rect = self.d.GetDialogItem(3)
122 Qd.SetPort(self.d)
123
124 Qd.FrameRect(bar_rect) # Draw outline
125
126 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
127 Qd.ForeColor(QuickDraw.whiteColor)
128 Qd.BackColor(QuickDraw.whiteColor)
129 Qd.PaintRect(inner_rect) # Clear internal
130
131 l, t, r, b = inner_rect
132 r = int(l + (r-l)*value/self.maxval)
133 inner_rect = l, t, r, b
134 Qd.ForeColor(QuickDraw.blackColor)
135 Qd.BackColor(QuickDraw.blackColor)
136 Qd.PaintRect(inner_rect) # Draw bar
137
138 # Restore settings
139 Qd.ForeColor(QuickDraw.blackColor)
140 Qd.BackColor(QuickDraw.whiteColor)
141
142 # Test for cancel button
143 if ModalDialog(self._filterfunc) == 1:
144 raise KeyboardInterrupt
145
146 def _filterfunc(self, d, e, *more):
147 return 2 # XXXX For now, this disables the cancel button
148
149 def set(self, value):
150 if value < 0: value = 0
151 if value > self.maxval: value = self.maxval
152 self._update(value)
153
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000154
155
156def test():
157 Message("Testing EasyDialogs.")
158 ok = AskYesNoCancel("Do you want to proceed?")
159 if ok > 0:
160 s = AskString("Enter your first name")
161 Message("Thank you,\015%s" % `s`)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000162 bar = ProgressBar("Counting...", 100)
163 for i in range(100):
164 bar.set(i)
165 del bar
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000166
167
168if __name__ == '__main__':
169 test()