home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part09 / w_file.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-27  |  11.0 KB  |  379 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 "resources.h"
  16. #include "object.h"
  17. #include "mode.h"
  18. #include "w_drawprim.h"        /* for char_height */
  19. #include "w_dir.h"
  20. #include "w_util.h"
  21. #include "w_setup.h"
  22.  
  23. extern Boolean    file_msg_is_popped;
  24. extern Widget    file_msg_popup;
  25.  
  26. extern String    text_translations;
  27. static char    load_msg[] = "The current figure is modified.\nDo you want to discard it and load the new file?";
  28. static char    buf[40];
  29.  
  30. DeclareStaticArgs(12);
  31. static Widget    file_status, num_objects;
  32. static Widget    cfile_lab, cfile_text;
  33. static Widget    cancel, save, merge, load;
  34. static Widget    file_w;
  35. static Position xposn, yposn;
  36. static String    file_name_translations =
  37.     "<Key>Return: load()\n";
  38. static void    file_panel_cancel(), do_merge();
  39. void        do_load(), do_save();
  40. static XtActionsRec    file_name_actions[] =
  41. {
  42.     {"load", (XtActionProc) do_load},
  43. };
  44. static String    file_translations =
  45.     "<Message>WM_PROTOCOLS: DismissFile()\n";
  46. static XtActionsRec    file_actions[] =
  47. {
  48.     {"DismissFile", (XtActionProc) file_panel_cancel},
  49.     {"cancel", (XtActionProc) file_panel_cancel},
  50.     {"load", (XtActionProc) do_load},
  51.     {"save", (XtActionProc) do_save},
  52.     {"merge", (XtActionProc) do_merge},
  53. };
  54.  
  55. /* Global so w_dir.c can access us */
  56.  
  57. Widget        file_panel,    /* so w_dir can access the scrollbars */
  58.         file_popup,    /* the popup itself */
  59.         file_selfile,    /* selected file widget */
  60.         file_mask,    /* mask widget */
  61.         file_dir,    /* current directory widget */
  62.         file_flist,    /* file list wiget */
  63.         file_dlist;    /* dir list wiget */
  64.  
  65. Boolean        file_up = False;
  66.  
  67. static void
  68. file_panel_dismiss()
  69. {
  70.     FirstArg(XtNstring, "\0");
  71.     SetValues(file_selfile);    /* clear Filename string */
  72.     XtPopdown(file_popup);
  73.     XtSetSensitive(file_w, True);
  74.     file_up = False;
  75. }
  76.  
  77. static void
  78. do_merge(w, ev)
  79.     Widget        w;
  80.     XButtonEvent   *ev;
  81. {
  82.     char        filename[100];
  83.     char       *fval, *dval;
  84.  
  85.     FirstArg(XtNstring, &fval);
  86.     GetValues(file_selfile);    /* check the ascii widget for a filename */
  87.     if (emptyname(fval))
  88.     fval = cur_filename;    /* "Filename" widget empty, use current filename */
  89.  
  90.     if (emptyname_msg(fval, "MERGE"))
  91.     return;
  92.  
  93.     FirstArg(XtNstring, &dval);
  94.     GetValues(file_dir);
  95.  
  96.     strcpy(filename, dval);
  97.     strcat(filename, "/");
  98.     strcat(filename, fval);
  99.     if (merge_file(filename) == 0)
  100.     file_panel_dismiss();
  101. }
  102.  
  103. void
  104. do_load(w, ev)
  105.     Widget        w;
  106.     XButtonEvent   *ev;
  107. {
  108.     char       *fval, *dval;
  109.  
  110.     if (file_popup) {
  111.     FirstArg(XtNstring, &dval);
  112.     GetValues(file_dir);
  113.     FirstArg(XtNstring, &fval);
  114.     GetValues(file_selfile);    /* check the ascii widget for a filename */
  115.     if (emptyname(fval))
  116.         fval = cur_filename;    /* "Filename" widget empty, use current filename */
  117.  
  118.     if (emptyname_msg(fval, "LOAD"))
  119.         return;
  120.  
  121.     if (!emptyfigure() && figure_modified) {
  122.         XtSetSensitive(load, False);
  123.         if (!popup_query(QUERY_YES, load_msg)) {
  124.         XtSetSensitive(load, True);
  125.         return;
  126.         }
  127.         XtSetSensitive(load, True);
  128.     }
  129.     if (change_directory(dval) == 0) {
  130.         if (load_file(fval) == 0) {
  131.         FirstArg(XtNlabel, fval);
  132.         SetValues(cfile_text);        /* set the current filename */
  133.         if (fval != cur_filename)
  134.             update_cur_filename(fval);    /* and update cur_filename */
  135.         update_def_filename();        /* and the default export filename */
  136.         file_panel_dismiss();
  137.         }
  138.     }
  139.     } else {
  140.     (void) load_file(cur_filename);
  141.     }
  142. }
  143.  
  144. void
  145. do_save(w)
  146.     Widget        w;
  147. {
  148.     char       *fval, *dval;
  149.  
  150.     if (emptyfigure_msg("Save"))
  151.     return;
  152.  
  153.     if (file_popup) {
  154.     FirstArg(XtNstring, &fval);
  155.     GetValues(file_selfile);    /* check the ascii widget for a filename */
  156.     if (emptyname(fval)) {
  157.         fval = cur_filename;    /* "Filename" widget empty, use current filename */
  158.         warnexist = False;        /* don't warn if this file exists */
  159.     /* copy the name from the file_name widget to the current filename */
  160.     } else
  161.         {
  162.         warnexist = True;            /* warn if this file exists */
  163.         }
  164.  
  165.     if (emptyname_msg(fval, "Save"))
  166.         return;
  167.  
  168.     /* get the directory from the ascii widget */
  169.     FirstArg(XtNstring, &dval);
  170.     GetValues(file_dir);
  171.  
  172.     if (change_directory(dval) == 0) {
  173.         XtSetSensitive(save, False);
  174.         if (write_file(fval) == 0) {
  175.         FirstArg(XtNlabel, fval);
  176.         SetValues(cfile_text);
  177.         if (strcmp(fval,cur_filename) != 0) {
  178.             update_cur_filename(fval);    /* update cur_filename */
  179.             update_def_filename();    /* update the default export filename */
  180.         }
  181.         reset_modifiedflag();
  182.         file_panel_dismiss();
  183.         }
  184.         XtSetSensitive(save, True);
  185.     }
  186.     } else {
  187.     /* not using popup => filename not changed so ok to write existing file */
  188.     warnexist = False;            
  189.     if (write_file(cur_filename) == 0)
  190.         reset_modifiedflag();
  191.     }
  192. }
  193.  
  194. static void
  195. file_panel_cancel(w, ev)
  196.     Widget        w;
  197.     XButtonEvent   *ev;
  198. {
  199.     file_panel_dismiss();
  200. }
  201.  
  202. popup_file_panel(w)
  203.     Widget        w;
  204. {
  205.     extern Atom     wm_delete_window;
  206.  
  207.     set_temp_cursor(wait_cursor);
  208.     XtSetSensitive(w, False);
  209.     file_up = True;
  210.  
  211.     if (!file_popup)
  212.     create_file_panel(w);
  213.     else
  214.     Rescan(0, 0, 0, 0);
  215.  
  216.     FirstArg(XtNlabel, (figure_modified ? "      File Status: Modified    " :
  217.                       "      File Status: Not modified"));
  218.     SetValues(file_status);
  219.     sprintf(buf, "Number of Objects: %d", object_count(&objects));
  220.     FirstArg(XtNlabel, buf);
  221.     SetValues(num_objects);
  222.     XtPopup(file_popup, XtGrabNonexclusive);
  223.     (void) XSetWMProtocols(XtDisplay(file_popup), XtWindow(file_popup),
  224.                &wm_delete_window, 1);
  225.     if (file_msg_is_popped)
  226.     XtAddGrab(file_msg_popup, False, False);
  227.     reset_cursor();
  228. }
  229.  
  230. create_file_panel(w)
  231.     Widget           w;
  232. {
  233.     Widget           file, dir, beside, below;
  234.     PIX_FONT       temp_font;
  235.     static int       actions_added=0;
  236.     file_w = w;
  237.     XtTranslateCoords(w, (Position) 0, (Position) 0, &xposn, &yposn);
  238.  
  239.     FirstArg(XtNx, xposn);
  240.     NextArg(XtNy, yposn + 50);
  241.     NextArg(XtNtitle, "Xfig: File menu");
  242.     file_popup = XtCreatePopupShell("xfig_file_menu",
  243.                     transientShellWidgetClass,
  244.                     tool, Args, ArgCount);
  245.     XtOverrideTranslations(file_popup,
  246.                XtParseTranslationTable(file_translations));
  247.  
  248.     file_panel = XtCreateManagedWidget("file_panel", formWidgetClass,
  249.                        file_popup, NULL, ZERO);
  250.  
  251.     FirstArg(XtNlabel, "");
  252.     NextArg(XtNwidth, 400);
  253.     NextArg(XtNjustify, XtJustifyLeft);
  254.     NextArg(XtNborderWidth, 0);
  255.     NextArg(XtNresize, False);
  256.     file_status = XtCreateManagedWidget("file_status", labelWidgetClass,
  257.                         file_panel, Args, ArgCount);
  258.  
  259.     FirstArg(XtNlabel, "");
  260.     NextArg(XtNwidth, 400);
  261.     NextArg(XtNfromVert, file_status);
  262.     NextArg(XtNjustify, XtJustifyLeft);
  263.     NextArg(XtNborderWidth, 0);
  264.     NextArg(XtNresize, False);
  265.     num_objects = XtCreateManagedWidget("num_objects", labelWidgetClass,
  266.                         file_panel, Args, ArgCount);
  267.  
  268.     FirstArg(XtNlabel, " Current Filename:");
  269.     NextArg(XtNfromVert, num_objects);
  270.     NextArg(XtNvertDistance, 15);
  271.     NextArg(XtNjustify, XtJustifyLeft);
  272.     NextArg(XtNborderWidth, 0);
  273.     cfile_lab = XtCreateManagedWidget("cur_file_label", labelWidgetClass,
  274.                       file_panel, Args, ArgCount);
  275.  
  276.     FirstArg(XtNlabel, cur_filename);
  277.     NextArg(XtNfromVert, num_objects);
  278.     NextArg(XtNfromHoriz, cfile_lab);
  279.     NextArg(XtNvertDistance, 15);
  280.     NextArg(XtNjustify, XtJustifyLeft);
  281.     NextArg(XtNborderWidth, 0);
  282.     NextArg(XtNwidth, 250);
  283.     cfile_text = XtCreateManagedWidget("cur_file_name", labelWidgetClass,
  284.                        file_panel, Args, ArgCount);
  285.  
  286.     FirstArg(XtNlabel, "         Filename:");
  287.     NextArg(XtNvertDistance, 15);
  288.     NextArg(XtNfromVert, cfile_lab);
  289.     NextArg(XtNborderWidth, 0);
  290.     file = XtCreateManagedWidget("file_label", labelWidgetClass,
  291.                      file_panel, Args, ArgCount);
  292.     FirstArg(XtNfont, &temp_font);
  293.     GetValues(file);
  294.  
  295.     FirstArg(XtNwidth, 350);
  296.     NextArg(XtNheight, char_height(temp_font) * 2 + 4);
  297.     NextArg(XtNeditType, "edit");
  298.     NextArg(XtNstring, cur_filename);
  299.     NextArg(XtNinsertPosition, strlen(cur_filename));
  300.     NextArg(XtNfromHoriz, file);
  301.     NextArg(XtNborderWidth, INTERNAL_BW);
  302.     NextArg(XtNvertDistance, 15);
  303.     NextArg(XtNfromVert, cfile_lab);
  304.     NextArg(XtNscrollHorizontal, XawtextScrollWhenNeeded);
  305.     file_selfile = XtCreateManagedWidget("file_name", asciiTextWidgetClass,
  306.                          file_panel, Args, ArgCount);
  307.     XtOverrideTranslations(file_selfile,
  308.                XtParseTranslationTable(text_translations));
  309.  
  310.     if (!actions_added) {
  311.         XtAppAddActions(tool_app, file_actions, XtNumber(file_actions));
  312.         actions_added = 1;
  313.         /* add action to load file */
  314.         XtAppAddActions(tool_app, file_name_actions, XtNumber(file_name_actions));
  315.     }
  316.  
  317.     /* make <return> in the filename window load the file */
  318.     XtOverrideTranslations(file_selfile,
  319.                XtParseTranslationTable(file_name_translations));
  320.  
  321.     create_dirinfo(file_panel, file_selfile, &beside, &below,
  322.                &file_mask, &file_dir, &file_flist, &file_dlist);
  323.     /* make <return> in the file list window load the file */
  324.     XtOverrideTranslations(file_flist,
  325.                XtParseTranslationTable(file_name_translations));
  326.     FirstArg(XtNlabel, "Cancel");
  327.     NextArg(XtNvertDistance, 15);
  328.     NextArg(XtNhorizDistance, 25);
  329.     NextArg(XtNheight, 25);
  330.     NextArg(XtNfromHoriz, beside);
  331.     NextArg(XtNfromVert, below);
  332.     NextArg(XtNborderWidth, INTERNAL_BW);
  333.     cancel = XtCreateManagedWidget("cancel", commandWidgetClass,
  334.                        file_panel, Args, ArgCount);
  335.     XtAddEventHandler(cancel, ButtonReleaseMask, (Boolean) 0,
  336.               (XtEventHandler)file_panel_cancel, (XtPointer) NULL);
  337.  
  338.     FirstArg(XtNlabel, "Save");
  339.     NextArg(XtNfromHoriz, cancel);
  340.     NextArg(XtNfromVert, below);
  341.     NextArg(XtNvertDistance, 15);
  342.     NextArg(XtNhorizDistance, 25);
  343.     NextArg(XtNheight, 25);
  344.     NextArg(XtNborderWidth, INTERNAL_BW);
  345.     save = XtCreateManagedWidget("save", commandWidgetClass,
  346.                      file_panel, Args, ArgCount);
  347.     XtAddEventHandler(save, ButtonReleaseMask, (Boolean) 0,
  348.               (XtEventHandler)do_save, (XtPointer) NULL);
  349.  
  350.     FirstArg(XtNlabel, "Load");
  351.     NextArg(XtNborderWidth, INTERNAL_BW);
  352.     NextArg(XtNfromHoriz, save);
  353.     NextArg(XtNfromVert, below);
  354.     NextArg(XtNvertDistance, 15);
  355.     NextArg(XtNhorizDistance, 25);
  356.     NextArg(XtNheight, 25);
  357.     load = XtCreateManagedWidget("load", commandWidgetClass,
  358.                      file_panel, Args, ArgCount);
  359.     XtAddEventHandler(load, ButtonReleaseMask, (Boolean) 0,
  360.               (XtEventHandler)do_load, (XtPointer) NULL);
  361.  
  362.     FirstArg(XtNlabel, "Merge Read");
  363.     NextArg(XtNfromHoriz, load);
  364.     NextArg(XtNfromVert, below);
  365.     NextArg(XtNborderWidth, INTERNAL_BW);
  366.     NextArg(XtNvertDistance, 15);
  367.     NextArg(XtNhorizDistance, 25);
  368.     NextArg(XtNheight, 25);
  369.     merge = XtCreateManagedWidget("merge", commandWidgetClass,
  370.                       file_panel, Args, ArgCount);
  371.     XtAddEventHandler(merge, ButtonReleaseMask, (Boolean) 0,
  372.               (XtEventHandler)do_merge, (XtPointer) NULL);
  373.  
  374.     XtInstallAccelerators(file_panel, cancel);
  375.     XtInstallAccelerators(file_panel, save);
  376.     XtInstallAccelerators(file_panel, load);
  377.     XtInstallAccelerators(file_panel, merge);
  378. }
  379.