home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / texview / config.m < prev    next >
Encoding:
Text File  |  1993-01-19  |  7.2 KB  |  250 lines

  1. /*
  2.  *   Reads and writes preferences to a file.
  3.  */
  4. #import <defaults/defaults.h>
  5. #import "TVPrint.h"
  6. #import <appkit/Window.h>
  7. #import "PageView.h"
  8. #import "ComScroll.h"
  9. #import <appkit/Text.h>
  10. #import <appkit/Font.h>
  11. /*
  12.  *   These are the variables we save.
  13.  */
  14. extern PageView *myPageView ;
  15. extern real defhsize, defvsize, defhmarg, defvmarg ;
  16. extern Boolean generatefonts, mocked ;
  17. extern int debugon, paranoia ;
  18. extern real userdpi[] ;
  19. extern char defaultfont[] ;
  20. extern char LastFormat[], CustomFormat[] ;
  21. extern real possibmag[] ;
  22. extern int strcmp() ;
  23. extern long atol() ;
  24. extern void upmock() ;
  25. extern int getuid() ;
  26. extern char *strcpy(), *strcat() ;
  27. extern struct printVals printVals ;
  28. struct windowStatus {
  29.    NXRect r ;
  30.    integer onscreen ;
  31. } ;
  32. static struct windowStatus windowRect =
  33.     { { { -10.0, -10.0 }, { -10.0, -10.0 }}, 1} ;
  34. static struct windowStatus consoleRect =
  35.     { { { -10.0, -10.0 }, { -10.0, -10.0 }}, 0} ;
  36. static struct windowStatus texRect =
  37.     { { { -10.0, -10.0 }, { -10.0, -10.0 }}, 0} ;
  38. static struct windowStatus commandRect =
  39.     { { { -10.0, -10.0 }, { -10.0, -10.0 }}, 1} ;
  40. struct prefs {
  41.    char *keyword ;
  42.    char *type ;
  43.    void *addr ;
  44. } prefs[] = {
  45.    { "hsize", "real", &defhsize } ,
  46.    { "vsize", "real", &defvsize } ,
  47.    { "hmarg", "real", &defhmarg } ,
  48.    { "vmarg", "real", &defvmarg } ,
  49.    { "generatefonts", "boolean", &generatefonts } ,
  50.    { "mocked", "boolean", &mocked } ,
  51.    { "unzoomres", "real", userdpi } ,
  52.    { "zoomres", "real", userdpi + 1 } ,
  53.    { "customdpi", "integer", &printVals.customdpi } ,
  54.    { "reversed", "boolean", &printVals.reversed },
  55.    { "compressed", "boolean", &printVals.compressed },
  56.    { "sync", "boolean", &printVals.sync },
  57.    { "windowposition", "window", &(windowRect) },
  58.    { "consoleposition", "window", &(consoleRect) },
  59.    { "texposition", "window", &(texRect) },
  60.    { "commandposition", "window", &(commandRect) },
  61.    { "defaultfont", "string", defaultfont },
  62.    { "defaultformat", "string", LastFormat },
  63.    { "customformat", "string", CustomFormat },
  64.    { "usecustom", "boolean", &printVals.customchosen },
  65.    { NULL, NULL, NULL }
  66. } ;
  67.  
  68. static NXDefaultsVector myDefaults = {
  69.    { "hsize", "8.5" },
  70.    { "vsize", "11" },
  71.    { "hmarg", "1" },
  72.    { "vmarg", "1" },
  73.    { "generatefonts", "false" },
  74.    { "mocked", "false" },
  75.    { "unzoomres", "91.287102" },
  76.    { "zoomres", "120" },
  77.    { "customdpi", "1270" },
  78.    { "reversed", "true" },
  79.    { "compressed", "false" },
  80.    { "sync", "true" },
  81.    { "windowposition", "" },
  82.    { "consoleposition", "" },
  83.    { "texposition", "" },
  84.    { "commandposition", "" },
  85.    { "defaultfont", "" },
  86.    { "defaultformat", "tex" },
  87.    { "customformat", "amstex" },
  88.    { "usecustom", "false" },
  89.    { NULL, NULL }
  90. } ;
  91.  
  92. extern void *error() ;
  93. extern char temperrorbuf[] ;
  94.  
  95. void loadconfig() {
  96.    register struct prefs *p ;
  97.    register int i, j ;
  98.    register real r, b, off, boff ;
  99.    const char *val ;
  100.    struct windowStatus *w ;
  101.  
  102.    NXRegisterDefaults("TeXview", myDefaults) ;
  103.    for (p=prefs; p->keyword; p++) {
  104.       val = NXGetDefaultValue("TeXview", p->keyword) ;
  105.       if (val)
  106.          switch (p->type[0]) {
  107. case 'r':
  108.             *(real *)(p->addr) = atof(val) ;
  109.             break ;
  110. case 'b':
  111.             if (*val == 't' || *val == 'T' || *val == '1')
  112.                *(Boolean *)(p->addr) = 1 ;
  113.             else
  114.                *(Boolean *)(p->addr) = 0 ;
  115.             break ;
  116. case 's':
  117.             strcpy((char *)p->addr, val); 
  118.             break ;
  119. case 'i':
  120.             *(integer *)(p->addr) = atol(val) ;
  121.             break ;
  122. case 'w':
  123.             w = (struct windowStatus *)(p->addr) ;
  124.             if (sscanf(val, "%f:%f:%f:%f:%ld", &(w->r.origin.x),
  125.     &(w->r.origin.y), &(w->r.size.width), &(w->r.size.height),
  126.     &(w->onscreen)) < 4)
  127.                w->r.size.width = -10 ;
  128.             break ;
  129.          }
  130.    }
  131.    if (val=NXGetDefaultValue("TeXview", "debug"))
  132.       debugon = *val - '0' ;
  133. /* if (val=NXGetDefaultValue("TeXview", "paranoia"))
  134.       paranoia = *val - '0' ; */
  135. /*
  136.  *   Now we check userdpi[] values for validity.
  137.  */
  138.    for (i=0; i<2; i++) {
  139.       r = userdpi[i] ;
  140.       if (r < 69.0)
  141.          r = 69.0 ;
  142.       else if (r > 1000.0)
  143.          r = 189.0 ;
  144.       b = 100.0 ;
  145.       boff = 99.9 ;
  146.       for (j = 17; j < 31; j++) {
  147.          off = possibmag[j] - r ;
  148.          if (off < 0)
  149.             off = - off ;
  150.          if (off < boff) {
  151.             b = possibmag[j] ;
  152.             boff = off ;
  153.          }
  154.       }
  155.       userdpi[i] = b ;
  156.    }
  157. /*
  158.  *   We load up the system defaults, if such exist.  We only
  159.  *   are concerned with Printer and PrinterResolution.
  160.  *
  161.  *   Actually, we let the PrintPanel code do this.
  162.  */
  163. /* if (val = NXGetDefaultValue("System", "Printer"))
  164.       strcpy(printVals.printername, val) ;
  165.    if (val = NXGetDefaultValue("System", "PrinterResolution")) {
  166.       if (strcmp(val, "400")==0) {
  167.          printVals.seldpi = 1 ;
  168.       } else if (strcmp(val, "300")==0) {
  169.          printVals.seldpi = 0 ;
  170.       } else {
  171.          printVals.seldpi = 2 ;
  172.          printVals.customdpi = atol(val) ;
  173.       }
  174.    } */
  175.    upmock() ;
  176. }
  177.  
  178. void configure() {
  179.    static virgin = 1 ;
  180.    if (virgin) {
  181.       loadconfig() ;
  182.       virgin = 0 ;
  183.    }
  184. }
  185.  
  186. static int lastwason ;
  187. NXRect *initsizewindow(int which) {
  188.    static char gotitonce[4] = { 0, 0, 0, 0 } ;
  189.    struct windowStatus *w = &windowRect ;
  190.  
  191.    switch (which) {
  192.       case 0: w = &windowRect ; break ;
  193.       case 1: w = &consoleRect ; break ;
  194.       case 2: w = &texRect ; break ;
  195.       case 3: w = &commandRect ; break ;
  196.       default: which = 0 ; break ;
  197.    }
  198.    configure() ;
  199.    if (w->r.size.width > 10.0 && gotitonce[which] == 0) {
  200.       gotitonce[which] = 1 ;
  201.       lastwason = w->onscreen ;
  202.       return &(w->r) ;
  203.    }
  204.    return 0 ;
  205. }
  206.  
  207. int initWasOnScreen(void) {
  208.    return lastwason ;
  209. }
  210.  
  211. void saveconfig() {
  212.    register struct prefs *p ;
  213.    int i ;
  214.    char val[100] ;
  215.    struct windowStatus *w ;
  216.    id tw = [myPageView getTeX] ;
  217.  
  218.    [[myPageView window] getFrame:&windowRect.r] ;
  219.    windowRect.onscreen = [[myPageView window] isVisible] ;
  220.    [[(ComScroll *)[myPageView getConsole] window] getFrame:&consoleRect.r] ;
  221.    consoleRect.onscreen = [[[myPageView getConsole] window] isVisible] ;
  222.    [[tw window] getFrame:&texRect.r] ;
  223.    texRect.onscreen = [[tw window] isVisible] ;
  224.    [(Window *)[myPageView getCommand] getFrame:&commandRect.r] ;
  225.    commandRect.onscreen = [[myPageView getCommand] isVisible] ;
  226.    [[(ComScroll *)[myPageView getTeX] window] getFrame:&texRect.r] ;
  227.    sprintf(defaultfont, "%s:%g",
  228.                      [[[tw text] font] name], [[[tw text] font] pointSize]) ;
  229.    for (p = prefs, i=0; p->keyword; p++, i++) {
  230.       switch (p->type[0]) {
  231. case 'r' : sprintf(val, "%g", *(real *)p->addr) ; break ;
  232. case 'b' : sprintf(val, "%s", (*(Boolean *)(p->addr) ? "true" : "false")) ; break ;
  233. case 's' : if (*(char *)(p->addr))
  234.                     strcpy(val, (char *)p->addr) ; else val[0] = 0 ; break ;
  235. case 'i' : sprintf(val, "%ld", *(integer *)p->addr) ; break ;
  236. case 'w' : w = (struct windowStatus *)(p->addr) ;
  237.            sprintf(val, "%g:%g:%g:%g:%ld", w->r.origin.x, w->r.origin.y, 
  238.        w->r.size.width, w->r.size.height, w->onscreen) ;
  239.            break ;
  240.       }
  241. /*
  242.  *   Only keep non-default defaults around.
  243.  */
  244.       if (strcmp(val, myDefaults[i].value) == 0)
  245.          NXRemoveDefault("TeXview", p->keyword) ;
  246.       else
  247.          NXWriteDefault("TeXview", p->keyword, val) ;
  248.    }
  249. }
  250.