home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / l / libxtgeo.zip / libgeo17 / geoTattler.c < prev    next >
C/C++ Source or Header  |  1992-06-23  |  5KB  |  234 lines

  1. #include "IntrinsicI.h"      /* for the class_name field */
  2. #include "VarargsI.h"        /* for _GeoPrintTrace */
  3. #include <X11/StringDefs.h>  /* XtRBoolean .. */
  4. #include "geoTattler.h"
  5.  
  6.  
  7. /**************************************************************************
  8.  GeoTattler API:
  9.   
  10.   if those lines are found in the resource database:
  11.  
  12.     myapp*draw.XmScale.geoTattler: ON
  13.     *XmScrollBar.geoTattler:ON
  14.     *XmRowColumn.exit_button.geoTattler:ON
  15.  
  16.    then:
  17.  
  18.     all the XmScale children of the widget named draw,
  19.     all the XmScrollBars,
  20.     the widget named exit_button in any XmRowColumn
  21.  
  22.    will return True to the function _GeoIsTattled(), and will generate
  23.    outlined trace in stdout.
  24.  
  25. *************************************************************************/
  26.  
  27. typedef struct {   Boolean   geo_tattler ;} GeoDataRec ;
  28.  
  29.  
  30. static XtResource geo_resources[] = {
  31.     { XtNgeoTattler, XtCGeoTattler, XtRBoolean, sizeof(Boolean),
  32.       XtOffsetOf(GeoDataRec, geo_tattler), 
  33.       XtRImmediate, (caddr_t) False }
  34. };
  35.  
  36. /* Now define a simple cache mechanism to be used in the main function */
  37.  
  38. typedef struct _GeoTatCache
  39. {
  40.    Widget widget;
  41.    String widget_name;
  42.    String class_name;
  43.    Boolean is_geotattled ;
  44.    struct _GeoTatCache * next;
  45. } GeoTatCache;
  46.  
  47. static GeoTatCache * geotat_cache = NULL;
  48.  
  49.  
  50.  
  51.  
  52. /****************************************************************
  53.   This function return True is the widget is already in the cache,
  54.   False otherwise. It also return the current value if True */
  55. Boolean 
  56. #if NeedFunctionPrototypes
  57. _GeoIsCached (Widget widget, Boolean* is_geotattled)
  58. #else
  59. _GeoIsCached (widget, is_geotattled)
  60. Widget widget;
  61. Boolean * is_geotattled ;
  62. #endif
  63. {
  64.     register GeoTatCache * cache_ptr;
  65.     
  66.     for (cache_ptr = geotat_cache; cache_ptr; cache_ptr = cache_ptr->next) {
  67.     if ((cache_ptr->widget == widget) &&
  68.         (strcmp (cache_ptr->widget_name, XtName(widget)) == 0)  &&
  69.         (strcmp (cache_ptr->class_name, XtClassName(widget)) == 0)) {
  70.         /* if something found return its state and True */
  71.  
  72.         *is_geotattled = cache_ptr->is_geotattled ;
  73.         return (True);
  74.     }
  75.     }
  76.     
  77.     return (False);
  78. }
  79.  
  80.  
  81. /****************************************************************
  82.    This function looks for a widget in the cache and if found, assigns
  83.    a new value for the is_geotattled field 
  84.    It is used for dynamic change (e.g. editres stuff) */
  85. void 
  86. #if NeedFunctionPrototypes
  87. _GeoChangeCache (Widget widget, Boolean geo_tat)
  88. #else
  89. _GeoChangeCache (widget, geo_tat)
  90. Widget widget;
  91. Boolean geo_tat ;
  92. #endif
  93. {
  94.     register GeoTatCache * cache_ptr;
  95.  
  96.     for (cache_ptr = geotat_cache; cache_ptr; cache_ptr = cache_ptr->next) {
  97.     if ((cache_ptr->widget == widget) &&
  98.         (strcmp (cache_ptr->widget_name, XtName(widget)) == 0)  &&
  99.         (strcmp (cache_ptr->class_name, XtClassName(widget)) == 0)) {
  100.         /* if something found, change the value */
  101.  
  102.         cache_ptr->is_geotattled = geo_tat;
  103.         return ;
  104.     }
  105.     }
  106.     
  107.     return ;
  108. }
  109.  
  110. /****************************************************************
  111.    This function adds an entry in the cache, based on the given widget
  112.    and bool */
  113.  
  114. void 
  115. #if NeedFunctionPrototypes
  116. _GeoCache (Widget widget, Boolean geo_tat)
  117. #else
  118. _GeoCache (widget, geo_tat)
  119. Widget widget;
  120. Boolean geo_tat ;
  121. #endif
  122. {
  123.     register GeoTatCache * cache_ptr;
  124.  
  125.     /* create a new entry in the geo cache */
  126.  
  127.     cache_ptr = XtNew (GeoTatCache);
  128.     cache_ptr->next = geotat_cache;
  129.     geotat_cache = cache_ptr;
  130.  
  131.     /* fill it, please, regular */
  132.  
  133.     cache_ptr->widget = widget ;
  134.     cache_ptr->widget_name = XtNewString(XtName(widget));
  135.     cache_ptr->class_name = XtNewString(XtClassName(widget));
  136.     cache_ptr->is_geotattled = geo_tat ;
  137. }
  138.  
  139. /************************************************************************
  140.   This function uses XtGetSubresources to find out if a widget
  141.   needs to be geo-spied by the caller.
  142.   Since XtGetSubresources is expensive, it also uses a cache. */
  143.  
  144. Boolean 
  145. #if NeedFunctionPrototypes
  146. _GeoIsTattled (Widget widget)
  147. #else
  148. _GeoIsTattled (widget)
  149. Widget widget;
  150. #endif
  151. {
  152.     GeoDataRec geo_data ;
  153.     Boolean is_geotattled, cach ;
  154.  
  155.        /* First check for a matching widget  */
  156.     if (_GeoIsCached (widget, &is_geotattled)) return is_geotattled ;
  157.  
  158.        /* no widget found in the cache, look in the database and cache */
  159.     XtGetSubresources(widget, (XtPointer)&geo_data,
  160.                       (String)NULL, (String)NULL, 
  161.               geo_resources, XtNumber(geo_resources), 
  162.               NULL, 0);
  163.     
  164.        /* add in the cache */
  165.     _GeoCache(widget, geo_data.geo_tattler) ;
  166.  
  167.     return geo_data.geo_tattler;
  168.  
  169. }  /* _GeoIsTattled */
  170.  
  171.  
  172. /************************************************************************
  173.   Trace stuff now */
  174.  
  175. static n_tab = 0 ;
  176.  
  177. void 
  178. #if NeedFunctionPrototypes
  179. _GeoTabTrace (void)
  180. #else
  181. _GeoTabTrace ()
  182. #endif
  183. {
  184.     n_tab ++ ;
  185. }
  186.  
  187.  
  188. void
  189. #if NeedFunctionPrototypes
  190. _GeoUnTabTrace (void)
  191. #else
  192. _GeoUnTabTrace ()
  193. #endif
  194. {
  195.     n_tab -- ;
  196. }
  197.  
  198.  
  199. void 
  200. #if NeedFunctionPrototypes
  201. _GeoPrintTab (void)
  202. #else
  203. _GeoPrintTab ()
  204. #endif
  205. {
  206.     int i ;
  207.  
  208.     for (i=0; i<n_tab; i++) printf("     ");
  209. }
  210.  
  211.  
  212. void 
  213. #if NeedVarargsPrototypes
  214. _GeoPrintTrace (Widget widget, ...)
  215. #else
  216. _GeoPrintTrace (widget, va_alist)
  217. Widget widget;
  218. va_dcl
  219. #endif
  220. {
  221.     va_list args;
  222.     char *fmt;
  223.  
  224.     if (_GeoIsTattled(widget)) {
  225.     Va_start(args, widget);
  226.     fmt = va_arg(args, char *);
  227.     _GeoPrintTab ();
  228.     (void) vprintf(fmt, args);
  229.     va_end(args);
  230.     }
  231. }
  232.  
  233.  
  234.