//##ex mcpp:cppc -d __IGNORE_NOT_SUPPORTED__ -gs -o WinPubScreen p:pLib/StartCode.o -pc WinPubScreen.c p:pLib/StdIO.o -l pOSxA -l pOSStub -l pOS -l List

/*\
*** 31.01.1997, Michael Christoph, proDAD
*** Example:
*** Tag WA_SmartRefresh
***     wird von pOS noch nicht unterstützt
\*/


/*
** winpubscreen.c
** open a window on the default public screen (usually the Workbench screen)
*/

#define INTUI_V36_NAMES_ONLY
#include <exec/types.h>
#include <intuition/intuition.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>

BOOL pOS_CmpDoubleClick(_R_A0 const struct pOS_DoubleKlick* just,_R_A1 const struct pOS_DoubleKlick*)
{return(FALSE);}

#ifdef LATTICE
int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
int chkabort(void) { return(0); }  /* really */
#endif

UBYTE *vers = "$VER: WinPubScreen 1.0 (31.01.1997) (proDAD, Michael Christoph)";

#ifdef __pOS__
struct pOS_IntuiDevice  *gb_IntuiBase;
struct pOS_GfxBase      *gb_GfxBase;      /*** wird zusätzlich benötigt ***/
struct pOS_UtilityBase  *gb_UtilityBase;  /*** wird zusätzlich benötigt ***/
#else
struct Library *IntuitionBase;
#endif

/* our function prototypes */
VOID handle_window_events(struct Window *win);


/*
** Open a simple window on the default public screen,
** then leave it open until the user selects the close gadget.
*/
VOID main(int argc, char **argv)
{
  struct Window *test_window = NULL;
  struct Screen *test_screen = NULL;

#ifdef __pOS__
  if(gb_UtilityBase=(struct pOS_UtilityBase *) pOS_OpenLibrary("pUtility.library",0))
  {
  if(gb_GfxBase=(struct pOS_GfxBase *) pOS_OpenLibrary("pGraphics.library",0))
  {
  if(gb_IntuiBase=(struct pOS_IntuiDevice *) pOS_OpenLibrary("pIntui.library",0))
#else
  if(IntuitionBase = OpenLibrary("intuition.library",37))
#endif
  {
      /* get a lock on the default public screen */
      if (test_screen = LockPubScreen(NULL))
      {
          /* open the window on the public screen */
          test_window = OpenWindowTags(NULL,
                    WA_Left,  10,    WA_Top,    20,
                    WA_Width, 300,   WA_Height, 100,
                    WA_DragBar,         TRUE,
                    WA_CloseGadget,     TRUE,
                    WA_SmartRefresh,    TRUE,
                    WA_NoCareRefresh,   TRUE,
                    WA_IDCMP,           IDCMP_CLOSEWINDOW,
                    WA_Title,           "Window Title",
                    WA_PubScreen,       test_screen,
                    TAG_END);

          /* Unlock the screen.  The window now acts as a lock on
          ** the screen, and we do not need the screen after the
          ** window has been closed.
          */
          UnlockPubScreen(NULL, test_screen);

          /* if we have a valid window open, run the rest of the
          ** program, then clean up when done.
          */
          if (test_window)
          {
              handle_window_events(test_window);
              CloseWindow(test_window);
          }
      }

#ifdef __pOS__
    pOS_CloseLibrary((struct pOS_Library*)gb_IntuiBase);
#else
    CloseLibrary(IntuitionBase);
#endif
  }
#ifdef __pOS__
  pOS_CloseLibrary((struct pOS_Library*)gb_GfxBase);
  }
  pOS_CloseLibrary((struct pOS_Library*)gb_UtilityBase);
  }
#endif
}

/*
** Wait for the user to select the close gadget.
*/
VOID handle_window_events(struct Window *win)
{
  struct IntuiMessage *msg;
  BOOL done = FALSE;

  while (! done)
  {
    /* We have no other ports of signals to wait on,
    ** so we'll just use WaitPort() instead of Wait()
    */
    WaitPort(win->UserPort);

    while ( (! done) &&
            (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
    {
        /* use a switch statement if looking for multiple event types */
        if (msg->Class == IDCMP_CLOSEWINDOW)
            done = TRUE;

        ReplyMsg((struct Message *)msg);
    }
  }
}
