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

#include <intuition/intuition.h>

#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>

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

#define RXLONG(x) *((LONG *) x)

struct ARexxCommand comm1 =  { "QUIT", NULL, RXID_QUIT };
struct ARexxCommand comm2 =  { "ABOUT", NULL, RXID_INFO };
struct ARexxCommand comm3 =  { "TESTIT", NULL, RXID_TEST };
struct ARexxCommand comm4 =  { "CREATEBARWINDOW", "TITLE,BUTTONTEXT,SCREEN,CLOSEGADGET/S", RXID_CREATEWIN };
struct ARexxCommand comm5 =  { "DELETEBARWINDOW", "WINHANDLE/N/A", RXID_DELETEWIN };
struct ARexxCommand comm6 =  { "OPENBARWINDOW", "WINHANDLE/N/A,TITLE", RXID_OPENWIN };
struct ARexxCommand comm7 =  { "CLOSEBARWINDOW", "WINHANDLE/N/A", RXID_CLOSEWIN };
struct ARexxCommand comm8 =  { "ADDBAR", "WINHANDLE/N/A,MAXVALUE/N,CURRENTVALUE/N,NAME/F", RXID_ADDBAR };
struct ARexxCommand comm9 =  { "REMOVEBAR", "WINHANDLE/N/A,BARHANDLE/N/A", RXID_REMBAR };
struct ARexxCommand comm10 = { "CLOSEALLBARWINDOWS", NULL, RXID_CLOSEALL };
struct ARexxCommand comm11 = { "SETCURRENT", "WINHANDLE/N/A,BARHANDLE/N/A,VALUE/N/A", RXID_SETCURVAL };
struct ARexxCommand comm12 = { "SETMAXIMUM", "WINHANDLE/N/A,BARHANDLE/N/A,VALUE/N/A", RXID_SETMAXVAL };
struct ARexxCommand comm13 = { "SETFILLPEN", "WINHANDLE/N/A,BARHANDLE/N/A,FILLPEN/N/A", RXID_SETFILLPEN };
struct ARexxCommand comm14 = { "SETBACKPEN", "WINHANDLE/N/A,BARHANDLE/N/A,BACKPEN/N/A", RXID_SETBACKPEN };
struct ARexxCommand comm15 = { "SETPROGRESSMODE", "WINHANDLE/N/A,BARHANDLE/N/A,MODE/N/A", RXID_SETPROGRESSMODE };
struct ARexxCommand comm16 = { "SETBARWINDOWWIDTH", "WINHANDLE/N/A,WIDTH/N/A", RXID_SETWINWIDTH };
struct ARexxCommand comm17 = { "SETBARWINDOWPOS", "WINHANDLE/N/A,X/N,Y/N", RXID_SETWINPOS };
struct ARexxCommand comm18 = { "TESTOKBUTTON", "WINHANDLE/N/A", RXID_TESTOKBUTTON };
struct ARexxCommand comm19 = { "DELAY", "MILLISECS/N", RXID_DELAY };

struct ARexxCommand *FreshBarARexxCommandList[] =
{
  &comm1,  &comm2,  &comm3,  &comm4,  &comm5,  &comm6,  &comm7,  &comm8,
  &comm9,  &comm10, &comm11, &comm12, &comm13, &comm14, &comm15, &comm16,
  &comm17, &comm18, &comm19, 0
};

char *glob_sErrString = "error";

extern CatalogC *glob_poCatalog;
extern BarWindowListC *glob_poBWList;

BOOL FreshBarARexxPortC::RX_Quit(LONG *args)
/*S*/
{
  TRACE("Entry");
BOOL done=TRUE;
return done;
}
/*E*/
void FreshBarARexxPortC::RX_Info(LONG *args)
/*S*/
{
  static struct EasyStruct about =
  {
    sizeof(struct EasyStruct),
    0,
    0,  // titel
    0,  // text
    0   // gadgets
  };

  TRACE("Entry");
  about.es_Title = glob_poCatalog->getString(T_INFOTITLE);
  about.es_TextFormat = glob_poCatalog->getString(T_INFOTEXT);
  about.es_GadgetFormat = glob_poCatalog->getString(T_INFOWEITER);

  EasyRequestArgs(NULL, &about, NULL, NULL);
}
/*E*/
void FreshBarARexxPortC::RX_Test(LONG *args)
/*S*/
{
  TRACE("Entry");
  BarWindowListC oBWList;
  BarWindowNodeC *poBWNode = oBWList.addBarWindow("Hallo!", "_Weiter", NULL, TRUE);

  poBWNode->openBarWindow();
  Delay(50);

  BarNodeC *poBarNode  = poBWNode->addBar("Copy: ", 2000);
  poBarNode->setCurrentVal(42*4);

  Delay(50);

  BarNodeC *poBarNode2 = poBWNode->addBar("Kuckuck!!!: ", 2000);
  Delay(20);
  poBarNode2->setCurrentVal(100);

//  poBWNode->addBar("Test: ", 100)->setCurrentVal(25);
//  poBWNode->addBar("A: ", 100)->setCurrentVal(75);
//  poBWNode->addBar("Das ist ein Test: ", 100)->setCurrentVal(100);

  poBWNode->setBarWindowWidth(800);
  Delay(30);

  for (ULONG i=2050; i>0; i--)
  {
    poBarNode->setCurrentVal(i);
    poBarNode2->setCurrentVal(2050-i);
    poBWNode->refreshBars();
  } // for
  //Delay(150);

  Delay(20);
  poBWNode->removeBar(poBarNode->getBarID());
  Delay(20);
  poBWNode->removeBar(poBarNode2->getBarID());
  Delay(20);
}
/*E*/
StringC FreshBarARexxPortC::RX_CreateWin(LONG *args)
/*S*/
{
  TRACE("Entry");

  // titel ermitteln
  char *sTitle = NULL;
  if (args[0])
  {
    sTitle = (char *) args[0];
  } // if

  // Buttontext ermitteln
  char *sButtonText = "";
  if (args[1])
  {
    sButtonText = (char *) args[1];
  } // if

  // Screenname ermitteln
  char *sScreen = NULL;
  if (args[2])
  {
    sScreen = (char *) args[2];
  } // if

  BOOL bCloseGad = args[3] ? TRUE : FALSE;

  BarWindowNodeC *poBWNode = glob_poBWList->addBarWindow(sTitle, sButtonText, sScreen, bCloseGad);
  if (poBWNode)
  {
    StringC oTemp(15);
    oTemp.formatString("%d", poBWNode->getBarWindowID());
    return oTemp;
  } // if
return glob_sErrString;
}
/*E*/
void FreshBarARexxPortC::RX_DeleteWin(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0])
  {
    return;
  } // if

  glob_poBWList->removeBarWindow(RXLONG(args[0]));
}
/*E*/
void FreshBarARexxPortC::RX_OpenWin(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0])
  {
    return;
  } // if

  BarWindowNodeC *poNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poNode)
  {
    if (args[1])
    {
      poNode->setBarWindowTitle((char *) args[1]);
    } // if
    poNode->openBarWindow();
  } // if
}
/*E*/
void FreshBarARexxPortC::RX_CloseWin(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0])
  {
    return;
  } // if

  BarWindowNodeC *poNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poNode)
  {
    poNode->closeBarWindow();
  } // if
}
/*E*/
StringC FreshBarARexxPortC::RX_AddBar(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0])
  {
    return glob_sErrString;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    // bestimme den Namen des Balkens
    char *sBarName = "";
    if (args[3])
    {
      sBarName = (char *) args[3];
    } // if

    // bestimme den Maximalweert
    ULONG ulMaxValue = 100;
    if (args[1])
    {
      ulMaxValue = RXLONG(args[1]);
    } // if

    BarNodeC *poBarNode = poBarWinNode->addBar(sBarName, ulMaxValue);
    if (poBarNode)
    {
      // den Current-Value festlegen
      if (args[2])
      {
        poBarNode->setCurrentVal(RXLONG(args[2]));
        poBarWinNode->refreshBars();
      } // if

      StringC oTemp(15);
      oTemp.formatString("%d", poBarNode->getBarID());
      return oTemp;
    } // if
  } // if
return glob_sErrString;
}
/*E*/
void FreshBarARexxPortC::RX_RemoveBar(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    poBarWinNode->removeBar(RXLONG(args[1]));
  } // if
}
/*E*/
void FreshBarARexxPortC::RX_CloseAllWindows(LONG *args)
/*S*/
{
  TRACE("Entry");
  glob_poBWList->removeAll();
}
/*E*/
void FreshBarARexxPortC::RX_SetCurValue(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1] || !args[2])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    BarNodeC *poBarNode = poBarWinNode->findBarNode(RXLONG(args[1]));
    if (poBarNode)
    {
      // den Current-Value festlegen
      poBarNode->setCurrentVal(RXLONG(args[2]));
      poBarWinNode->refreshBars();
    } // if
  } // if
return;
}
/*E*/
void FreshBarARexxPortC::RX_SetMaxValue(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1] || !args[2])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    BarNodeC *poBarNode = poBarWinNode->findBarNode(RXLONG(args[1]));
    if (poBarNode)
    {
      // den Current-Value festlegen
      poBarNode->setMaxVal(RXLONG(args[2]));
      poBarWinNode->refreshBars();
    } // if
  } // if
return;
}
/*E*/
void FreshBarARexxPortC::RX_SetFillPen(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1] || !args[2])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    BarNodeC *poBarNode = poBarWinNode->findBarNode(RXLONG(args[1]));
    if (poBarNode)
    {
      // den Current-Value festlegen
      poBarNode->setFillPen(RXLONG(args[2]));
      poBarWinNode->refreshBars();
    } // if
  } // if
return;
}
/*E*/
void FreshBarARexxPortC::RX_SetBackPen(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1] || !args[2])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    BarNodeC *poBarNode = poBarWinNode->findBarNode(RXLONG(args[1]));
    if (poBarNode)
    {
      // den Current-Value festlegen
      poBarNode->setBackPen(RXLONG(args[2]));
      poBarWinNode->refreshBars();
    } // if
  } // if
return;
}
/*E*/
void FreshBarARexxPortC::RX_SetProgressMode(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1] || !args[2])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    BarNodeC *poBarNode = poBarWinNode->findBarNode(RXLONG(args[1]));
    if (poBarNode)
    {
      // den Current-Value festlegen
      poBarNode->setProgressMode((BarNodeC::ProgressMode) RXLONG(args[2]));
      poBarWinNode->refreshBars();
    } // if
  } // if
return;
}
/*E*/
void FreshBarARexxPortC::RX_SetWindowWidth(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0] || !args[1])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    poBarWinNode->setBarWindowWidth(RXLONG(args[1]));
  } // if
return;
}
/*E*/
void FreshBarARexxPortC::RX_SetWindowPos(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0])
  {
    return;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    int iPosX = -2;
    if (args[1])
    {
      iPosX = RXLONG(args[1]);
    } // if

    int iPosY = -2;
    if (args[2])
    {
      iPosY = RXLONG(args[2]);
    } // if

    poBarWinNode->setBarWindowPos(iPosX, iPosY);
  } // if
return;
}
/*E*/
StringC FreshBarARexxPortC::RX_TestOkButton(LONG *args)
/*S*/
{
  TRACE("Entry");
  if (!args[0])
  {
    return glob_sErrString;
  } // if

  BarWindowNodeC *poBarWinNode = glob_poBWList->findBarWindowNode(RXLONG(args[0]));

  if (poBarWinNode)
  {
    return poBarWinNode->testOKButton() ? "1" : "0";
  } // if
return glob_sErrString;
}
/*E*/
void FreshBarARexxPortC::RX_Delay(LONG *args)
/*S*/
{
  TRACE("Entry");
  int iDelay = 50;
  if (args[0])
  {
    iDelay = RXLONG(args[0]);
  } // if

  Delay(iDelay);
}
/*E*/

BOOL FreshBarARexxPortC::dispatchARexxCommand(ULONG arg_ulCmdID, LONG *arg_alArgArray, LONG &arg_rlRC, LONG &arg_rlRC2, StringC &roResult)
/*S*/
{
BOOL done = FALSE;
  TRACE("Entry");
  roResult.emptyString();
  arg_rlRC = NULL;
  arg_rlRC2 = NULL;

  switch (arg_ulCmdID)
  {
    case RXID_QUIT:
      done = RX_Quit(arg_alArgArray);
      break;
    case RXID_INFO:
      RX_Info(arg_alArgArray);
      break;
    case RXID_TEST:
      RX_Test(arg_alArgArray);
      break;
    case RXID_CREATEWIN:
      roResult = RX_CreateWin(arg_alArgArray);
      if (roResult == glob_sErrString)
      {               
        roResult.emptyString();
        arg_rlRC = 5;
      } // if
      break;
    case RXID_DELETEWIN:
      RX_DeleteWin(arg_alArgArray);
      break;
    case RXID_OPENWIN:
      RX_OpenWin(arg_alArgArray);
      break;
    case RXID_CLOSEWIN:
      RX_CloseWin(arg_alArgArray);
      break;
    case RXID_ADDBAR:
      roResult = RX_AddBar(arg_alArgArray);
      if (roResult == glob_sErrString)
      {
        roResult.emptyString();
        arg_rlRC = 5;
      } // if
      break;
    case RXID_REMBAR:
      RX_RemoveBar(arg_alArgArray);
      break;
    case RXID_CLOSEALL:
      RX_CloseAllWindows(arg_alArgArray);
      break;
    case RXID_SETCURVAL:
      RX_SetCurValue(arg_alArgArray);
      break;
    case RXID_SETMAXVAL:
      RX_SetMaxValue(arg_alArgArray);
      break;
    case RXID_SETFILLPEN:
      RX_SetFillPen(arg_alArgArray);
      break;
    case RXID_SETBACKPEN:
      RX_SetBackPen(arg_alArgArray);
      break;
    case RXID_SETPROGRESSMODE:
      RX_SetProgressMode(arg_alArgArray);
      break;
    case RXID_SETWINWIDTH:
      RX_SetWindowWidth(arg_alArgArray);
      break;
    case RXID_SETWINPOS:
      RX_SetWindowPos(arg_alArgArray);
      break;
    case RXID_TESTOKBUTTON:
      roResult = RX_TestOkButton(arg_alArgArray);
      if (roResult == glob_sErrString)
      {
        roResult.emptyString();
        arg_rlRC = 5;
      } // if
      break; 
    case RXID_DELAY:
      RX_Delay(arg_alArgArray);
      break;
    default:
      inherited::dispatchARexxCommand(arg_ulCmdID, arg_alArgArray, arg_rlRC, arg_rlRC2, roResult);
      break;
  } // switch
return done;
}
/*E*/

