//****************************************************************//
// Filename:    FreshBar.cpp
// Autor:       Christian Taulien of Strange Intelligence
// Purpose:     Testprogram for the SIFC
// Creation:    10. Mai 1998
//****************************************************************//

#include <iostream.h>
#include <string.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <rexx/storage.h>
#include <intuition/intuition.h>
#include <libraries/gadtools.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>
#include <clib/rexxsyslib_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/intuition_protos.h>

#include "global.h"
#include "VisualInfo.h"
#include "FreshLocale.h"
#include "internalcatalog.h"
#include "FreshBar_ARexxPort.h"
#include "WindowList.h"

/*S*/ // Variablendefs
struct Library *DosBase;
struct Library *IntuitionBase;
struct Library *GfxBase;
struct Library *RexxBase;
struct Library *GadToolsBase;
struct Library *LocaleBase;
struct Window *mainwindow=NULL;
CatalogC *glob_poCatalog = NULL;
FreshBarARexxPortC *glob_poARexxPort = NULL;
BarWindowListC *glob_poBWList = NULL;
/*E*/
/*S*/ // WindowStuff
// *********************** Start der Window UP's ***************************
void RemoveMsgFromMainWindow(void)
/*S*/
{
struct IntuiMessage *imsg;
  TRACE("Entry");
  if (mainwindow)
  {
    while (imsg=GT_GetIMsg(mainwindow->UserPort))
    {
      GT_ReplyIMsg(imsg);
    } // while
  } // if
}
/*E*/
void CloseMainWindow(void)
/*S*/
{
  TRACE("Entry");
  RemoveMsgFromMainWindow();
  if (mainwindow)
  {
    CloseWindow(mainwindow);
    mainwindow = NULL;
  } // if
}
/*E*/
BOOL MainWindowHandler(struct IntuiMessage *imsg)
/*S*/
// Behandelt Ereignisse des Control Panels
{
LONG myevent=imsg->Class;
APTR myiaddress=imsg->IAddress;
WORD mycode=imsg->Code;
BOOL done = FALSE;
// ULONG myqualifier=imsg->Qualifier;
    TRACE("Entry");
    GT_ReplyIMsg(imsg);
    switch (myevent)
    {
      case IDCMP_CLOSEWINDOW:
        done = TRUE;
        break;
    } // switch

    // entprellen
    if (mainwindow)
    {
      while (imsg=GT_GetIMsg(mainwindow->UserPort))
      {
        GT_ReplyIMsg(imsg);
      } // if
    } // if
return done;
}
/*E*/
// *********************** Ende der Window UP's ****************************
/*E*/
/*S*/ // Library Stuff
// ********************** START der Libs UP's *************************
void CloseLibs(void)
/*S*/
// Schließen aller geöffneten Libs
{
  TRACE("Entry");
  if (GfxBase)
  {
    CloseLibrary(GfxBase);
    GfxBase=NULL;
  } // if

  if (IntuitionBase)
  {
    CloseLibrary(IntuitionBase);
    IntuitionBase=NULL;
  } // if

  if (DosBase)
  {
    CloseLibrary(DosBase);
    DosBase=NULL;
  } // if

  if (RexxBase)
  {
    CloseLibrary(RexxBase);
    RexxBase=NULL;
  } // if

  if (GadToolsBase)
  {
    CloseLibrary(GadToolsBase);
    GadToolsBase = NULL;
  } // if

  if (::LocaleBase)
  {
    CloseLibrary(::LocaleBase);
    ::LocaleBase = NULL;
  } // if
}
/*E*/
BOOL OpenLibs(void)
/*S*/
// Öffnen aller benötigten Libs
{
  TRACE("Entry");
  if (IntuitionBase=OpenLibrary("intuition.library",39L))
  {
    if (GfxBase=OpenLibrary("graphics.library",39L))
    {
      if (DosBase=OpenLibrary("dos.library",36L))
      {
        if (RexxBase=OpenLibrary(RXSNAME,NULL))
        {
          if (GadToolsBase=OpenLibrary("gadtools.library", 39L))
          {
            if (::LocaleBase=OpenLibrary("locale.library", 39L))
            {
              return TRUE;
            } else TRACE("Failed to open locale.library v39");
          } else TRACE("Failed to open gadtools.library v39");
        } else TRACE("Failed to open rexxsyslib.library");
      } else TRACE("Failed to open dos.library v36");
    } else TRACE("Failed to open graphics.library v39");
  } else TRACE("Failed to open intuition.library v39");
  CloseLibs();
return FALSE;
}
/*E*/
// ********************** ENDE der Libs UP's *************************
/*E*/
/*S*/ // Controller
void Controller(void)
/*S*/
// Dieses UP ermöglicht asynchrone Bedienung!!!
{
BOOL done=FALSE;
  TRACE("Entry");
  while (!done)
  {
    ULONG signalbits=NULL,erhaltene_signale;
    struct IntuiMessage *imsg;
    // Sammeln der SignalBits der einzelnen Fenster
    if (glob_poARexxPort)
    {
      signalbits=1L<<glob_poARexxPort->getMsgPort()->mp_SigBit;
    } // if rexxhost

    if (mainwindow)
    {
      signalbits|=1L<<mainwindow->UserPort->mp_SigBit;
    } // if mainwindow

    if (signalbits) // wenn überhaupt ein Bit gesammelt wurde
    {
      erhaltene_signale=Wait(signalbits);
      if (glob_poARexxPort)
      {
        if (erhaltene_signale & (1L<<glob_poARexxPort->getMsgPort()->mp_SigBit))
        {
          done=glob_poARexxPort->processARexxCommand();
        } // if
      }
      if (!done)
      {
        if (mainwindow)
        {
          if (((erhaltene_signale) & (1L<<mainwindow->UserPort->mp_SigBit)) && (imsg=GT_GetIMsg(mainwindow->UserPort)))
          {
            done=MainWindowHandler(imsg);
          } // if
        } // if
      }
      else
      {
        RemoveMsgFromMainWindow();
      } // if not done
    }
    else
    {
      done=TRUE;
    } // if signalbits are vorhanden
  }
  CloseMainWindow();
}
/*E*/
/*E*/

void main()
/*S*/
{
  TRACE("Entry");
  char glob_version[]="$VER: FreshBar 1.1 (98-08-01)\0";
  if (OpenLibs())
  {
    if (glob_poCatalog = new CatalogC("FreshBar.catalog", 1, NULL))
    {
      if (glob_poARexxPort = new FreshBarARexxPortC("FRESHBAR_REXX"))
      {
        if (glob_poBWList = new BarWindowListC)
        {
          Controller();
          delete glob_poBWList;
        } // if
        delete glob_poARexxPort;
      } // if
      delete glob_poCatalog;
    } // if
    CloseLibs();
  } // if
} // main()
/*E*/

