home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / editres / actions.c next >
Encoding:
C/C++ Source or Header  |  1991-07-09  |  8.5 KB  |  333 lines

  1. /*
  2.  * $XConsortium: actions.c,v 1.12 91/07/09 12:09:07 rws Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <X11/Intrinsic.h>
  26. #include <X11/Xutil.h>
  27.  
  28. #include <X11/Xaw/Cardinals.h>    
  29.  
  30. #include "editresP.h"
  31.  
  32. /*
  33.  * External Functions.
  34.  */
  35.  
  36. extern void SetMessage(), _TreeSelect(), _TreeSelectNode(), _FindWidget();
  37. extern void _TreeActivateNode(), _TreeRelabel(), _TreeRelabelNode();
  38. extern void PrepareToLayoutTree(), LayoutTree(), _PopdownFileDialog();
  39.  
  40. /*
  41.  * Private data.
  42.  */
  43.  
  44. struct ActionValues {
  45.     String name;
  46.     int type;
  47. };
  48.  
  49. static struct ActionValues select_values[] = {
  50.     { "widget", (int) SelectWidget },
  51.     { "all", (int) SelectAll },
  52.     { "nothing", (int) SelectNone },
  53.     { "invert", (int) SelectInvert },
  54.     { "children", (int) SelectChildren },
  55.     { "descendants", (int) SelectDescendants },
  56.     { "parent", (int) SelectParent },
  57.     { "ancestors", (int) SelectAncestors }
  58. };
  59.  
  60. static struct  ActionValues label_values[] = {
  61.     { "name", (int) NameLabel },
  62.     { "class", (int) ClassLabel },
  63.     { "id", (int) IDLabel },
  64.     { "window", (int) WindowLabel },
  65.     { "toggle", (int) ToggleLabel }
  66. };
  67.  
  68. static WNode * FindTreeNodeFromWidget();
  69. static Boolean CheckAndFindEntry();
  70.  
  71. /*    Function Name: SelectAction
  72.  *    Description: 
  73.  *      Arguments: w - any widget in the widget tree.
  74.  *                 event - NOT USED.
  75.  *                 params, num_params - the parameters paseed to the action
  76.  *                                      routine. 
  77.  *
  78.  * params[0] - One of "nothing", "parent", "children", "ancestors",
  79.  *                    "descendants", "invert", "all"
  80.  * num_params - must be one.
  81.  */
  82.  
  83. /* ARGSUSED */
  84. static void
  85. SelectAction(w, event, params, num_params)
  86. Widget w;
  87. XEvent * event;
  88. String * params;
  89. Cardinal * num_params;
  90. {
  91.     WNode * node;
  92.     int type;
  93.  
  94.     if (!CheckAndFindEntry("Select", params, *num_params, 
  95.                select_values, XtNumber(select_values), &type))
  96.     return;
  97.  
  98.     switch(type) {
  99.     case SelectAll:
  100.     case SelectNone:
  101.     case SelectInvert:
  102.     _TreeSelect(global_tree_info, type);
  103.     break;
  104.     case SelectWidget:
  105.     _FindWidget(XtParent(w));
  106.     break;
  107.     default:
  108.     node = FindTreeNodeFromWidget(w);
  109.     if (node)
  110.         _TreeActivateNode(node, type);    
  111.     else
  112.         _TreeSelect(global_tree_info, type);
  113.     break;
  114.     }
  115. }
  116.  
  117. /*    Function Name: RelabelAction
  118.  *    Description: 
  119.  *      Arguments: w - any widget in the widget tree.
  120.  *                 event - NOT USED.
  121.  *                 params, num_params - the parameters paseed to the action
  122.  *                                      routine. 
  123.  *
  124.  * params[0] - One of "name", "class", "id"
  125.  * num_params - must be one.
  126.  */
  127.  
  128. /* ARGSUSED */
  129. static void
  130. RelabelAction(w, event, params, num_params)
  131. Widget w;
  132. XEvent * event;
  133. String * params;
  134. Cardinal * num_params;
  135. {
  136.     WNode * node;
  137.     int type;
  138.  
  139.     if (!CheckAndFindEntry("Relabel", params, *num_params, 
  140.                label_values, XtNumber(label_values), &type))
  141.     return;
  142.  
  143.     if ((node = FindTreeNodeFromWidget(w)) == NULL) 
  144.     _TreeRelabel(global_tree_info, type);
  145.     else {
  146.     PrepareToLayoutTree(global_tree_info->tree_widget); 
  147.     _TreeRelabelNode(node, type, FALSE);
  148.     LayoutTree(global_tree_info->tree_widget); 
  149.     }
  150. }
  151.  
  152. /*    Function Name: PopdownFileDialogAction
  153.  *    Description: Pops down the file dialog widget.
  154.  *                   and calls the approipriate handler.
  155.  *    Arguments: w - any child of the dialog widget.
  156.  *                 event - the event that caused this action.
  157.  *                 params, num_params - params passed to the action routine.
  158.  * RETURNED        none.
  159.  */
  160.  
  161. /* ARGSUSED */
  162.  
  163. static void
  164. PopdownFileDialogAction(w, event, params, num_params)
  165. Widget w;
  166. XEvent * event;
  167. String * params;
  168. Cardinal * num_params;
  169. {
  170.     char buf[BUFSIZ];
  171.     Boolean val;
  172.  
  173.     if (*num_params != 1) {
  174.     sprintf(buf, "Action `%s' must have exactly one argument.", 
  175.         "PopdownFileDialog");
  176.  
  177.     SetMessage(global_screen_data.info_label, buf);
  178.     return;
  179.     }
  180.  
  181.     XmuCopyISOLatin1Lowered(buf, params[0]);
  182.  
  183.     if (streq(buf, "cancel"))
  184.     val = FALSE;
  185.     else if (streq(buf, "okay"))
  186.     val = TRUE;
  187.     else {
  188.     sprintf(buf, "Action %s's argument must be either `cancal' or `okay'.",
  189.         "PopdownFileDialog");
  190.  
  191.     SetMessage(global_screen_data.info_label, buf);
  192.     return;
  193.     }
  194.  
  195.     _PopdownFileDialog(w, (XtPointer) val, NULL);
  196. }
  197.  
  198. /*    Function Name: ActionQuit
  199.  *    Description: This function prints a message to stdout.
  200.  *    Arguments: w - ** UNUSED **
  201.  *                 call_data - ** UNUSED **
  202.  *                 client_data - ** UNUSED **
  203.  *    Returns: none
  204.  */
  205.  
  206. extern Widget toplevel;
  207. /* ARGSUSED */
  208. static void
  209. ActionQuit(w, event, params, num_params)
  210. Widget w;
  211. XEvent * event;
  212. String * params;
  213. Cardinal * num_params;
  214. {
  215.   if (w==toplevel) {
  216.     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  217.     exit(0);
  218.   }
  219.   else  {
  220.     if (streq(XtName(w), RESOURCE_BOX))
  221.       global_resource_box_up = FALSE;
  222.     XtPopdown(w);
  223.     XtDestroyWidget(w);
  224.   }
  225. }
  226.  
  227. /*    Function Name: SetApplicationActions
  228.  *    Description: Sets my application actions.
  229.  *    Arguments: app_con - the application context.
  230.  *    Returns: none.
  231.  */
  232.  
  233. extern void ModifySVEntry();
  234.  
  235. static XtActionsRec actions[] = {
  236.   {"Select",            SelectAction},
  237.   {"SVActiveEntry",     ModifySVEntry},
  238.   {"Relabel",          RelabelAction}, 
  239.   {"PopdownFileDialog", PopdownFileDialogAction},
  240.   {"quit",              ActionQuit}
  241. };
  242.  
  243. void
  244. SetApplicationActions(app_con)
  245. XtAppContext app_con;
  246. {
  247.     XtAppAddActions(app_con, actions, XtNumber(actions));
  248.     
  249. }
  250.  
  251.  
  252. /************************************************************
  253.  *
  254.  * Private functions    
  255.  *
  256.  ************************************************************/
  257.  
  258. /*    Function Name: CheckAndFindEntry
  259.  *    Description: Checks the args to make sure they are valid,
  260.  *                   then parses the arg list to find the correct action
  261.  *                   to take.
  262.  *    Arguments: action_name - name of the action (for error messages).
  263.  *                 params, num_params - params passed to the action routine.
  264.  *                 table, num_table - table to check the parameters against.
  265.  * RETURNED        type - info about the action to take.
  266.  *    Returns: TRUE if the arguments are okay.
  267.  */
  268.  
  269. static Boolean
  270. CheckAndFindEntry(action_name, params, num_params, table, num_table, type)
  271. String * params, action_name;
  272. Cardinal num_params, num_table;
  273. struct ActionValues * table;
  274. int * type;
  275. {
  276.     char buf[BUFSIZ];
  277.     int i;
  278.  
  279.     if (num_params != 1) {
  280.     sprintf(buf, "Action `%s' must have exactly one argument.", 
  281.         action_name);
  282.     SetMessage(global_screen_data.info_label, buf);
  283.     return(FALSE);
  284.     }
  285.     
  286.     XmuCopyISOLatin1Lowered(buf, params[0]);
  287.     for ( i = 0 ; i < num_table; i++ ) 
  288.     if (streq(buf, table[i].name)) {
  289.         *type = table[i].type;
  290.         return(TRUE);
  291.     }
  292.     
  293.     sprintf(buf,"Unknown parameter to action `%s' must be one of:\n", 
  294.         action_name);
  295.  
  296.     for (i = 0; i < num_table; ) {
  297.     strcat(buf, table[i++].name);
  298.     
  299.     if (i == (num_table - 1))
  300.         strcat(buf, ", or ");
  301.     else if (i < num_table)
  302.         strcat(buf, ", ");
  303.     }
  304.     
  305.     SetMessage(global_screen_data.info_label, buf);
  306.     return(FALSE);
  307. }
  308.  
  309. /*    Function Name: FindTreeNodeFromWidget
  310.  *    Description: finds the tree node associated with a widget.
  311.  *    Arguments: w - widget to check.
  312.  *    Returns: the node associated with this widget, or NULL.
  313.  */
  314.  
  315. static WNode *
  316. FindTreeNodeFromWidget(w)
  317. Widget w;
  318. {
  319.     int ret_val;
  320.     caddr_t data_return;
  321.  
  322.     /*
  323.      * Yes, I really am casting a widget to a window  ** TRUST ME ***
  324.      */
  325.  
  326.     ret_val = XFindContext(XtDisplay(w), (Window) w, NODE_INFO, &data_return);
  327.  
  328.     if (ret_val == 0) 
  329.     return((WNode *) data_return);
  330.     return(NULL);
  331. }
  332.  
  333.