/***********************************************************************
 *                                                                     *
 *                            COPYRIGHTS                               *
 *                                                                     *
 *   Copyright (c) 1990  Commodore-Amiga, Inc.  All Rights Reserved.   *
 *                                                                     *
 **********************************************************************/

/*
 * Code to test AppWindow feature of Workbench
 */

#include <intuition/intuition.h>
#include <exec/memory.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#include <stdio.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/icon_protos.h>
#include <clib/wb_protos.h>
#include <clib/dos_protos.h>

struct Library *IntuitionBase;
struct Library *WorkbenchBase;
struct Library *IconBase;

void            main(void);

void 
main(void)
{
  struct MsgPort *msgport;
  struct Window  *win;
  struct AppWindow *aw;
  struct IntuiMessage *imsg;
  struct AppMessage *amsg;
  struct WBArg   *argptr;

  ULONG           id = 1, userdata = 0;
  BOOL            done = FALSE;
  int             i;

  printf("aw: enter\n");

  if (IntuitionBase = OpenLibrary("intuition.library", 36)) {
    if (WorkbenchBase = OpenLibrary("workbench.library", 36)) {
      if (IconBase = OpenLibrary("icon.library", 36)) {
	if (msgport = CreateMsgPort()) {
	  if (win = OpenWindowTags(NULL,
				   WA_Left, 0,
				   WA_Top, 1,
				   WA_Width, 160,
				   WA_Height, 50,
				   WA_IDCMP, CLOSEWINDOW,
				   WA_Flags, WINDOWCLOSE | WINDOWDRAG,
				   WA_Title, "AppWindow",
				   TAG_END)) {
            printf("aw: calling AddAppWindow...\n");
            if (aw = AddAppWindow(id, userdata, win, msgport, NULL)) {
                  printf("aw: ok, aw = %lx, going to sleep\n", aw);
		  do {
		    Wait(1 << win->UserPort->mp_SigBit | 1 << msgport->mp_SigBit);
		    while (imsg = (struct IntuiMessage *) GetMsg(win->UserPort)) {
		      if (imsg->Class = CLOSEWINDOW)
			done = TRUE;
		      ReplyMsg((struct Message *) imsg);
		    }
		    while (amsg = (struct AppMessage *) GetMsg(msgport)) {
		      printf("aw: appmsg=%lx, Type=%ld, ID=%ld, UserData=%ld, NumArgs=%ld\n", amsg, amsg->am_Type, amsg->am_ID, amsg->am_UserData, amsg->am_NumArgs);
		      argptr = amsg->am_ArgList;
		      for (i = 0; i < amsg->am_NumArgs; i++) {
			printf("\targ(%ld): Name='%s', Lock=%lx\n", i, argptr->wa_Name, argptr->wa_Lock);
			argptr++;
		      }
		      ReplyMsg((struct Message *) amsg);
		    }
		  } while (!done);
                printf("calling RemoveAppWindow\n");
                RemoveAppWindow(aw);
            } else /* !aw */
                printf("Couldn't AddAppWindow\n");
	    CloseWindow(win);
	  } else		/* !win */
	    printf("Couldn't open window\n");
          /* Make sure there are no more outstanding messages */
          while(amsg = (struct AppMessage *)GetMsg(msgport))
            ReplyMsg((struct Message *)amsg);
	  DeleteMsgPort(msgport);
	} else			/* !msgport */
	  printf("Coulnd't create messageport\n");
	CloseLibrary(IconBase);
      } else			/* !IconBase */
	printf("Couldn't open icon.library\n");
      CloseLibrary(WorkbenchBase);
    } else			/* !WorkbenchBase */
      printf("Couldn't open workbench.library\n");
    CloseLibrary(IntuitionBase);
  } else			/* !IntuitionBase */
    printf("Couldn't open intuition.library\n");
  printf("aw: done\n");
}
