setvalues.c revision ad47b356
1/*
2 *
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24 *
25 * Author:  Chris D. Peterson, MIT X Consortium
26 */
27
28#include <X11/Intrinsic.h>
29#include <X11/StringDefs.h>
30#include <X11/Xresource.h>
31
32#include <stdio.h>
33
34#include <X11/Xaw/AsciiText.h>
35#include <X11/Xaw/Cardinals.h>
36#include <X11/Xfuncs.h>
37#include <X11/Xos.h>
38#include "editresP.h"
39
40#define RESOURCE_NAME ("name")
41#define RESOURCE_CLASS ("Class")
42
43/*	Function Name: PrintSetValuesError
44 *	Description: Allow the SetValues error to be printed.
45 *	Arguments: event - the set values call that caused this event.
46 *	Returns: str - a string contining the errors.
47 */
48
49char *
50PrintSetValuesError(Event *event)
51{
52    char * errors = NULL;
53    WNode * node;
54    int i;
55    SetValuesEvent * sv_event = (SetValuesEvent *) event;
56    char buf[BUFSIZ];
57
58    if (sv_event->num_entries == 0)
59	return(XtNewString("SetValues was Successful."));
60
61    for (i = 0 ; i < (int)sv_event->num_entries ; i++) {
62	node = FindNode(global_tree_info->top_node,
63			sv_event->info[i].widgets.ids,
64			sv_event->info[i].widgets.num_widgets);
65
66	if (node == NULL) {
67	    snprintf(buf, sizeof(buf),
68                     "Editres Internal Error: Unable to FindNode.\n");
69	    AddString(&errors, buf);
70	    continue;
71	}
72
73	snprintf(buf, sizeof(buf), "%s(0x%lx) - %s\n", node->name, node->id,
74                 sv_event->info[i].message);
75	AddString(&errors, buf);
76    }
77    return(errors);
78}
79
80/*	Function Name: GetResourceValueForSetValues(node);
81 *	Description: Returns the value that should be sent to SetValues.
82 *	Arguments: node - the node which contains the resource box.
83 *	Returns: value - allocated value.
84 */
85
86char *
87GetResourceValueForSetValues(WNode *node, unsigned short *size)
88{
89    Arg args[1];
90    char *ptr, *temp;
91    XrmDatabase db = NULL;
92    XrmValue value;
93
94    XtSetArg(args[0], XtNstring, &ptr);
95    XtGetValues(node->resources->res_box->value_wid, args, ONE);
96
97    /*
98     * This makes sure that exactly the same thing happens during a set
99     * values, that would happend of we were to insert this value into
100     * the resource database.
101     */
102
103    XtAsprintf(&temp, "%s:%s", RESOURCE_NAME, ptr);
104    XrmPutLineResource(&db, temp);
105    XtFree(temp);
106
107    XrmGetResource(db, RESOURCE_NAME, RESOURCE_CLASS, &temp, &value);
108
109    ptr = XtMalloc(sizeof(char) * value.size);
110    memmove( ptr, value.addr, value.size);
111    XrmDestroyDatabase(db);
112
113    *size = (unsigned short) value.size;
114    return(ptr);
115}
116