home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / e / EasyGUI_v33b2.lha / Src / EasyGUI / led_test2.e < prev    next >
Text File  |  1997-02-12  |  4KB  |  150 lines

  1. -> This one shows the use of multi-window GUIs with PLUGINs, and multiple,
  2. -> pseudo-asynchronous activities.
  3. MODULE 'tools/easygui', 'tools/exceptions',
  4.        'graphics/text', 'intuition/intuition',
  5.        'plugins/led'
  6.  
  7. -> Global for multiforall().
  8. DEF gh:PTR TO guihandle
  9.  
  10. -> Number of GUIs counting.
  11. DEF counting=0
  12.  
  13. CONST NUM_VALUES=2
  14.  
  15. -> Store all the GUI data in one place.
  16. OBJECT mygui
  17.   -> In particular, keep PLUGIN references here.
  18.   ledplug:PTR TO led
  19.   gh:PTR TO guihandle
  20.   gui
  21.   title
  22.   going
  23.   values[NUM_VALUES]:ARRAY OF INT
  24. ENDOBJECT
  25.  
  26. -> The main loop: create one window to start with.
  27. PROC main() HANDLE
  28.   DEF mh=NIL
  29.   mh:=multiinit()
  30.   create(mh)
  31.   multiloop(mh)
  32. EXCEPT DO
  33.   cleanmulti(mh)
  34.   report_exception()
  35. ENDPROC
  36.  
  37. -> Create a new GUI.
  38. PROC create(mh) HANDLE
  39.   DEF t, g, gui=NIL:PTR TO mygui
  40.   NEW gui
  41.   -> Allocate all PLUGINs for the GUI like this.
  42.   NEW gui.ledplug.led(NUM_VALUES,gui.values,TRUE)
  43.   -> Now we can try opening a GUI.
  44.   gui.gh:=addmultiA(mh,'BOOPSI in EasyGUI!',
  45.                    g:=NEW [ROWS,
  46.                      t:=NEW [TEXT,'LED Boopsi image tester...',NIL,TRUE,16],
  47.                      NEW [COLS,
  48.                         NEW [EQROWS,
  49.                           NEW [BUTTON,{runaction},'Run/Stop'],
  50.                           NEW [BUTTON,{spawn},'Spawn'],
  51.                           NEW [BUTTON,0,'Quit']
  52.                         ],
  53.                         NEW [PLUGIN,0,gui.ledplug]
  54.                      ]
  55.                    ],
  56.                    [EG_INFO,gui, EG_LEFT,Rnd(400), EG_TOP,Rnd(400),
  57.                     -> The cleanup routine will deallocate the PLUGINs used.
  58.                     EG_CLEAN,{cleanmygui}, EG_CLOSE,{close}, NIL])
  59.   gui.gui:=g
  60.   gui.title:=t
  61. EXCEPT
  62.   -> If there was any problem then it may have been the creation of PLUGINs
  63.   -> or addmultiA().  Luckily, addmultiA() (and guiinitA()) will *not* call
  64.   -> the EG_CLEAN function if they caused the problem, so we can (safely).
  65.   cleanmygui(gui)
  66.   ReThrow()
  67. ENDPROC
  68.  
  69. -> The custom clean up code for each GUI.
  70. PROC cleanmygui(gui:PTR TO mygui)
  71.   IF gui
  72.     disposegui(gui.gui)
  73.     END gui.ledplug
  74.     END gui
  75.   ENDIF
  76. ENDPROC
  77.  
  78. -> The action function creates a new GUI in the group.
  79. PROC spawn(info:PTR TO mygui) IS create(info.gh.mh)
  80.  
  81. -> The run/stop action.
  82. PROC runaction(info:PTR TO mygui)
  83.   IF info.going
  84.     settext(info.gh,info.title,'You stopped me!')
  85.     stop(info)
  86.   ELSE
  87.     settext(info.gh,info.title,'Started counting...')
  88.     Delay(10)
  89.     settext(info.gh,info.title,'Counting...')
  90.     run(info)
  91.   ENDIF
  92. ENDPROC
  93.  
  94. PROC stop(info:PTR TO mygui)
  95.   -> Going, so stop.
  96.   info.going:=FALSE
  97.   -> One less is counting.
  98.   DEC counting
  99. ENDPROC
  100.  
  101. PROC run(info:PTR TO mygui)
  102.   DEF mh
  103.   -> Stopped, so go.
  104.   info.going:=TRUE
  105.   -> One more counting.
  106.   INC counting
  107.   -> We're the first so we're the loop.
  108.   IF counting=1
  109.     mh:=info.gh.mh
  110.     -> While there is someone counting
  111.     WHILE counting
  112.       -> Tick each GUI that's going.
  113.       multiforall({gh},mh,`next(gh.info))
  114.       checkmulti(mh)
  115.       Delay(10)
  116.     ENDWHILE
  117.   ENDIF
  118. ENDPROC
  119.  
  120. -> Next count.
  121. PROC next(info:PTR TO mygui)
  122.   DEF l:PTR TO led,h,m
  123.   IF info.going
  124.     l:=info.ledplug
  125.     l.colon:=(l.colon=FALSE)
  126.     m:=info.values[1]+1
  127.     IF m=60
  128.       m:=0
  129.       h:=info.values[]+1
  130.       IF h=13
  131.         h:=0
  132.         info.going:=FALSE
  133.         DEC counting
  134.         settext(info.gh,info.title,'Finished!')
  135.       ENDIF
  136.       l.values[]:=h
  137.     ENDIF
  138.     l.values[1]:=m
  139.     l.redisplay()
  140.   ENDIF
  141. ENDPROC
  142.  
  143. -> Close function.
  144. PROC close(info:PTR TO mygui)
  145.   -> Stop if running.
  146.   IF info.going THEN stop(info)
  147.   -> Destroy window.
  148.   cleangui(info.gh)
  149. ENDPROC
  150.