home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / game / think / chaos / src / outami.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-27  |  11.4 KB  |  465 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.1a
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: OutWnd.c,v $
  20.     $Revision: 2.5 $
  21.     $Date: 1993/12/06 00:32:30 $
  22.  
  23.     These are the system dependent output functions.
  24.  
  25.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  26.  
  27.     Author:    Jochen Wiedmann
  28.         Am Eisteich 9
  29.       72555 Metzingen
  30.         Tel. 07123 / 14881
  31.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  32. */
  33.  
  34.  
  35. #ifndef CHAOS_H
  36. #include "chaos.h"
  37. #endif
  38.  
  39. #ifdef AMIGA
  40. #ifndef PREFS_PREFHDR_H
  41. #include <prefs/prefhdr.h>
  42. #endif
  43. #ifndef PREFS_PRINTERTXT_H
  44. #include <prefs/printertxt.h>
  45. #endif
  46. #ifndef CLIB_IFFPARSE_PROTOS_H
  47. #include <clib/iffparse_protos.h>
  48. #endif
  49. #ifdef AZTEC
  50. #ifndef __PRAMASE_IFFPARSE_LIB_H
  51. #include <pragmas/iffparse_lib.h>
  52. #endif
  53. #include
  54. #endif    /*  AZTEC   */
  55. #endif    /*  AMIGA   */
  56.  
  57.  
  58.  
  59.  
  60. /*
  61.     The following function makes text being centered on the printer.
  62.     This isn't needed at all and might be a stub function on other systems
  63.     than the Amiga.
  64.  
  65.     Inputs: device - the device to which the output goes
  66.  
  67.     Results: TRUE, if successfull, FALSE otherwise
  68. */
  69. int CenterText(int device)
  70.  
  71. #ifdef AMIGA    /*  The following lines produce centered output */
  72. {
  73.   if ((device == DEVICE_PrinterLQ  ||  device == DEVICE_PrinterDraft)  &&
  74.       !lprint("\2331 F"))
  75.   { return(FALSE);
  76.   }
  77.   return(TRUE);
  78. }
  79. #endif    /*  AMIGA   */
  80.  
  81.  
  82.  
  83.  
  84. /*
  85.     The InitOutput() function gets called by output functions before they
  86.     do any output via lprint() or longlprint().
  87.  
  88.     Inputs: title     - the printed document's title
  89.         prthead   - the head to print above each page (printer output)
  90.         scrhead   - the head to appear on top of the output window
  91.             (screen output)
  92.         filename  - the destination file (used if output to file only);
  93.             this can be NULL, in which case InitOutput() brings
  94.             up a filerequester asking for the filename
  95.         device    - the output device: DEVICE_Screen, DEVICE_PrinterDraft,
  96.             DEVICE_PrinterLQ, Device_FileAscii or DEVICE_FileTeX
  97.         prtlines  - the number of lines, that one item needs on the
  98.             printer
  99.         scrlines  - the number of lines, that one item needs on the
  100.             screen
  101.  
  102.     Result: TRUE, if successfull, FALSE otherwise
  103. */
  104. static char *titleOutput;
  105. static char *prtheadOutput;
  106. static char *scrheadOutput;
  107. static char *filenameOutput;
  108. static int deviceOutput;
  109. static int prtlinesOutput;
  110. static int scrlinesOutput;
  111. void *OutputMemList;
  112. struct MinList OutputList;
  113. char *OutputLine;
  114. int OutputLineLen;
  115. int OutputLineMaxLen;
  116. int OutputReturnCode;
  117.  
  118. int InitOutput(char *title, char *prthead, char *scrhead, char *filename,
  119.            char *ending, int device, int prtlines, int scrlines)
  120.  
  121. #ifdef AMIGA
  122. {
  123.   prtlinesOutput = prtlines;
  124.   scrlinesOutput = scrlines;
  125.  
  126.   OutputReturnCode = 0;
  127.   OutputMemList = NULL;
  128.   OutputLine = NULL;
  129.   OutputLineLen = OutputLineMaxLen = (device == DEVICE_Screen) ? 0 : -1;
  130.   deviceOutput = device;
  131.   NewList((struct List *) &OutputList);
  132.  
  133.   if ((device == DEVICE_FileAscii  ||  device == DEVICE_FileTeX)  &&
  134.       filename == NULL)
  135.   { if ((filename = FileRequest(NULL,
  136.                 (char *) GetChaosString(WND_ASCIIFILE_TITLE),
  137.                 (device == DEVICE_FileTeX) ? "#?.tex" : ending,
  138.                 TRUE))
  139.           ==  NULL)
  140.     { OutputReturnCode = RETURN_WARN;
  141.       return(FALSE);
  142.     }
  143.   }
  144.  
  145.   if (!(titleOutput = GetStringMem(&OutputMemList, title))  ||
  146.       !(prtheadOutput = GetStringMem(&OutputMemList, prthead))  ||
  147.       !(scrheadOutput = GetStringMem(&OutputMemList, scrhead))  ||
  148.        (filename != NULL  &&
  149.     !(filenameOutput = GetStringMem(&OutputMemList, filename))))
  150.   { TerminateOutput();
  151.     OutputReturnCode = RETURN_ERROR;
  152.     return(FALSE);
  153.   }
  154.   return(TRUE);
  155. }
  156. #endif    /*  AMIGA   */
  157.  
  158.  
  159.  
  160.  
  161. /*
  162.     TerminateOutput() gets called from the output functions if an error
  163.     occurs or if the output is ended.
  164. */
  165. void TerminateOutput(void)
  166.  
  167. #ifdef AMIGA
  168. {
  169.   PutMemList(&OutputMemList);
  170. }
  171. #endif    /*  AMIGA   */
  172.  
  173.  
  174.  
  175.  
  176. /*
  177.     ProcessOutput() gets called, if a list of lines to be printed is setup
  178.     with lprint or "by hand". (see OutRound)
  179.  
  180.     Inputs: list -  list of lines to be printed; this can be NULL, in which
  181.             case OutputList is assumed
  182. */
  183. void ProcessOutput(void)
  184.  
  185. #ifdef AMIGA
  186. { struct MinNode *line;
  187.  
  188.  
  189.   /*
  190.       Output to a file is rather simple...
  191.   */
  192.   if (deviceOutput == DEVICE_FileAscii  ||  deviceOutput == DEVICE_FileTeX)
  193.   { FILE *fh;
  194.  
  195.     if ((fh = fopen(filenameOutput, "w"))  ==  NULL)
  196.     { ShowError((char *) GetChaosString(MSG_NO_WRITE_FILE),
  197.         filenameOutput, IoErr());
  198.       OutputReturnCode = RETURN_ERROR;
  199.       return;
  200.     }
  201.  
  202.     if (deviceOutput == DEVICE_FileAscii  &&
  203.     (fputs(TrnName, fh) < 0  ||  fputs("\n\n", fh) < 0  ||
  204.      fputs(titleOutput, fh) < 0  ||  fputs("\n\n", fh) < 0  ||
  205.      fputs(prtheadOutput, fh) < 0  ||  fputs("\n\n", fh) < 0))
  206.     { OutputReturnCode = RETURN_ERROR;
  207.     }
  208.     else
  209.     { for(line = OutputList.mlh_Head;  line->mln_Succ != NULL;
  210.       line = line->mln_Succ)
  211.       { if (fputs((char *) (line+1), fh) < 0  ||
  212.         fputc('\n', fh) == EOF)
  213.     { OutputReturnCode = RETURN_ERROR;
  214.       break;
  215.     }
  216.       }
  217.     }
  218.     fclose(fh);
  219.   }
  220.  
  221.   /*
  222.       Output to the printer is a little bit more complicated, because it
  223.       is page oriented. The largest problem is to get the paper length.
  224.       Sigh! Why are the new preferences that complicated?
  225.   */
  226.   else if (deviceOutput != DEVICE_Screen)
  227.   { int paperlen, pagenr, lines;
  228.     FILE *fh;
  229.     int j, writeerr;
  230.  
  231.     /*
  232.     Get the paperlength
  233.     */
  234.     struct Preferences Prefs;
  235.  
  236.     GetPrefs(&Prefs, sizeof(Prefs));
  237.     paperlen = Prefs.PaperLength;
  238. #ifdef V39_INCLUDES
  239.     { struct IFFHandle *iffhandle;
  240.       struct StoredProperty *sp;
  241.       int ifferr;
  242.       extern struct Library *SysBase;
  243.       extern struct Library *IFFParseBase;
  244.  
  245.       if(SysBase->lib_Version >= 38  &&  IFFParseBase != NULL)
  246.       { if ((iffhandle = AllocIFF())  !=  NULL)
  247.     { if ((iffhandle->iff_Stream = Open((STRPTR) "ENV:SYS/Printer.prefs",
  248.                         MODE_OLDFILE))
  249.                      !=  NULL)
  250.       { InitIFFasDOS(iffhandle);
  251.         if (OpenIFF(iffhandle, IFFF_READ)  ==  0)
  252.         { if (PropChunk(iffhandle, ID_PREF, ID_PTXT)  ==  0)
  253.           { for(;;)
  254.         { ifferr = ParseIFF(iffhandle, IFFPARSE_STEP);
  255.           if (ifferr  !=  0)
  256.           { if (ifferr  ==  IFFERR_EOC)
  257.             { continue;
  258.             }
  259.             else
  260.             { break;
  261.             }
  262.           }
  263.  
  264.           if ((sp = FindProp(iffhandle, ID_PREF, ID_PTXT))
  265.               !=  NULL)
  266.           { paperlen = ((struct PrinterTxtPrefs *)
  267.                    (sp->sp_Data))->pt_PaperLength;
  268.             break;
  269.           }
  270.         }
  271.           }
  272.           CloseIFF(iffhandle);
  273.         }
  274.         Close(iffhandle->iff_Stream);
  275.       }
  276.       FreeIFF(iffhandle);
  277.     }
  278.       }
  279.     }
  280. #endif
  281.  
  282.  
  283.     if ((fh = fopen("prt:", "w"))  ==  NULL)
  284.     { ShowError((char *) GetChaosString(MSG_NO_PRINTER));
  285.       OutputReturnCode = RETURN_ERROR;
  286.       return;
  287.     }
  288.     writeerr = TRUE;
  289.  
  290.     /*
  291.     Put application into sleep mode.
  292.     */
  293.     set(App, MUIA_Application_Sleep, TRUE);
  294.  
  295.     /*
  296.     Put printer into draft or LQ mode
  297.     */
  298.     if (fprintf(fh, "%s",
  299.         (deviceOutput == DEVICE_PrinterDraft)  ?
  300.             "\2331\"z" : "\2332\"z")    <  0)
  301.     { goto WriteError;
  302.     }
  303.  
  304.  
  305.     line = OutputList.mlh_Head;
  306.     pagenr = 0;
  307.     while (line->mln_Succ != NULL)
  308.     {
  309.       if (fprintf(fh, "%s\n\n", TrnName)  <  0                  ||
  310.       fprintf(fh, "%-65s  %7s %d\n\n", titleOutput,
  311.           GetChaosString(MSG_PAGENR), ++pagenr)  <  0   ||
  312.       fprintf(fh, "%s\n\n", prtheadOutput)  <  0)
  313.       { goto WriteError;
  314.       }
  315.       lines = 7 + prtlinesOutput;
  316.  
  317.       while (lines+prtlinesOutput < paperlen  &&  line->mln_Succ != NULL)
  318.       { lines += prtlinesOutput;
  319.     for (j = 0;  j < prtlinesOutput;  j++)
  320.     { if (fprintf(fh, "%s\n", line+1)  <  0)
  321.       { goto WriteError;
  322.       }
  323.       line = line->mln_Succ;
  324.     }
  325.       }
  326.  
  327.       while (lines++ < paperlen)
  328.       { if (fprintf(fh, "\n")  <  0)
  329.     { goto WriteError;
  330.     }
  331.       }
  332.  
  333.       if (fprintf(fh, "\n%s\n", PVERSION)  <  0     ||
  334.       fprintf(fh, "\f")  <  0)
  335.       { goto WriteError;
  336.       }
  337.     }
  338.     writeerr = FALSE;
  339.  
  340. WriteError:
  341.     fclose(fh);
  342.     if (writeerr)
  343.     { ShowError((char *) GetChaosString(MSG_PrinterError));
  344.     }
  345.  
  346.     /*
  347.     Awake application
  348.     */
  349.     set(App, MUIA_Application_Sleep, FALSE);
  350.   }
  351.  
  352.   /*
  353.       At last output to the screen, which isn't very complicated too.
  354.       Thanks MUI.
  355.   */
  356.   else
  357.   { ULONG open, signal;
  358.     APTR OutWnd;    /*  output window            */
  359.     APTR OutWnd_Head;    /*  page head gadget            */
  360.     APTR OutWnd_LV;    /*  output text gadget            */
  361.  
  362.     OutWnd = WindowObject,
  363.         MUIA_Window_ID, MAKE_ID('O','U','T','\0'),
  364.         MUIA_Window_Width, MUIV_Window_Width_MinMax(70),
  365.         MUIA_Window_Height, MUIV_Window_Height_MinMax(30),
  366.         MUIA_Window_Title, titleOutput,
  367.         WindowContents, VGroup,
  368.             Child, OutWnd_Head = TextObject,
  369.             MUIA_Text_Contents, scrheadOutput,
  370.             ReadListFrame,
  371.             End,
  372.             Child, OutWnd_LV = ListviewObject,
  373.             MUIA_Listview_List, FloattextObject,
  374.                 MUIA_Floattext_Text, OutputLine,
  375.                 ReadListFrame,
  376.             End,
  377.             End,
  378.         End,
  379.         End;
  380.  
  381.     if (!OutWnd)
  382.     { return;
  383.     }
  384.  
  385.  
  386.     /*
  387.     Add the new window as a member to the application.
  388.     Setting up the notification events for the output window:
  389.     CloseWindow gadget only
  390.     */
  391. #define ID_OutWnd_Cancel 100
  392.     DoMethod(App, OM_ADDMEMBER, OutWnd);
  393.     DoMethod(OutWnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  394.          App, 2, MUIM_Application_ReturnID, ID_OutWnd_Cancel);
  395.  
  396.     /*
  397.     Open the window
  398.     */
  399.     set(OutWnd, MUIA_Window_Open, TRUE);
  400.     get(OutWnd, MUIA_Window_Open, &open);
  401.     if (!open)
  402.     { MUIError((char *) GetChaosString(ERRMSG_CANNOT_OPEN_WINDOW));
  403.       DoMethod(App, OM_REMMEMBER, OutWnd);
  404.       MUI_DisposeObject(OutWnd);
  405.       return;
  406.     }
  407.  
  408.     /*
  409.     Close the main window while the output window is open
  410.     */
  411.     set(MainWnd, MUIA_Window_Open, FALSE);
  412.  
  413.     /*
  414.     Wait for user actions
  415.     */
  416.     for(;;)
  417.     { switch(DoMethod(App, MUIM_Application_Input, &signal))
  418.       { case MUIV_Application_ReturnID_Quit:
  419.       if (TestSaved())
  420.       { Cleanup(0, NULL);
  421.       }
  422.       break;
  423.     case ID_OutWnd_Cancel:
  424.       set(OutWnd, MUIA_Window_Open, FALSE);
  425.       DoMethod(App, OM_REMMEMBER, OutWnd);
  426.       MUI_DisposeObject(OutWnd);
  427.       return;
  428.       }
  429.  
  430.       if (signal)
  431.       { Wait(signal);
  432.       }
  433.     }
  434.   }
  435. }
  436. #endif    /*  AMIGA   */
  437.  
  438.  
  439.  
  440.  
  441. /*
  442.     AskForBirthday() should bring up a requester and ask for the birthday
  443.     of a player whose birthday field isn't set or valid.
  444.  
  445.     Inputs: plr - the player who is asked for
  446.  
  447.     Results: 1 = Assume that player is 20 or younger
  448.          2 = Assume that player is between 21 and 25 years old
  449.          3 = Assume that player is 26 or older
  450.          4 = The player wants to modify the players data
  451.          5 = Skip this player (will not be present in the DWZ report)
  452.          0 = Cancel
  453. */
  454. int AskForBirthday(struct Player *plr)
  455.  
  456. #ifdef AMIGA
  457. {
  458.   return(MUI_Request(App, MainWnd, 0,
  459.              (char *) GetChaosString(MSG_ATTENTION),
  460.              (char *) GetChaosString(MSG_NO_BIRTHDAY_GADGETS),
  461.              (char *) GetChaosString(MSG_NO_BIRTHDAY),
  462.              plr->Name));
  463. }
  464. #endif    /*  AMIGA   */
  465.