home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / PowerD_mui / examples / InputHandler.d < prev    next >
Encoding:
Text File  |  2002-03-13  |  7.6 KB  |  261 lines

  1. OPT MUI
  2.  
  3. MODULE 'devices/timer','lib/amiga','libraries/mui'
  4.  
  5.  
  6. /* Instance Data */
  7.  
  8. OBJECT MyData
  9.     port:PTR TO MsgPort,
  10.     req:PTR TO TimeRequest,
  11.     ihnode:MUI_InputHandlerNode,
  12.     index:LONG
  13.  
  14.  
  15. /* Attributes and methods for the custom class */
  16.  
  17. CONST    MUISERIALNR_STUNTZI=1,
  18.         TAGBASE_STUNTZI=TAG_USER | MUISERIALNR_STUNTZI << 16,
  19.         MUIM_Class5_Trigger=TAGBASE_STUNTZI | $0001
  20.  
  21.  
  22. /* IO macros */
  23.  
  24. #define IO_SIGBIT(req)  (req.IO.MN.ReplyPort.SigBit)
  25. #define IO_SIGMASK(req) (1<<IO_SIGBIT(req))
  26.  
  27.  
  28. /* Some strings to display */
  29.  
  30. DEF    LifeOfBrian=[
  31.         'Cheer up, Brian. You know what they say.',
  32.         'Some things in life are bad,',
  33.         'They can really make you mad.',
  34.         'Other things just make you swear and curse.',
  35.         'When you\are chewing on life\as grissle,',
  36.         'Don\at grumble, give a whistle.',
  37.         'And this\all help things turn out for the best,',
  38.         'And...',
  39.         'Always look on the bright side of life',
  40.         'Always look on the light side of life',
  41.         'If life seems jolly rotten,',
  42.         'There\as something you\ave forgotten,',
  43.         'And that\as to laugh, and smile, and dance, and sing.',
  44.         'When you\are feeling in the dumps,',
  45.         'Don\at be silly chumps,',
  46.         'Just purse your lips and whistle, that\as the thing.',
  47.         'And...',
  48.         'Always look on the bright side of life, come on!',
  49.         'Always look on the right side of life',
  50.         'For life is quite absurd,',
  51.         'And death\as the final word.',
  52.         'You must always face the curtain with a bow.',
  53.         'Forget about your sin,',
  54.         'Give the audience a grin.',
  55.         'Enjoy it, it\as your last chance anyhow,',
  56.         'So...',
  57.         'Always look on the bright side of death',
  58.         'Just before you draw your terminal breath.',
  59.         'Life\as a piece of shit,',
  60.         'When you look at it.',
  61.         'Life\as a laugh, and death\as a joke, it\as true.',
  62.         'You\all see it\as all a show,',
  63.         'Keep \aem laughing as you go,',
  64.         'Just remember that the last laugh is on you.',
  65.         'And...',
  66.         'Always look on the bright side of life !',
  67.         '...',
  68.         '*** THE END ***',
  69.         '',
  70.         NIL]
  71.  
  72.  
  73.  
  74. /***************************************************************************/
  75. /* Here is the beginning of our new class...                               */
  76. /***************************************************************************/
  77.  
  78.  
  79. PROC mNew(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(ULONG)
  80.     DEF    data:PTR TO MyData
  81.  
  82.     IFN obj:=DoSuperMethodA(cl,obj,msg) THEN RETURN 0
  83.  
  84.     data := INST_DATA(cl,obj)
  85.  
  86.     IF data.port := CreateMsgPort()
  87.         IF data.req := CreateIORequest(data.port,SIZEOF_TimeRequest)
  88.             IFN OpenDevice(TIMERNAME,UNIT_VBLANK,data.req,0)
  89.  
  90.                 data.ihnode.Object  := obj
  91.                 data.ihnode.Signals := IO_SIGMASK(data.req)
  92.                 data.ihnode.Method  := MUIM_Class5_Trigger
  93.                 data.ihnode.Flags   := 0
  94.  
  95.                 data.index := 0
  96.  
  97.                 RETURN obj
  98.             ENDIF
  99.         ENDIF
  100.     ENDIF
  101.  
  102.     CoerceMethodA(cl,obj,OM_DISPOSE)
  103. ENDPROC 0
  104.  
  105.  
  106. PROC mDispose(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(ULONG)
  107.     DEF    data:PTR TO MyData
  108.  
  109.     data := INST_DATA(cl,obj)
  110.  
  111.     IF data.req
  112.         IF data.req.IO.Device THEN CloseDevice(data.req)
  113.         DeleteIORequest(data.req)
  114.     ENDIF
  115.  
  116.     IF data.port THEN DeleteMsgPort(data.port)
  117. ENDPROC DoSuperMethodA(cl,obj,msg)
  118.  
  119.  
  120. PROC mSetup(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(ULONG)
  121.     DEF    data:PTR TO MyData
  122.  
  123.     data := INST_DATA(cl,obj)
  124.  
  125.     IFN DoSuperMethodA(cl,obj,msg) THEN RETURN FALSE
  126.  
  127.     data.req.IO.Command := TR_ADDREQUEST
  128.     data.req.Time.Secs  := 1
  129.     data.req.Time.Micro := 0
  130.     SendIO(data.req)
  131.  
  132.     DoMethod(_app(obj),MUIM_Application_AddInputHandler,data.ihnode)
  133.  
  134. ENDPROC TRUE
  135.  
  136. PROC mCleanup(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(ULONG)
  137.     DEF    data:PTR TO MyData
  138.  
  139.     data := INST_DATA(cl,obj)
  140.  
  141.     DoMethod(_app(obj),MUIM_Application_RemInputHandler,data.ihnode)
  142.  
  143.     IFN CheckIO(data.req) THEN AbortIO(data.req)
  144.     WaitIO(data.req)
  145.  
  146. ENDPROC DoSuperMethodA(cl,obj,msg)
  147.  
  148. PROC mTrigger(cl:PTR TO IClass,obj:PTR TO _Object,msg:PTR TO Msg)(ULONG)
  149.     DEF    data:PTR TO MyData
  150.  
  151.     data := INST_DATA(cl,obj)
  152.  
  153.     IF CheckIO(data.req)
  154.         WaitIO(data.req)
  155.         data.req.IO.Command := TR_ADDREQUEST
  156.         data.req.Time.Secs  := 1
  157.         data.req.Time.Micro := 0
  158.         SendIO(data.req)
  159.  
  160.         set(obj,MUIA_Text_Contents,LifeOfBrian[data.index])
  161.  
  162.         IFN LifeOfBrian[++data.index] THEN data.index := 0
  163.  
  164.         RETURN TRUE
  165.     ENDIF
  166.  
  167. ENDPROC FALSE
  168.  
  169.  
  170. /*
  171. ** Here comes the dispatcher for our custom class.
  172. */
  173.  
  174. PROC MyDispatcher(cl:PTR TO IClass IN a0,obj IN a2,msg:PTR TO Msg IN a1)(LONG)
  175.  
  176.     SELECT msg.MethodID
  177.     CASE OM_NEW              ; RETURN mNew    (cl,obj,msg)
  178.     CASE OM_DISPOSE          ; RETURN mDispose(cl,obj,msg)
  179.     CASE MUIM_Setup          ; RETURN mSetup  (cl,obj,msg)
  180.     CASE MUIM_Cleanup        ; RETURN mCleanup(cl,obj,msg)
  181.     CASE MUIM_Class5_Trigger ; RETURN mTrigger(cl,obj,msg)
  182.     ENDSELECT
  183.  
  184. ENDPROC DoSuperMethodA(cl,obj,msg)
  185.  
  186.  
  187. /***************************************************************************/
  188. /* Thats all there is about it. Now lets see how things are used...        */
  189. /***************************************************************************/
  190.  
  191. PROC main()
  192.     DEF    app:PTR TO _Object,window:PTR TO _Object,MyObj:PTR TO _Object
  193.     DEF    mcc:PTR TO MUI_CustomClass
  194.     DEFUL    sigs
  195.  
  196.     /* Create the new custom class with a call to MUI_CreateCustomClass(). */
  197.     /* Caution: This function returns not a struct IClass, but a           */
  198.     /* struct MUI_CustomClass which contains a struct IClass to be         */
  199.     /* used with NewObject() calls.                                        */
  200.     /* Note well: MUI creates the dispatcher hook for you, you may         */
  201.     /* *not* use its h_Data field! If you need custom data, use the        */
  202.     /* cl_UserData of the IClass structure!                                */
  203.  
  204.     IFN mcc := MUI_CreateCustomClass(NIL,MUIC_Text,NIL,SIZEOF_MyData,&MyDispatcher) THEN Raise('Could not create custom class.')
  205.  
  206.     app := ApplicationObject,
  207.         MUIA_Application_Title      , 'Class5',
  208.         MUIA_Application_Version    , '$VER: Class5 19.5 (12.02.97)',
  209.         MUIA_Application_Copyright  , '©1993, Stefan Stuntz',
  210.         MUIA_Application_Author     , 'Stefan Stuntz',
  211.         MUIA_Application_Description, 'Demonstrate the use of custom classes.',
  212.         MUIA_Application_Base       , 'Class5',
  213.  
  214.         SubWindow, window := WindowObject,
  215.             MUIA_Window_Title, 'Input Handler Class',
  216.             MUIA_Window_ID   , "CLS5",
  217.             WindowContents, VGroup,
  218.                 Child, TextObject,
  219.                     TextFrame,
  220.                     MUIA_Background, MUII_TextBack,
  221.                     MUIA_Text_Contents, '\ecDemonstration of a class that reacts on\nevents (here: timer signals) automatically.',
  222.                 End,
  223.                 Child, MyObj := NewObject(mcc.Class,NIL,
  224.                     TextFrame,
  225.                     MUIA_Background, MUII_BACKGROUND,
  226.                     MUIA_Text_PreParse, '\ec',
  227.                 TAG_DONE),
  228.             End,
  229.         End,
  230.     End
  231.  
  232.     IFN app THEN Raise('Failed to create Application.')
  233.  
  234.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,MUI_TRUE,
  235.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit)
  236.  
  237. /*
  238. ** This is the ideal input loop for an object oriented MUI application.
  239. ** Everything is encapsulated in classes, no return ids need to be used,
  240. ** we just check if the program shall terminate.
  241. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  242. ** from Wait() (or 0). This makes the input loop significantly faster.
  243. */
  244.  
  245.     set(window,MUIA_Window_Open,MUI_TRUE)
  246.  
  247.     WHILE (DoMethod(app,MUIM_Application_NewInput,&sigs) <> MUIV_Application_ReturnID_Quit)
  248.         IF sigs THEN sigs:=Wait(sigs)
  249.     ENDWHILE
  250.  
  251.     set(window,MUIA_Window_Open,FALSE)
  252.  
  253. EXCEPTDO
  254. /*
  255. ** Shut down...
  256. */
  257.     IF app THEN MUI_DisposeObject(app)     /* dispose all objects. */
  258.     IF mcc THEN MUI_DeleteCustomClass(mcc) /* delete the custom class. */
  259.     IF exception THEN PrintF('\s\n',exception)
  260. ENDPROC
  261.