/* WinInfo.c by Fredrik Söderberg
 * ///S and ///E means fold, S for Start and E for End (GoldEd)
 */

///S includes
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/IntuitionBase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <stdio.h>
#include <stdlib.h>
///E

///S Global
struct IntuitionBase *IntuitionBase;
//struct Library *DosBase;
struct Screen *screen;
struct Window *hidewin;
///E

///S prototypes
void getwinstat(struct Window *);
void error(char *);
void eventloop(void);
struct Window *createwin(struct Screen *);
int main(void);
///E


///S getwinstat(win)
void getwinstat(struct Window *win)
{
   Printf("ScreenTitle: %s\n", win->ScreenTitle);
   Printf("Title      : %s\n", win->Title);
   Printf("Width      : %ld\n", win->Width);
   Printf("Height     : %ld\n", win->Height);
   Printf("TopEdge    : %ld\n", win->TopEdge);
   Printf("LeftEdge   : %ld\n", win->LeftEdge);
}
///E

///S main(void)
int main(void)
{
   if(!(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37L)))
      error("Could not open intuition.library V37");
//   if(!(DosBase = OpenLibrary("dos.library", 37L)))
//      error("Could not open dos.library V37");
   if(!(screen = LockPubScreen(NULL)))
      error("Could not LockPubScreen");
   if(!(hidewin = createwin(screen)))
      error("Could not open window");

   eventloop();
   error("");  // No error, just exit.

return 0;

}     
///E

///S eventloop(void)
void eventloop(void)
{
   struct Window *infowin;
   struct IntuiMessage *intmess;
   ULONG class;

   WaitPort(hidewin->UserPort);
   while(intmess = (struct IntuiMessage *)GetMsg(hidewin->UserPort))
   {
      class = intmess->Class;
      ReplyMsg((struct Message *)intmess);
      switch(class)
      {
         case IDCMP_INACTIVEWINDOW:
            {
               infowin = IntuitionBase->ActiveWindow;
               getwinstat(infowin);
            }
            break;
      }
   }
}
///E

///S win = createwin(screen)
struct Window *createwin(struct Screen *crscreen)
{
   struct Window *crwin;

   if(!(crwin = OpenWindowTags(NULL,
      WA_Width, 1,
      WA_Height, 1,
      WA_Left, 0,
      WA_DragBar, FALSE,
      WA_DepthGadget, FALSE,
      WA_CloseGadget, FALSE,
      WA_SizeGadget, FALSE,
      WA_Activate, TRUE ,
      WA_IDCMP, IDCMP_INACTIVEWINDOW,
      WA_PubScreen, crscreen,
      TAG_END)))
   {
      Printf("Could not open window");
      return(FALSE);
   }
   return(crwin);
}
///E

///S error(string)
void error(char *errstr)
{
   Printf("%s\n",errstr);

   if(hidewin) CloseWindow(hidewin);
   if(screen) UnlockPubScreen(NULL, screen);
   if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);

   exit(0);
}
///E

