actions.c revision 33c89af1
133c89af1Smrg/*
233c89af1Smrg * $Xorg: actions.c,v 1.4 2001/02/09 02:05:29 xorgcvs Exp $
333c89af1Smrg *
433c89af1SmrgCopyright 1989, 1998  The Open Group
533c89af1Smrg
633c89af1SmrgPermission to use, copy, modify, distribute, and sell this software and its
733c89af1Smrgdocumentation for any purpose is hereby granted without fee, provided that
833c89af1Smrgthe above copyright notice appear in all copies and that both that
933c89af1Smrgcopyright notice and this permission notice appear in supporting
1033c89af1Smrgdocumentation.
1133c89af1Smrg
1233c89af1SmrgThe above copyright notice and this permission notice shall be included in
1333c89af1Smrgall copies or substantial portions of the Software.
1433c89af1Smrg
1533c89af1SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1633c89af1SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1733c89af1SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1833c89af1SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1933c89af1SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2033c89af1SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2133c89af1Smrg
2233c89af1SmrgExcept as contained in this notice, the name of The Open Group shall not be
2333c89af1Smrgused in advertising or otherwise to promote the sale, use or other dealings
2433c89af1Smrgin this Software without prior written authorization from The Open Group.
2533c89af1Smrg */
2633c89af1Smrg/* $XFree86: xc/programs/editres/actions.c,v 1.6tsi Exp $ */
2733c89af1Smrg
2833c89af1Smrg#include <stdio.h>
2933c89af1Smrg#include <stdlib.h>
3033c89af1Smrg#include <X11/Intrinsic.h>
3133c89af1Smrg#include <X11/Xutil.h>
3233c89af1Smrg
3333c89af1Smrg#include <X11/Xaw/Cardinals.h>
3433c89af1Smrg#include <X11/Xmu/CharSet.h>
3533c89af1Smrg
3633c89af1Smrg#include "editresP.h"
3733c89af1Smrg
3833c89af1Smrg/*
3933c89af1Smrg * Private data.
4033c89af1Smrg */
4133c89af1Smrg
4233c89af1Smrgstruct ActionValues {
4333c89af1Smrg    String name;
4433c89af1Smrg    int type;
4533c89af1Smrg};
4633c89af1Smrg
4733c89af1Smrgstatic struct ActionValues select_values[] = {
4833c89af1Smrg    { "widget", (int) SelectWidget },
4933c89af1Smrg    { "all", (int) SelectAll },
5033c89af1Smrg    { "nothing", (int) SelectNone },
5133c89af1Smrg    { "invert", (int) SelectInvert },
5233c89af1Smrg    { "children", (int) SelectChildren },
5333c89af1Smrg    { "descendants", (int) SelectDescendants },
5433c89af1Smrg    { "parent", (int) SelectParent },
5533c89af1Smrg    { "ancestors", (int) SelectAncestors }
5633c89af1Smrg};
5733c89af1Smrg
5833c89af1Smrgstatic struct  ActionValues label_values[] = {
5933c89af1Smrg    { "name", (int) NameLabel },
6033c89af1Smrg    { "class", (int) ClassLabel },
6133c89af1Smrg    { "id", (int) IDLabel },
6233c89af1Smrg    { "window", (int) WindowLabel },
6333c89af1Smrg    { "toggle", (int) ToggleLabel }
6433c89af1Smrg};
6533c89af1Smrg
6633c89af1Smrgstatic void EnableGetVal ( Widget w, XEvent *event,
6733c89af1Smrg			   String *params, Cardinal * num_params );
6833c89af1Smrgstatic void SelectAction ( Widget w, XEvent *event,
6933c89af1Smrg			   String *params, Cardinal *num_params );
7033c89af1Smrgstatic void RelabelAction ( Widget w, XEvent *event,
7133c89af1Smrg			    String *params, Cardinal *num_params );
7233c89af1Smrgstatic void PopdownFileDialogAction ( Widget w, XEvent *event,
7333c89af1Smrg				      String *params, Cardinal *num_params );
7433c89af1Smrgstatic void ActionQuit ( Widget w, XEvent *event,
7533c89af1Smrg			 String *params, Cardinal *num_params );
7633c89af1Smrgstatic WNode * FindTreeNodeFromWidget ( Widget w );
7733c89af1Smrgstatic Boolean CheckAndFindEntry ( String action_name,
7833c89af1Smrg				   String * params, Cardinal num_params,
7933c89af1Smrg				   struct ActionValues * table,
8033c89af1Smrg				   Cardinal num_table, int * type );
8133c89af1Smrg
8233c89af1Smrg/*	Function Name: EnableGetVal
8333c89af1Smrg *	Description: sets a global variable to notify the Notify action
8433c89af1Smrg *                   for the resource list widet to do GetValues.
8533c89af1Smrg *      Arguments: w - any widget in the widget tree.
8633c89af1Smrg *                 event - NOT USED.
8733c89af1Smrg *                 params, num_params - the parameters paseed to the action
8833c89af1Smrg *                                      routine.
8933c89af1Smrg *
9033c89af1Smrg */
9133c89af1Smrg
9233c89af1SmrgBoolean do_get_values = False;
9333c89af1Smrg
9433c89af1Smrg/* ARGSUSED */
9533c89af1Smrgstatic void
9633c89af1SmrgEnableGetVal(w, event, params, num_params)
9733c89af1SmrgWidget w;
9833c89af1SmrgXEvent * event;
9933c89af1SmrgString * params;
10033c89af1SmrgCardinal * num_params;
10133c89af1Smrg{
10233c89af1Smrg  do_get_values = True;
10333c89af1Smrg}
10433c89af1Smrg
10533c89af1Smrg/*	Function Name: SelectAction
10633c89af1Smrg *	Description:
10733c89af1Smrg *      Arguments: w - any widget in the widget tree.
10833c89af1Smrg *                 event - NOT USED.
10933c89af1Smrg *                 params, num_params - the parameters paseed to the action
11033c89af1Smrg *                                      routine.
11133c89af1Smrg *
11233c89af1Smrg * params[0] - One of "nothing", "parent", "children", "ancestors",
11333c89af1Smrg *                    "descendants", "invert", "all"
11433c89af1Smrg * num_params - must be one.
11533c89af1Smrg */
11633c89af1Smrg
11733c89af1Smrg/* ARGSUSED */
11833c89af1Smrgstatic void
11933c89af1SmrgSelectAction(w, event, params, num_params)
12033c89af1SmrgWidget w;
12133c89af1SmrgXEvent * event;
12233c89af1SmrgString * params;
12333c89af1SmrgCardinal * num_params;
12433c89af1Smrg{
12533c89af1Smrg    WNode * node;
12633c89af1Smrg    int type;
12733c89af1Smrg
12833c89af1Smrg    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)
14433c89af1Smrg	    _TreeActivateNode(node, (SelectTypes)type);
14533c89af1Smrg	else
14633c89af1Smrg	    _TreeSelect(global_tree_info, (SelectTypes)type);
14733c89af1Smrg	break;
14833c89af1Smrg    }
14933c89af1Smrg}
15033c89af1Smrg
15133c89af1Smrg/*	Function Name: RelabelAction
15233c89af1Smrg *	Description:
15333c89af1Smrg *      Arguments: w - any widget in the widget tree.
15433c89af1Smrg *                 event - NOT USED.
15533c89af1Smrg *                 params, num_params - the parameters paseed to the action
15633c89af1Smrg *                                      routine.
15733c89af1Smrg *
15833c89af1Smrg * params[0] - One of "name", "class", "id"
15933c89af1Smrg * num_params - must be one.
16033c89af1Smrg */
16133c89af1Smrg
16233c89af1Smrg/* ARGSUSED */
16333c89af1Smrgstatic void
16433c89af1SmrgRelabelAction(w, event, params, num_params)
16533c89af1SmrgWidget w;
16633c89af1SmrgXEvent * event;
16733c89af1SmrgString * params;
16833c89af1SmrgCardinal * num_params;
16933c89af1Smrg{
17033c89af1Smrg    WNode * node;
17133c89af1Smrg    int type;
17233c89af1Smrg
17333c89af1Smrg    if (!CheckAndFindEntry("Relabel", params, *num_params,
17433c89af1Smrg			   label_values, XtNumber(label_values), &type))
17533c89af1Smrg	return;
17633c89af1Smrg
17733c89af1Smrg    if ((node = FindTreeNodeFromWidget(w)) == NULL)
17833c89af1Smrg	_TreeRelabel(global_tree_info, (LabelTypes)type);
17933c89af1Smrg    else {
18033c89af1Smrg	PrepareToLayoutTree(global_tree_info->tree_widget);
18133c89af1Smrg	_TreeRelabelNode(node, (LabelTypes)type, FALSE);
18233c89af1Smrg	LayoutTree(global_tree_info->tree_widget);
18333c89af1Smrg    }
18433c89af1Smrg}
18533c89af1Smrg
18633c89af1Smrg/*	Function Name: PopdownFileDialogAction
18733c89af1Smrg *	Description: Pops down the file dialog widget.
18833c89af1Smrg *                   and calls the approipriate handler.
18933c89af1Smrg *	Arguments: w - any child of the dialog widget.
19033c89af1Smrg *                 event - the event that caused this action.
19133c89af1Smrg *                 params, num_params - params passed to the action routine.
19233c89af1Smrg * RETURNED        none.
19333c89af1Smrg */
19433c89af1Smrg
19533c89af1Smrg/* ARGSUSED */
19633c89af1Smrg
19733c89af1Smrgstatic void
19833c89af1SmrgPopdownFileDialogAction(w, event, params, num_params)
19933c89af1SmrgWidget w;
20033c89af1SmrgXEvent * event;
20133c89af1SmrgString * params;
20233c89af1SmrgCardinal * num_params;
20333c89af1Smrg{
20433c89af1Smrg    char buf[BUFSIZ];
20533c89af1Smrg    Boolean val;
20633c89af1Smrg
20733c89af1Smrg    if (*num_params != 1) {
20833c89af1Smrg	sprintf(buf, res_labels[2],
20933c89af1Smrg		"PopdownFileDialog");
21033c89af1Smrg
21133c89af1Smrg	SetMessage(global_screen_data.info_label, buf);
21233c89af1Smrg	return;
21333c89af1Smrg    }
21433c89af1Smrg
21533c89af1Smrg    XmuCopyISOLatin1Lowered(buf, params[0]);
21633c89af1Smrg
21733c89af1Smrg    if (streq(buf, "cancel"))
21833c89af1Smrg	val = FALSE;
21933c89af1Smrg    else if (streq(buf, "okay"))
22033c89af1Smrg	val = TRUE;
22133c89af1Smrg    else {
22233c89af1Smrg	sprintf(buf, res_labels[1],
22333c89af1Smrg		"PopdownFileDialog");
22433c89af1Smrg
22533c89af1Smrg	SetMessage(global_screen_data.info_label, buf);
22633c89af1Smrg	return;
22733c89af1Smrg    }
22833c89af1Smrg
22933c89af1Smrg    _PopdownFileDialog(w, (XtPointer)(long) val, NULL);
23033c89af1Smrg}
23133c89af1Smrg
23233c89af1Smrg/*	Function Name: ActionQuit
23333c89af1Smrg *	Description: This function prints a message to stdout.
23433c89af1Smrg *	Arguments: w - ** UNUSED **
23533c89af1Smrg *                 call_data - ** UNUSED **
23633c89af1Smrg *                 client_data - ** UNUSED **
23733c89af1Smrg *	Returns: none
23833c89af1Smrg */
23933c89af1Smrg
24033c89af1Smrg/* ARGSUSED */
24133c89af1Smrgstatic void
24233c89af1SmrgActionQuit(w, event, params, num_params)
24333c89af1SmrgWidget w;
24433c89af1SmrgXEvent * event;
24533c89af1SmrgString * params;
24633c89af1SmrgCardinal * num_params;
24733c89af1Smrg{
24833c89af1Smrg  if (w==global_toplevel) {
24933c89af1Smrg    XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
25033c89af1Smrg    exit(0);
25133c89af1Smrg  }
25233c89af1Smrg  else  {
25333c89af1Smrg    if (streq(XtName(w), RESOURCE_BOX))
25433c89af1Smrg      global_resource_box_up = FALSE;
25533c89af1Smrg    XtPopdown(w);
25633c89af1Smrg    XtDestroyWidget(w);
25733c89af1Smrg  }
25833c89af1Smrg}
25933c89af1Smrg
26033c89af1Smrg/*	Function Name: SetApplicationActions
26133c89af1Smrg *	Description: Sets my application actions.
26233c89af1Smrg *	Arguments: app_con - the application context.
26333c89af1Smrg *	Returns: none.
26433c89af1Smrg */
26533c89af1Smrg
26633c89af1Smrgstatic XtActionsRec actions[] = {
26733c89af1Smrg  {"EnableGetVal",      EnableGetVal},
26833c89af1Smrg  {"Select",            SelectAction},
26933c89af1Smrg  {"SVActiveEntry",     ModifySVEntry},
27033c89af1Smrg  {"Relabel",      	RelabelAction},
27133c89af1Smrg  {"PopdownFileDialog", PopdownFileDialogAction},
27233c89af1Smrg  {"quit",              ActionQuit}
27333c89af1Smrg};
27433c89af1Smrg
27533c89af1Smrgvoid
27633c89af1SmrgSetApplicationActions(app_con)
27733c89af1SmrgXtAppContext app_con;
27833c89af1Smrg{
27933c89af1Smrg    XtAppAddActions(app_con, actions, XtNumber(actions));
28033c89af1Smrg
28133c89af1Smrg}
28233c89af1Smrg
28333c89af1Smrg
28433c89af1Smrg/************************************************************
28533c89af1Smrg *
28633c89af1Smrg * Private functions
28733c89af1Smrg *
28833c89af1Smrg ************************************************************/
28933c89af1Smrg
29033c89af1Smrg/*	Function Name: CheckAndFindEntry
29133c89af1Smrg *	Description: Checks the args to make sure they are valid,
29233c89af1Smrg *                   then parses the arg list to find the correct action
29333c89af1Smrg *                   to take.
29433c89af1Smrg *	Arguments: action_name - name of the action (for error messages).
29533c89af1Smrg *                 params, num_params - params passed to the action routine.
29633c89af1Smrg *                 table, num_table - table to check the parameters against.
29733c89af1Smrg * RETURNED        type - info about the action to take.
29833c89af1Smrg *	Returns: TRUE if the arguments are okay.
29933c89af1Smrg */
30033c89af1Smrg
30133c89af1Smrgstatic Boolean
30233c89af1SmrgCheckAndFindEntry(action_name, params, num_params, table, num_table, type)
30333c89af1SmrgString * params, action_name;
30433c89af1SmrgCardinal num_params, num_table;
30533c89af1Smrgstruct ActionValues * table;
30633c89af1Smrgint * type;
30733c89af1Smrg{
30833c89af1Smrg    char buf[BUFSIZ];
30933c89af1Smrg    int i;
31033c89af1Smrg
31133c89af1Smrg    if (num_params != 1) {
31233c89af1Smrg	sprintf(buf, res_labels[2],
31333c89af1Smrg		action_name);
31433c89af1Smrg	SetMessage(global_screen_data.info_label, buf);
31533c89af1Smrg	return(FALSE);
31633c89af1Smrg    }
31733c89af1Smrg
31833c89af1Smrg    XmuCopyISOLatin1Lowered(buf, params[0]);
31933c89af1Smrg    for ( i = 0 ; i < num_table; i++ )
32033c89af1Smrg	if (streq(buf, table[i].name)) {
32133c89af1Smrg	    *type = table[i].type;
32233c89af1Smrg	    return(TRUE);
32333c89af1Smrg	}
32433c89af1Smrg
32533c89af1Smrg    sprintf(buf,res_labels[3],
32633c89af1Smrg	    action_name);
32733c89af1Smrg
32833c89af1Smrg    for (i = 0; i < num_table; ) {
32933c89af1Smrg	strcat(buf, table[i++].name);
33033c89af1Smrg
33133c89af1Smrg	if (i == (num_table - 1))
33233c89af1Smrg	    strcat(buf, ", or ");
33333c89af1Smrg	else if (i < num_table)
33433c89af1Smrg	    strcat(buf, ", ");
33533c89af1Smrg    }
33633c89af1Smrg
33733c89af1Smrg    SetMessage(global_screen_data.info_label, buf);
33833c89af1Smrg    return(FALSE);
33933c89af1Smrg}
34033c89af1Smrg
34133c89af1Smrg/*	Function Name: FindTreeNodeFromWidget
34233c89af1Smrg *	Description: finds the tree node associated with a widget.
34333c89af1Smrg *	Arguments: w - widget to check.
34433c89af1Smrg *	Returns: the node associated with this widget, or NULL.
34533c89af1Smrg */
34633c89af1Smrg
34733c89af1Smrgstatic WNode *
34833c89af1SmrgFindTreeNodeFromWidget(w)
34933c89af1SmrgWidget w;
35033c89af1Smrg{
35133c89af1Smrg    int ret_val;
35233c89af1Smrg    XPointer data_return;
35333c89af1Smrg
35433c89af1Smrg    /*
35533c89af1Smrg     * Yes, I really am casting a widget to a window  ** TRUST ME ***
35633c89af1Smrg     */
35733c89af1Smrg
35833c89af1Smrg    ret_val = XFindContext(XtDisplay(w), (Window) w, NODE_INFO, &data_return);
35933c89af1Smrg
36033c89af1Smrg    if (ret_val == 0)
36133c89af1Smrg	return((WNode *) data_return);
36233c89af1Smrg    return(NULL);
36333c89af1Smrg}
36433c89af1Smrg
365