/* ******************************************************************** */
/* C code generated by:							*/
/* Visual Arts Version 2.1						*/
/* Copyright 1994-95 Danny Y. Wong  All rights reserved			*/
/* Calgary, Alberta (CANADA)                        			*/
/* Partial of the code is copyright by Jaba Development			*/
/* ******************************************************************** */

#include <VisualArts.h>
#include <clib/VisualArts_protos.h>

#define ID_ObjID1                         			0

#define AppMenuNumGads						1

#include "AppMenu_func.c" 

int ObjID1Obj(struct VAobject VAObject);

int GetPubScreen(void);
void ClosePubScreen(void);
int OpenAppMenuWindow(char windtitle[]);
void CloseAppMenuWindow(void);
int AppMenuHandler(void);
int AppMenuMainHandler(void);
extern int AppMenuAppWindHandler(struct MsgPort *appwindport);
void DrawAppMenuObjs(void);
int main(int argc, char *argv[]);

UBYTE				*PubScrName = "Workbench";
struct DrawInfo			*ScrDrawInfo = NULL;
APTR				 VisualInfo = NULL;
struct Screen			*Scr = NULL;
struct Window			*AppMenuWnd = NULL;
struct AppWindow		*AppMenuAppWind = NULL;
struct MsgPort			*AppMenuAppWindPort = NULL;
struct Gadget			*AppMenuGList = NULL;
struct Gadget			*AppMenuGadgets[AppMenuNumGads];
struct IntuiMessage		 AppMenuMsg;
UWORD				 AppMenuLeft = 157;
UWORD				 AppMenuTop = 53;
UWORD				 AppMenuWidth = 224;
UWORD				 AppMenuHeight = 50;
struct TextAttr topaz8 = { (STRPTR)"topaz.font", 8, 0x00, 0x01 };
struct TextAttr topaz800 = { (STRPTR)"topaz.font", 8, 0x00, 0x00 };

struct IntuiText AppMenuIText[] = {
  1, 0, JAM2, 25, 3, &topaz800, (UBYTE *)"Drag icons into window.", NULL,
};

WORD AppMenuGadTypes[] = {
	BUTTON_KIND,
};

struct NewGadget AppMenuNGads[] = {
	59, 15, 90, 15, (UBYTE *)"Quit",&topaz800, ID_ObjID1, PLACETEXT_IN, NULL, (APTR)ObjID1Obj,
};

ULONG AppMenuNTags[] = {
	TAG_DONE,
};


int GetPubScreen(void)
{
  if (!(Scr = LockPubScreen(PubScrName)))
	return(1L);

  if (!(VisualInfo = GetVisualInfo(Scr, TAG_DONE)))
	return(2L);

  if (!(ScrDrawInfo = GetScreenDrawInfo(Scr)))
	return(3L);
  return(0L);
}

void ClosePubScreen(void)
{
  if (VisualInfo)
    FreeVisualInfo(VisualInfo);
  if (Scr)
    UnlockPubScreen(NULL,  Scr);
  if (ScrDrawInfo)
    FreeScreenDrawInfo(Scr,  ScrDrawInfo);
}

int OpenAppMenuWindow(char windtitle[80])
{
  struct NewGadget	NewGad;
  struct Gadget   	*Gad;
  register UWORD i, j;
  UWORD offsetx = Scr->WBorLeft;
  UWORD offsety = Scr->WBorTop + Scr->Font->ta_YSize + 1;

  if (!(Gad = CreateContext(&AppMenuGList)))
  	return(1L);

  for (i=0, j=0; i < AppMenuNumGads; i++)
  {
    CopyMem((char *)&AppMenuNGads[i], (char *)&NewGad, (long)sizeof(struct NewGadget));

    NewGad.ng_VisualInfo = VisualInfo;
    NewGad.ng_LeftEdge += offsetx;
    NewGad.ng_TopEdge  += offsety;

    AppMenuGadgets[i] = Gad = CreateGadgetA((ULONG)AppMenuGadTypes[i], Gad, &NewGad,
	(struct TagItem *)&AppMenuNTags[j]);
    while (AppMenuNTags[j])
        j +=2;
    j++;
    if (!Gad)
       return(2L);
  }
  if (!(AppMenuWnd = OpenWindowTags(NULL,
  	WA_Left,	AppMenuLeft,
  	WA_Top, 	AppMenuTop,
  	WA_Width,	AppMenuWidth,
  	WA_Height,	AppMenuHeight + kWindowOffSetY,
  	WA_NewLookMenus, TRUE,
  	WA_IDCMP,	IDCMP_CLOSEWINDOW | IDCMP_GADGETUP ,
  	WA_Flags,	WFLG_CLOSEGADGET  |  WFLG_SMART_REFRESH | 
			 WFLG_RMBTRAP |  WFLG_ACTIVATE | 
			 WFLG_DRAGBAR ,
  	WA_Gadgets,	AppMenuGList,
  	WA_Title,	windtitle,
  	WA_ScreenTitle,	"Visual Arts V2.2 Copyright 1994-95 Danny Y. Wong  All Rights Reserved.",
  	WA_PubScreen,	Scr,
  	WA_MinWidth,	160,
  	WA_MinHeight,	50,
  	WA_MaxWidth,	640,
  	WA_MaxHeight,	200,
  	TAG_DONE)))
  	    return(3L);

  if (AppMenuAppWindPort = CreateMsgPort())
  {
	AppMenuAppWind = AddAppWindow(1, 0, AppMenuWnd, AppMenuAppWindPort, NULL);
	if (AppMenuAppWind == NULL)
	{
	  CloseAppMenuWindow();
	  return(-1L);
	}
  }
  else
    return(-2L);
  DrawAppMenuObjs();
  GT_RefreshWindow(AppMenuWnd, NULL);
  RefreshGadgets(AppMenuGadgets[0], AppMenuWnd, NULL);
  return(0L);
}

void CloseAppMenuWindow(void)
{
  struct AppMessage *appwindmsg;

  if (AppMenuWnd)
	CloseWindow(AppMenuWnd);
  if (AppMenuGList)
	FreeGadgets(AppMenuGList);
  if (AppMenuAppWind)
  {
    RemoveAppWindow(AppMenuAppWind);
    while (appwindmsg = (struct AppMessage *)GetMsg(AppMenuAppWindPort))
		ReplyMsg((struct Message *)appwindmsg);
    DeleteMsgPort(AppMenuAppWindPort);
  }
}

int AppMenuHandler(void)
{
  struct IntuiMessage	*msg;
  struct VAobject	VAObject;
  int running	= 1;
  int (*func)(struct VAobject VAObject);
  ULONG class;
  UWORD code;

  while (msg=GT_GetIMsg(AppMenuWnd->UserPort))
  {
    CopyMem((char *)msg, (char *)&AppMenuMsg, (long)sizeof(struct IntuiMessage));
    class = msg->Class;
    code  = msg->Code;

    VAObject.va_Window = (struct Window *)AppMenuWnd;
    VAObject.va_Gadget = (struct Gadget *)msg->IAddress;
    VAObject.va_IntuiMsg = (struct IntuiMessage *)msg;
    VAObject.va_Flags = 0;
    VAObject.va_UserData = 0;

    GT_ReplyIMsg(msg);
    switch(class)
    {

	  case IDCMP_CLOSEWINDOW:
	     return(0);
	     break;

	  case IDCMP_GADGETUP:
	    func = (void *)((struct Gadget *)AppMenuMsg.IAddress)->UserData;
	    if (func != NULL)
	      running =  func(VAObject);
	    break;

    }
  }
  return(running);
}

void DrawAppMenuObjs(void)
{
  PrintIText(AppMenuWnd->RPort, AppMenuIText, AppMenuWnd->BorderLeft, AppMenuWnd->BorderTop);

}

int AppMenuAppWindHandler(struct MsgPort *appwindport)
{
  struct WBArg	*wbargptr;
  struct AppMessage	*appwindmsg;
  int i;

  while (appwindmsg = (struct AppMessage *)GetMsg(appwindport))
  {
	wbargptr = appwindmsg->am_ArgList;

	/*  examine the icons dragged into the window here.   */ 
	/*  wbargptr->wa_Name ---> name of the icon           */ 
	/*  wbargptr->wa_Lock ---> directory this icon is in. */ 

	for (i=0; i < appwindmsg->am_NumArgs; i++)
	{
           printf("Icon name %s\n", wbargptr->wa_Name);
	   wbargptr++;
	}
	ReplyMsg((struct Message *)appwindmsg);
  }
  return(0);
}

int AppMenuMainHandler(void)
{
  int running = 1;
  ULONG windsig, signals;

  ULONG appwindsig;

  appwindsig = 1L << AppMenuAppWindPort->mp_SigBit;
  windsig = 1L << AppMenuWnd->UserPort->mp_SigBit;

  while (running == 1)
  {
    signals = Wait( windsig | appwindsig );
    if (signals & windsig)
    {
	  running = AppMenuHandler();
    }
    else
    if (signals & appwindsig)
	  AppMenuAppWindHandler(AppMenuAppWindPort);
  }
  return(running);
}

int main(int argc, char *argv[])
{
  int rc;

  if (!(GetPubScreen()))
  {
	if (!(OpenAppMenuWindow("AppMenu Demo")))
	{
	  rc = AppMenuMainHandler();
	  CloseAppMenuWindow();
	}
	ClosePubScreen();
  }
  return(0L);
}
