home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 083.lha / twm / test1.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  6KB  |  187 lines

  1. /* test1.c
  2.  
  3.    This is a test program for TWM, and provides an example of using twmClient
  4.    in an application. It puts up a window named "Press any key"; if a key is
  5.    pressed, the window is taken down, and is replaced by either a tiny window
  6.    or by a gadget in TWM's window (if PostMe() returns TRUE).  Clicking in
  7.    the main part of the tiny window kills it, and brings the big window back.
  8.    The same applies to the gadget in the TWM window, if it is being used
  9.    instead: clicking on it kills it and bring up this program's big window.
  10.    Clicking close in either the big or the small window exits the program.
  11.  
  12.    The compiled test1 program may be copied to test2, test3 etc, and all
  13.    copies run simultaneously, to get a better idea of how TWM works.
  14.  
  15.    Under Aztec, put this program and twmClient.c in the current directory,
  16.    and twm.h in the subdirectory "header", then compile and link thus:
  17.  
  18.       cc +Htwm.p header/twm.h
  19.       cc +Itwm.p test1
  20.       cc +Itwm.p twmClient
  21.  
  22.       ln test1.o twmClient.o -lc
  23. */
  24.  
  25. #include "header/twm.h"
  26.  
  27. #define HUGE_WINDOW_NAME   ((UBYTE *)"Press any key")
  28.  
  29. struct NewWindow wtiny =
  30. {
  31.    280, 120, 120, 20,   /* left, top, width, height   */
  32.      0,  1,             /* detail pen, block pen      */
  33.    MOUSEBUTTONS         /* IDCMP flags                */
  34.    | CLOSEWINDOW,
  35.    WINDOWDRAG           /* Window flags               */
  36.    | WINDOWCLOSE
  37.    | WINDOWDEPTH
  38.    | SMART_REFRESH
  39.    | ACTIVATE,
  40.    NULL,                /* application gadget list    */
  41.    NULL,                /* special checkmark imagery  */
  42.    NULL,                /* window title               */
  43.    NULL,                /* custom screen pointer      */
  44.    NULL,                /* super bitmap pointer       */
  45.    0, 0, 0, 0,          /* min/max width and height   */
  46.    WBENCHSCREEN         /* screen type                */
  47. };
  48.  
  49. struct NewWindow whuge =
  50. {
  51.    280, 120, 360, 60,   /* left, top, width, height   */
  52.      0,  1,             /* detail pen, block pen      */
  53.    CLOSEWINDOW          /* IDCMP flags                */
  54.    | RAWKEY,
  55.    WINDOWDRAG           /* Window flags               */
  56.    | WINDOWCLOSE
  57.    | WINDOWDEPTH
  58.    | SMART_REFRESH
  59.    | ACTIVATE,
  60.    NULL,                /* application gadget list    */
  61.    NULL,                /* special checkmark imagery  */
  62.    HUGE_WINDOW_NAME,    /* window title               */
  63.    NULL,                /* custom screen pointer      */
  64.    NULL,                /* super bitmap pointer       */
  65.    0, 0, 0, 0,          /* min/max width and height   */
  66.    WBENCHSCREEN         /* screen type                */
  67. };
  68.  
  69. extern VOID *OpenWindow(), *OpenLibrary(), *AllocMem(), *GetMsg();
  70.  
  71. struct IntuitionBase *IntuitionBase = NULL;
  72.  
  73. struct NewWindow    *nw    = NULL;
  74. struct Window       *w     = NULL;
  75. struct IntuiMessage *Imsg  = NULL;
  76.  
  77. main (argc, argv)
  78. int argc;
  79. char **argv;
  80. {
  81. int exitflag;  /* true when close gadget has been pressed         */
  82. int swapflag;  /* true when it's time to switch between windows   */
  83. int tinyflag;  /* true when the tiny window is up                 */
  84. UWORD class;   /* IDCMP message class                             */
  85. UWORD code;    /* IDCMP message code                              */
  86. char filename[31];
  87. register int i, c;
  88.  
  89.    /* Use program name as title of tiny window */
  90.    for (i = strlen(argv[0]) - 1;
  91.          i > 0 && (c = argv[0][i - 1]) != ':' && c != '/';
  92.          i--)
  93.       ;
  94.    wtiny.Title = (UBYTE *)(&argv[0][i]);
  95.  
  96.    if ((IntuitionBase = OpenLibrary("intuition.library", 33L)) == NULL)
  97.       CloseStuff(E_OPEN_INTUI);
  98.  
  99.    /* Let twmClient do its allocations... we don't care this time, but it
  100.       returns FALSE if the allocations fail
  101.    */
  102.    twmInit();
  103.  
  104.    /* initialize flags */
  105.    tinyflag = swapflag = exitflag = FALSE;
  106.  
  107.    /* open the big window, and save pointer to its NewWindow struct */
  108.    if ((w = OpenWindow(nw = &whuge)) == NULL)
  109.       CloseStuff(E_OPEN_WINDOW);
  110.  
  111.    /* IDCMP loop */
  112.    while (!exitflag)
  113.    {
  114.       Wait(1L << w->UserPort->mp_SigBit);
  115.  
  116.       while (Imsg = GetMsg(w->UserPort))
  117.       {
  118.          class = Imsg->Class;
  119.          code  = Imsg->Code;
  120.          ReplyMsg(Imsg);
  121.  
  122.          if (class == CLOSEWINDOW)
  123.             exitflag = TRUE;
  124.  
  125.          /* swap if tiny window clicked, or key pressed in big window */
  126.          else if ((class == MOUSEBUTTONS && code == SELECTUP && tinyflag)
  127.                   || class == RAWKEY)
  128.             swapflag = TRUE;
  129.       }
  130.  
  131.       if (swapflag)
  132.       {
  133.          swapflag = FALSE;
  134.  
  135.          /* remember where this window is now stationed, and close it */
  136.          SavePosCloseW(nw, w);
  137.  
  138.          /* if tiny window is now up, or PostMe() fails, open other window */
  139.          if (tinyflag || !PostMe(wtiny.Title))
  140.          {
  141.             nw = tinyflag ? &whuge : &wtiny;
  142.             tinyflag = !tinyflag;
  143.          }
  144.  
  145.          if ((w = OpenWindow(nw)) == NULL)
  146.             CloseStuff(E_OPEN_WINDOW);
  147.       }
  148.    }
  149.  
  150.    CloseStuff(E_OK);
  151. }
  152.  
  153. /* CloseStuff
  154. *
  155. *  Call twmCleanUp() to deallocate messages and port we've been using,
  156. *  then close our own stuff and exit.
  157. */
  158.  
  159. CloseStuff (error)
  160. int error;     /* errors start at 500 (see twm.h) except for E_OK = 0   */
  161. {
  162.    twmCleanUp();  /* twmClient deallocations */
  163.  
  164.    if (w)               CloseWindow(w);
  165.    if (IntuitionBase)   CloseLibrary(IntuitionBase);
  166.  
  167.    exit(error);
  168. }
  169.  
  170. /* SavePosCloseW
  171. *
  172. *  Save the current window position and size in the NewWindow structure for
  173. *  that window, then close the window.
  174. */
  175.  
  176. static SavePosCloseW (nw, w)
  177. register struct NewWindow *nw;
  178. register struct Window *w;
  179. {
  180.    nw->LeftEdge = w->LeftEdge;
  181.    nw->TopEdge  = w->TopEdge;
  182.    nw->Width    = w->Width;
  183.    nw->Height   = w->Height;
  184.  
  185.    CloseWindow(w);
  186. }
  187.