1 """Easy to use dialogs.
3 Message(msg) -- display a message and an OK button.
4 AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
5 AskPassword(prompt, default) -- like AskString(), but shows text as bullets.
6 AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
7 bar = Progress(label, maxvalue) -- Display a progress bar
8 bar.set(value) -- Set value
9 bar.inc( *amount ) -- increment value by amount (default=1)
10 bar.label( *newlabel ) -- get or set text label.
12 More documentation in each function.
13 This module uses DLOG resources 256, 257 and 258.
14 Based upon STDWIN dialogs with the same names and functions.
17 from Dlg
import GetNewDialog
, SetDialogItemText
, GetDialogItemText
, ModalDialog
22 import Dlg
,Win
,Evt
,Events
# sdm7g
28 text
= string
.join(string
.split(text
, '\r'), '\n')
33 text
= string
.join(string
.split(text
, '\n'), '\r')
35 text
= text
[:253] + '\311'
38 def Message(msg
, id=256, ok
=None):
39 """Display a MESSAGE string.
41 Return when the user clicks the OK button or presses Return.
43 The MESSAGE string can be at most 255 characters long.
46 d
= GetNewDialog(id, -1)
48 print "Can't get DLOG resource with id =", id
50 tp
, h
, rect
= d
.GetDialogItem(2)
51 SetDialogItemText(h
, lf2cr(msg
))
53 tp
, h
, rect
= d
.GetDialogItem(1)
54 h
.as_Control().SetControlTitle(ok
)
55 d
.SetDialogDefaultItem(1)
62 def AskString(prompt
, default
= "", id=257, ok
=None, cancel
=None):
63 """Display a PROMPT string and a text entry field with a DEFAULT string.
65 Return the contents of the text entry field when the user clicks the
66 OK button or presses Return.
67 Return None when the user clicks the Cancel button.
69 If omitted, DEFAULT is empty.
71 The PROMPT and DEFAULT strings, as well as the return value,
72 can be at most 255 characters long.
76 d
= GetNewDialog(id, -1)
78 print "Can't get DLOG resource with id =", id
80 tp
, h
, rect
= d
.GetDialogItem(3)
81 SetDialogItemText(h
, lf2cr(prompt
))
82 tp
, h
, rect
= d
.GetDialogItem(4)
83 SetDialogItemText(h
, lf2cr(default
))
84 d
.SelectDialogItemText(4, 0, 999)
85 # d.SetDialogItem(4, 0, 255)
87 tp
, h
, rect
= d
.GetDialogItem(1)
88 h
.as_Control().SetControlTitle(ok
)
90 tp
, h
, rect
= d
.GetDialogItem(2)
91 h
.as_Control().SetControlTitle(cancel
)
92 d
.SetDialogDefaultItem(1)
93 d
.SetDialogCancelItem(2)
97 tp
, h
, rect
= d
.GetDialogItem(4)
98 return cr2lf(GetDialogItemText(h
))
99 if n
== 2: return None
101 def AskPassword(prompt
, default
='', id=257):
102 """Display a PROMPT string and a text entry field with a DEFAULT string.
103 The string is displayed as bullets only.
105 Return the contents of the text entry field when the user clicks the
106 OK button or presses Return.
107 Return None when the user clicks the Cancel button.
109 If omitted, DEFAULT is empty.
111 The PROMPT and DEFAULT strings, as well as the return value,
112 can be at most 255 characters long.
114 d
= GetNewDialog(id, -1)
116 print "Can't get DLOG resource with id =", id
118 tp
, h
, rect
= d
.GetDialogItem(3) # STATIC TEXT ITEM <= prompt
119 SetDialogItemText(h
, lf2cr(prompt
))
120 tp
, h
, rect
= d
.GetDialogItem(4) # EDIT TEXT ITEM
121 bullets
= '\245'*len(default
)
122 SetDialogItemText(h
, bullets
)
123 d
.SelectDialogItemText(4, 999, 999)
124 d
.SetDialogDefaultItem(Dialogs
.ok
)
125 d
.SetDialogCancelItem(Dialogs
.cancel
)
127 oldschedparams
= MacOS
.SchedParams(0,0)
129 ready
,ev
= Evt
.WaitNextEvent(Events
.everyEvent
, 6)
130 if not ready
: continue
131 what
,msg
,when
,where
,mod
= ev
132 if what
== 0 : Dlg
.DialogSelect(ev
) # for blinking caret
133 elif Dlg
.IsDialogEvent(ev
):
134 if what
in (Events
.keyDown
, Events
.autoKey
):
135 charcode
= msg
& Events
.charCodeMask
136 if ( mod
& Events
.cmdKey
):
138 continue # don't do cut & paste commands
140 if charcode
== Events
.kReturnCharCode
:
142 elif charcode
== Events
.kEscapeCharCode
:
145 elif charcode
in (Events
.kLeftArrowCharCode
,
146 Events
.kBackspaceCharCode
):
149 string
= string
+ chr(charcode
)
150 msg
= 0245 # Octal code for bullet
151 ev
= (what
,msg
,when
,where
,mod
)
152 rs
, win
, item
= Dlg
.DialogSelect(ev
)
153 if item
== Dialogs
.ok
:
155 elif item
== Dialogs
.cancel
:
158 elif what
== Events
.mouseDown
:
159 part
, win
= Win
.FindWindow(where
)
160 if part
== Windows
.inDrag
and win
:
161 win
.DragWindow(where
, screenbounds
)
162 elif part
== Windows
.inMenuBar
:
163 MacOS
.HandleEvent(ev
)
165 MacOS
.SysBeep() # Cannot handle selections, unfortunately
167 elif what
== Events
.updateEvt
: MacOS
.HandleEvent(ev
)
168 apply(MacOS
.SchedParams
, oldschedparams
)
171 def AskYesNoCancel(question
, default
= 0, yes
=None, no
=None, cancel
=None, id=258):
172 ## """Display a QUESTION string which can be answered with Yes or No.
174 ## Return 1 when the user clicks the Yes button.
175 ## Return 0 when the user clicks the No button.
176 ## Return -1 when the user clicks the Cancel button.
178 ## When the user presses Return, the DEFAULT value is returned.
179 ## If omitted, this is 0 (No).
181 ## The QUESTION strign ca be at most 255 characters.
184 d
= GetNewDialog(id, -1)
186 print "Can't get DLOG resource with id =", id
188 # Button assignments:
189 # 1 = default (invisible)
193 # The question string is item 5
194 tp
, h
, rect
= d
.GetDialogItem(5)
195 SetDialogItemText(h
, lf2cr(question
))
197 tp
, h
, rect
= d
.GetDialogItem(2)
198 h
.as_Control().SetControlTitle(yes
)
200 tp
, h
, rect
= d
.GetDialogItem(3)
201 h
.as_Control().SetControlTitle(no
)
206 tp
, h
, rect
= d
.GetDialogItem(4)
207 h
.as_Control().SetControlTitle(cancel
)
208 d
.SetDialogCancelItem(4)
210 d
.SetDialogDefaultItem(2)
212 d
.SetDialogDefaultItem(3)
214 d
.SetDialogDefaultItem(4)
216 n
= ModalDialog(None)
217 if n
== 1: return default
225 screenbounds
= Qd
.qd
.screenBits
.bounds
226 screenbounds
= screenbounds
[0]+4, screenbounds
[1]+4, \
227 screenbounds
[2]-4, screenbounds
[3]-4
231 def __init__(self
, title
="Working...", maxval
=100, label
="", id=259):
234 self
.d
= GetNewDialog(id, -1)
240 self
.d
.BringToFront()
244 def title(self
, newstr
=""):
245 """title(text) - Set title of progress window"""
246 self
.d
.BringToFront()
247 w
= self
.d
.GetDialogWindow()
250 def label( self
, *newstr
):
251 """label(text) - Set text in progress box"""
252 self
.d
.BringToFront()
254 self
._label
= lf2cr(newstr
[0])
255 tp
, text_h
, rect
= self
.d
.GetDialogItem(2)
256 SetDialogItemText(text_h
, self
._label
)
259 def _update(self
, value
):
260 self
.d
.BringToFront()
261 tp
, h
, bar_rect
= self
.d
.GetDialogItem(3)
264 Qd
.FrameRect(bar_rect
) # Draw outline
266 inner_rect
= Qd
.InsetRect(bar_rect
, 1, 1)
267 l
, t
, r
, b
= inner_rect
269 Qd
.ForeColor(QuickDraw
.blackColor
)
270 Qd
.BackColor(QuickDraw
.blackColor
)
271 Qd
.PaintRect((l
, t
, int(l
+ (r
-l
)*value
/self
.maxval
), b
)) # Draw bar
273 Qd
.ForeColor(QuickDraw
.whiteColor
)
274 Qd
.BackColor(QuickDraw
.whiteColor
)
275 Qd
.PaintRect((int(l
+ (r
-l
)*value
/self
.maxval
), t
, r
, b
)) # Clear rest
278 Qd
.ForeColor(QuickDraw
.blackColor
)
279 Qd
.BackColor(QuickDraw
.whiteColor
)
281 # Test for cancel button
283 ready
, ev
= Evt
.WaitNextEvent( Events
.mDownMask
, 1 )
285 what
,msg
,when
,where
,mod
= ev
286 part
= Win
.FindWindow(where
)[0]
287 if Dlg
.IsDialogEvent(ev
):
288 ds
= Dlg
.DialogSelect(ev
)
289 if ds
[0] and ds
[1] == self
.d
and ds
[-1] == 1:
290 raise KeyboardInterrupt, ev
292 if part
== 4: # inDrag
293 self
.d
.DragWindow(where
, screenbounds
)
295 MacOS
.HandleEvent(ev
)
298 def set(self
, value
):
299 """set(value) - Set progress bar position"""
300 if value
< 0: value
= 0
301 if value
> self
.maxval
: value
= self
.maxval
306 """inc(amt) - Increment progress bar position"""
307 self
.set(self
.curval
+ n
)
312 Message("Testing EasyDialogs.")
313 ok
= AskYesNoCancel("Do you want to proceed?")
314 ok
= AskYesNoCancel("Do you want to identify?", yes
="Indentify", no
="Don't identify")
316 s
= AskString("Enter your first name", "Joe")
317 Message("Thank you,\n%s" % `s`
)
318 text
= ( "Working Hard...", "Hardly Working..." ,
319 "So far, so good!", "Keep on truckin'" )
320 bar
= ProgressBar("Progress, progress...", 100)
322 appsw
= MacOS
.SchedParams(1, 0)
327 bar
.label(text
[(i
/10) % 4])
329 time
.sleep(0.3) # give'em a chance to see the done.
332 apply(MacOS
.SchedParams
, appsw
)
337 if __name__
== '__main__':
340 except KeyboardInterrupt:
341 Message("Operation Canceled.")