blob: 3dfe9f3d62a91c1f4561fee2f65375e5446211cc [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
16import addpack
17addpack.addpack('Tools')
18addpack.addpack('bgen')
19addpack.addpack('qd')
20import QuickDraw
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000021
22
23def Message(msg):
24 """Display a MESSAGE string.
25
26 Return when the user clicks the OK button or presses Return.
27
28 The MESSAGE string can be at most 255 characters long.
29 """
30
31 id = 256
32 d = GetNewDialog(id, -1)
33 if not d:
34 print "Can't get DLOG resource with id =", id
35 return
Jack Jansene4b40381995-07-17 13:25:15 +000036 tp, h, rect = d.GetDialogItem(2)
37 SetDialogItemText(h, msg)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000038 d.SetDialogDefaultItem(1)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000039 while 1:
40 n = ModalDialog(None)
41 if n == 1:
42 return
43
44
45def AskString(prompt, default = ""):
46 """Display a PROMPT string and a text entry field with a DEFAULT string.
47
48 Return the contents of the text entry field when the user clicks the
49 OK button or presses Return.
50 Return None when the user clicks the Cancel button.
51
52 If omitted, DEFAULT is empty.
53
54 The PROMPT and DEFAULT strings, as well as the return value,
55 can be at most 255 characters long.
56 """
57
58 id = 257
59 d = GetNewDialog(id, -1)
60 if not d:
61 print "Can't get DLOG resource with id =", id
62 return
Jack Jansene4b40381995-07-17 13:25:15 +000063 tp, h, rect = d.GetDialogItem(3)
64 SetDialogItemText(h, prompt)
65 tp, h, rect = d.GetDialogItem(4)
66 SetDialogItemText(h, default)
67# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000068 d.SetDialogDefaultItem(1)
69 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000070 while 1:
71 n = ModalDialog(None)
72 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000073 tp, h, rect = d.GetDialogItem(4)
74 return GetDialogItemText(h)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000075 if n == 2: return None
76
77
78def AskYesNoCancel(question, default = 0):
79## """Display a QUESTION string which can be answered with Yes or No.
80##
81## Return 1 when the user clicks the Yes button.
82## Return 0 when the user clicks the No button.
83## Return -1 when the user clicks the Cancel button.
84##
85## When the user presses Return, the DEFAULT value is returned.
86## If omitted, this is 0 (No).
87##
88## The QUESTION strign ca be at most 255 characters.
89## """
90
91 id = 258
92 d = GetNewDialog(id, -1)
93 if not d:
94 print "Can't get DLOG resource with id =", id
95 return
96 # Button assignments:
97 # 1 = default (invisible)
98 # 2 = Yes
99 # 3 = No
100 # 4 = Cancel
101 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +0000102 tp, h, rect = d.GetDialogItem(5)
103 SetDialogItemText(h, question)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000104 d.SetDialogCancelItem(4)
105 if default in (2, 3, 4):
106 d.SetDialogDefaultItem(default)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000107 while 1:
108 n = ModalDialog(None)
109 if n == 1: return default
110 if n == 2: return 1
111 if n == 3: return 0
112 if n == 4: return -1
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000113
114class ProgressBar:
115 def __init__(self, label="Working...", maxval=100):
116 self.label = label
117 self.maxval = maxval
118 self.curval = -1
119 self.d = GetNewDialog(259, -1)
120 tp, text_h, rect = self.d.GetDialogItem(2)
121 SetDialogItemText(text_h, "Progress...")
122 self._update(0)
123
124 def _update(self, value):
125 tp, h, bar_rect = self.d.GetDialogItem(3)
126 Qd.SetPort(self.d)
127
128 Qd.FrameRect(bar_rect) # Draw outline
129
130 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
131 Qd.ForeColor(QuickDraw.whiteColor)
132 Qd.BackColor(QuickDraw.whiteColor)
133 Qd.PaintRect(inner_rect) # Clear internal
134
135 l, t, r, b = inner_rect
136 r = int(l + (r-l)*value/self.maxval)
137 inner_rect = l, t, r, b
138 Qd.ForeColor(QuickDraw.blackColor)
139 Qd.BackColor(QuickDraw.blackColor)
140 Qd.PaintRect(inner_rect) # Draw bar
141
142 # Restore settings
143 Qd.ForeColor(QuickDraw.blackColor)
144 Qd.BackColor(QuickDraw.whiteColor)
145
146 # Test for cancel button
147 if ModalDialog(self._filterfunc) == 1:
148 raise KeyboardInterrupt
149
150 def _filterfunc(self, d, e, *more):
151 return 2 # XXXX For now, this disables the cancel button
152
153 def set(self, value):
154 if value < 0: value = 0
155 if value > self.maxval: value = self.maxval
156 self._update(value)
157
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000158
159
160def test():
161 Message("Testing EasyDialogs.")
162 ok = AskYesNoCancel("Do you want to proceed?")
163 if ok > 0:
164 s = AskString("Enter your first name")
165 Message("Thank you,\015%s" % `s`)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000166 bar = ProgressBar("Counting...", 100)
167 for i in range(100):
168 bar.set(i)
169 del bar
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000170
171
172if __name__ == '__main__':
173 test()