home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / StrToWidg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-23  |  5.8 KB  |  177 lines

  1. /* $XConsortium: StrToWidg.c,v 1.8 91/07/23 15:19:48 converse Exp $ */
  2.  
  3. /* Copyright 1988, 1991 Massachusetts Institute of Technology
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of M.I.T. not be used in advertising or
  10.  * publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.  M.I.T. makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  17.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  *
  23.  * XmuCvtStringToWidget
  24.  *
  25.  *   static XtConvertArgRec parentCvtArgs[] = {
  26.  *    {XtBaseOffset, (caddr_t)XtOffset(Widget, core.parent), sizeof(Widget)},
  27.  *   };
  28.  *
  29.  * matches the string against the name of the immediate children (normal
  30.  * or popup) of the parent.  If none match, compares string to classname
  31.  * & returns first match.  Case is significant.
  32.  */
  33. #include <X11/IntrinsicP.h>
  34. #include <X11/StringDefs.h>
  35. #include <X11/ObjectP.h>
  36.  
  37. #define    done(address, type) \
  38.     { toVal->size = sizeof(type); \
  39.       toVal->addr = (XPointer) address; \
  40.       return; \
  41.     }
  42.  
  43. /* ARGSUSED */
  44. void XmuCvtStringToWidget(args, num_args, fromVal, toVal)
  45.     XrmValuePtr args;        /* parent */
  46.     Cardinal    *num_args;      /* 1 */
  47.     XrmValuePtr fromVal;
  48.     XrmValuePtr toVal;
  49. {
  50.     static Widget widget, *widgetP, parent;
  51.     XrmName name = XrmStringToName(fromVal->addr);
  52.     int i;
  53.  
  54.     if (*num_args != 1)
  55.     XtErrorMsg("wrongParameters", "cvtStringToWidget", "xtToolkitError",
  56.            "StringToWidget conversion needs parent arg", NULL, 0);
  57.  
  58.     parent = *(Widget*)args[0].addr;
  59.     /* try to match names of normal children */
  60.     if (XtIsComposite(parent)) {
  61.     i = ((CompositeWidget)parent)->composite.num_children;
  62.     for (widgetP = ((CompositeWidget)parent)->composite.children;
  63.          i; i--, widgetP++) {
  64.         if ((*widgetP)->core.xrm_name == name) {
  65.         widget = *widgetP;
  66.         done(&widget, Widget);
  67.         }
  68.     }
  69.     }
  70.     /* try to match names of popup children */
  71.     i = parent->core.num_popups;
  72.     for (widgetP = parent->core.popup_list; i; i--, widgetP++) {
  73.     if ((*widgetP)->core.xrm_name == name) {
  74.         widget = *widgetP;
  75.         done(&widget, Widget);
  76.     }
  77.     }
  78.     /* try to match classes of normal children */
  79.     if (XtIsComposite(parent)) {
  80.     i = ((CompositeWidget)parent)->composite.num_children;
  81.     for (widgetP = ((CompositeWidget)parent)->composite.children;
  82.          i; i--, widgetP++) {
  83.         if ((*widgetP)->core.widget_class->core_class.xrm_class == name) {
  84.         widget = *widgetP;
  85.         done(&widget, Widget);
  86.         }
  87.     }
  88.     }
  89.     /* try to match classes of popup children */
  90.     i = parent->core.num_popups;
  91.     for (widgetP = parent->core.popup_list; i; i--, widgetP++) {
  92.     if ((*widgetP)->core.widget_class->core_class.xrm_class == name) {
  93.         widget = *widgetP;
  94.         done(&widget, Widget);
  95.     }
  96.     }
  97.     XtStringConversionWarning(fromVal->addr, XtRWidget);
  98.     toVal->addr = NULL;
  99.     toVal->size = 0;
  100. }
  101.  
  102. #undef done
  103.  
  104. #define    newDone(type, value) \
  105.     {                            \
  106.         if (toVal->addr != NULL) {                \
  107.         if (toVal->size < sizeof(type)) {        \
  108.             toVal->size = sizeof(type);            \
  109.             return False;                \
  110.         }                        \
  111.         *(type*)(toVal->addr) = (value);        \
  112.         }                            \
  113.         else {                        \
  114.         static type static_val;                \
  115.         static_val = (value);                \
  116.         toVal->addr = (XtPointer)&static_val;        \
  117.         }                            \
  118.         toVal->size = sizeof(type);                \
  119.         return True;                    \
  120.     }
  121.  
  122.  
  123. /*ARGSUSED*/
  124. Boolean XmuNewCvtStringToWidget(dpy, args, num_args, fromVal, toVal, 
  125.                 converter_data)
  126.      Display *dpy;
  127.      XrmValue *args;        /* parent */
  128.      Cardinal *num_args;    /* 1 */
  129.      XrmValue *fromVal;
  130.      XrmValue *toVal;
  131.      XtPointer *converter_data;
  132. {
  133.     Widget *widgetP, parent;
  134.     XrmName name = XrmStringToName(fromVal->addr);
  135.     int i;
  136.  
  137.     if (*num_args != 1)
  138.     XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
  139.             "wrongParameters","cvtStringToWidget","xtToolkitError",
  140.             "String To Widget conversion needs parent argument",
  141.             (String *)NULL, (Cardinal *)NULL);
  142.  
  143.     parent = *(Widget*)args[0].addr;
  144.     /* try to match names of normal children */
  145.     if (XtIsComposite(parent)) {
  146.     i = ((CompositeWidget)parent)->composite.num_children;
  147.     for (widgetP = ((CompositeWidget)parent)->composite.children;
  148.          i; i--, widgetP++) {
  149.         if ((*widgetP)->core.xrm_name == name)
  150.         newDone(Widget, *widgetP);
  151.     }
  152.     }
  153.     /* try to match names of popup children */
  154.     i = parent->core.num_popups;
  155.     for (widgetP = parent->core.popup_list; i; i--, widgetP++) {
  156.     if ((*widgetP)->core.xrm_name == name)
  157.         newDone(Widget, *widgetP);
  158.     }
  159.     /* try to match classes of normal children */
  160.     if (XtIsComposite(parent)) {
  161.     i = ((CompositeWidget)parent)->composite.num_children;
  162.     for (widgetP = ((CompositeWidget)parent)->composite.children;
  163.          i; i--, widgetP++) {
  164.         if ((*widgetP)->core.widget_class->core_class.xrm_class == name)
  165.         newDone(Widget, *widgetP);
  166.     }
  167.     }
  168.     /* try to match classes of popup children */
  169.     i = parent->core.num_popups;
  170.     for (widgetP = parent->core.popup_list; i; i--, widgetP++) {
  171.     if ((*widgetP)->core.widget_class->core_class.xrm_class == name)
  172.         newDone(Widget, *widgetP);
  173.     }
  174.     XtDisplayStringConversionWarning(dpy, (String)fromVal->addr, XtRWidget);
  175.     return False;
  176. }
  177.