
#include "defs.h"

Prototype           void  do_forget_it(void);
Prototype           void  do_print_it(void);
Prototype __regargs void  do_storage_str(SHORT);
Prototype           void  do_storage_list(void);
Prototype           void  do_quit(void);
Prototype __regargs SHORT do_request(UBYTE *, UBYTE *, UWORD *);
Prototype           void  do_filereq(void);
Prototype __regargs void  do_togglegad(struct Gadget *);
Prototype __regargs void  do_all(SHORT);

Prototype SHORT pagesize;
Prototype SHORT linelength;
Prototype SHORT quit;
Prototype SHORT split;
Prototype SHORT title;
Prototype SHORT autom;

Prototype struct EasyStruct textreq;

SHORT pagesize   = 1;
SHORT linelength = 1;
SHORT quit       = 0;
SHORT split      = 0;
SHORT title      = 1;
SHORT autom      = 1;

/***********************  textreq ************************************/
const UBYTE titlereq[] = " Some serious question:";

struct EasyStruct textreq =
{
  sizeof(struct EasyStruct),            /* ULONG es_StructSize      */
  0l,                                   /* ULONG es_Flags           */
  titlereq,                             /* UBYTE *es_Title          */
  NULL,                                 /* UBYTE *es_TextFormat     */
  NULL,                                 /* UBYTE *es_GadgetFormat   */
};

/***********************  do_print_it(void) **************************/
void
do_print_it(void)
{
  if (Buf_storage_str[0] != '\0')
  {
    PE *pe;
    if (pe = (PE *)FindName(&List_storage_list, Buf_storage_str))
    {
      if (init_par(pe))
      {
        if (init_prt())
        {
          do_togglegad(Gad_print_it);
          Write(prt, pe->SplitBuffer, pe->Size);
          close_prt();
          del_prienv(pe);
          do_togglegad(Gad_print_it);
          return;
        }
      }
    }
    do_storage_str(NULL);
  }
}

/***********************  do_forget_it(void)  ************************/
void
do_forget_it(void)
{
  if (Buf_storage_str[0])
  {
    PE *pe;
    if (pe = (PE *)FindName(&List_storage_list, Buf_storage_str))
      del_prienv(pe);
    else
      do_storage_str(NULL);
  }
}

/***********************  do_quit(void) ******************************/
const UBYTE reallyquit[] = "Really quit now ???";
const UBYTE yescancel[]  = "Yes|Cancel";

void
do_quit(void)
{
  do_togglegad(Gad_quit);
  if ((quit = do_request(reallyquit, yescancel, 0)) == 0)
    do_togglegad(Gad_quit);
}

/***********************  do_request(UBYTE *) ************************/
__regargs SHORT
do_request(UBYTE *text, UBYTE *gads, UWORD *args)
{
  textreq.es_TextFormat   = text;
  textreq.es_GadgetFormat = gads;

  return (EasyRequestArgs(win, &textreq, NULL, &args));
}

/***********************  do_storage_list(void) **********************/
void
do_storage_list(void)
{
  PE *pe;
  if (pe = (PE *)FindName(&List_storage_list, Buf_storage_str))
  {
    pagesize   = pe->pagesize;
    linelength = pe->linelength;

    GT_SetGadgetAttrs(Gad_pagesize, win, NULL,
                      GTCY_Active, pagesize,
                      TAG_END);

    GT_SetGadgetAttrs(Gad_linelength, win, NULL,
                      GTCY_Active, linelength,
                      TAG_END);
  }
}

/***********************  do_storage_str(SHORT)  **********************/
__regargs void
do_storage_str(SHORT show)
{
  if (!show)
    Buf_storage_str[0] = '\0';

  GT_SetGadgetAttrs(Gad_storage_str, win, NULL,
                    GTST_String, Buf_storage_str,
                    TAG_END);
}

/***********************  do_filereq()  **********************/
const UBYTE chooztext[] = "Chooz text to load:";
const UBYTE loadit[]    = "Load it";

void
do_filereq(void)
{
  do_togglegad(Gad_filereq);

  UBYTE ok = FALSE;
  struct FileRequester *freq;
  if (freq = AllocAslRequestTags(ASL_FileRequest,
                                 ASL_Hail,      chooztext,
                                 ASL_Window,    win,
                                 ASL_LeftEdge,  win->LeftEdge,
                                 ASL_TopEdge,   win->TopEdge,
                                 ASL_Width,     WIN_WIDTH,
                                 ASL_Height,    WIN_HEIGHT,
                                 ASL_OKText,    loadit,
                                 TAG_END))

  {
    if (ok = AslRequest(freq, NULL))
    {
      UBYTE *d = Buf_storage_str;
      UBYTE *s = freq->rf_Dir;
      if (*s)
      {
        for (; *d = *s; ++s, ++d);    /*  strcpy dirname  */

        --d;
        if  (*d != ':' && *d != '/')
          *++d = '/';
        ++d;
      }
      s = freq->rf_File;
      if (ok = *s)
        for (; *d = *s; ++s, ++d);    /*  strcat filename */
    }
    FreeAslRequest(freq);
  }
  if (ok)
    do_process();

  do_storage_str(ok);

  do_togglegad(Gad_filereq);
}

/***********************  do_togglegad  **************************/
__regargs void
do_togglegad(struct Gadget *gadget)
{
  USHORT gad_pos = RemoveGadget(win, gadget);

  if (gadget->Flags & SELECTED)
    gadget->Flags  -= SELECTED;
  else
    gadget->Flags  += SELECTED;

  AddGadget(win, gadget, gad_pos);
  RefreshGList(gadget, win, NULL, 1);
}

/***********************  do_all() ********************************/
__regargs void
do_all(SHORT side)
{
  PE *pe     = (PE *) List_storage_list.mlh_Head;
  while (pe != (PE *)&List_storage_list.mlh_Tail)
  {
    PE *pet = pe->pe_node.ln_Succ;
    if (pe->pe_node.ln_Type == side)
      ask_print(pe->pe_node.ln_Name);

    pe = pet;
  }
}

