1. David Cuny's GUI

hey i'm using david cuny's gui and i want to make a dialog box that will
track the progress of a fairly cpu intensive operation...so like it's
doin a whole bunch of stuff and while it's doing it says like
"64% done" and "24 seconds elapsed" and all stuff like that

what i'm trying to do right now is set up the dialog:

new_dialog("Progress", 0, 0, 50, 6, {})
add_control(1, BUTTON, "&Cancel", 4, 2, 12, 0, {CANCEL_BUTTON})
add_control(2, LABEL, "", 2, 2, 0, 0, {})  -- progress indicator

okay and then what i want to do is: (in pseudocode)

while get_dialog(EXIT_STATE) != CANCELLED do
  progress = do_stuff()
  set_attrib(2, TITLE, sprintf("%d precent done", progress))
  show_dialog()
end while

except it doesn't really work...and i can't really do edit_dialog()
at all because then it would wait for a key to be pressed or
whatever :\

any suggestions?

--

Mike Burrell
http://mikpos.base.org/
mikpos at hempseed.com

new topic     » topic index » view message » categorize

2. Re: David Cuny's GUI

------ =_NextPart_000_01BCEDCF.05F840A0

Hi, Mike.

As you've noticed, you can't just show the dialog and exit, because
the user may want to press the Cancel button while the action is
taking place.

So you've got to be able to do something like:

   display the dialog

   while true do
      do a little cpu intensive work
      if finished then
         exit
      end if

      process the keyboard an mouse
      if dialog is cancelled then
         exit
      end if

      update the dialog
   end while

   close the dialog

So you are sort of multitasking - doing a slice of work, then giving
up the cpu to the dialog.

Here's the main dialog loop, in DIALOG.E:

   global procedure run_dialog()

       -- generic dialog run routine

       -- display the objects
       show_dialog()

       -- loop forever
       while get_dialog(EXIT_STATE) = MORE do
           edit_dialog()    -- process keys
       end while

   end procedure


There are two problems to solve:

1. Adding the CPU intensive code.
2. Updating the dialog.

The first problem lies in edit_dialog(), also DIALOG.E. The code
that waits for a key or mouse event is:

    keyPressed = watch_keys()       -- wait for a key

Problem is, it really does /wait/ for a key/mouse event before continuing. What
 you
really want to have happen is for watch_keys to return a null key (-1) if
 nothing has
happened.

My solution would be to set up a flag in watch_keys() that would tell the
 routine NOT
to wait. The function is found in KEYS.E:


   -- new variable, placed at the top of the file
   global integer WAIT_FOR_KEY
   WAIT_FOR_KEY = 1


   < SNIP >

   global function watch_keys()

      < SNIP >

       keyPress = 0
       while keyPress = 0 do

      < SNIP >

         -- new code, placed at bottom of while loop
         -- exits loop if WAIT_FOR_KEY is false
         if not WAIT_FOR_KEY then
            -- dummy value
            keyPress = -1
         end if

      end while

       return keyPress

   end function

OK, now we can tell wait_keys() not to really wait for
a key. Now we need to rewrite run_dialog to check for keys
AND run that cpu intensive code:

   global procedure run_special_dialog()

       -- special dialog routine

       atom percent

       -- display the objects
       show_dialog()

       -- don't wait for a key from the user
       WAIT_FOR_KEY = 0

       -- loop forever
       while get_dialog(EXIT_STATE) = MORE do

           -- do a slice of work, return amoount of work left
           percent = cpu_intensive()

           -- finished? assume 1 = 100%
           if percent = 1 then
              -- set flag to exit loop
              set_dialog( EXIT_STATE, DONE )
           end if

           -- update the status on the dialog
           -- obviously, this code is incomplete
           position( col, row )
           put_colors( OF_DIALOG, sprinf( ... )

           -- check for a key/mouse event
           edit_dialog()    -- process keys

       end while

       -- turn key wait back on
       WAIT_FOR_KEY = 1

   end procedure

Notice that the dialog isn't being redrawn each time - only the status
 information.
This avoids a flickering dialog box on the screen.

Does this help?

-- David Cuny

------ =_NextPart_000_01BCEDCF.05F840A0

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu