home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 595.lha / Windex_v1.1 / Windex.c < prev    next >
C/C++ Source or Header  |  1991-12-12  |  5KB  |  215 lines

  1. /*
  2.  *                        © Pierre Dak Baillargeon 1991.
  3.  *
  4.  *
  5.  *  NAME
  6.  *      Windex.c -- Translate RAWKEY to CLOSEWINDOW events.
  7.  *
  8.  *  SYNOPSIS
  9.  *      Windex
  10.  *
  11.  *  FUNCTION
  12.  *      Scan the input event chain to translate the esc keycode from
  13.  *      RAWKEY events in CLOSEWINDOW events and WindowToFront calls.
  14.  *
  15.  *  DESCRIPTION
  16.  *      Scan the event one by one.  If its class is RAWKEY, its code
  17.  *      is ESC and its qualifier CTRL, then take the currently active
  18.  *      window and put it in the event address field and set the class
  19.  *      to CLOSEWINDOW.  If the code is an arrow key, browse through
  20.  *        the list of windows and screens.
  21.  *
  22.  *      The scanner take the active window pointer from IntuitionBase.
  23.  *      It also check for ACTIVEWINDOW class events.  When it finds one,
  24.  *      it update its internal active window pointer (NOT the one found
  25.  *      in IntuitionBase.  Let it do that by itself !).
  26.  *
  27.  *  NOTE
  28.  *      I might let the user choose his own keycode and qualifier.  But
  29.  *      that would requires that he knew the keycode and qualifier codes
  30.  *      unless we would create a translation table, which would increase
  31.  *      substantially the executable size.
  32.  *
  33.  *      To stop Windex, hit ctrl-C or use the break CLI command.
  34.  *
  35.  */
  36.  
  37.  
  38. void main(void);
  39. void PopScreen (struct IntuitionBase *);
  40. void PushScreen (struct IntuitionBase *);
  41. void PopWindow (struct IntuitionBase *);
  42. void PushWindow (struct IntuitionBase *);
  43. struct InputEvent * __asm Handler (register __a0 struct InputEvent *, register __a1 struct IntuitionBase *);
  44.  
  45.  
  46. void main(void)
  47. {
  48.     struct IntuitionBase *    IntuitionBase;
  49.     struct MsgPort *        InputPort = NULL;
  50.     struct Interrupt *        InputHandler = NULL;
  51.     struct IOStdReq *        InputReq = NULL;
  52.  
  53.     if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library", 0L)))
  54.     {
  55.         if ((InputPort = CreatePort (0L, 0L)))
  56.         {
  57.             if ( (InputReq = (struct IOStdReq *)CreateExtIO (InputPort, sizeof (struct IOStdReq))))
  58.             {
  59.                 if (!OpenDevice ("input.device", NULL, InputReq, NULL))
  60.                 {
  61.                     if ( (InputHandler = AllocMem (sizeof (struct Interrupt), MEMF_PUBLIC|MEMF_CLEAR)))
  62.                     {
  63.                         InputHandler->is_Code = (void (*__far)(void))Handler;
  64.                         InputHandler->is_Data = (APTR)IntuitionBase;
  65.                         InputHandler->is_Node.ln_Pri = 100;
  66.                         InputHandler->is_Node.ln_Name = "Escape Window";
  67.  
  68.                         InputReq->io_Command = IND_ADDHANDLER;
  69.                         InputReq->io_Data = (APTR)InputHandler;
  70.                         DoIO (InputReq);
  71.  
  72.                         Wait (SIGBREAKF_CTRL_C);
  73.  
  74.                         InputReq->io_Command = IND_REMHANDLER;
  75.                         InputReq->io_Data = (APTR)InputHandler;
  76.                         DoIO (InputReq);
  77.  
  78.                         FreeMem (InputHandler, sizeof (struct Interrupt));
  79.                     }
  80.                     CloseDevice (InputReq);
  81.                 }
  82.                 DeleteExtIO (InputReq);
  83.             }
  84.             DeletePort (InputPort);
  85.         }
  86.         CloseLibrary (IntuitionBase);
  87.     }
  88. }
  89.  
  90. #define ESC        0x45
  91. #define LEFT    0x4F
  92. #define RIGHT    0x4E
  93. #define UP        0x4C
  94. #define DOWN    0x4D
  95. #define QUAL    IEQUALIFIER_CONTROL
  96.  
  97. struct InputEvent * __asm Handler (register __a0 struct InputEvent *chain, register __a1 struct IntuitionBase *IntuitionBase)
  98. {
  99.     struct InputEvent *ie = chain;
  100.  
  101.     while (ie)
  102.     {
  103.         if (ie->ie_Class == IECLASS_RAWKEY)
  104.         {
  105.             if (ie->ie_Qualifier & QUAL)
  106.             {
  107.                 switch (ie->ie_Code)
  108.                 {
  109.                     case ESC:
  110.                         if (IntuitionBase->ActiveWindow->IDCMPFlags & CLOSEWINDOW)
  111.                         {
  112.                             if ( (IntuitionBase->ActiveWindow->Flags & BACKDROP+WBENCHWINDOW) != BACKDROP+WBENCHWINDOW)
  113.                             {
  114.                                 ie->ie_Class = IECLASS_CLOSEWINDOW;
  115.                                 ie->ie_EventAddress = (APTR)IntuitionBase->ActiveWindow;
  116.                             }
  117.                         }
  118.                         break;
  119.                     case UP:
  120.                         PushScreen (IntuitionBase);
  121.                         ie->ie_Class = IECLASS_NULL;
  122.                         break;
  123.                     case DOWN:
  124.                         PopScreen (IntuitionBase);
  125.                         ie->ie_Class = IECLASS_NULL;
  126.                         break;
  127.                     case RIGHT:
  128.                         PushWindow (IntuitionBase);
  129.                         ie->ie_Class = IECLASS_NULL;
  130.                         break;
  131.                     case LEFT:
  132.                         PopWindow (IntuitionBase);
  133.                         ie->ie_Class = IECLASS_NULL;
  134.                         break;
  135.                     default:
  136.                         break;
  137.                 }
  138.             }
  139.         }
  140.         ie = ie->ie_NextEvent;
  141.     }
  142.     return chain;
  143. }
  144.  
  145.  
  146. void PopScreen (struct IntuitionBase *IntuitionBase)
  147. {
  148.     struct Screen *s = IntuitionBase->ActiveScreen;
  149.  
  150.     if (s != NULL)
  151.     {
  152.         while (s->NextScreen)
  153.             s = s->NextScreen;
  154.         ScreenToFront (s);
  155.         if (s->FirstWindow)
  156.             ActivateWindow (s->FirstWindow);
  157.     }
  158. }
  159.  
  160.  
  161. void PushScreen (struct IntuitionBase *IntuitionBase)
  162. {
  163.     struct Screen *s = IntuitionBase->ActiveScreen;
  164.  
  165.     if (s)
  166.     {
  167.         ScreenToBack (s);
  168.         s = IntuitionBase->FirstScreen;
  169.         if (s)
  170.         {
  171.             if (s->FirstWindow)
  172.             {
  173.                 ActivateWindow (s->FirstWindow);
  174.             }
  175.         }
  176.     }
  177. }
  178.  
  179.  
  180. void PopWindow (struct IntuitionBase *IntuitionBase)
  181. {
  182.     struct Window *w = IntuitionBase->ActiveWindow;
  183.     struct Window *nw;
  184.  
  185.     if (w)
  186.     {
  187.         nw = IntuitionBase->ActiveScreen->FirstWindow;
  188.         while (nw->NextWindow != NULL && nw->NextWindow != w)
  189.                 nw = nw->NextWindow;
  190.         if (nw != w)
  191.         {
  192.             WindowToFront (nw);
  193.             ActivateWindow (nw);
  194.         }
  195.     }
  196. }
  197.  
  198.  
  199. void PushWindow (struct IntuitionBase *IntuitionBase)
  200. {
  201.     struct Window *w = IntuitionBase->ActiveWindow;
  202.     struct Window *nw;
  203.  
  204.     if (w)
  205.     {
  206.         if ((nw = w->NextWindow) == NULL)
  207.             nw = IntuitionBase->ActiveScreen->FirstWindow;
  208.         if (nw != w)
  209.         {
  210.             WindowToBack (w);
  211.             ActivateWindow (nw);
  212.         }
  213.     }
  214. }
  215.