#include <utility/tagitem.h>
#include <exec/execbase.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/exec.h>
#include <intuition/gadgetclass.h>
#include <intuition/screens.h>
struct ExecBase *ExecBase;
struct IntuitionBase *IntuitionBase = NULL;
struct GadToolsBase *GadToolsBase = NULL;
struct Window *window1 = NULL;
struct Screen *WBScreen = NULL;
APTR vi = NULL;
BOOL activated = TRUE;
struct Gadget *currgad,*g0,*g1;
struct Gadget *gadlist = NULL;
#define REFRESHBUTTON 2
#define NODEGAD_ID 1

void Bummer(char *errmsg)
{
    puts(errmsg);
    CleanExit(5);
}
int CleanExit(returnvalue)
int returnvalue;
{
    if (gadlist)
        FreeGadgets(gadlist);
    if (vi)
        FreeVisualInfo(vi);
    if (window1) CloseWindow (window1);
    if (WBScreen != NULL)
        UnlockPubScreen (NULL, WBScreen);
    if (GadToolsBase)
        CloseLibrary ((struct Library *)GadToolsBase);
    if (IntuitionBase)
        CloseLibrary ((struct Library *)IntuitionBase);
    if (ExecBase)
        CloseLibrary ((struct Library *)ExecBase);
    exit (returnvalue);
    return (returnvalue);
}

void handleIDCMP(struct Window *win)
{
    struct IntuiMessage *message = NULL;
    struct Gadget *gad = NULL;
    ULONG class, signals, signalmask;
    WORD code;

    signalmask = 1L << win->UserPort->mp_SigBit;
    while (1)
    {
        signals = Wait(signalmask);
        if (signals & signalmask)
            while (message = (struct IntuiMessage *)GT_GetIMsg(win->UserPort))
            {
                class = message->Class;
                code = (WORD)message->Code;
                gad = (struct Gadget *) message->IAddress;
                GT_ReplyIMsg(message);
/* 								printf("Gadget: %d,  Code: %d\n",gad->GadgetID,code); */
                switch(class)
                {
                    case CLOSEWINDOW:
                       CleanExit(0);
                    case GADGETUP:
                      if (gad->GadgetID = REFRESHBUTTON)
                    	  GT_SetGadgetAttrs(g0, window1, NULL,GTLV_Labels,&(ExecBase->PortList), TAG_DONE);
				    				default:
                       break;
                }
            }
    }
}

struct TagItem WindowTags[] = {
    {WA_Title, (ULONG)" Port List "},
    {WA_Flags, (ULONG)SMART_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|ACTIVATE},
    {WA_GimmeZeroZero, TRUE},
    {WA_AutoAdjust, TRUE},
    {WA_InnerHeight, 145L},
    {WA_InnerWidth, 196L},
    {WA_IDCMP, (ULONG)CLOSEWINDOW|SLIDERIDCMP},
    {TAG_DONE, }
};

struct NewGadget ng = {
    13, 15,
    165, 95,
    "G0",
    NULL,
    1,
    PLACETEXT_BELOW,
    NULL,
    NULL
};


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

    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 36);
    if (IntuitionBase == NULL)
        CleanExit(30);

    ExecBase = (struct ExecBase *) OpenLibrary ("exec.library", 36);
    if (ExecBase == NULL)
        CleanExit(30);

    GadToolsBase = (struct GadToolsBase *) OpenLibrary ("gadtools.library", 36);
    if (GadToolsBase == NULL)
        CleanExit(30);
    WBScreen = LockPubScreen(NULL);
    if (WBScreen == NULL)
        CleanExit(30);

    vi = GetVisualInfo (WBScreen, TAG_DONE);
    if (vi == NULL)
        CleanExit(30);

    ng.ng_TextAttr = WBScreen->Font;
    ng.ng_VisualInfo = vi;
    currgad = CreateContext (&gadlist);
		ng.ng_GadgetText="";
		ng.ng_Flags= PLACETEXT_IN;
		ng.ng_GadgetID=NODEGAD_ID;      /* note the Bevy of ready-to-use execbase lists */
    g0 = CreateGadget(LISTVIEW_KIND, currgad, &ng,
/*                     GTLV_Labels,&(ExecBase->MemList), */
/*                     GTLV_Labels,&(ExecBase->ResourceList), */
/*                     GTLV_Labels,&(ExecBase->DeviceList), */
/*                     GTLV_Labels,&(ExecBase->IntrList), */
/*                     GTLV_Labels,&(ExecBase->LibList), */
                    GTLV_Labels,&(ExecBase->PortList),
/*                     GTLV_Labels,&(ExecBase->TaskReady), */
/*                     GTLV_Labels,&(ExecBase->TaskWait), */
/*                     GTLV_ShowSelected,NULL, */
/*                     GTLV_Active, 1, */
										TAG_DONE);
    if (!g0)
        Bummer("ListView Gad failed");
		ng.ng_TopEdge= 115L;
		ng.ng_Height= (WBScreen->Font->ta_YSize *4)/3;
    ng.ng_Flags= PLACETEXT_IN;
		ng.ng_GadgetText=" REFRESH ";
		ng.ng_GadgetID=REFRESHBUTTON;
    g1 = CreateGadget(BUTTON_KIND, g0, &ng, TAG_DONE);
    if (!g1)
        Bummer("Button Gad failed");
    window1 = OpenWindowTagList (NULL, WindowTags);
    AddGList (window1, gadlist, -1, -1, NULL);
    RefreshGList(window1->FirstGadget, window1, NULL, (UWORD)-1);
    GT_RefreshWindow (window1, NULL);
    handleIDCMP (window1);
}
