/* $Id: JOBMAN.CPP 2.4 1995/07/20 11:24:33 leon Exp $
 */

#define Uses_TEvent

#include <tvision/tv.h>


#define Uses_TInputDouble

#include "input.h"



#include "comm.h"

/** global communication channel **/
extern Comm *comm;          
#if !defined( __JOBMAN_H )

#include "jobman.h"

#endif


#include "gisel.h"

#include "config.h"


const double speedChange = 0.05;

struct jobManagerdataRec  FeedrateRatio = { 1.0, 1.0, 1.0};

JobManager::JobManager() :
       TDialog(TRect(33, 9, 78, 21), "Job Manager"),
       TWindowInit(JobManager::initFrame)

{
 TView *control;


 number = JobManagerWndNum;

 control = new TButton(TRect(5, 9, 16, 11), "~P~ause", cmPause, bfDefault);
 insert(control);

 control = new TButton(TRect(16, 9, 28, 11), "~C~ontinue", cmContinue, bfNormal);
 insert(control);

 control = new TButton(TRect(29, 9, 40, 11), "~D~equeue", cmDequeue, bfNormal);
 insert(control);

 control = new TButton(TRect(32, 6, 40, 8), "XYZ+", cmXYZplus, bfNormal);
 insert(control);

 control = new TButton(TRect(32, 4, 40, 6), "XYZ-", cmXYZminus, bfNormal);
 insert(control);

 control = new TButton(TRect(5, 6, 13, 8), "X+", cmXplus, bfNormal);
 insert(control);

 control = new TButton(TRect(5, 4, 13, 6), "X-", cmXminus, bfNormal);
 insert(control);

 control = new TButton(TRect(14, 6, 22, 8), "Y+", cmYplus, bfNormal);
 insert(control);

 control = new TButton(TRect(14, 4, 22, 6), "Y-", cmYminus, bfNormal);
 insert(control);

 control = new TButton(TRect(23, 6, 31, 8), "Z+", cmZplus, bfNormal);
 insert(control);

 control = new TButton(TRect(23, 4, 31, 6), "Z-", cmZminus, bfNormal);
 insert(control);

 speedX = new TInputDouble(TRect(8, 2, 16, 3), 11, 0.1, 10.0);
 insert(speedX);

   insert(new TLabel(TRect(5, 2, 8, 3), "X:", speedX));

 speedY = new TInputDouble(TRect(20, 2, 28, 3), 11, 0.1, 10.0);
 insert(speedY);

   insert(new TLabel(TRect(17, 2, 20, 3), "Y:", speedY));

 speedZ = new TInputDouble(TRect(32, 2, 40, 3), 11, 0.1, 10.0);
 insert(speedZ);

   insert(new TLabel(TRect(29, 2, 32, 3), "Z:", speedZ));

 control = new TStaticText(TRect(10, 1, 36, 2), "\003Relative Axis Feedrate");
 insert(control);

 selectNext(False);

 setData(&FeedrateRatio);
}

/**
 ** Changes relative speed for one axis only.
 **/
void JobManager::changeRatio(ushort command)
{
#define SPEEDUP(axis, speed)                                \

        FeedrateRatio.speed += speedChange;                 \
        if (FeedrateRatio.speed > 10 )                      \
          FeedrateRatio.speed = 10.0;                       \
        comm->setRelativeSpeed(axis, FeedrateRatio.speed)
#define SLOWDOWN(axis, speed)                               \

        FeedrateRatio.speed -=  speedChange;                \
        if (FeedrateRatio.speed < 0.1)                      \
          FeedrateRatio.speed = 0.1;                        \
        comm->setRelativeSpeed(axis, FeedrateRatio.speed)

  switch (command)
    {
      case cmXplus:
        SPEEDUP(0, speedX);
        break;
      case cmXminus:
        SLOWDOWN(0, speedX);
        break;
      case cmYplus:
        SPEEDUP(1, speedY);
        break;
      case cmYminus:
        SLOWDOWN(1, speedY);
        break;
      case cmZplus:
        SPEEDUP(2, speedZ);
        break;
      case cmZminus:
        SLOWDOWN(2, speedZ);
        break;
      case cmXYZplus:
        SPEEDUP(0, speedX);
        SPEEDUP(1, speedY);
        SPEEDUP(2, speedZ);
        break;
      case cmXYZminus:
        SLOWDOWN(0, speedX);
        SLOWDOWN(1, speedY);
        SLOWDOWN(2, speedZ);
        break;
    }
  setData(&FeedrateRatio);
}

/**
 ** Handles Job Manager events
 **/
void JobManager::handleEvent( TEvent& event)
{
    TDialog::handleEvent(event);

    switch (event.what)
      {
        case evKeyboard:
          switch (event.keyDown.keyCode)
            {
        	    case kbIns:
                changeRatio(cmXminus);
            		break;
      	      case kbDel:
                changeRatio(cmXplus);
           		  break;
        	    case kbHome:
                changeRatio(cmYminus);
            		break;
      	      case kbEnd:
                changeRatio(cmYplus);
           		  break;
              case kbPgUp:
                changeRatio(cmZminus);
                break;
              case kbPgDn:
                changeRatio(cmZplus);
              case kbGrayMinus:
                changeRatio(cmXYZminus);
                break;
              case kbGrayPlus:
                changeRatio(cmXYZplus);
                break;
            }
          break;

        case evCommand:
          switch( event.message.command )
            {
              case cmPause:
                comm->pauseQueue();
                break;
              case cmContinue:
                comm->continueQueue();
                break;
              case cmDequeue:
                comm->dequeue();
                break;
              case cmXplus:
              case cmYplus:
              case cmZplus:
              case cmXminus:
              case cmYminus:
              case cmZminus:
              case cmXYZplus:
              case cmXYZminus:
                changeRatio(event.message.command);
                break;
            }
        default:
          return;
      }
      clearEvent(event);
}

const char * const JobManager::name = "JobManager";

/**
 ** Writes JobMAneger window into the stream
 **/
void JobManager::write( opstream& os )
{
 TDialog::write( os );
 os << speedX << speedY << speedY;
}

/**
 ** Reads Job Manager window from the stream
 **/
void *JobManager::read( ipstream& is )
{
 TDialog::read( is );
 is >> speedX >> speedY >> speedY;
 return this;
}

/**
 ** Builds job manager window
 **/
TStreamable *JobManager::build()
{
    return new JobManager( streamableInit );
}

// From here to end of file may be removed if JobManager will not be streamed.


TStreamableClass RobManager( JobManager::name,
                        JobManager::build,
                        __DELTA(JobManager)
                      );

__link(RobManager)
__link(RButton)
__link(RInputDouble)
__link(RLabel)
__link(RStaticText)



syntax highlighted by Code2HTML, v. 0.9.1