133c89af1Smrg/*
233c89af1Smrg *
333c89af1SmrgCopyright 1989, 1998  The Open Group
433c89af1Smrg
533c89af1SmrgPermission to use, copy, modify, distribute, and sell this software and its
633c89af1Smrgdocumentation for any purpose is hereby granted without fee, provided that
733c89af1Smrgthe above copyright notice appear in all copies and that both that
833c89af1Smrgcopyright notice and this permission notice appear in supporting
933c89af1Smrgdocumentation.
1033c89af1Smrg
1133c89af1SmrgThe above copyright notice and this permission notice shall be included in
1233c89af1Smrgall copies or substantial portions of the Software.
1333c89af1Smrg
1433c89af1SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1533c89af1SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1633c89af1SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1733c89af1SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1833c89af1SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1933c89af1SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2033c89af1Smrg
2133c89af1SmrgExcept as contained in this notice, the name of The Open Group shall not be
2233c89af1Smrgused in advertising or otherwise to promote the sale, use or other dealings
2333c89af1Smrgin this Software without prior written authorization from The Open Group.
2433c89af1Smrg */
2533c89af1Smrg
2638d718bdSmrg#ifdef HAVE_CONFIG_H
2738d718bdSmrg# include "config.h"
2838d718bdSmrg#endif
2938d718bdSmrg
3033c89af1Smrg#include <stdio.h>
3133c89af1Smrg#include <stdlib.h>
3233c89af1Smrg#include <X11/Intrinsic.h>
3333c89af1Smrg#include <X11/Xutil.h>
3433c89af1Smrg
35ad47b356Smrg#include <X11/Xaw/Cardinals.h>
3633c89af1Smrg#include <X11/Xmu/CharSet.h>
3733c89af1Smrg
3833c89af1Smrg#include "editresP.h"
3933c89af1Smrg
4033c89af1Smrg/*
4133c89af1Smrg * Private data.
4233c89af1Smrg */
4333c89af1Smrg
4438d718bdSmrg//struct ActionValues {
4538d718bdSmrg//    String name;
4638d718bdSmrg//    int type;
4738d718bdSmrg//};
4838d718bdSmrg
4933c89af1Smrgstruct ActionValues {
5038d718bdSmrg	const char * name;
5138d718bdSmrg        int  type;
5233c89af1Smrg};
5333c89af1Smrg
5438d718bdSmrg
5533c89af1Smrgstatic struct ActionValues select_values[] = {
5633c89af1Smrg    { "widget", (int) SelectWidget },
5733c89af1Smrg    { "all", (int) SelectAll },
5833c89af1Smrg    { "nothing", (int) SelectNone },
5933c89af1Smrg    { "invert", (int) SelectInvert },
6033c89af1Smrg    { "children", (int) SelectChildren },
6133c89af1Smrg    { "descendants", (int) SelectDescendants },
6233c89af1Smrg    { "parent", (int) SelectParent },
6333c89af1Smrg    { "ancestors", (int) SelectAncestors }
6433c89af1Smrg};
6533c89af1Smrg
6633c89af1Smrgstatic struct  ActionValues label_values[] = {
6733c89af1Smrg    { "name", (int) NameLabel },
6833c89af1Smrg    { "class", (int) ClassLabel },
6933c89af1Smrg    { "id", (int) IDLabel },
7033c89af1Smrg    { "window", (int) WindowLabel },
7133c89af1Smrg    { "toggle", (int) ToggleLabel }
7233c89af1Smrg};
7333c89af1Smrg
74ad47b356Smrgstatic void EnableGetVal ( Widget w, XEvent *event,
7533c89af1Smrg			   String *params, Cardinal * num_params );
76ad47b356Smrgstatic void SelectAction ( Widget w, XEvent *event,
7733c89af1Smrg			   String *params, Cardinal *num_params );
78ad47b356Smrgstatic void RelabelAction ( Widget w, XEvent *event,
7933c89af1Smrg			    String *params, Cardinal *num_params );
80ad47b356Smrgstatic void PopdownFileDialogAction ( Widget w, XEvent *event,
8133c89af1Smrg				      String *params, Cardinal *num_params );
82ad47b356Smrgstatic void ActionQuit ( Widget w, XEvent *event,
8333c89af1Smrg			 String *params, Cardinal *num_params );
8433c89af1Smrgstatic WNode * FindTreeNodeFromWidget ( Widget w );
8538d718bdSmrgstatic Boolean CheckAndFindEntry (  const char *action_name,
86ad47b356Smrg				   String * params, Cardinal num_params,
87ad47b356Smrg				   struct ActionValues * table,
8833c89af1Smrg				   Cardinal num_table, int * type );
8933c89af1Smrg
9033c89af1Smrg/*	Function Name: EnableGetVal
9133c89af1Smrg *	Description: sets a global variable to notify the Notify action
9233c89af1Smrg *                   for the resource list widet to do GetValues.
9333c89af1Smrg *      Arguments: w - any widget in the widget tree.
9433c89af1Smrg *                 event - NOT USED.
9533c89af1Smrg *                 params, num_params - the parameters paseed to the action
96ad47b356Smrg *                                      routine.
9733c89af1Smrg *
9833c89af1Smrg */
9933c89af1Smrg
10033c89af1SmrgBoolean do_get_values = False;
10133c89af1Smrg
10233c89af1Smrg/* ARGSUSED */
10333c89af1Smrgstatic void
104278eca22SmrgEnableGetVal(Widget w, XEvent *event, String *params, Cardinal *num_params)
10533c89af1Smrg{
10633c89af1Smrg  do_get_values = True;
10733c89af1Smrg}
10833c89af1Smrg
10933c89af1Smrg/*	Function Name: SelectAction
110ad47b356Smrg *	Description:
11133c89af1Smrg *      Arguments: w - any widget in the widget tree.
11233c89af1Smrg *                 event - NOT USED.
11333c89af1Smrg *                 params, num_params - the parameters paseed to the action
114ad47b356Smrg *                                      routine.
11533c89af1Smrg *
11633c89af1Smrg * params[0] - One of "nothing", "parent", "children", "ancestors",
11733c89af1Smrg *                    "descendants", "invert", "all"
11833c89af1Smrg * num_params - must be one.
11933c89af1Smrg */
12033c89af1Smrg
12133c89af1Smrg/* ARGSUSED */
12233c89af1Smrgstatic void
123278eca22SmrgSelectAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
12433c89af1Smrg{
12533c89af1Smrg    WNode * node;
12633c89af1Smrg    int type;
12733c89af1Smrg
128ad47b356Smrg    if (!CheckAndFindEntry("Select", params, *num_params,
12933c89af1Smrg			   select_values, XtNumber(select_values), &type))
13033c89af1Smrg	return;
13133c89af1Smrg
13233c89af1Smrg    switch(type) {
13333c89af1Smrg    case SelectAll:
13433c89af1Smrg    case SelectNone:
13533c89af1Smrg    case SelectInvert:
13633c89af1Smrg	_TreeSelect(global_tree_info, (SelectTypes)type);
13733c89af1Smrg	break;
13833c89af1Smrg    case SelectWidget:
13933c89af1Smrg	_FindWidget(XtParent(w));
14033c89af1Smrg	break;
14133c89af1Smrg    default:
14233c89af1Smrg	node = FindTreeNodeFromWidget(w);
14333c89af1Smrg	if (node)
144ad47b356Smrg	    _TreeActivateNode(node, (SelectTypes)type);
14533c89af1Smrg	else
14633c89af1Smrg	    _TreeSelect(global_tree_info, (SelectTypes)type);
14733c89af1Smrg	break;
14833c89af1Smrg    }
14933c89af1Smrg}
15033c89af1Smrg
15133c89af1Smrg/*	Function Name: RelabelAction
152ad47b356Smrg *	Description:
15333c89af1Smrg *      Arguments: w - any widget in the widget tree.
15433c89af1Smrg *                 event - NOT USED.
15533c89af1Smrg *                 params, num_params - the parameters paseed to the action
156ad47b356Smrg *                                      routine.
15733c89af1Smrg *
15833c89af1Smrg * params[0] - One of "name", "class", "id"
15933c89af1Smrg * num_params - must be one.
16033c89af1Smrg */
16133c89af1Smrg
16233c89af1Smrg/* ARGSUSED */
16333c89af1Smrgstatic void
164278eca22SmrgRelabelAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
16533c89af1Smrg{
16633c89af1Smrg    WNode * node;
16733c89af1Smrg    int type;
16833c89af1Smrg
169ad47b356Smrg    if (!CheckAndFindEntry("Relabel", params, *num_params,
17033c89af1Smrg			   label_values, XtNumber(label_values), &type))
17133c89af1Smrg	return;
17233c89af1Smrg
173ad47b356Smrg    if ((node = FindTreeNodeFromWidget(w)) == NULL)
17433c89af1Smrg	_TreeRelabel(global_tree_info, (LabelTypes)type);
17533c89af1Smrg    else {
176ad47b356Smrg	PrepareToLayoutTree(global_tree_info->tree_widget);
17733c89af1Smrg	_TreeRelabelNode(node, (LabelTypes)type, FALSE);
178ad47b356Smrg	LayoutTree(global_tree_info->tree_widget);
17933c89af1Smrg    }
18033c89af1Smrg}
18133c89af1Smrg
18233c89af1Smrg/*	Function Name: PopdownFileDialogAction
18333c89af1Smrg *	Description: Pops down the file dialog widget.
18433c89af1Smrg *                   and calls the approipriate handler.
18533c89af1Smrg *	Arguments: w - any child of the dialog widget.
18633c89af1Smrg *                 event - the event that caused this action.
18733c89af1Smrg *                 params, num_params - params passed to the action routine.
18833c89af1Smrg * RETURNED        none.
18933c89af1Smrg */
19033c89af1Smrg
19133c89af1Smrg/* ARGSUSED */
19233c89af1Smrg
19333c89af1Smrgstatic void
194278eca22SmrgPopdownFileDialogAction(Widget w, XEvent *event,
195278eca22Smrg			String *params, Cardinal *num_params)
19633c89af1Smrg{
19733c89af1Smrg    char buf[BUFSIZ];
19833c89af1Smrg    Boolean val;
19933c89af1Smrg
20033c89af1Smrg    if (*num_params != 1) {
201ad47b356Smrg	snprintf(buf, sizeof(buf), res_labels[2], "PopdownFileDialog");
20233c89af1Smrg
20333c89af1Smrg	SetMessage(global_screen_data.info_label, buf);
20433c89af1Smrg	return;
20533c89af1Smrg    }
20633c89af1Smrg
20733c89af1Smrg    XmuCopyISOLatin1Lowered(buf, params[0]);
20833c89af1Smrg
20933c89af1Smrg    if (streq(buf, "cancel"))
21033c89af1Smrg	val = FALSE;
21133c89af1Smrg    else if (streq(buf, "okay"))
21233c89af1Smrg	val = TRUE;
21333c89af1Smrg    else {
214ad47b356Smrg	snprintf(buf, sizeof(buf), res_labels[1], "PopdownFileDialog");
21533c89af1Smrg
21633c89af1Smrg	SetMessage(global_screen_data.info_label, buf);
21733c89af1Smrg	return;
21833c89af1Smrg    }
21933c89af1Smrg
22033c89af1Smrg    _PopdownFileDialog(w, (XtPointer)(long) val, NULL);
22133c89af1Smrg}
22233c89af1Smrg
22333c89af1Smrg/*	Function Name: ActionQuit
22433c89af1Smrg *	Description: This function prints a message to stdout.
22533c89af1Smrg *	Arguments: w - ** UNUSED **
22633c89af1Smrg *                 call_data - ** UNUSED **
22733c89af1Smrg *                 client_data - ** UNUSED **
22833c89af1Smrg *	Returns: none
22933c89af1Smrg */
23033c89af1Smrg
23133c89af1Smrg/* ARGSUSED */
23233c89af1Smrgstatic void
233278eca22SmrgActionQuit(Widget w, XEvent *event, String *params, Cardinal *num_params)
23433c89af1Smrg{
23533c89af1Smrg  if (w==global_toplevel) {
23633c89af1Smrg    XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
23733c89af1Smrg    exit(0);
23833c89af1Smrg  }
23933c89af1Smrg  else  {
24033c89af1Smrg    if (streq(XtName(w), RESOURCE_BOX))
24133c89af1Smrg      global_resource_box_up = FALSE;
24233c89af1Smrg    XtPopdown(w);
24333c89af1Smrg    XtDestroyWidget(w);
24433c89af1Smrg  }
24533c89af1Smrg}
24633c89af1Smrg
24733c89af1Smrg/*	Function Name: SetApplicationActions
24833c89af1Smrg *	Description: Sets my application actions.
24933c89af1Smrg *	Arguments: app_con - the application context.
25033c89af1Smrg *	Returns: none.
25133c89af1Smrg */
25233c89af1Smrg
25333c89af1Smrgstatic XtActionsRec actions[] = {
25438d718bdSmrg  {(String)"EnableGetVal",      EnableGetVal},
25538d718bdSmrg  {(String)"Select",            SelectAction},
25638d718bdSmrg  {(String)"SVActiveEntry",     ModifySVEntry},
25738d718bdSmrg  {(String)"Relabel",      	RelabelAction},
25838d718bdSmrg  {(String)"PopdownFileDialog", PopdownFileDialogAction},
25938d718bdSmrg  {(String)"quit",              ActionQuit}
26033c89af1Smrg};
26133c89af1Smrg
26233c89af1Smrgvoid
263278eca22SmrgSetApplicationActions(XtAppContext app_con)
26433c89af1Smrg{
26533c89af1Smrg    XtAppAddActions(app_con, actions, XtNumber(actions));
266ad47b356Smrg
26733c89af1Smrg}
26833c89af1Smrg
26933c89af1Smrg
27033c89af1Smrg/************************************************************
27133c89af1Smrg *
272ad47b356Smrg * Private functions
27333c89af1Smrg *
27433c89af1Smrg ************************************************************/
27533c89af1Smrg
27633c89af1Smrg/*	Function Name: CheckAndFindEntry
27733c89af1Smrg *	Description: Checks the args to make sure they are valid,
27833c89af1Smrg *                   then parses the arg list to find the correct action
27933c89af1Smrg *                   to take.
28033c89af1Smrg *	Arguments: action_name - name of the action (for error messages).
28133c89af1Smrg *                 params, num_params - params passed to the action routine.
28233c89af1Smrg *                 table, num_table - table to check the parameters against.
28333c89af1Smrg * RETURNED        type - info about the action to take.
28433c89af1Smrg *	Returns: TRUE if the arguments are okay.
28533c89af1Smrg */
28633c89af1Smrg
28733c89af1Smrgstatic Boolean
28838d718bdSmrgCheckAndFindEntry(const char *action_name, String *params, Cardinal num_params,
289278eca22Smrg		  struct ActionValues *table, Cardinal num_table, int *type)
29033c89af1Smrg{
29133c89af1Smrg    char buf[BUFSIZ];
292ad47b356Smrg    Cardinal i;
29333c89af1Smrg
29433c89af1Smrg    if (num_params != 1) {
295ad47b356Smrg	snprintf(buf, sizeof(buf), res_labels[2], action_name);
29633c89af1Smrg	SetMessage(global_screen_data.info_label, buf);
29733c89af1Smrg	return(FALSE);
29833c89af1Smrg    }
299ad47b356Smrg
30033c89af1Smrg    XmuCopyISOLatin1Lowered(buf, params[0]);
301ad47b356Smrg    for ( i = 0 ; i < num_table; i++ )
30233c89af1Smrg	if (streq(buf, table[i].name)) {
30333c89af1Smrg	    *type = table[i].type;
30433c89af1Smrg	    return(TRUE);
30533c89af1Smrg	}
306ad47b356Smrg
307ad47b356Smrg    snprintf(buf, sizeof(buf), res_labels[3], action_name);
30833c89af1Smrg
30933c89af1Smrg    for (i = 0; i < num_table; ) {
31033c89af1Smrg	strcat(buf, table[i++].name);
311ad47b356Smrg
31233c89af1Smrg	if (i == (num_table - 1))
31333c89af1Smrg	    strcat(buf, ", or ");
31433c89af1Smrg	else if (i < num_table)
31533c89af1Smrg	    strcat(buf, ", ");
31633c89af1Smrg    }
317ad47b356Smrg
31833c89af1Smrg    SetMessage(global_screen_data.info_label, buf);
31933c89af1Smrg    return(FALSE);
32033c89af1Smrg}
32133c89af1Smrg
32233c89af1Smrg/*	Function Name: FindTreeNodeFromWidget
32333c89af1Smrg *	Description: finds the tree node associated with a widget.
32433c89af1Smrg *	Arguments: w - widget to check.
32533c89af1Smrg *	Returns: the node associated with this widget, or NULL.
32633c89af1Smrg */
32733c89af1Smrg
32833c89af1Smrgstatic WNode *
329278eca22SmrgFindTreeNodeFromWidget(Widget w)
33033c89af1Smrg{
33133c89af1Smrg    int ret_val;
33233c89af1Smrg    XPointer data_return;
33333c89af1Smrg
33433c89af1Smrg    /*
33533c89af1Smrg     * Yes, I really am casting a widget to a window  ** TRUST ME ***
33633c89af1Smrg     */
33733c89af1Smrg
33833c89af1Smrg    ret_val = XFindContext(XtDisplay(w), (Window) w, NODE_INFO, &data_return);
33933c89af1Smrg
340ad47b356Smrg    if (ret_val == 0)
34133c89af1Smrg	return((WNode *) data_return);
34233c89af1Smrg    return(NULL);
34333c89af1Smrg}
34433c89af1Smrg
345