home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Appls / dpv / dpvcontrol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  5.0 KB  |  303 lines  |  [TEXT/????]

  1. /* dpv -- ditroff previewer.  User interface, controlling the rest. */
  2.  
  3. #include "dpv.h"
  4. #include "dpvmachine.h"
  5. #include "dpvoutput.h"
  6. #include "dpvsite.h"
  7.  
  8. #if defined(unix) || defined(_AIX)
  9. #define DO_PRINTMENU
  10. #endif
  11.  
  12. char *devname;
  13.  
  14. preview(file, firstpage)
  15.     char *file;
  16.     int firstpage;
  17. {
  18.     initialize(file, firstpage);
  19.     addmenus();
  20.     eventloop(file);
  21.     cleanup();
  22.     wdone();
  23. }
  24.  
  25. MENU *mainmmenu;
  26.  
  27. /* Menu IDs */
  28. #define MAINMENU    1
  29. #define PRINTMENU    2
  30.  
  31. /* Control menu items */
  32. #define FIRSTPAGE    0
  33. #define PREVPAGE    1
  34. #define NEXTPAGE    2
  35. #define LASTPAGE    3
  36. /* --- */
  37. #define GOTOPAGE    5
  38. /* --- */
  39. #define QUIT        7
  40.  
  41. #ifdef DO_PRINTMENU
  42.  
  43. MENU *printmenu;
  44.  
  45. #define MAXNPRINT 50    /* Max # items in print menu */
  46.  
  47. struct _printitem {
  48.     char *text;    /* Menu item text */
  49.     char *device;    /* Required device, or NULL if N/A */
  50.     char *command;    /* Shell command to execute */
  51. } printitems[MAXNPRINT]= {
  52.     /* The following is defined in "dpvsite.h" */
  53.     PRINT_MENU_TABLE
  54. };
  55.  
  56. int nprint;
  57.  
  58. int
  59. countprintmenu()
  60. {
  61.     while (nprint < MAXNPRINT && printitems[nprint].text != NULL)
  62.         nprint++;
  63. }
  64.  
  65. int
  66. addprinter(name)
  67.     char *name;
  68. {
  69.     char buf[100];
  70.     countprintmenu();
  71.     if (nprint >= MAXNPRINT) {
  72.         error(WARNING, "too many printer definitions, rest igonred");
  73.         return;
  74.     }
  75.     sprintf(buf, "Print on %s", name);
  76.     printitems[nprint].text = strdup(buf);
  77.     printitems[nprint].device = NULL; /* Unspecified */
  78.     sprintf(buf, "lpr -P%s -n %%s", name);
  79.     printitems[nprint].command = strdup(buf);
  80.     nprint++;
  81. }
  82.  
  83. #endif
  84.  
  85. addmenus()
  86. {
  87.     MENU *mp;
  88.     int i;
  89.     
  90.     mainmmenu= mp= wmenucreate(MAINMENU, "Command");
  91.     
  92.     wmenuadditem(mp, "First page", 'F');
  93.     wmenuadditem(mp, "Previous page", 'P');
  94.     wmenuadditem(mp, "Next page", 'N');
  95.     wmenuadditem(mp, "Last page", 'L');
  96.     wmenuadditem(mp, "", -1);
  97.     wmenuadditem(mp, "Go to page...", 'G');
  98.     wmenuadditem(mp, "", -1);
  99.     wmenuadditem(mp, "Quit", 'Q');
  100.     
  101. #ifdef DO_PRINTMENU
  102.     countprintmenu();
  103.     printmenu= mp= wmenucreate(PRINTMENU, "Print");
  104.     for (i= 0; i < nprint; ++i) {
  105.         wmenuadditem(mp, printitems[i].text, -1);
  106.         if (!canprint(i))
  107.             wmenuenable(mp, i, FALSE);
  108.     }
  109. #endif
  110. }
  111.  
  112. eventloop(filename)
  113.     char *filename;
  114. {
  115.     int num= -1;
  116.     for (;;) {
  117.         EVENT e;
  118.         int lastnum= num;
  119.         wgetevent(&e);
  120.         num= -1;
  121.         switch(e.type) {
  122.         
  123.         case WE_MENU:
  124.             switch (e.u.m.id) {
  125.             case MAINMENU:
  126.                 if (e.u.m.item == QUIT)
  127.                     return;
  128.                 do_mainmenu(e.u.m.item);
  129.                 break;
  130.             case PRINTMENU:
  131.                 do_printmenu(filename, e.u.m.item);
  132.                 break;
  133.             }
  134.             break;
  135.         
  136.         case WE_CHAR:
  137.             /* The mnemonics used here may remind you of
  138.                the main menu's shortcuts, the 'vi' editor,
  139.                the 'more' pages, or the 'rn' news reader... */
  140.             switch (e.u.character) {
  141.             case 'q':
  142.             case 'Q':
  143.                 return;
  144.             case ' ':
  145.             case '+':
  146.             case 'n':
  147.             case 'N':
  148.                 forwpage(lastnum);
  149.                 break;
  150.             case '-':
  151.                 if (lastnum > 0)
  152.                     backpage(lastnum);
  153.                 else
  154.                     gotopage(prevpage);
  155.                 break;
  156.             case 'b':
  157.             case 'B':
  158.             case 'p':
  159.             case 'P':
  160.                 backpage(lastnum);
  161.                 break;
  162.             case '^':
  163.             case 'f':
  164.             case 'F':
  165.                 gotopage(1);
  166.                 break;
  167.             case '$':
  168.                 gotopage(32000);
  169.                 break;
  170.             case 'g':
  171.             case 'G':
  172.             case 'l':
  173.             case 'L':
  174.                 if (lastnum > 0)
  175.                     gotopage(lastnum);
  176.                 else
  177.                     gotopage(32000);
  178.                 break;
  179.             case '.':
  180.                 if (lastnum > 0)
  181.                     gotopage(lastnum);
  182.                 else
  183.                     changeall(); /* Force a redraw */
  184.                 break;
  185.             default:
  186.                 if (isdigit(e.u.character)) {
  187.                     num= e.u.character - '0';
  188.                     if (lastnum > 0)
  189.                         num += 10*lastnum;
  190.                 }
  191.                 else {
  192.                     wfleep();
  193.                     lastnum= 0;
  194.                 }
  195.             }
  196.             break;
  197.         
  198.         case WE_CLOSE:
  199.             return;
  200.  
  201.         case WE_COMMAND:
  202.             switch (e.u.command) {
  203.             case WC_RETURN:
  204.                 if (lastnum > 0)
  205.                     gotopage(lastnum);
  206.                 else
  207.                     forwpage(1);
  208.                 break;
  209.             case WC_DOWN:
  210.                 forwpage(lastnum);
  211.                 break;
  212.             case WC_UP:
  213.             case WC_BACKSPACE:
  214.                 backpage(lastnum);
  215.                 break;
  216.             case WC_CLOSE:
  217.             /*
  218.             case WC_CANCEL:
  219.             */
  220.                 return;
  221.             default:
  222.                 wfleep();
  223.             }
  224.             break;
  225.         
  226.         }
  227.     }
  228. }
  229.  
  230. do_mainmenu(item)
  231.     int item;
  232. {
  233.     switch (item) {
  234.     case FIRSTPAGE:
  235.         gotopage(1);
  236.         break;
  237.     case PREVPAGE:
  238.         backpage(1);
  239.         break;
  240.     case NEXTPAGE:
  241.         forwpage(1);
  242.         break;
  243.     case LASTPAGE:
  244.         gotopage(32000);
  245.         break;
  246.     case GOTOPAGE:
  247.         {
  248.             static char buf[10];
  249.             int num;
  250.             char extra;
  251.             if (!waskstr("Go to page:", buf, sizeof buf)
  252.                     || buf[0] == '\0')
  253.                 return;
  254.             if (sscanf(buf, " %d %c", &num, &extra) != 1 ||
  255.                 num <= 0) {
  256.                 wmessage("Invalid page number");
  257.                 return;
  258.             }
  259.             gotopage(num);
  260.         }
  261.         break;
  262.     }
  263. }
  264.  
  265. do_printmenu(filename, item)
  266.     char *filename;
  267.     int item;
  268. {
  269. #ifdef DO_PRINTMENU
  270.     char buf[256];
  271.     int sts;
  272.     
  273.     if (item < 0 || item >= nprint)
  274.         return;
  275.     
  276.     if (!canprint(item)) {
  277.         sprintf(buf, "Can't convert %s output to %s",
  278.             devname == NULL ? "unspecified" : devname,
  279.             printitems[item].device);
  280.         wmessage(buf);
  281.         return;
  282.     }
  283.     
  284.     sprintf(buf, printitems[item].command, filename);
  285.     sts= system(buf);
  286.     if (sts != 0) {
  287.         sprintf(buf, "Print command failed, exit status 0x%x", sts);
  288.         wmessage(buf);
  289.     }
  290. #endif
  291. }
  292.  
  293. #ifdef DO_PRINTMENU
  294. static bool
  295. canprint(item)
  296.     int item;
  297. {
  298.     return printitems[item].device == NULL ||
  299.         devname != NULL &&
  300.             strcmp(printitems[item].device, devname) == 0;
  301. }
  302. #endif
  303.