home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / stex2-18.zip / SeeTeX / Xtex / file.c < prev    next >
C/C++ Source or Header  |  1992-06-23  |  12KB  |  501 lines

  1. /*
  2.  * Copyright 1989 Dirk Grunwald
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Dirk Grunwald or M.I.T.
  9.  * not be used in advertising or publicity pertaining to distribution of
  10.  * the software without specific, written prior permission.  Dirk
  11.  * Grunwald and M.I.T. makes no representations about the suitability of
  12.  * this software for any purpose.  It is provided "as is" without express
  13.  * or implied warranty.
  14.  * 
  15.  * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL M.I.T.  BE LIABLE FOR ANY SPECIAL, INDIRECT
  18.  * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  19.  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  20.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  21.  * OR PERFORMANCE OF THIS SOFTWARE.
  22.  * 
  23.  * Author:
  24.  *     Dr. Dirk Grunwald
  25.  *     Dept. of Computer Science
  26.  *     Campus Box 430
  27.  *     Univ. of Colorado, Boulder
  28.  *     Boulder, CO 80309
  29.  * 
  30.  *     grunwald@colorado.edu
  31.  *     
  32.  */ 
  33.  
  34. #include "xtex.h"
  35.  
  36. #include <errno.h>
  37. #include <sys/param.h>
  38. #include <assert.h>
  39.  
  40. #include <X11/Xaw/Label.h>
  41. #include <X11/Xaw/Text.h>
  42. #include <X11/Xaw/AsciiSrc.h>
  43.  
  44. #include <X11/Xatom.h>
  45.  
  46. #include "dvi-simple.h"
  47. #include "page.h"
  48. #include "mark.h"
  49. #include "widgets.h"
  50.  
  51. #ifdef SELFILE
  52. #include <sys/types.h>
  53. #ifdef __STDC__
  54. extern FILE *XsraSelFile (Widget, char *, char *, char *, char *, char *,
  55.   char *, int * (char *, char **, struct stat *), char **);
  56. static int isDviFileName (char *, char **, struct stat *);
  57. #else
  58. extern FILE *XsraSelFile ();
  59. static int isDviFileName ();
  60. #endif
  61. #endif
  62.  
  63. char TeXInputFileNameString[MAXPATHLEN+40];
  64. char *TeXInputFileName;
  65.  
  66. char TeXInputDirNameString[MAXPATHLEN+40];
  67. char *TeXInputDirName;
  68.  
  69. char fileNameInput[MAXPATHLEN];
  70.  
  71. #ifdef __hpux
  72. #define getwd(d) getcwd(d, MAXPATHLEN)
  73. #endif
  74.  
  75. /*
  76.  *    If it's a file name, set to the directory of that file,
  77.  *    otherwise, set to the directory
  78.  */
  79. void
  80.   setDirectory(dir)
  81. char *dir;
  82. {
  83.   Arg argList[20];
  84.   Cardinal args;
  85.   
  86.   char pDir[MAXPATHLEN];
  87.   char *endPDir;
  88.   int len;
  89.   
  90.   strncpy(pDir,dir,MAXPATHLEN);
  91.   len = strlen(pDir);
  92.   endPDir = &pDir[len];
  93.   
  94.   while (chdir(pDir) < 0) {
  95.     /* skip back to last slash */
  96.     while (endPDir != pDir && *endPDir != '/') endPDir--;
  97.     
  98.     if (endPDir == pDir) {
  99.       getwd(pDir);
  100.     }
  101.     else {
  102.       *endPDir = 0;
  103.     }
  104.     
  105.   }
  106.   
  107.   getwd(pDir);
  108.   
  109.   strcpy(TeXInputDirNameString, "Directory: ");
  110.   TeXInputDirName = &TeXInputDirNameString[strlen(TeXInputDirNameString)];
  111.   strncpy(TeXInputDirName, pDir, MAXPATHLEN);
  112.   
  113.   if ( directoryLabel != NULL ) {
  114.     args = 0;
  115.     XtSetArg(argList[args], XtNlabel, TeXInputDirNameString); args++;
  116.     XtSetValues( directoryLabel, argList, args);
  117.   }
  118. }
  119.  
  120. void setFileName(fileName)
  121.      char *fileName;
  122. {
  123.   if ( fileNameText && XtIsRealized( fileNameText ) ) {
  124.     
  125.     XawTextBlock tblk;
  126.     
  127.     int len = (fileName == 0) ? 0 : strlen(fileName);
  128.     if ( len > 0 ) {
  129.       Arg argList[20];
  130.       Cardinal args;
  131.       char *currentString;
  132.       XawTextPosition start;
  133.       XawTextPosition end;
  134.       
  135.       args = 0;
  136.       XtSetArg(argList[args], XtNstring, ¤tString); args++;
  137.       XtGetValues(fileNameText, argList, args);
  138.       
  139.       start = 0; end = strlen(currentString);
  140.       
  141.       tblk.firstPos = 0;
  142.       tblk.length = len;
  143.       tblk.ptr = fileName;
  144.       tblk.format = FMT8BIT;
  145.       
  146.       XawTextDisableRedisplay(fileNameText);
  147.       XawTextReplace( fileNameText, start, end, &tblk );
  148.       XawTextEnableRedisplay(fileNameText);
  149.     }
  150.   }
  151. }
  152.  
  153. void
  154.   setFile(fileName, copy)
  155. char *fileName;
  156. int copy;
  157. {
  158.   extern double atof();
  159.   DviDpi = (int) xtexResources.dpiHoriz;
  160.   
  161.   TeXPageCloseFile();    /* close open pages stuff */
  162.   
  163.   if( DviInit(fileName, copy )) {
  164.     disablePageLevel();
  165.     TeXPageSlamPages();
  166.     error(0, 0,"[fopen] Unable to open ``%s''", fileName);
  167.   }
  168.   else {
  169.     
  170.     enablePageLevel();
  171.     
  172.     error(0, 0, "%s the %d page file ``%s''",
  173.       (copy) ? "Copied" : "Opened",
  174.       DviTotalPages, DVIFileName);
  175.     
  176.     if ( xtexResources.updateNames ) {
  177.       char spbuffer[1024];
  178.       
  179.       sprintf(spbuffer,"%s (%s)",
  180.           ProgName, DVIFileName);
  181.       
  182.       XChangeProperty(XtDisplay(TopLevelWidget),
  183.               XtWindow(TopLevelWidget),
  184.               XA_WM_NAME, XA_STRING, 8, PropModeReplace,
  185.               (unsigned char*) spbuffer, strlen(spbuffer));
  186.       
  187.       XChangeProperty(XtDisplay(TopLevelWidget),
  188.               XtWindow(TopLevelWidget),
  189.               XA_WM_ICON_NAME, XA_STRING, 8, PropModeReplace,
  190.               (unsigned char*) spbuffer, strlen(spbuffer));
  191.     }
  192.     
  193.     strcpy(TeXInputFileNameString, "File: ");
  194.     TeXInputFileName = &TeXInputFileNameString[strlen(TeXInputFileNameString)];
  195.     strncpy(TeXInputFileName, DVIFileName, MAXPATHLEN);
  196.     
  197.     if ( fileLabel != NULL) {
  198.       Arg argList[20];
  199.       Cardinal args;
  200.       
  201.       args = 0;
  202.       XtSetArg(argList[args], XtNlabel, TeXInputFileNameString); args++;
  203.       XtSetValues( fileLabel, argList, args );
  204.     }
  205.   }
  206.   TeXFontNewFile();
  207.   TeXMarkNewFile();
  208.   TeXPageOpenFile();
  209. }
  210.  
  211. /*
  212.  *    Page related callbacks & routines
  213.  */
  214.  
  215. enablePageLevel()
  216. {
  217.   Arg argList[20];
  218.   Cardinal args;
  219.   
  220.   args = 0;
  221.   XtSetArg(argList[args], XtNsensitive, True); args++;
  222.   
  223.   XtSetValues(pageButton, argList, args);
  224.   XtSetValues(printAllButton, argList, args);
  225.   XtSetValues(printMarkedButton, argList, args);
  226.   XtSetValues(printUnmarkedButton, argList, args);
  227.   
  228.   XtSetValues(clearMarksButton, argList, args);
  229.   if ( markMenuList ) {
  230.     XtSetValues(markMenuList, argList, args);
  231.   }
  232. }
  233.  
  234. disablePageLevel()
  235. {
  236.   Arg argList[20];
  237.   Cardinal args;
  238.   
  239.   args = 0;
  240.   XtSetArg(argList[args], XtNsensitive, False); args++;
  241.   
  242.   XtSetValues(pageButton, argList, args);
  243.   XtSetValues(printAllButton, argList, args);
  244.   XtSetValues(printMarkedButton, argList, args);
  245.   XtSetValues(printUnmarkedButton, argList, args);
  246.   
  247.   XtSetValues(clearMarksButton, argList, args);
  248.   if ( markMenuList ) {
  249.     XtSetValues(markMenuList, argList, args);
  250.   }
  251. }
  252.  
  253. static void actionQuit(w, event, params, num_params)
  254.      Widget w;
  255.      XEvent *event;
  256.      String *params;
  257.      Cardinal *num_params;
  258. {
  259.   exit(0);
  260. }
  261.  
  262. static void actionNoop(w, event, params, num_params)
  263.      Widget w;
  264.      XEvent *event;
  265.      String *params;
  266.      Cardinal *num_params;
  267. {
  268.   XBell(XtDisplay(w), 100);
  269. }
  270.  
  271. int FileOpenShouldCopy = 0;
  272.  
  273. static void actionFile(w, event, params, num_params)
  274.      Widget w;
  275.      XEvent *event;
  276.      String *params;
  277.      Cardinal *num_params;
  278. {
  279.   char *fileName;
  280.   
  281.   if ( *num_params < 1 ) {
  282.     FileOpenShouldCopy = 1;        /* default to copy */
  283.   }
  284.   else if ( strncmp(params[0],"close", 5) == 0 ) {
  285.     DviFini();
  286.     TeXFontNewFile();
  287.     TeXMarkNewFile();
  288.     TeXPageCloseFile();
  289.     TeXPageSlamPages();
  290.     disablePageLevel();
  291.   }
  292. #ifdef SELFILE
  293.   else if ( strncmp(params[0],"select", 6) == 0 ) {
  294.     FILE *fp;
  295.   
  296.      FileOpenShouldCopy = 0;        /* default to open */
  297.      fp = XsraSelFile((Widget) TopLevelWidget, "Open File :", "okay", "cancel",
  298.        "Cannot open file : ", (char *) NULL, "r", isDviFileName, &fileName);
  299.      if (fp == NULL)
  300.           return;
  301.      else {
  302.        Arg argList[20];
  303.        Cardinal args;
  304.        char *p, *currentString;
  305.  
  306.        setDirectory (fileName);
  307.        /* Peel off directory names and set file name with remainder */
  308.        p = fileName;
  309.        p += strlen (fileName) - 1;
  310.       while( p != fileName && *p != '/' )
  311.         --p;
  312.       if( *p == '/' ) p++;
  313.  
  314.       setFileName( p );
  315.       args = 0;
  316.       XtSetArg(argList[args], XtNstring, ¤tString); args++;
  317.       XtGetValues(fileNameText, argList, args);
  318.       setFile( currentString, FileOpenShouldCopy );
  319.       }
  320.  
  321.     XtFree( fileName );
  322.   }
  323. #endif
  324.   else {
  325.     int doCd = 0;
  326.     
  327.     if ( strncmp(params[0],"open", 4) == 0 ) {
  328.       FileOpenShouldCopy = 0;        /* default to open */
  329.     }
  330.     else if ( strncmp(params[0],"copy", 4) == 0 ) {
  331.       FileOpenShouldCopy = 1;        /* default to copy */
  332.     }
  333.     else if ( strncmp(params[0],"cd", 2) == 0 ) {
  334.       doCd = 1;
  335.     }
  336.     else if ( strncmp(params[0],"reopen", 6) == 0 ) {
  337.       /* use old value */
  338.     }
  339.     else {
  340.       error(0,0,"syntax: xtex-file(open/copy/reopen/close[,filename])");
  341.       FileOpenShouldCopy = 0;
  342.     }
  343.     
  344.     if ( *num_params == 2 ) {
  345.       fileName = params[1];
  346.     }
  347.     else if ( *num_params == 1 ) {
  348.       Arg argList[20];
  349.       Cardinal args;
  350.       char *currentString;
  351.       
  352.       args = 0;
  353.       XtSetArg(argList[args], XtNstring, ¤tString); args++;
  354.       XtGetValues(fileNameText, argList, args);
  355.       fileName = currentString;
  356.     }
  357.     else {
  358.       error(0,0,"syntax: xtex-file(open|copy|reopen|close[,filename])");
  359.       fileName = fileNameInput;
  360.     }
  361.     
  362.     if ( fileName == NULL ) {
  363.       error(0, 0, "No file specified?");
  364.     } 
  365.     else {
  366.       if ( doCd ) {
  367.     setDirectory( fileName );
  368.       }
  369.       else {
  370.     setFile( fileName, FileOpenShouldCopy );
  371.       }
  372.     }
  373.   }
  374. }
  375.  
  376. static void actionPopupPage(w, event, params, num_params)
  377.      Widget w;
  378.      XEvent *event;
  379.      String *params;
  380.      Cardinal *num_params;
  381. {
  382.   if (DviFile != 0 ) {
  383.     TeXPageBuild();
  384.   }
  385. }
  386.  
  387. static XtActionsRec fileInputActionsTable[] = {
  388.   {"xtex-file",   actionFile},
  389.   {"xtex-page-popup",   actionPopupPage},
  390.   {"xtex-quit",   actionQuit},
  391.   {"xtex-noop",   actionNoop}
  392. };
  393.  
  394. void
  395.   TeXFileInstallActions()
  396. {
  397.   XtAddActions(fileInputActionsTable, XtNumber(fileInputActionsTable));
  398. }
  399.  
  400.  
  401. void
  402.   BuildFileLevel()
  403. {
  404.   Arg argList[20];
  405.   Cardinal args;
  406.   int len = strlen(fileNameInput);
  407.   
  408.   args = 0;
  409.   XtSetArg(argList[args], XtNeditType,XawtextEdit); args++;
  410.   XtSetArg(argList[args], XtRAsciiType, XawAsciiString); args++;
  411.   XtSetArg(argList[args], XtNstring, fileNameInput); args++;
  412.   XtSetArg(argList[args], XtNlength, MAXPATHLEN); args++;
  413.   fileNameText =
  414.     XtCreateManagedWidget("fileName",
  415.               asciiTextWidgetClass, topPane,
  416.               argList, args);
  417.   
  418.   quitButton =
  419.     XtCreateManagedWidget("quitButton",
  420.               commandWidgetClass, topPane,
  421.               0,0);
  422. #ifdef SELFILE
  423.   selectButton =
  424.     XtCreateManagedWidget("selectButton",
  425.               commandWidgetClass, topPane,
  426.               0,0);
  427. #endif
  428.  
  429.   openButton =
  430.     XtCreateManagedWidget("openButton",
  431.               commandWidgetClass, topPane,
  432.               0,0);
  433.   copyButton =
  434.     XtCreateManagedWidget("copyButton",
  435.               commandWidgetClass, topPane,
  436.               0,0);
  437.   closeButton =
  438.     XtCreateManagedWidget("closeButton",
  439.               commandWidgetClass, topPane,
  440.               0,0);
  441.   cdButton =
  442.     XtCreateManagedWidget("cdButton",
  443.               commandWidgetClass, topPane,
  444.               0,0);
  445.   pageButton
  446.     = XtCreateManagedWidget("pageButton",
  447.                 commandWidgetClass, topPane,
  448.                 0,0);
  449.   
  450.   printAllButton
  451.     = XtCreateManagedWidget("printAllButton",
  452.                 commandWidgetClass, topPane,
  453.                 0,0);
  454.   printMarkedButton
  455.     = XtCreateManagedWidget("printMarkedButton",
  456.                 commandWidgetClass, topPane,
  457.                 0,0);
  458.   printUnmarkedButton
  459.     = XtCreateManagedWidget("printUnmarkedButton",
  460.                 commandWidgetClass, topPane,
  461.                 0,0);
  462.   
  463.   clearMarksButton
  464.     = XtCreateManagedWidget("clearMarksButton",
  465.                 commandWidgetClass, topPane,
  466.                 0,0);
  467.   disablePageLevel();
  468.   
  469.   args = 0;
  470.   XtSetArg(argList[args], XtNlabel, TeXInputDirNameString); args++;
  471.   XtSetArg(argList[args], XtNresize, True); args++;
  472.   directoryLabel = 
  473.     XtCreateManagedWidget("directoryLabel",
  474.               labelWidgetClass, topPane,
  475.               argList, args);
  476.   
  477.   args = 0;
  478.   XtSetArg(argList[args], XtNlabel, TeXInputFileNameString); args++;
  479.   XtSetArg(argList[args], XtNresize, True); args++;
  480.   fileLabel = 
  481.     XtCreateManagedWidget("fileLabel",
  482.               labelWidgetClass, topPane,
  483.               argList, args);
  484. }
  485.  
  486. #ifdef SELFILE
  487. static int
  488.   isDviFileName (fileName, retFileName, statbuf)
  489. char *fileName;
  490. char **retFileName;
  491. struct stat *statbuf;
  492. {
  493.     int extIdx;
  494.  
  495.   *retFileName = fileName;
  496.   extIdx = strlen (fileName) - 4;
  497.   return (S_ISDIR (statbuf->st_mode) || 
  498.      ((extIdx > 0) && !strcmp (fileName + extIdx, ".dvi")));
  499. }
  500. #endif
  501.