/********************************************************************/
/*                                                                  */
/*  Utility program to set print characteristics for the Panasonic  */
/*  KX-P1091i line printer.                                         */
/*                                                                  */
/*                                                                  */
/*                      Copyright (c) 1992 by                       */
/*                                                                  */
/*                      Leonard A. Kaczorowski                      */
/*                         January 24, 1988                         */
/*                                                                  */
/*                           Version 3.10                           */
/*                          April 18, 1989                          */
/*                                                                  */
/********************************************************************/




/*  Include files  */

#include <gemdefs.h>
#include <obdefs.h>
#include <osbind.h>
#include "panaset.h"




/*  Constants  */

#define BELL     7
#define ESC     27
#define FALSE    0
#define TRUE    -1
#define end
#define endif
#define endwhile

OBJECT *printer;




/*  Global variables  */

int  ap_id;                        /*  Application identification   */
int  contrl[12];                   /*                               */
int  cpi;                          /*  Characters per inch          */
int  done;                         /*  Done with dialog             */
int  dmy;                          /*  Dummy parameter              */
int  event;                        /*  Event flags                  */
int  hbox;                         /*                               */
int  hchar;                        /*                               */
int  wbox;                         /*                               */
int  wchar;                        /*                               */
int  handle;                       /*  Virtual workstation handle   */
int  i;                            /*  Miscellaneous variable       */
int  ibm;                          /*  IBM mode selected            */
int  intin[128];                   /*                               */
int  intout[128];                  /*                               */
int  lpi;                          /*  Lines per inch               */
int  menu_id;                      /*  Menu identification          */
int  msgbuff[8];                   /*  Message buffer               */
int  n;                            /*  Loop counter                 */
int  phys_handle;                  /*  Physical workstation handle  */
int  ptsin[128];                   /*                               */
int  ptsout[128];                  /*                               */ 
int  retry;                        /*  Retry the printer            */
int  setups[100];                  /*  Printer setup commands       */
int  epson;                        /*  Epson mode selected          */
int  work_in[11];                  /*  Input array                  */
int  work_out[57];                 /*  Output array                 */
int  xobj, yobj, wobj, hobj;       /*  Object coordinates           */
int  xdesk, ydesk, wdesk, hdesk;   /*  Desktop coordinates          */




main()
    {
    init_prg();
    while (TRUE)                                                    /*.ACC*/
        {                                                           /*.ACC*/
        evnt_mesag(msgbuff);                                        /*,ACC*/
        if (msgbuff[0] == AC_OPEN)                                  /*.ACC*/
            {                                                       /*.ACC*/
            if (msgbuff[4] == menu_id)                              /*.ACC*/
                {                                                   /*.ACC*/
                setup();
                }                                                   /*.ACC*/
            endif                                                   /*.ACC*/
            }                                                       /*.ACC*/
        endif                                                       /*.ACC*/
        }                                                           /*.ACC*/
    endwhile                                                        /*.ACC*/
    appl_exit();
    }
end




direction()
    {
    setups[n++] = ESC;
    setups[n++] = 'U';
    if (printer[B_1_DIR].ob_state == SELECTED)
        {
        setups[n++] = 1;
        }
    else
        {
        setups[n++] = 0;
        }
    endif
    }
end




draft()
    {
    setups[n++] = ESC;
    if (epson)
        {
        setups[n++] = 'x';
        setups[n++] = 0;
        }
    else
        {
        setups[n++] = 'H';
        }
    endif
    }
end




init_prg()
    {
    ap_id = appl_init();
    phys_handle = graf_handle(&wchar, &hchar, &wbox, &hbox);
    wind_get(0, WF_WORKXYWH, &xdesk, &ydesk, &wdesk, &hdesk);
    menu_id = menu_register(ap_id, "  Printer Setup");              /*.ACC*/
    mrsrc_load(PANASETRSC);
    rsrc_gaddr(R_TREE, PRINTER, &printer);
    printer[B_IBM].ob_state &= ~SELECTED;                           /*EPSON*/
    ibm = FALSE;                                                    /*EPSON*/
    printer[B_EPSON].ob_state |= SELECTED;                          /*EPSON*/
    epson = TRUE;                                                   /*EPSON*/
    printer[B_PROPO].ob_state &= ~DISABLED;                         /*EPSON*/
/*  printer[B_IBM].ob_state |= SELECTED;                          *//*IBM*/
/*  ibm = TRUE;                                                   *//*IBM*/
/*  printer[B_EPSON].ob_state &= ~SELECTED;                       *//*IBM*/
/*  epson = FALSE;                                                *//*IBM*/
/*  printer[B_PROPO].ob_state |= DISABLED;                        *//*IBM*/
    printer[B_2_DIR].ob_state |= SELECTED;
    printer[B_1_DIR].ob_state &= ~SELECTED;
    printer[B_SINGLE].ob_state |= SELECTED;
    printer[B_DOUBLE].ob_state &= ~SELECTED;
    printer[B_NORMAL].ob_state |= SELECTED;
    printer[B_EXPAND].ob_state &= ~SELECTED;
    printer[B_CONT].ob_state |= SELECTED;
    printer[B_SKIP].ob_state &= ~SELECTED;
    printer[B_LPI_6].ob_state |= SELECTED;
    printer[B_LPI_8].ob_state &= ~SELECTED;
    printer[B_INCH_0].ob_state |= SELECTED;
    printer[B_INCH_1].ob_state &= ~SELECTED;
    printer[B_PICA].ob_state |= SELECTED;
    printer[B_ELITE].ob_state &= ~SELECTED;
    printer[B_CONDEN].ob_state &= ~SELECTED;
    printer[B_PROPO].ob_state &= ~SELECTED;
    printer[B_LETTER].ob_state &= ~SELECTED;
    printer[B_CANCEL].ob_state &= ~SELECTED;
    printer[B_DONE].ob_state &= ~SELECTED;
    printer[B_FORM].ob_state &= ~SELECTED;
    }
end




letter()
    {
    setups[n++] = ESC;
    if (epson)
        {
        setups[n++] = 'x';
        setups[n++] = 1;
        }
    else
        {
        setups[n++] = 'G';
        }
    endif
    }
end




lines()
    {
    setups[n++] = ESC;
    if (printer[B_LPI_8].ob_state == SELECTED)
        {
        lpi = 8;
        setups[n++] = '0';
        }
    else
        {
        lpi = 6;
        if (ibm)
            {
            setups[n++] = 'A';
            setups[n++] = 12;
            setups[n++] = ESC;
            }
        endif
        setups[n++] = '2';
        }
    endif
    }
end




margin()
    {
    setups[n++] = ESC;
    if (epson)
        {
        setups[n++] = 'l';
        }
    else
        {
        setups[n++] = 'X';
        }
    endif
    if (printer[B_INCH_1].ob_state == SELECTED)
        {
        if (epson)
            {
            setups[n++] = cpi;
            }
        else
            {
            setups[n++] = cpi + 1;
            setups[n++] = 0;
            }
        endif
        }
    else
        {
        if (epson)
            {
            setups[n++] = 0;
            }
        else
            {
            setups[n++] = 1;
            setups[n++] = 0;
            }
        endif
        }
    endif
    }
end




mode()
    {
    ibm = FALSE;
    epson = FALSE;
    if (printer[B_EPSON].ob_state == SELECTED)
        {
        epson = TRUE;
        if (printer[B_PROPO].ob_state == DISABLED)
            {
            printer[B_PROPO].ob_state &= ~DISABLED;
            objc_draw(printer, B_PROPO, 0, 0, 0, wdesk, hdesk);
            }
        endif
        }
    else
        {
        ibm = TRUE;
        if (printer[B_PROPO].ob_state != DISABLED)
            {
            if (printer[B_PROPO].ob_state == SELECTED)
                {
                printer[B_PROPO].ob_state &= ~SELECTED;
                printer[B_PICA].ob_state |= SELECTED;
                objc_draw(printer, B_PICA, 0, 0, 0, wdesk, hdesk);
                }
            endif
            printer[B_PROPO].ob_state |= DISABLED;
            objc_draw(printer, B_PROPO, 0, 0, 0, wdesk, hdesk);
            }
        endif
        }
    endif
    }
end



open_vwork()
    {
    for (n=0; n<10; work_in[n++] = 1);
    work_in[10] = 2;
    handle = phys_handle;
    v_opnvwk(work_in, &handle, work_out);
    }
end




perf()
    {
    setups[n++] = ESC;
    if (printer[B_SKIP].ob_state == SELECTED)
        {
        setups[n++] = 'N';
        setups[n++] = lpi;
        }
    else
        {
        setups[n++] = 'O';
        }
    endif
    }
end




pitch()
    {
    setups[n++] = ESC;
    setups[n++] = 'W';
    if (printer[B_EXPAND].ob_state == SELECTED)
        {
        setups[n++] = 1;
        if (epson)
            {
            cpi = (cpi + 1) / 2;
            }
        endif
        }
    else
        {
        setups[n++] = 0;
        }
    endif
    }
end




print()
    {
    i = 0;
    retry = FALSE;
    if (Cprnos() == FALSE)
        {
        i = form_alert(2,"[1][ |The Printer Is| |  Not Ready][QUIT|RETRY]");
        if (i == 1)
            {
            done = TRUE;
            }
        else
            {
            retry = TRUE;
            }
        endif
        i = n + 1;
        }
    else
        {
        while (i <= n)
            {
            Cprnout(setups[i++]);
            }
        endwhile
        }
    endif
    }
end




quality()
    {
    if (printer[B_LETTER].ob_state == SELECTED)
        {
        letter();
        }
    else
        {
        draft();
        }
    endif
    }
end




setup()
    {
    open_vwork();
    form_center(printer, &xobj, &yobj, &wobj, &hobj);
    form_dial(FMD_START, dmy, dmy, dmy, dmy, xobj, yobj, wobj, hobj);
    form_dial(FMD_GROW, 0, 10, 100, 50, xobj, yobj, wobj, hobj);
    objc_draw(printer, 0, MAX_DEPTH, 0, 0, wdesk, hdesk);
    graf_mouse(ARROW);
    done = FALSE;
    while (done == FALSE)
        {
        form_do(printer,0);
        n = 0;
        if (printer[B_CANCEL].ob_state == SELECTED)
            {
            done = TRUE;
            printer[B_CANCEL].ob_state &= ~SELECTED;
            }
        else if (printer[B_FORM].ob_state == SELECTED)
            {
            setups[n] = 12;
            print();
            printer[B_FORM].ob_state &= ~SELECTED;
            if (done == FALSE)
                {
                objc_draw(printer, B_FORM, 0, 0, 0, wdesk,hdesk);
                }
            endif
            }
        else
            {
            n = 0;
            mode();
            style();
            quality();
            direction();
            strike();
            pitch();
            lines();
            perf();
            margin();
            if (printer[B_DONE].ob_state == SELECTED)
                {
                setups[n++] = BELL;
                setups[n] = 13;
                print();
                printer[B_DONE].ob_state &= ~SELECTED;
                if (retry == TRUE)
                    {
                    objc_draw(printer, B_DONE, 0, 0, 0, wdesk, hdesk);
                    }
                else
                    {
                    done = TRUE;
                    }
                endif
                }
            endif
            }
        endif
        }
    endwhile
    form_dial(FMD_SHRINK, 0, 10, 100, 50, xobj, yobj, wobj, hobj);
    form_dial(FMD_FINISH, dmy, dmy, dmy, dmy, xobj, yobj, wobj, hobj);
    v_clsvwk(handle);
    }
end




strike()
    {
    setups[n++] = ESC;
    if (printer[B_DOUBLE].ob_state == SELECTED)
        {
        setups[n++] = 'E';
        }
    else
        {
        setups[n++] = 'F';
        }
    endif
    }
end




style()
    {
    setups[n++] = 18;
    if (epson)
        {
        setups[n++] = ESC;
        setups[n++] = 'P';
        }
    endif
    if (printer[B_CONDEN].ob_state == SELECTED)
        {
        if (printer[B_DOUBLE].ob_state == SELECTED)
            {
            printer[B_DOUBLE].ob_state &= ~SELECTED;
            printer[B_SINGLE].ob_state |= SELECTED;
            objc_draw(printer, B_SINGLE, 0, 0, 0, wdesk, hdesk);
            }
        endif
        if (printer[B_DOUBLE].ob_state != DISABLED)
            {
            printer[B_DOUBLE].ob_state |= DISABLED;
            objc_draw(printer, B_DOUBLE, 0, 0, 0, wdesk, hdesk);
            }
        endif
        setups[n++] = 15;
        cpi = 17;
        }
    else
        {
        if (printer[B_DOUBLE].ob_state == DISABLED)
            {
            printer[B_DOUBLE].ob_state &= ~DISABLED;
            objc_draw(printer, B_DOUBLE, 0, 0, 0, wdesk, hdesk);
            }
        endif
        if (printer[B_ELITE].ob_state == SELECTED)
            {
            setups[n++] = ESC;
            if (epson)
                {
                setups[n++] = 'M';
                }
            else
                {
                setups[n++] = ':';
                }
            endif
            cpi = 12;
            }
        else
            {
            if (epson)
                {
                setups[n++] = ESC;
                setups[n++] = 'p';
                if (printer[B_PROPO].ob_state == SELECTED)
                    {
                    setups[n++] = 1;
                    }
                else
                    {
                    setups[n++] = 0;
                    }
                endif
                }
            endif
            cpi = 10;
            }
        endif
        }
    endif
    }
end
