home *** CD-ROM | disk | FTP | other *** search
- /* test1.c
-
- This is a test program for TWM, and provides an example of using twmClient
- in an application. It puts up a window named "Press any key"; if a key is
- pressed, the window is taken down, and is replaced by either a tiny window
- or by a gadget in TWM's window (if PostMe() returns TRUE). Clicking in
- the main part of the tiny window kills it, and brings the big window back.
- The same applies to the gadget in the TWM window, if it is being used
- instead: clicking on it kills it and bring up this program's big window.
- Clicking close in either the big or the small window exits the program.
-
- The compiled test1 program may be copied to test2, test3 etc, and all
- copies run simultaneously, to get a better idea of how TWM works.
-
- Under Aztec, put this program and twmClient.c in the current directory,
- and twm.h in the subdirectory "header", then compile and link thus:
-
- cc +Htwm.p header/twm.h
- cc +Itwm.p test1
- cc +Itwm.p twmClient
-
- ln test1.o twmClient.o -lc
- */
-
- #include "header/twm.h"
-
- #define HUGE_WINDOW_NAME ((UBYTE *)"Press any key")
-
- struct NewWindow wtiny =
- {
- 280, 120, 120, 20, /* left, top, width, height */
- 0, 1, /* detail pen, block pen */
- MOUSEBUTTONS /* IDCMP flags */
- | CLOSEWINDOW,
- WINDOWDRAG /* Window flags */
- | WINDOWCLOSE
- | WINDOWDEPTH
- | SMART_REFRESH
- | ACTIVATE,
- NULL, /* application gadget list */
- NULL, /* special checkmark imagery */
- NULL, /* window title */
- NULL, /* custom screen pointer */
- NULL, /* super bitmap pointer */
- 0, 0, 0, 0, /* min/max width and height */
- WBENCHSCREEN /* screen type */
- };
-
- struct NewWindow whuge =
- {
- 280, 120, 360, 60, /* left, top, width, height */
- 0, 1, /* detail pen, block pen */
- CLOSEWINDOW /* IDCMP flags */
- | RAWKEY,
- WINDOWDRAG /* Window flags */
- | WINDOWCLOSE
- | WINDOWDEPTH
- | SMART_REFRESH
- | ACTIVATE,
- NULL, /* application gadget list */
- NULL, /* special checkmark imagery */
- HUGE_WINDOW_NAME, /* window title */
- NULL, /* custom screen pointer */
- NULL, /* super bitmap pointer */
- 0, 0, 0, 0, /* min/max width and height */
- WBENCHSCREEN /* screen type */
- };
-
- extern VOID *OpenWindow(), *OpenLibrary(), *AllocMem(), *GetMsg();
-
- struct IntuitionBase *IntuitionBase = NULL;
-
- struct NewWindow *nw = NULL;
- struct Window *w = NULL;
- struct IntuiMessage *Imsg = NULL;
-
- main (argc, argv)
- int argc;
- char **argv;
- {
- int exitflag; /* true when close gadget has been pressed */
- int swapflag; /* true when it's time to switch between windows */
- int tinyflag; /* true when the tiny window is up */
- UWORD class; /* IDCMP message class */
- UWORD code; /* IDCMP message code */
- char filename[31];
- register int i, c;
-
- /* Use program name as title of tiny window */
- for (i = strlen(argv[0]) - 1;
- i > 0 && (c = argv[0][i - 1]) != ':' && c != '/';
- i--)
- ;
- wtiny.Title = (UBYTE *)(&argv[0][i]);
-
- if ((IntuitionBase = OpenLibrary("intuition.library", 33L)) == NULL)
- CloseStuff(E_OPEN_INTUI);
-
- /* Let twmClient do its allocations... we don't care this time, but it
- returns FALSE if the allocations fail
- */
- twmInit();
-
- /* initialize flags */
- tinyflag = swapflag = exitflag = FALSE;
-
- /* open the big window, and save pointer to its NewWindow struct */
- if ((w = OpenWindow(nw = &whuge)) == NULL)
- CloseStuff(E_OPEN_WINDOW);
-
- /* IDCMP loop */
- while (!exitflag)
- {
- Wait(1L << w->UserPort->mp_SigBit);
-
- while (Imsg = GetMsg(w->UserPort))
- {
- class = Imsg->Class;
- code = Imsg->Code;
- ReplyMsg(Imsg);
-
- if (class == CLOSEWINDOW)
- exitflag = TRUE;
-
- /* swap if tiny window clicked, or key pressed in big window */
- else if ((class == MOUSEBUTTONS && code == SELECTUP && tinyflag)
- || class == RAWKEY)
- swapflag = TRUE;
- }
-
- if (swapflag)
- {
- swapflag = FALSE;
-
- /* remember where this window is now stationed, and close it */
- SavePosCloseW(nw, w);
-
- /* if tiny window is now up, or PostMe() fails, open other window */
- if (tinyflag || !PostMe(wtiny.Title))
- {
- nw = tinyflag ? &whuge : &wtiny;
- tinyflag = !tinyflag;
- }
-
- if ((w = OpenWindow(nw)) == NULL)
- CloseStuff(E_OPEN_WINDOW);
- }
- }
-
- CloseStuff(E_OK);
- }
-
- /* CloseStuff
- *
- * Call twmCleanUp() to deallocate messages and port we've been using,
- * then close our own stuff and exit.
- */
-
- CloseStuff (error)
- int error; /* errors start at 500 (see twm.h) except for E_OK = 0 */
- {
- twmCleanUp(); /* twmClient deallocations */
-
- if (w) CloseWindow(w);
- if (IntuitionBase) CloseLibrary(IntuitionBase);
-
- exit(error);
- }
-
- /* SavePosCloseW
- *
- * Save the current window position and size in the NewWindow structure for
- * that window, then close the window.
- */
-
- static SavePosCloseW (nw, w)
- register struct NewWindow *nw;
- register struct Window *w;
- {
- nw->LeftEdge = w->LeftEdge;
- nw->TopEdge = w->TopEdge;
- nw->Width = w->Width;
- nw->Height = w->Height;
-
- CloseWindow(w);
- }
-