home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / x / xfig.lha / src / x11 / w_export.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  13.4 KB  |  468 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  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_dir.h"
  18. #include "w_drawprim.h"        /* for char_height */
  19. #include "w_setup.h"
  20. #include "w_util.h"
  21.  
  22. extern String    text_translations;
  23. extern Widget    make_popup_menu();
  24. extern char    *panel_get_value();
  25. extern Widget    file_popup;
  26. extern Widget    file_dir;
  27.  
  28. /* global so w_file.c can access it */
  29. char        default_export_file[PATH_MAX];
  30. char        export_dir[PATH_MAX];
  31.  
  32. /* LOCAL */
  33.  
  34. static String    file_name_translations =
  35.     "<Key>Return: ExportFile()\n";
  36. void        do_export();
  37. static XtActionsRec    file_name_actions[] =
  38. {
  39.     {"ExportFile", (XtActionProc) do_export},
  40. };
  41. static String   export_translations =
  42.         "<Message>WM_PROTOCOLS: DismissExport()\n";
  43. static void     export_panel_cancel();
  44. static XtActionsRec     export_actions[] =
  45. {
  46.     {"DismissExport", (XtActionProc) export_panel_cancel},
  47.     {"cancel", (XtActionProc) export_panel_cancel},
  48.     {"export", (XtActionProc) do_export},
  49. };
  50.  
  51. static char    named_file[60];
  52.  
  53. static char    *orient_items[] = {
  54.     "Portrait ",
  55.     "Landscape"};
  56.  
  57. static char    *just_items[] = {
  58.     "Centered",
  59.     "Flush left"};
  60.  
  61. static void    orient_select();
  62. static Widget    orient_panel, orient_menu, orient_lab;
  63.  
  64. static void    lang_select();
  65. static Widget    lang_panel, lang_menu, lang_lab;
  66.  
  67. static void    just_select();
  68. static Widget    just_panel, just_menu, just_lab;
  69.  
  70. static Widget    cancel_but, export_but;
  71. static Widget    dfile_lab, dfile_text, nfile_lab;
  72. static Widget    export_popup, mag_lab, mag_text, export_w;
  73. static Position xposn, yposn;
  74.  
  75. /* Global so w_dir.c can access us */
  76.  
  77. Widget        export_panel,    /* so w_dir can access the scrollbars */
  78.         exp_selfile,    /* output (selected) file widget */
  79.         exp_mask,    /* mask widget */
  80.         exp_dir,    /* current directory widget */
  81.         exp_flist,    /* file list wiget */
  82.         exp_dlist;    /* dir list wiget */
  83.  
  84. Boolean        export_up = False;
  85.  
  86. static void
  87. export_panel_dismiss()
  88. {
  89.     DeclareArgs(1);
  90.  
  91.     FirstArg(XtNstring, "\0");
  92.     SetValues(exp_selfile);        /* clear ascii widget string */
  93.     XtPopdown(export_popup);
  94.     XtSetSensitive(export_w, True);
  95.     export_up = False;
  96. }
  97.  
  98. static void
  99. export_panel_cancel(w, ev)
  100.     Widget        w;
  101.     XButtonEvent   *ev;
  102. {
  103.     export_panel_dismiss();
  104. }
  105.  
  106. static char    export_msg[] = "EXPORT";
  107.  
  108. void
  109. do_export(w)
  110.     Widget        w;
  111. {
  112.     DeclareArgs(1);
  113.     float        mag;
  114.     char       *fval;
  115.  
  116.     if (emptyfigure_msg(export_msg))
  117.         return;
  118.  
  119.     if (!export_popup) 
  120.         create_export_panel(w);
  121.     FirstArg(XtNstring, &fval);
  122.     GetValues(exp_selfile);
  123.     if (emptyname(fval)) {        /* output filename is empty, use default */
  124.         fval = default_export_file;
  125.         warnexist = False;        /* don't warn if this file exists */
  126.     } else {
  127.         warnexist = True;        /* otherwise warn if the file exists */
  128.     }
  129.  
  130.     /* if not absolute path, change directory */
  131.     if (*fval != '/') {
  132.         if (change_directory(export_dir) != 0)
  133.         return;
  134.     }
  135.  
  136.     /* check for XBitmap first */
  137.     if (cur_exp_lang == LANG_XBITMAP) {
  138.         XtSetSensitive(export_but, False);
  139.         if (write_bitmap(fval) == 0)
  140.         {
  141.         FirstArg(XtNlabel, fval);
  142.         SetValues(dfile_text);        /* set the default filename */
  143.         if (strcmp(fval,default_export_file) != 0)
  144.             strcpy(default_export_file,fval); /* and copy to default */
  145.         export_panel_dismiss();
  146.         }
  147.         XtSetSensitive(export_but, True);
  148.     } else {
  149.         mag = (float) atof(panel_get_value(mag_text)) / 100.0;
  150.         if (mag <= 0.0)
  151.         mag = 1.0;
  152.         XtSetSensitive(export_but, False);
  153.         if (print_to_file(fval, lang_items[cur_exp_lang],
  154.                   mag, export_flushleft) == 0)
  155.         {
  156.         FirstArg(XtNlabel, fval);
  157.         SetValues(dfile_text);        /* set the default filename */
  158.         if (strcmp(fval,default_export_file) != 0)
  159.             strcpy(default_export_file,fval); /* and copy to default */
  160.         export_panel_dismiss();
  161.         }
  162.         XtSetSensitive(export_but, True);
  163.     }
  164. }
  165.  
  166. static void
  167. orient_select(w, new_orient, garbage)
  168.     Widget        w;
  169.     XtPointer        new_orient, garbage;
  170. {
  171.     DeclareArgs(1);
  172.  
  173.     FirstArg(XtNlabel, XtName(w));
  174.     SetValues(orient_panel);
  175.     print_landscape = (int) new_orient;
  176. }
  177.  
  178. static void
  179. just_select(w, new_just, garbage)
  180.     Widget        w;
  181.     XtPointer        new_just, garbage;
  182. {
  183.     DeclareArgs(1);
  184.  
  185.     FirstArg(XtNlabel, XtName(w));
  186.     SetValues(just_panel);
  187.     export_flushleft = (new_just? True: False);
  188. }
  189.  
  190. static void
  191. lang_select(w, new_lang, garbage)
  192.     Widget        w;
  193.     XtPointer        new_lang, garbage;
  194. {
  195.     DeclareArgs(1);
  196.  
  197.     FirstArg(XtNlabel, XtName(w));
  198.     SetValues(lang_panel);
  199.     cur_exp_lang = (int) new_lang;
  200.     if (cur_exp_lang == LANG_XBITMAP) {
  201.     XtSetSensitive(mag_lab, False);
  202.     XtSetSensitive(mag_text, False);
  203.     XtSetSensitive(orient_lab, False);
  204.     XtSetSensitive(orient_panel, False);
  205.     } else {
  206.     XtSetSensitive(mag_lab, True);
  207.     XtSetSensitive(mag_text, True);
  208.     XtSetSensitive(orient_lab, True);
  209.     XtSetSensitive(orient_panel, True);
  210.     }
  211.  
  212.     if (cur_exp_lang == LANG_PS) {
  213.     XtSetSensitive(just_lab, True);
  214.     XtSetSensitive(just_panel, True);
  215.     } else {
  216.     XtSetSensitive(just_lab, False);
  217.     XtSetSensitive(just_panel, False);
  218.     }
  219.  
  220.     update_def_filename();
  221.     FirstArg(XtNlabel, default_export_file);
  222.     SetValues(dfile_text);
  223. }
  224.  
  225. popup_export_panel(w)
  226.     Widget        w;
  227. {
  228.     extern Atom wm_delete_window;
  229.  
  230.     DeclareArgs(10);
  231.  
  232.     set_temp_cursor(wait_cursor);
  233.     XtSetSensitive(w, False);
  234.     export_up = True;
  235.  
  236.     if (!export_popup)
  237.         create_export_panel(w);
  238.  
  239.     /* set the directory widget to the current export directory */
  240.     FirstArg(XtNstring, export_dir);
  241.     SetValues(exp_dir);
  242.  
  243.     Rescan(0, 0, 0, 0);
  244.  
  245.     FirstArg(XtNlabel, default_export_file);
  246.     NextArg(XtNwidth, 250);
  247.     SetValues(dfile_text);
  248.     XtPopup(export_popup, XtGrabNonexclusive);
  249.         (void) XSetWMProtocols(XtDisplay(export_popup), XtWindow(export_popup),
  250.                    &wm_delete_window, 1);
  251.     reset_cursor();
  252. }
  253.  
  254. create_export_panel(w)
  255.     Widget        w;
  256. {
  257.     Widget            beside, below;
  258.     PIX_FONT    temp_font;
  259.     DeclareArgs(10);
  260.  
  261.     export_w = w;
  262.     XtTranslateCoords(w, (Position) 0, (Position) 0, &xposn, &yposn);
  263.  
  264.     FirstArg(XtNx, xposn);
  265.     NextArg(XtNy, yposn + 50);
  266.     NextArg(XtNtitle, "Xfig: Export menu");
  267.     export_popup = XtCreatePopupShell("xfig_export_menu",
  268.                       transientShellWidgetClass,
  269.                       tool, Args, ArgCount);
  270.     XtOverrideTranslations(export_popup,
  271.                XtParseTranslationTable(export_translations));
  272.     XtAppAddActions(tool_app, export_actions, XtNumber(export_actions));
  273.  
  274.     export_panel = XtCreateManagedWidget("export_panel", formWidgetClass,
  275.                          export_popup, NULL, ZERO);
  276.  
  277.     FirstArg(XtNlabel, "   Magnification%:");
  278.     NextArg(XtNjustify, XtJustifyLeft);
  279.     NextArg(XtNborderWidth, 0);
  280.     mag_lab = XtCreateManagedWidget("mag_label", labelWidgetClass,
  281.                     export_panel, Args, ArgCount);
  282.  
  283.     FirstArg(XtNwidth, 40);
  284.     NextArg(XtNfromHoriz, mag_lab);
  285.     NextArg(XtNeditType, "edit");
  286.     NextArg(XtNstring, "100");
  287.     NextArg(XtNinsertPosition, 3);
  288.     NextArg(XtNborderWidth, INTERNAL_BW);
  289.     mag_text = XtCreateManagedWidget("magnification", asciiTextWidgetClass,
  290.                      export_panel, Args, ArgCount);
  291.     XtOverrideTranslations(mag_text,
  292.                XtParseTranslationTable(text_translations));
  293.  
  294.     FirstArg(XtNlabel, "      Orientation:");
  295.     NextArg(XtNjustify, XtJustifyLeft);
  296.     NextArg(XtNborderWidth, 0);
  297.     NextArg(XtNfromVert, mag_text);
  298.     orient_lab = XtCreateManagedWidget("orient_label", labelWidgetClass,
  299.                        export_panel, Args, ArgCount);
  300.  
  301.     FirstArg(XtNlabel, orient_items[print_landscape]);
  302.     NextArg(XtNfromHoriz, orient_lab);
  303.     NextArg(XtNfromVert, mag_text);
  304.     NextArg(XtNborderWidth, INTERNAL_BW);
  305.     orient_panel = XtCreateManagedWidget("orientation",
  306.                          menuButtonWidgetClass,
  307.                          export_panel, Args, ArgCount);
  308.     orient_menu = make_popup_menu(orient_items, XtNumber(orient_items),
  309.                       orient_panel, orient_select);
  310.  
  311.     FirstArg(XtNlabel, "    Justification:");
  312.     NextArg(XtNjustify, XtJustifyLeft);
  313.     NextArg(XtNborderWidth, 0);
  314.     NextArg(XtNfromVert, orient_panel);
  315.     just_lab = XtCreateManagedWidget("just_label", labelWidgetClass,
  316.                      export_panel, Args, ArgCount);
  317.  
  318.     FirstArg(XtNlabel, just_items[export_flushleft? 1 : 0]);
  319.     NextArg(XtNfromHoriz, just_lab);
  320.     NextArg(XtNfromVert, orient_panel);
  321.     NextArg(XtNborderWidth, INTERNAL_BW);
  322.     NextArg(XtNresizable, True);
  323.     just_panel = XtCreateManagedWidget("justify",
  324.                        menuButtonWidgetClass,
  325.                        export_panel, Args, ArgCount);
  326.     just_menu = make_popup_menu(just_items, XtNumber(just_items),
  327.                     just_panel, just_select);
  328.  
  329.     FirstArg(XtNlabel, "         Language:");
  330.     NextArg(XtNjustify, XtJustifyLeft);
  331.     NextArg(XtNborderWidth, 0);
  332.     NextArg(XtNfromVert, just_panel);
  333.     lang_lab = XtCreateManagedWidget("lang_label", labelWidgetClass,
  334.                      export_panel, Args, ArgCount);
  335.  
  336.     FirstArg(XtNlabel, lang_texts[cur_exp_lang]);
  337.     NextArg(XtNfromHoriz, lang_lab);
  338.     NextArg(XtNfromVert, just_panel);
  339.     NextArg(XtNborderWidth, INTERNAL_BW);
  340.     lang_panel = XtCreateManagedWidget("language",
  341.                        menuButtonWidgetClass,
  342.                        export_panel, Args, ArgCount);
  343.     lang_menu = make_popup_menu(lang_texts, XtNumber(lang_texts),
  344.                     lang_panel, lang_select);
  345.  
  346.     FirstArg(XtNlabel, " Default Filename:");
  347.     NextArg(XtNfromVert, lang_panel);
  348.     NextArg(XtNvertDistance, 15);
  349.     NextArg(XtNjustify, XtJustifyLeft);
  350.     NextArg(XtNborderWidth, 0);
  351.     dfile_lab = XtCreateManagedWidget("def_file_label", labelWidgetClass,
  352.                       export_panel, Args, ArgCount);
  353.  
  354.     FirstArg(XtNlabel, default_export_file);
  355.     NextArg(XtNfromVert, lang_panel);
  356.     NextArg(XtNfromHoriz, dfile_lab);
  357.     NextArg(XtNvertDistance, 15);
  358.     NextArg(XtNjustify, XtJustifyLeft);
  359.     NextArg(XtNborderWidth, 0);
  360.     NextArg(XtNwidth, 250);
  361.     dfile_text = XtCreateManagedWidget("def_file_name", labelWidgetClass,
  362.                        export_panel, Args, ArgCount);
  363.  
  364.     FirstArg(XtNlabel, "  Output Filename:");
  365.     NextArg(XtNfromVert, dfile_text);
  366.     NextArg(XtNjustify, XtJustifyLeft);
  367.     NextArg(XtNborderWidth, 0);
  368.     nfile_lab = XtCreateManagedWidget("out_file_name", labelWidgetClass,
  369.                       export_panel, Args, ArgCount);
  370.  
  371.     FirstArg(XtNfont, &temp_font);
  372.     GetValues(nfile_lab);
  373.  
  374.     FirstArg(XtNwidth, 350);
  375.     NextArg(XtNheight, char_height(temp_font) * 2 + 4);
  376.     NextArg(XtNfromHoriz, nfile_lab);
  377.     NextArg(XtNfromVert, dfile_text);
  378.     NextArg(XtNeditType, "edit");
  379.     NextArg(XtNborderWidth, INTERNAL_BW);
  380.     NextArg(XtNstring, named_file);
  381.     NextArg(XtNinsertPosition, strlen(named_file));
  382.     NextArg(XtNscrollHorizontal, XawtextScrollWhenNeeded);
  383.     exp_selfile = XtCreateManagedWidget("file", asciiTextWidgetClass,
  384.                         export_panel, Args, ArgCount);
  385.     XtOverrideTranslations(exp_selfile,
  386.                XtParseTranslationTable(text_translations));
  387.  
  388.     /* add action to export file for following translation */
  389.     XtAppAddActions(tool_app, file_name_actions, XtNumber(file_name_actions));
  390.  
  391.     /* make <return> in the filename window export the file */
  392.     XtOverrideTranslations(exp_selfile,
  393.                XtParseTranslationTable(file_name_translations));
  394.  
  395.     create_dirinfo(export_panel, exp_selfile, &beside, &below,
  396.                &exp_mask, &exp_dir, &exp_flist, &exp_dlist);
  397.     /* make <return> in the file list window export the file */
  398.     XtOverrideTranslations(exp_flist,
  399.                XtParseTranslationTable(file_name_translations));
  400.  
  401.     FirstArg(XtNlabel, "Cancel");
  402.     NextArg(XtNfromHoriz, beside);
  403.     NextArg(XtNfromVert, below);
  404.     NextArg(XtNvertDistance, 15);
  405.     NextArg(XtNhorizDistance, 25);
  406.     NextArg(XtNheight, 25);
  407.     NextArg(XtNborderWidth, INTERNAL_BW);
  408.     cancel_but = XtCreateManagedWidget("cancel", commandWidgetClass,
  409.                        export_panel, Args, ArgCount);
  410.     XtAddEventHandler(cancel_but, ButtonReleaseMask, (Boolean) 0,
  411.               (XtEventHandler)export_panel_cancel, (XtPointer) NULL);
  412.  
  413.     FirstArg(XtNlabel, "Export");
  414.     NextArg(XtNfromVert, below);
  415.     NextArg(XtNfromHoriz, cancel_but);
  416.     NextArg(XtNvertDistance, 15);
  417.     NextArg(XtNhorizDistance, 25);
  418.     NextArg(XtNheight, 25);
  419.     NextArg(XtNborderWidth, INTERNAL_BW);
  420.     export_but = XtCreateManagedWidget("export", commandWidgetClass,
  421.                        export_panel, Args, ArgCount);
  422.     XtAddEventHandler(export_but, ButtonReleaseMask, (Boolean) 0,
  423.               (XtEventHandler)do_export, (XtPointer) NULL);
  424.  
  425.     XtInstallAccelerators(export_panel, cancel_but);
  426.     XtInstallAccelerators(export_panel, export_but);
  427.  
  428.     if (cur_exp_lang == LANG_XBITMAP) {
  429.         XtSetSensitive(mag_lab, False);
  430.         XtSetSensitive(mag_text, False);
  431.         XtSetSensitive(orient_lab, False);
  432.         XtSetSensitive(orient_panel, False);
  433.     }
  434.     if (cur_exp_lang != LANG_PS) {
  435.         XtSetSensitive(just_lab, False);
  436.         XtSetSensitive(just_panel, False);
  437.     }
  438.     update_def_filename();
  439. }
  440.  
  441. update_def_filename()
  442. {
  443.     int            i;
  444.     DeclareArgs(1);
  445.     char       *dval;
  446.  
  447.     (void) strcpy(default_export_file, cur_filename);
  448.     if (default_export_file[0] != '\0') {
  449.     i = strlen(default_export_file);
  450.     if (i >= 4 && strcmp(&default_export_file[i - 4], ".fig") == 0)
  451.         default_export_file[i - 4] = '\0';
  452.     (void) strcat(default_export_file, ".");
  453.     (void) strcat(default_export_file, lang_items[cur_exp_lang]);
  454.     }
  455.     /* remove trailing blanks */
  456.     for (i = strlen(default_export_file) - 1; i >= 0; i--)
  457.     if (default_export_file[i] == ' ')
  458.         default_export_file[i] = '\0';
  459.     else
  460.         i = 0;
  461.     /* set the current directory from the file popup directory */
  462.     if (file_popup) {
  463.     FirstArg(XtNstring, &dval);
  464.     GetValues(file_dir);
  465.     strcpy(export_dir,dval);
  466.     }
  467. }
  468.