home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part06 / w_cmdpanel.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-27  |  7.5 KB  |  269 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 "resources.h"
  16. #include "mode.h"
  17. #include "w_canvas.h"        /* for null_proc() */
  18. #include "w_drawprim.h"
  19. #include "w_mousefun.h"
  20. #include "w_util.h"
  21. #include "w_setup.h"
  22.  
  23. extern        erase_objecthighlight();
  24. extern        emptyfigure();
  25. extern        do_print(), do_print_batch(), do_export(), do_save();
  26. extern void    undo(), redisplay_canvas();
  27. extern void    popup_print_panel(), popup_file_panel(), popup_export_panel();
  28.  
  29. void        init_cmd_panel();
  30. void        setup_cmd_panel();
  31.  
  32. /* internal features and definitions */
  33.  
  34. #define cmd_action(z)        (z->cmd_func)(z->widget)
  35. #define quick_action(z)        (z->quick_func)(z->widget)
  36. #define shift_quick_action(z)    (z->shift_quick_func)(z->widget)
  37.  
  38. /* prototypes */
  39. static void    sel_cmd_but();
  40. static void    enter_cmd_but();
  41. void        quit();
  42. void        delete_all_cmd();
  43. void        paste();
  44.  
  45. /* cmd panel definitions */
  46. #define CMD_LABEL_LEN    16
  47. typedef struct cmd_switch_struct {
  48.     char        label[CMD_LABEL_LEN];    /* label on the button */
  49.     char        cmd_name[CMD_LABEL_LEN];    /* command name for resources */
  50.     void        (*cmd_func) ();        /* mouse button 1 func */
  51.     int            (*quick_func) ();        /* mouse button 3 func */
  52.     int            (*shift_quick_func) ();    /* shift-mouse button 3 func */
  53.     char        mousefun_l[CMD_LABEL_LEN];    /* label for mouse 1 func */
  54.     char        mousefun_r[CMD_LABEL_LEN];    /* label for mouse 3 func */
  55.     Widget        widget;            /* widget */
  56. }        cmd_sw_info;
  57.  
  58. /* command panel of switches below the lower ruler */
  59. cmd_sw_info cmd_switches[] = {
  60.     {"Quit",       "quit", quit, null_proc, null_proc, "Quit", ""},
  61.     {"Delete ALL", "delete_all", delete_all_cmd, null_proc, null_proc, 
  62.                 "Delete all", ""},
  63.     {"Undo",       "undo", undo, null_proc, null_proc, "Undo", ""},
  64.     {"Redraw",       "redraw", redisplay_canvas, null_proc, null_proc, 
  65.                 "Redraw", ""},
  66.     {"Paste",       "paste", paste, null_proc, null_proc, "Paste", ""},
  67.     {"File...",       "file", popup_file_panel, do_save, null_proc, 
  68.                 "Popup", "Save Shortcut"},
  69.     {"Export...",  "export", popup_export_panel, do_export, null_proc, 
  70.                 "Popup", "Export Shortcut"},
  71.     {"Print...",   "print", popup_print_panel, do_print, do_print_batch, 
  72.                 "Popup","Print Shortcut"},
  73. };
  74.  
  75. #define        NUM_CMD_SW  (sizeof(cmd_switches) / sizeof(cmd_sw_info))
  76.  
  77. static XtActionsRec cmd_actions[] =
  78. {
  79.     {"LeaveCmdSw", (XtActionProc) clear_mousefun},
  80.     {"quit", (XtActionProc) quit},
  81.     {"delete_all", (XtActionProc) delete_all_cmd},
  82.     {"undo", (XtActionProc) undo},
  83.     {"redraw", (XtActionProc) redisplay_canvas},
  84.     {"paste", (XtActionProc) paste},
  85.     {"file", (XtActionProc) popup_file_panel},
  86.     {"export", (XtActionProc) popup_export_panel},
  87.     {"print", (XtActionProc) popup_print_panel},
  88. };
  89.  
  90. static String    cmd_translations =
  91. "<Btn1Down>:set()\n\
  92.     <Btn1Up>:unset()\n\
  93.     <LeaveWindow>:LeaveCmdSw()reset()\n";
  94.  
  95. DeclareStaticArgs(11);
  96.  
  97. int
  98. num_cmd_sw()
  99. {
  100.     return (NUM_CMD_SW);
  101. }
  102.  
  103. /* command panel */
  104. void
  105. init_cmd_panel(tool)
  106.     Widget        tool;
  107. {
  108.     register int    i;
  109.     register cmd_sw_info *sw;
  110.     Widget        beside = NULL;
  111.  
  112.     FirstArg(XtNborderWidth, 0);
  113.     NextArg(XtNdefaultDistance, 0);
  114.     NextArg(XtNhorizDistance, 0);
  115.     NextArg(XtNvertDistance, 0);
  116.     NextArg(XtNleft, XtChainLeft);
  117.     NextArg(XtNright, XtChainLeft);
  118.     NextArg(XtNtop, XtChainTop);
  119.     NextArg(XtNbottom, XtChainTop);
  120.     cmd_panel = XtCreateWidget("commands", formWidgetClass, tool,
  121.                    Args, ArgCount);
  122.     XtAppAddActions(tool_app, cmd_actions, XtNumber(cmd_actions));
  123.  
  124.     FirstArg(XtNborderWidth, INTERNAL_BW);
  125.     NextArg(XtNfont, button_font);
  126.     NextArg(XtNheight, CMDPANEL_HT - 2 * INTERNAL_BW);
  127.     NextArg(XtNwidth, CMDPANEL_WD / NUM_CMD_SW - INTERNAL_BW);
  128.     NextArg(XtNvertDistance, 0);
  129.     NextArg(XtNhorizDistance, 0);
  130.     for (i = 0; i < NUM_CMD_SW; ++i) {
  131.     sw = &cmd_switches[i];
  132.     NextArg(XtNlabel, sw->label);
  133.     NextArg(XtNfromHoriz, beside);
  134.     sw->widget = XtCreateManagedWidget(sw->cmd_name, commandWidgetClass,
  135.                        cmd_panel, Args, ArgCount);
  136.     /* setup callback and default actions */
  137.     XtAddEventHandler(sw->widget, ButtonReleaseMask, (Boolean) 0,
  138.               sel_cmd_but, (XtPointer) sw);
  139.     XtAddEventHandler(sw->widget, EnterWindowMask, (Boolean) 0,
  140.               enter_cmd_but, (XtPointer) sw);
  141.     XtOverrideTranslations(sw->widget,
  142.                    XtParseTranslationTable(cmd_translations));
  143.     ArgCount -= 3;
  144.     NextArg(XtNhorizDistance, -INTERNAL_BW);
  145.     beside = sw->widget;
  146.     }
  147. }
  148.  
  149. void
  150. setup_cmd_panel()
  151. {
  152.     register int    i;
  153.     register cmd_sw_info *sw;
  154.  
  155.     XDefineCursor(tool_d, XtWindow(cmd_panel), arrow_cursor);
  156.  
  157.     for (i = 0; i < NUM_CMD_SW; ++i) {
  158.     sw = &cmd_switches[i];
  159.     FirstArg(XtNfont, button_font); /* label font */
  160.     SetValues(sw->widget);
  161.     /* install the keyboard accelerators for this command button 
  162.        from the resources */
  163.     /*XtInstallAllAccelerators(sw->widget, tool);*/
  164.     }
  165. }
  166.  
  167. static void
  168. enter_cmd_but(widget, closure, event, continue_to_dispatch)
  169.     Widget        widget;
  170.     XtPointer        closure;
  171.     XEvent*        event;
  172.     Boolean*        continue_to_dispatch;
  173. {
  174.     cmd_sw_info *sw = (cmd_sw_info *) closure;
  175.     clear_mousefun();
  176.     draw_mousefun(sw->mousefun_l, "", sw->mousefun_r);
  177. }
  178.  
  179. static void
  180. sel_cmd_but(widget, closure, event, continue_to_dispatch)
  181.     Widget        widget;
  182.     XtPointer        closure;
  183.     XEvent*        event;
  184.     Boolean*        continue_to_dispatch;
  185. {
  186.     cmd_sw_info *sw = (cmd_sw_info *) closure;
  187.     XButtonEvent button;
  188.     
  189.     button = event->xbutton;
  190.  
  191.     if ((button.button == Button2) ||
  192.     (button.button == Button3 && button.state & Mod1Mask))
  193.         return;
  194.  
  195.     if (action_on) {
  196.     if (cur_mode == F_TEXT)
  197.         finish_text_input();/* finish up any text input */
  198.     else {
  199.         put_msg("FINISH drawing current object first");
  200.         return;
  201.     }
  202.     } else if (highlighting)
  203.     erase_objecthighlight();
  204.  
  205.     if (button.button == Button1)
  206.     cmd_action(sw);
  207.     else if (button.state & ShiftMask)
  208.     shift_quick_action(sw);
  209.     else
  210.     quick_action(sw);
  211. }
  212.  
  213. static char    quit_msg[] = "The current figure is modified.\nDo you want to save it before quitting?";
  214.  
  215. void
  216. quit(w)
  217.     Widget        w;
  218. {
  219.     int            qresult;
  220.  
  221.     if (!emptyfigure() && figure_modified && !aborting) {
  222.     XtSetSensitive(w, False);
  223.     if ((qresult = popup_query(QUERY_YESNO, quit_msg)) == RESULT_CANCEL) {
  224.         XtSetSensitive(w, True);
  225.         return;
  226.     } else if (qresult == RESULT_YES) {
  227.         do_save(w);
  228.         /*
  229.          * if saving was not successful, figure_modified is still true:
  230.          * do not quit!
  231.          */
  232.         if (figure_modified) {
  233.         XtSetSensitive(w, True);
  234.         return;
  235.         }
  236.     }
  237.     }
  238.     /* delete the cut buffer only if it is in a temporary directory */
  239.     if (strncmp(cut_buf_name, TMPDIR, strlen(TMPDIR)) == 0)
  240.     unlink(cut_buf_name);
  241.  
  242.     /* delete any batch print file */
  243.     if (batch_exists)
  244.     unlink(batch_file);
  245.  
  246.     /* free all the GC's */
  247.     free_GCs();
  248.     XtDestroyWidget(tool);
  249.     exit(0);
  250. }
  251.  
  252. void
  253. paste()
  254. {
  255.     merge_file(cut_buf_name);
  256. }
  257.  
  258. void
  259. delete_all_cmd()
  260. {
  261.     if (emptyfigure()) {
  262.     put_msg("Figure already empty");
  263.     return;
  264.     }
  265.     delete_all();
  266.     put_msg("Immediate Undo will restore the figure");
  267.     redisplay_canvas();
  268. }
  269.