55AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
66bar = Progress(label, maxvalue) -- Display a progress bar
77bar.set(value) -- Set value
8+ bar.inc( *amount ) -- increment value by amount (default=1)
9+ bar.label( *newlabel ) -- get or set text label.
810
911More documentation in each function.
1012This module uses DLOG resources 256, 257 and 258.
1416from Dlg import GetNewDialog , SetDialogItemText , GetDialogItemText , ModalDialog
1517import Qd
1618import QuickDraw
17-
19+ import Dlg , Win , Evt , Events # sdm7g
1820
1921def Message (msg ):
2022 """Display a MESSAGE string.
@@ -110,18 +112,43 @@ def AskYesNoCancel(question, default = 0):
110112 if n == 2 : return 1
111113 if n == 3 : return 0
112114 if n == 4 : return - 1
115+
116+
113117
118+
119+ screenbounds = Qd .qd .screenBits .bounds
120+ screenbounds = screenbounds [0 ]+ 4 , screenbounds [1 ]+ 4 , \
121+ screenbounds [2 ]- 4 , screenbounds [3 ]- 4
122+
123+
114124class ProgressBar :
115- def __init__ (self , label = "Working..." , maxval = 100 ):
116- self .label = label
125+ def __init__ (self , title = "Working..." , maxval = 100 , label = "" ):
117126 self .maxval = maxval
118127 self .curval = - 1
119128 self .d = GetNewDialog (259 , - 1 )
120- tp , text_h , rect = self .d . GetDialogItem ( 2 )
121- SetDialogItemText ( text_h , label )
129+ self .title ( title )
130+ self . label ( label )
122131 self ._update (0 )
132+
133+ def __del__ ( self ):
134+ self .d .HideWindow ()
135+ del self .d
136+
137+ def title (self , newstr = "" ):
138+ """title(text) - Set title of progress window"""
139+ w = self .d .GetDialogWindow ()
140+ w .SetWTitle (newstr )
123141
142+ def label ( self , * newstr ):
143+ """label(text) - Set text in progress box"""
144+ if newstr :
145+ self ._label = newstr [0 ]
146+ tp , text_h , rect = self .d .GetDialogItem (2 )
147+ SetDialogItemText (text_h , self ._label )
148+
149+
124150 def _update (self , value ):
151+ self .d .BringToFront ()
125152 tp , h , bar_rect = self .d .GetDialogItem (3 )
126153 Qd .SetPort (self .d )
127154
@@ -143,28 +170,64 @@ def _update(self, value):
143170 Qd .BackColor (QuickDraw .whiteColor )
144171
145172 # Test for cancel button
146- if ModalDialog (_ProgressBar_filterfunc ) == 1 :
147- raise KeyboardInterrupt
173+
174+ ready , ev = Evt .WaitNextEvent ( Events .mDownMask , 1 )
175+ if ready :
176+ what ,msg ,when ,where ,mod = ev
177+ part = Win .FindWindow (where )[0 ]
178+ if Dlg .IsDialogEvent (ev ):
179+ ds = Dlg .DialogSelect (ev )
180+ if ds [0 ] and ds [1 ] == self .d and ds [- 1 ] == 1 :
181+ raise KeyboardInterrupt , ev
182+ else :
183+ if part == 4 : # inDrag
184+ self .d .DragWindow (where , screenbounds )
185+ else :
186+ MacOS .HandleEvent (ev )
187+
148188
149189 def set (self , value ):
190+ """set(value) - Set progress bar position"""
150191 if value < 0 : value = 0
151192 if value > self .maxval : value = self .maxval
193+ self .curval = value
152194 self ._update (value )
153-
154- def _ProgressBar_filterfunc (* args ):
155- return 2 # Disabled, for now.
156-
195+
196+ def inc (self , n = 1 ):
197+ """inc(amt) - Increment progress bar position"""
198+ self .set (self .curval + n )
199+
157200def test ():
201+ import time
202+ import MacOS
203+
158204 Message ("Testing EasyDialogs." )
159205 ok = AskYesNoCancel ("Do you want to proceed?" )
160206 if ok > 0 :
161207 s = AskString ("Enter your first name" )
162208 Message ("Thank you,\015 %s" % `s` )
163- bar = ProgressBar ("Counting..." , 100 )
164- for i in range (100 ):
165- bar .set (i )
166- del bar
209+ text = ( "Working Hard..." , "Hardly Working..." ,
210+ "So far, so good!" , "Keep on truckin'" )
211+ bar = ProgressBar ("Progress, progress..." , 100 )
212+ try :
213+ appsw = MacOS .EnableAppswitch (0 )
214+ for i in range (100 ):
215+ bar .set (i )
216+ time .sleep (0.1 )
217+ if i % 10 == 0 :
218+ bar .label (text [(i / 10 ) % 4 ])
219+ bar .label ("Done." )
220+ time .sleep (0.3 ) # give'em a chance to see the done.
221+ finally :
222+ del bar
223+ MacOS .EnableAppswitch (appsw )
167224
168225
226+
227+
169228if __name__ == '__main__' :
170- test ()
229+ try :
230+ test ()
231+ except KeyboardInterrupt :
232+ Message ("Operation Canceled." )
233+
0 commit comments