Progress Bar Example

Dave Cragg dcragg at lacscentre.co.uk
Fri Mar 24 17:37:13 EST 2006


On 24 Mar 2006, at 21:42, Sarah Reichelt wrote:

> On 3/25/06, Jeff Honken <jhonken at x12.info> wrote:
>>
>> I would like to use a "progress bar" to monitor a "go stack URL".   
>> I'm
>> clueless on how to write the code.  Does someone have an example  
>> of this
>> or can someone please point me in the correct direction. Jeff
>
> I haven't done it using stacks, but I have done it for downloading
> pictures from the web and I guess it's much the same. Firstly, you
> need to use "load" instead of "go". This is non-blocking and reports
> it's status so you can show what's happening.

You can show status using "go" as well. libUrlSetStatusCallback works  
for both blocking (e.g. load url) and non-blocking (.e.g. get url)  
calls.

Below is the script of a *very crude* progress palette. The stack  
consists of two fields (one named "url" and the other named  
"status"), and a scrollbar (progress bar) named "progress" . Name the  
stack "url_status" (or anything you want).

Make this stack a substack of your main stack. Then somewhere (e.g in  
your mainstack's preopenstack handler), include the following:

   start using "url_status"

That's it. After that, it should work for all downloads and uploads  
(which may not be what you want).

Cheers
Dave

--------------------------------------------------
local sUrls
----------------------------
on libraryStack
   libUrlSetStatusCallback "urlCallback", the long id of me
   palette me
   hide me
end libraryStack

-------------------------
on releaseStack
   libUrlSetStatusCallback empty
   close me
end releaseStack
---------------------------

on urlCallback pUrl, pStatusString

   show me
   put pUrl into field "url" of card 1 of me
   put item 1 of pStatusString into tStatus
   put tStatus into field "status" of card 1 of me
   put 1 into sUrls[pUrl]
   if tStatus is among the items of "loading,downloading,uploading" then

     put item 2 of pStatusString into tPart
     put item 3 of pStatusString into tWhole
     if tWhole <> empty then

       showProgress tPart,tWhole
     else
       hide scrollbar "progress" of me
       put "," && tPart && "bytes" after field "status" of card 1 of me
     end if
     --unlock screen
   else if tStatus is among the items of  
"loaded,downloaded,uploaded,cached" then
     delete local sUrls[pUrl]
     if the visible of scrollbar "progress" of me then showProgress 1,1
     send "hideStatus" to me in 200 milliseconds ##leave visible for  
short time

   else if tStatus is among the items of "error,timeout" then
     delete local sUrls[pUrl]
     send "hideStatus" to me in 200 milliseconds ##leave visible for  
short time
   else
     hide scrollbar "progress" of me
   end if
end urlCallback
-------------------------
on showProgress pPart,pWhole
   put the endValue of scrollbar  "progress" of me into tMax
   set the thumbPosition of scrollbar "progress" of me to round(tMax  
* pPart / pWhole)
   show scrollbar "progress" of me
   wait 10 milliseconds
end showProgress
-----------------------
on hideStatus
   if keys(sUrls) is empty then
     hide me
   end if
end hideStatus




More information about the use-livecode mailing list