home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / graphics / utilities / showgadgets / showgadgets.c < prev    next >
C/C++ Source or Header  |  1990-12-20  |  7KB  |  233 lines

  1. /*========================================================*/
  2. /*                                                                                                                */
  3. /* Show gadgets in a window    V1.0                                                    */
  4. /* © J.Tyberghein                                                                                    */
  5. /*        Mon Mar  5 09:16:17 1990 V1.0                                                */
  6. /*                                                                                                                */
  7. /*========================================================*/
  8.  
  9. #include <exec/types.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12. #include <clib/graphics_protos.h>
  13. #include <clib/intuition_protos.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/graphics.h>
  17. #include <proto/intuition.h>
  18. #include <exec/interrupts.h>
  19. #include <devices/inputevent.h>
  20. #include <devices/input.h>
  21. #include <intuition/intuition.h>
  22. #include <intuition/intuitionbase.h>
  23. #include <string.h>
  24.  
  25. /*=============================== Data ======================================*/
  26.  
  27. APTR SysBase;
  28. struct DosLibrary *DOSBase;
  29. struct IntuitionBase *IntuitionBase;
  30. struct GfxBase *GfxBase;
  31.  
  32. void OpenStuff ();
  33. void __regargs CloseStuff (int);
  34. void Print (char *);
  35.  
  36. /* NewWindow structure for our dummy window */
  37. struct NewWindow nWin =
  38.     {
  39.         0,0,0,0,0,1,
  40.         NULL,
  41.         BORDERLESS,
  42.         NULL,NULL,NULL,NULL,NULL,
  43.         0,0,0,0,
  44.         CUSTOMSCREEN
  45.     };
  46.  
  47. /* Everything for the input device */
  48. #define GKEY                0x24
  49. #define ESCAPEKEY        0x45
  50.  
  51. /* The following structure is needed to pass information from our input
  52. device handler to our task */
  53. typedef struct
  54.     {
  55.         struct Task *TaskToSig;                /* Pointer to our own task */
  56.         ULONG QuitSig,QuitSigNum;            /* Signal to quit ShowGadgets */
  57.         ULONG ActionSig,ActionSigNum;    /* Someone pressed AMIGA-AMIGA-G */
  58.     } Global_Data;
  59.  
  60. Global_Data Global;
  61.  
  62. struct MsgPort *InputDevPort = NULL;
  63. struct IOStdReq *InputRequestBlock = NULL;
  64. struct Interrupt HandlerStuff;
  65.  
  66. /*=============================== Code ======================================*/
  67.  
  68.  
  69. /*------------------------------ main program -------------------------------*/
  70.  
  71. void __saveds myMain ()
  72. {
  73.     struct Window *win,*myWin;
  74.     int w,h,sig,xx,yy;
  75.     struct RastPort *rp;
  76.     struct Gadget *g;
  77.  
  78.     OpenStuff ();
  79.     Print ("ShowGadgets 1.0  Written by J.Tyberghein 5 Mar 90\n");
  80.     Print ("Press 'AMIGA-AMIGA-G' for gadget view ");
  81.     Print ("(Left mouse button to stop view)\n");
  82.     Print ("Press 'AMIGA-AMIGA-ESC' to quit\n");
  83.     for ( ; ; )
  84.         {
  85.             sig = Wait (Global.QuitSig | Global.ActionSig);
  86.             if (sig & Global.QuitSig) break;
  87.             if (sig & Global.ActionSig)
  88.                 {
  89.                     win = IntuitionBase->ActiveWindow;
  90.                     nWin.LeftEdge = win->LeftEdge;
  91.                     nWin.TopEdge = win->TopEdge;
  92.                     w = nWin.Width = win->Width;
  93.                     h = nWin.Height = win->Height;
  94.                     nWin.Screen = win->WScreen;
  95.                     if (!(myWin = (struct Window *)OpenWindow (&nWin)))
  96.                         {
  97.                             Print ("Error opening window\n");
  98.                             DisplayBeep (0L);
  99.                         }
  100.                     else
  101.                         {
  102.                             rp = myWin->RPort;
  103.                             Forbid ();
  104.                             SetRast (rp,0L);
  105.                             SetAPen (rp,1L);
  106.                             g = win->FirstGadget;
  107.                             while (g)
  108.                                 {
  109.                                     int x1,y1,x2,y2;
  110.  
  111.                                     x1 = g->LeftEdge; y1 = g->TopEdge;
  112.                                     if (g->Flags & GRELRIGHT) x1 += w;
  113.                                     if (g->Flags & GRELBOTTOM) y1 += h;
  114.                                     if (g->Flags & GRELWIDTH) xx = x2 = x1+g->Width+w-1;
  115.                                     else x2 = x1+g->Width-1;
  116.                                     if (g->Flags & GRELHEIGHT) yy = y2 = y1+g->Height+h-1;
  117.                                     else y2 = y1+g->Height-1;
  118.                                     Move (rp,x1,y1);
  119.                                     Draw (rp,x2,y1);
  120.                                     Draw (rp,x2,y2);
  121.                                     Draw (rp,x1,y2);
  122.                                     Draw (rp,x1,y1);
  123.                                     g = g->NextGadget;
  124.                                 }
  125.                             while ((*(BYTE *)0xbfe001)&64) ;
  126.                             CloseWindow (myWin);
  127.                             Permit ();
  128.                         }
  129.                 }
  130.         }
  131.     Print ("Done !\n");
  132.     CloseStuff (0);
  133. }
  134.  
  135. /*-------------------------- Print something --------------------------------*/
  136.  
  137. void Print (char *str)
  138. {
  139.     Write (Output (),str,strlen (str));
  140. }
  141.  
  142. /*------------------------ InputEvent handler -------------------------------*/
  143.  
  144. struct InputEvent * __saveds __asm MyHandler
  145.             (register __a0 struct InputEvent *ev, register __a1 Global_Data *gdptr)
  146. {
  147.     register struct InputEvent *ep;
  148.  
  149.     for (ep=ev ; ep ; ep=ep->ie_NextEvent)
  150.         if (ep->ie_Class == IECLASS_RAWKEY)
  151.             if (ep->ie_Code == GKEY && (ep->ie_Qualifier &
  152.                 IEQUALIFIER_RCOMMAND) && (ep->ie_Qualifier & IEQUALIFIER_LCOMMAND))
  153.                 {
  154.                     ep->ie_Class = IECLASS_NULL;
  155.                     Signal (gdptr->TaskToSig,gdptr->ActionSig);
  156.                 }
  157.             else if (ep->ie_Code == ESCAPEKEY && (ep->ie_Qualifier &
  158.                 IEQUALIFIER_RCOMMAND) && (ep->ie_Qualifier & IEQUALIFIER_LCOMMAND))
  159.                 {
  160.                     ep->ie_Class = IECLASS_NULL;
  161.                     Signal (gdptr->TaskToSig,gdptr->QuitSig);
  162.                 }
  163.     return (ev);
  164. }
  165.  
  166. /*----------------------------- OpenStuff -----------------------------------*/
  167.  
  168. void OpenStuff ()
  169. {
  170.     SysBase = (APTR)*(LONG *)4;
  171.     DOSBase = (struct DosLibrary *)OpenLibrary ("dos.library",0L);
  172.     IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library",0L);
  173.     GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library",0L);
  174.     Global.QuitSigNum = Global.ActionSigNum = 0L;
  175.     Global.TaskToSig = FindTask (0L);
  176.     if ((Global.QuitSigNum = AllocSignal (-1L)) == -1L)
  177.         {
  178.             Print ("Error allocating signal\n");
  179.             CloseStuff (4);
  180.         }
  181.     Global.QuitSig = 1L<<Global.QuitSigNum;
  182.     if ((Global.ActionSigNum = AllocSignal (-1L)) == -1L)
  183.         {
  184.             Print ("Error allocating signal\n");
  185.             CloseStuff (4);
  186.         }
  187.     Global.ActionSig = 1L<<Global.ActionSigNum;
  188.     if (!(InputDevPort = CreatePort (0L,0L)))
  189.         {
  190.             Print ("Error creating InputDevPort\n");
  191.             CloseStuff (1);
  192.         }
  193.     if (!(InputRequestBlock = CreateStdIO (InputDevPort)))
  194.         {
  195.             Print ("Error creating Standard IO\n");
  196.             CloseStuff (2);
  197.         }
  198.     HandlerStuff.is_Data = (APTR)&Global;
  199.     HandlerStuff.is_Code = (VOID (*)())MyHandler;
  200.     HandlerStuff.is_Node.ln_Pri = 53;
  201.     if (OpenDevice ("input.device",0L,(struct IORequest *)InputRequestBlock,0L))
  202.         {
  203.             Print ("Error opening Input.device\n");
  204.             CloseStuff (3);
  205.         }
  206.     InputRequestBlock->io_Command = IND_ADDHANDLER;
  207.     InputRequestBlock->io_Data = (APTR)&HandlerStuff;
  208.     DoIO ((struct IORequest *)InputRequestBlock);
  209. }
  210.  
  211. /*---------------------------- CloseStuff -----------------------------------*/
  212.  
  213. void __regargs CloseStuff (int Error)
  214. {
  215.     if (DOSBase) CloseLibrary ((struct Library *)DOSBase);
  216.     if (IntuitionBase) CloseLibrary ((struct Library *)IntuitionBase);
  217.     if (GfxBase) CloseLibrary ((struct Library *)GfxBase);
  218.     if (Global.ActionSigNum) FreeSignal (Global.ActionSigNum);
  219.     if (Global.QuitSigNum) FreeSignal (Global.QuitSigNum);
  220.     if (InputRequestBlock)
  221.         {
  222.             InputRequestBlock->io_Command = IND_REMHANDLER;
  223.             InputRequestBlock->io_Data = (APTR)&HandlerStuff;
  224.             DoIO ((struct IORequest *)InputRequestBlock);
  225.             CloseDevice ((struct IORequest *)InputRequestBlock);
  226.             DeleteStdIO (InputRequestBlock);
  227.         }
  228.     if (InputDevPort) DeletePort (InputDevPort);
  229.     Exit (Error);
  230. }
  231.  
  232. /*================================ End ======================================*/
  233.