StrToGrav.c revision 6c321187
1/* $Xorg: StrToGrav.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
2
3/*
4
5Copyright 1989, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27*/
28/* $XFree86: xc/lib/Xmu/StrToGrav.c,v 1.6 2001/01/17 19:42:57 dawes Exp $ */
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <X11/Intrinsic.h>
34#include <X11/Xmu/Converters.h>
35#include <X11/Xmu/CharSet.h>
36
37#define done(address, type) \
38{ (*toVal).size = sizeof(type); (*toVal).addr = (XPointer) address; }
39
40/*
41 * Initialization
42 */
43static struct _namepair {
44    XrmQuark quark;
45    char *name;
46    XtGravity gravity;
47} names[] = {
48    { NULLQUARK, XtEForget, ForgetGravity },
49    { NULLQUARK, XtENorthWest, NorthWestGravity },
50    { NULLQUARK, XtENorth, NorthGravity },
51    { NULLQUARK, XtENorthEast, NorthEastGravity },
52    { NULLQUARK, XtEWest, WestGravity },
53    { NULLQUARK, XtECenter, CenterGravity },
54    { NULLQUARK, XtEEast, EastGravity },
55    { NULLQUARK, XtESouthWest, SouthWestGravity },
56    { NULLQUARK, XtESouth, SouthGravity },
57    { NULLQUARK, XtESouthEast, SouthEastGravity },
58    { NULLQUARK, XtEStatic, StaticGravity },
59    { NULLQUARK, XtEUnmap, UnmapGravity },
60    { NULLQUARK, XtEleft, WestGravity },
61    { NULLQUARK, XtEtop, NorthGravity },
62    { NULLQUARK, XtEright, EastGravity },
63    { NULLQUARK, XtEbottom, SouthGravity },
64    { NULLQUARK, NULL, ForgetGravity }
65};
66
67/*
68 * This function is deprecated as of the addition of
69 * XtCvtStringToGravity in R6
70 */
71void
72XmuCvtStringToGravity(XrmValuePtr args, Cardinal *num_args,
73		      XrmValuePtr fromVal, XrmValuePtr toVal)
74{
75  static Boolean haveQuarks = False;
76  char name[10];
77    XrmQuark q;
78    struct _namepair *np;
79
80    if (*num_args != 0)
81        XtWarningMsg("wrongParameters","cvtStringToGravity","XtToolkitError",
82                  "String to Gravity conversion needs no extra arguments",
83		 (String *)NULL, (Cardinal *)NULL);
84
85  if (!haveQuarks)
86    {
87      for (np = names; np->name; np++)
88	np->quark = XrmPermStringToQuark(np->name);
89      haveQuarks = True;
90    }
91
92  XmuNCopyISOLatin1Lowered(name, (char *)fromVal->addr, sizeof(name));
93  q = XrmStringToQuark(name);
94
95  for (np = names; np->name; np++)
96    {
97      if (np->quark == q)
98	{
99	  done(&np->gravity, XtGravity);
100		return;
101	    }
102	}
103
104  XtStringConversionWarning((char *)fromVal->addr, XtRGravity);
105}
106
107/*ARGSUSED*/
108Boolean
109XmuCvtGravityToString(Display *dpy, XrmValue *args, Cardinal *num_args,
110		      XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
111{
112  static char *buffer;
113  Cardinal size;
114  struct _namepair *np;
115  XtGravity gravity;
116
117  gravity = *(XtGravity *)fromVal->addr;
118  buffer = NULL;
119  for (np = names; np->name; np++)
120    if (np->gravity == gravity)
121      {
122	buffer = np->name;
123	break;
124      }
125
126  if (!buffer)
127    {
128      XtAppWarning(XtDisplayToApplicationContext(dpy),
129		   "Cannot convert Gravity to String");
130      toVal->addr = NULL;
131      toVal->size = 0;
132
133      return (False);
134    }
135
136  size = strlen(buffer) + 1;
137  if (toVal->addr != NULL)
138    {
139      if (toVal->size <= size)
140	{
141	  toVal->size = size;
142	  return (False);
143	}
144      strcpy((char *)toVal->addr, buffer);
145    }
146  else
147    toVal->addr = (XPointer)buffer;
148  toVal->size = size;
149
150  return (True);
151}
152