home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part12 / w_print.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-27  |  14.4 KB  |  497 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1991 by Brian V. Smith
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "figx.h"
  15. #include "mode.h"
  16. #include "resources.h"
  17. #include "w_icons.h"
  18. #include "w_setup.h"
  19. #include "w_util.h"
  20.  
  21. extern String    text_translations;
  22. extern Widget    make_popup_menu();
  23. extern char    *panel_get_value();
  24. extern char     batch_file[];
  25. extern Boolean  batch_exists;
  26. extern char    *shell_protect_string();
  27.  
  28. /* LOCAL */
  29.  
  30. static char    *orient_items[] = {
  31.     "portrait ",
  32.     "landscape"};
  33.  
  34. static char    *just_items[] = {
  35.     "Centered  ",
  36.     "Flush left"};
  37.  
  38. static void    orient_select();
  39. static Widget    orient_panel, orient_menu, orient_lab;
  40.  
  41. static void    just_select();
  42. static Widget    just_panel, just_menu, just_lab;
  43.  
  44. static Widget    print_panel, print_popup, dismiss, print, 
  45.         printer_text, param_text, printer_lab, param_lab, clear_batch, print_batch, 
  46.         mag_lab, print_w, mag_text, num_batch_lab, num_batch;
  47. static Position xposn, yposn;
  48. static String   prin_translations =
  49.         "<Message>WM_PROTOCOLS: DismissPrin()\n";
  50. static void     print_panel_dismiss(), do_clear_batch();
  51. void        do_print(), do_print_batch();
  52. static XtActionsRec     prin_actions[] =
  53. {
  54.     {"DismissPrin", (XtActionProc) print_panel_dismiss},
  55.     {"dismiss", (XtActionProc) print_panel_dismiss},
  56.     {"print_batch", (XtActionProc) do_print_batch},
  57.     {"clear_batch", (XtActionProc) do_clear_batch},
  58.     {"print", (XtActionProc) do_print},
  59. };
  60.  
  61. static void
  62. print_panel_dismiss(w, ev)
  63.     Widget        w;
  64.     XButtonEvent   *ev;
  65. {
  66.     XtPopdown(print_popup);
  67.     XtSetSensitive(print_w, True);
  68. }
  69.  
  70. static char    print_msg[] = "PRINT";
  71.  
  72. void
  73. do_print(w)
  74.     Widget        w;
  75. {
  76.     DeclareArgs(1);
  77.     float        mag;
  78.     char       *printer_val;
  79.     char       *param_val;
  80.     char        cmd[100];
  81.  
  82.     if (emptyfigure_msg(print_msg) && !batch_exists)
  83.         return;
  84.  
  85.     /* create popup panel if not already there so we have all the
  86.        resources necessary (e.g. printer name etc.) */
  87.     if (!print_popup) 
  88.         create_print_panel(w);
  89.     mag = (float) atof(panel_get_value(mag_text)) / 100.0;
  90.     if (mag <= 0.0)
  91.         mag = 1.0;
  92.  
  93.     FirstArg(XtNstring, &printer_val);
  94.     GetValues(printer_text);
  95.     /* no printer name specified in resources, get PRINTER environment
  96.        var and put it into the widget */
  97.     if (emptyname(printer_val)) {
  98.         printer_val=getenv("PRINTER");
  99.         FirstArg(XtNstring, printer_val);
  100.         SetValues(printer_text);
  101.     }
  102.     FirstArg(XtNstring, ¶m_val);
  103.     GetValues(param_text);
  104.     if (batch_exists)
  105.         {
  106.         gen_print_cmd(cmd,batch_file,printer_val,param_val);
  107.         if (system(cmd) != 0)
  108.         file_msg("Error during PRINT");
  109.         /* clear the batch file and the count */
  110.         do_clear_batch(w);
  111.         }
  112.     else
  113.         {
  114.         print_to_printer(printer_val, mag, print_flushleft, param_val);
  115.         }
  116. }
  117.  
  118. gen_print_cmd(cmd,file,printer,pr_params)
  119.     char       *cmd;
  120.     char       *file;
  121.     char       *printer;
  122.     char       *pr_params;
  123. {
  124.     if (emptyname(printer)) {    /* send to default printer */
  125. #if defined(SYSV) || defined(SVR4)
  126.     sprintf(cmd, "lp %s -oPS %s", 
  127.         pr_params,
  128.         shell_protect_string(file));
  129. #else
  130.     sprintf(cmd, "lpr %s -J %s %s",
  131.         pr_params,
  132.         shell_protect_string(file),
  133.         shell_protect_string(file));
  134. #endif
  135.     put_msg("Printing figure on default printer in %s mode ...     ",
  136.         print_landscape ? "LANDSCAPE" : "PORTRAIT");
  137.     } else {
  138. #if defined(SYSV) || defined(SVR4)
  139.     sprintf(cmd, "lp %s, -d%s -oPS %s",
  140.         pr_params,
  141.         printer, 
  142.         shell_protect_string(file));
  143. #else
  144.     sprintf(cmd, "lpr %s -J %s -P%s %s", 
  145.         pr_params,
  146.         shell_protect_string(file),
  147.         printer,
  148.         shell_protect_string(file));
  149. #endif
  150.     put_msg("Printing figure on printer %s in %s mode ...     ",
  151.         printer, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  152.     }
  153.     app_flush();        /* make sure message gets displayed */
  154. }
  155.  
  156. static int num_batch_figures=0;
  157. static Boolean writing_batch=False;
  158.  
  159. void
  160. do_print_batch(w)
  161.     Widget        w;
  162. {
  163.     DeclareArgs(1);
  164.     float        mag;
  165.     FILE       *infp,*outfp;
  166.     char        tmp_exp_file[32];
  167.     char        str[255];
  168.  
  169.     if (writing_batch || emptyfigure_msg(print_msg))
  170.         return;
  171.  
  172.     /* set lock so we don't come here while still writing a file */
  173.     /* this could happen if the user presses the button too fast */
  174.     writing_batch = True;
  175.  
  176.     /* make a temporary name to write the batch stuff to */
  177.     sprintf(batch_file, "%s/%s%06d", TMPDIR, "xfig-batch", getpid());
  178.     /* make a temporary name to write this figure to */
  179.     sprintf(tmp_exp_file, "%s/%s%06d", TMPDIR, "xfig-exp", getpid());
  180.     batch_exists = True;
  181.     if (!print_popup) 
  182.         create_print_panel(w);
  183.     mag = (float) atof(panel_get_value(mag_text)) / 100.0;
  184.     if (mag <= 0.0)
  185.         mag = 1.0;
  186.  
  187.     print_to_file(tmp_exp_file, "ps", mag, print_flushleft);
  188.     put_msg("Appending to batch file \"%s\" (%s mode) ... done",
  189.             batch_file, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  190.     app_flush();        /* make sure message gets displayed */
  191.  
  192.     /* now append that to the batch file */
  193.     if ((infp = fopen(tmp_exp_file, "r")) == NULL) {
  194.         file_msg("Error during PRINT - can't open temporary file to read");
  195.         return;
  196.         }
  197.     if ((outfp = fopen(batch_file, "a")) == NULL) {
  198.         file_msg("Error during PRINT - can't open print file to append");
  199.         return;
  200.         }
  201.     while (fgets(str,255,infp) != NULL)
  202.         (void) fputs(str,outfp);
  203.     fclose(infp);
  204.     fclose(outfp);
  205.     unlink(tmp_exp_file);
  206.     /* count this batch figure */
  207.     num_batch_figures++ ;
  208.     /* and update the label widget */
  209.     update_batch_count();
  210.     /* we're done */
  211.     writing_batch = False;
  212. }
  213.  
  214. static void
  215. do_clear_batch(w)
  216.     Widget        w;
  217. {
  218.     DeclareArgs(1);
  219.   
  220.     unlink(batch_file);
  221.     batch_exists = False;
  222.     num_batch_figures = 0;
  223.     /* update the label widget */
  224.     update_batch_count();
  225. }
  226.  
  227. /* update the label widget with the current number of figures in the batch file */
  228.  
  229. update_batch_count()
  230. {
  231.     String        num[4];
  232.     DeclareArgs(1);
  233.  
  234.     sprintf(num,"%3d",num_batch_figures);
  235.     FirstArg(XtNlabel,num);
  236.     SetValues(num_batch);
  237.     if (num_batch_figures) {
  238.         XtSetSensitive(clear_batch, True);
  239.         FirstArg(XtNlabel, "Print BATCH \nto Printer");
  240.         SetValues(print);
  241.     } else {
  242.         XtSetSensitive(clear_batch, False);
  243.         FirstArg(XtNlabel, "Print FIGURE\nto Printer");
  244.         SetValues(print);
  245.     }
  246. }
  247.  
  248. static void
  249. orient_select(w, new_orient, garbage)
  250.     Widget        w;
  251.     XtPointer        new_orient, garbage;
  252. {
  253.     DeclareArgs(1);
  254.  
  255.     FirstArg(XtNlabel, XtName(w));
  256.     SetValues(orient_panel);
  257.     print_landscape = (int) new_orient;
  258. }
  259.  
  260. static void
  261. just_select(w, new_just, garbage)
  262.     Widget        w;
  263.     XtPointer        new_just, garbage;
  264. {
  265.     DeclareArgs(1);
  266.  
  267.     FirstArg(XtNlabel, XtName(w));
  268.     SetValues(just_panel);
  269.     print_flushleft = (new_just? True: False);
  270. }
  271.  
  272. popup_print_panel(w)
  273.     Widget        w;
  274. {
  275.     Widget        image;
  276.     Pixmap        p;
  277.     DeclareArgs(10);
  278.     extern        Atom wm_delete_window;
  279.  
  280.     set_temp_cursor(wait_cursor);
  281.     XtSetSensitive(w, False);
  282.     if (!print_popup)
  283.     create_print_panel(w);
  284.     XtPopup(print_popup, XtGrabNone);
  285.     (void) XSetWMProtocols(XtDisplay(print_popup), XtWindow(print_popup),
  286.                            &wm_delete_window, 1);
  287.     reset_cursor();
  288.  
  289. }
  290.  
  291. create_print_panel(w)
  292.     Widget        w;
  293. {
  294.     Widget        image;
  295.     Pixmap        p;
  296.     DeclareArgs(10);
  297.  
  298.     print_w = w;
  299.     XtTranslateCoords(w, (Position) 0, (Position) 0, &xposn, &yposn);
  300.  
  301.     FirstArg(XtNx, xposn);
  302.     NextArg(XtNy, yposn + 50);
  303.     NextArg(XtNtitle, "Xfig: Print menu");
  304.     print_popup = XtCreatePopupShell("xfig_print_menu",
  305.                      transientShellWidgetClass,
  306.                      tool, Args, ArgCount);
  307.         XtOverrideTranslations(print_popup,
  308.                            XtParseTranslationTable(prin_translations));
  309.         XtAppAddActions(tool_app, prin_actions, XtNumber(prin_actions));
  310.  
  311.     print_panel = XtCreateManagedWidget("print_panel", formWidgetClass,
  312.                         print_popup, NULL, ZERO);
  313.  
  314.     p = XCreateBitmapFromData(tool_d, XtWindow(canvas_sw),
  315.               (char *) printer_ic.data, printer_ic.width, printer_ic.height);
  316.  
  317.     FirstArg(XtNlabel, "   ");
  318.     NextArg(XtNwidth, printer_ic.width);
  319.     NextArg(XtNheight, printer_ic.height);
  320.     NextArg(XtNbitmap, p);
  321.     NextArg(XtNborderWidth, 0);
  322.     NextArg(XtNinternalWidth, 0);
  323.     NextArg(XtNinternalHeight, 0);
  324.     NextArg(XtNresize, False);
  325.     NextArg(XtNresizable, False);
  326.     image = XtCreateManagedWidget("printer_image", labelWidgetClass,
  327.                       print_panel, Args, ArgCount);
  328.  
  329.     FirstArg(XtNlabel, "  Magnification%:");
  330.     NextArg(XtNfromVert, image);
  331.     NextArg(XtNjustify, XtJustifyLeft);
  332.     NextArg(XtNborderWidth, 0);
  333.     mag_lab = XtCreateManagedWidget("mag_label", labelWidgetClass,
  334.                     print_panel, Args, ArgCount);
  335.  
  336.     FirstArg(XtNwidth, 40);
  337.     NextArg(XtNfromVert, image);
  338.     NextArg(XtNfromHoriz, mag_lab);
  339.     NextArg(XtNeditType, "edit");
  340.     NextArg(XtNstring, "100");
  341.     NextArg(XtNinsertPosition, 3);
  342.     NextArg(XtNborderWidth, INTERNAL_BW);
  343.     mag_text = XtCreateManagedWidget("magnification", asciiTextWidgetClass,
  344.                      print_panel, Args, ArgCount);
  345.     XtOverrideTranslations(mag_text,
  346.                    XtParseTranslationTable(text_translations));
  347.  
  348.     FirstArg(XtNlabel, "     Orientation:");
  349.     NextArg(XtNjustify, XtJustifyLeft);
  350.     NextArg(XtNborderWidth, 0);
  351.     NextArg(XtNfromVert, mag_text);
  352.     orient_lab = XtCreateManagedWidget("orient_label", labelWidgetClass,
  353.                        print_panel, Args, ArgCount);
  354.  
  355.     FirstArg(XtNfromHoriz, orient_lab);
  356.     NextArg(XtNfromVert, mag_text);
  357.     NextArg(XtNborderWidth, INTERNAL_BW);
  358.     orient_panel = XtCreateManagedWidget(orient_items[print_landscape],
  359.                          menuButtonWidgetClass,
  360.                          print_panel, Args, ArgCount);
  361.     orient_menu = make_popup_menu(orient_items, XtNumber(orient_items),
  362.                       orient_panel, orient_select);
  363.  
  364.     FirstArg(XtNlabel, "   Justification:");
  365.     NextArg(XtNjustify, XtJustifyLeft);
  366.     NextArg(XtNborderWidth, 0);
  367.     NextArg(XtNfromVert, orient_panel);
  368.     just_lab = XtCreateManagedWidget("just_label", labelWidgetClass,
  369.                      print_panel, Args, ArgCount);
  370.  
  371.     FirstArg(XtNlabel, just_items[print_flushleft? 1 : 0]);
  372.     NextArg(XtNfromHoriz, just_lab);
  373.     NextArg(XtNfromVert, orient_panel);
  374.     NextArg(XtNborderWidth, INTERNAL_BW);
  375.     NextArg(XtNresizable, True);
  376.     just_panel = XtCreateManagedWidget("justify",
  377.                        menuButtonWidgetClass,
  378.                        print_panel, Args, ArgCount);
  379.     just_menu = make_popup_menu(just_items, XtNumber(just_items),
  380.                     just_panel, just_select);
  381.  
  382.  
  383.     FirstArg(XtNlabel, "         Printer:");
  384.     NextArg(XtNfromVert, just_panel);
  385.     NextArg(XtNjustify, XtJustifyLeft);
  386.     NextArg(XtNborderWidth, 0);
  387.     printer_lab = XtCreateManagedWidget("printer_label", labelWidgetClass,
  388.                         print_panel, Args, ArgCount);
  389.     /*
  390.      * don't SetValue the XtNstring so the user may specify the default
  391.      * printer in a resource, e.g.:     *printer*string: at6
  392.      */
  393.  
  394.     FirstArg(XtNwidth, 100);
  395.     NextArg(XtNfromVert, just_panel);
  396.     NextArg(XtNfromHoriz, printer_lab);
  397.     NextArg(XtNeditType, "edit");
  398.     NextArg(XtNinsertPosition, 0);
  399.     NextArg(XtNborderWidth, INTERNAL_BW);
  400.     printer_text = XtCreateManagedWidget("printer", asciiTextWidgetClass,
  401.                          print_panel, Args, ArgCount);
  402.  
  403.     XtOverrideTranslations(printer_text,
  404.                    XtParseTranslationTable(text_translations));
  405.  
  406.     FirstArg(XtNlabel, "Print Job Params:");
  407.     NextArg(XtNfromVert, printer_text);
  408.     NextArg(XtNjustify, XtJustifyLeft);
  409.     NextArg(XtNborderWidth, 0);
  410.     param_lab = XtCreateManagedWidget("job_params_label", labelWidgetClass,
  411.                         print_panel, Args, ArgCount);
  412.     /*
  413.      * don't SetValue the XtNstring so the user may specify the default
  414.      * job parameters in a resource, e.g.:     *param*string: -K2
  415.      */
  416.  
  417.     FirstArg(XtNwidth, 100);
  418.     NextArg(XtNfromVert, printer_text);
  419.     NextArg(XtNfromHoriz, param_lab);
  420.     NextArg(XtNeditType, "edit");
  421.     NextArg(XtNinsertPosition, 0);
  422.     NextArg(XtNborderWidth, INTERNAL_BW);
  423.     param_text = XtCreateManagedWidget("job_params", asciiTextWidgetClass,
  424.                          print_panel, Args, ArgCount);
  425.  
  426.     XtOverrideTranslations(param_text,
  427.                    XtParseTranslationTable(text_translations));
  428.  
  429.     FirstArg(XtNlabel, "Figures in batch:");
  430.     NextArg(XtNfromVert, param_text);
  431.     NextArg(XtNjustify, XtJustifyLeft);
  432.     NextArg(XtNborderWidth, 0);
  433.     num_batch_lab = XtCreateManagedWidget("num_batch_label", labelWidgetClass,
  434.                         print_panel, Args, ArgCount);
  435.     FirstArg(XtNwidth, 30);
  436.     NextArg(XtNlabel, "  0");
  437.     NextArg(XtNfromVert, param_text);
  438.     NextArg(XtNfromHoriz, num_batch_lab);
  439.     NextArg(XtNjustify, XtJustifyLeft);
  440.     NextArg(XtNborderWidth, INTERNAL_BW);
  441.     num_batch = XtCreateManagedWidget("num_batch", labelWidgetClass,
  442.                          print_panel, Args, ArgCount);
  443.  
  444.     FirstArg(XtNlabel, "Dismiss");
  445.     NextArg(XtNfromVert, num_batch);
  446.     NextArg(XtNvertDistance, 10);
  447.     NextArg(XtNhorizDistance, 6);
  448.     NextArg(XtNheight, 30);
  449.     NextArg(XtNborderWidth, INTERNAL_BW);
  450.     dismiss = XtCreateManagedWidget("dismiss", commandWidgetClass,
  451.                        print_panel, Args, ArgCount);
  452.     XtAddEventHandler(dismiss, ButtonReleaseMask, (Boolean) 0,
  453.               (XtEventHandler)print_panel_dismiss, (XtPointer) NULL);
  454.  
  455.     FirstArg(XtNlabel, "Print FIGURE\nto Printer");
  456.     NextArg(XtNfromVert, num_batch);
  457.     NextArg(XtNfromHoriz, dismiss);
  458.     NextArg(XtNheight, 30);
  459.     NextArg(XtNborderWidth, INTERNAL_BW);
  460.     NextArg(XtNvertDistance, 10);
  461.     NextArg(XtNhorizDistance, 6);
  462.     print = XtCreateManagedWidget("print", commandWidgetClass,
  463.                       print_panel, Args, ArgCount);
  464.     XtAddEventHandler(print, ButtonReleaseMask, (Boolean) 0,
  465.               (XtEventHandler)do_print, (XtPointer) NULL);
  466.  
  467.     FirstArg(XtNlabel, "Print FIGURE\nto Batch");
  468.     NextArg(XtNfromVert, num_batch);
  469.     NextArg(XtNfromHoriz, print);
  470.     NextArg(XtNheight, 30);
  471.     NextArg(XtNborderWidth, INTERNAL_BW);
  472.     NextArg(XtNvertDistance, 10);
  473.     NextArg(XtNhorizDistance, 6);
  474.     print_batch = XtCreateManagedWidget("print_batch", commandWidgetClass,
  475.                       print_panel, Args, ArgCount);
  476.     XtAddEventHandler(print_batch, ButtonReleaseMask, (Boolean) 0,
  477.               (XtEventHandler)do_print_batch, (XtPointer) NULL);
  478.  
  479.     FirstArg(XtNlabel, "Clear\nBatch");
  480.     NextArg(XtNfromVert, num_batch);
  481.     NextArg(XtNfromHoriz, print_batch);
  482.     NextArg(XtNheight, 30);
  483.     NextArg(XtNborderWidth, INTERNAL_BW);
  484.     NextArg(XtNvertDistance, 10);
  485.     NextArg(XtNhorizDistance, 6);
  486.     clear_batch = XtCreateManagedWidget("clear_batch", commandWidgetClass,
  487.                       print_panel, Args, ArgCount);
  488.     XtAddEventHandler(clear_batch, ButtonReleaseMask, (Boolean) 0,
  489.               (XtEventHandler)do_clear_batch, (XtPointer) NULL);
  490.  
  491.     XtInstallAccelerators(print_panel, dismiss);
  492.     XtInstallAccelerators(print_panel, print_batch);
  493.     XtInstallAccelerators(print_panel, clear_batch);
  494.     XtInstallAccelerators(print_panel, print);
  495.     update_batch_count();
  496. }
  497.