home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / tcl / tclmotif.1 / tclmotif / tm.1.2 / src / tmUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-03  |  8.8 KB  |  456 lines

  1. /*
  2.  * tmUtils.c --
  3.  *    This module contains general purpose routines
  4.  *    used by the Tm toolkit.
  5.  *
  6.  * Copyright 1993 Jan Newmarch, University of Canberra.
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The author
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #include <sys/time.h>
  17. #include <ctype.h>
  18. #include <X11/keysym.h>
  19. #include "tm.h"
  20. #include "tmFuncs.h"
  21.  
  22. /*
  23.  *--------------------------------------------------------------
  24.  *
  25.  * Tm_StoreWidgetInfo --
  26.  *
  27.  *    create a hash table entry for a new widget, with useful
  28.  *    info in it.
  29.  *
  30.  * Results:
  31.  *
  32.  *    modifies hash table in "interp"
  33.  *
  34.  * Side effects:
  35.  *
  36.  *--------------------------------------------------------------
  37.  */
  38.  
  39. void
  40. Tm_StoreWidgetInfo(path, w, interp)
  41.     char *path;
  42.     Tm_Widget *w;
  43.     Tcl_Interp *interp;
  44. {
  45.     int new;
  46.     Tcl_HashEntry *hPtr;
  47.  
  48. /*
  49.     hPtr = Tcl_CreateHashEntry(&WidgetTable, path, &new);
  50.     Tcl_SetHashValue(hPtr, (ClientData) w);
  51. */
  52. }
  53.  
  54.  
  55. /*
  56.  *--------------------------------------------------------------
  57.  *
  58.  * Tm_NameFromPath --
  59.  *
  60.  *    find the part of the path after the last '.'
  61.  *
  62.  * Results:
  63.  *
  64.  *    returns a pointer to the name
  65.  *
  66.  * Side effects:
  67.  *
  68.  *--------------------------------------------------------------
  69.  */
  70.  
  71. char *
  72. Tm_NameFromPath(pathName)
  73.     char *pathName;
  74. {
  75.     char *p;
  76.  
  77.     if ((p = strrchr(pathName, '.')) == NULL) {
  78.     return pathName;
  79.     } else {
  80.     return (p + 1);
  81.     }
  82. }
  83.  
  84.  
  85.  
  86. /*
  87.  *--------------------------------------------------------------
  88.  *
  89.  * Tm_HiddenParentPath --
  90.  *
  91.  *    Create a path based on current path that cannot be duplicated
  92.  *    actually, makes "first.last" into "first..last"
  93.  *
  94.  * Results:
  95.  *
  96.  *    returns a pointer to the name
  97.  *
  98.  * Side effects:
  99.  *
  100.  *    allocates memory
  101.  *--------------------------------------------------------------
  102.  */
  103.  
  104. char *
  105. Tm_HiddenParentPath(path)
  106.     char *path;
  107. {
  108.     char *p;
  109.     char *parent;
  110.     int len;
  111.  
  112.     p = strrchr(path, '.');
  113.     if (p == NULL) {
  114.         return NULL;
  115.     }
  116.     len = p - path + 1;
  117.  
  118.     parent = XtMalloc(strlen(path) + 2);
  119.     strncpy(parent, path, len);
  120.     parent[len] = '.';
  121.     strcpy(parent + len + 1, p + 1);
  122.  
  123.     return parent;
  124. }
  125.  
  126.  
  127. /*
  128.  *--------------------------------------------------------------
  129.  *
  130.  * Tm_HiddenParentPath --
  131.  *
  132.  *    Create a path based on current path that cannot be duplicated
  133.  *    actually, makes "first.last" into "first..last"
  134.  *
  135.  * Results:
  136.  *
  137.  *    returns a pointer to the name
  138.  *
  139.  * Side effects:
  140.  *
  141.  *    allocates memory
  142.  *--------------------------------------------------------------
  143.  */
  144.  
  145. char *
  146. Tm_ParentPath(path)
  147.     char *path;
  148. {
  149.     char *p;
  150.     char *parent;
  151.     int len;
  152.  
  153.     p = strrchr(path, '.');
  154.     if (p == NULL) {
  155.         return NULL;
  156.     }
  157.     if (p == path)
  158.     len = 1; /* parent is `.' */
  159.     else
  160.         len = p - path;
  161.  
  162.     parent = XtMalloc(len + 1);
  163.     strncpy(parent, path, len);
  164.     parent[len] = '\0';
  165.  
  166.     return parent;
  167. }
  168.  
  169. /*
  170.  *--------------------------------------------------------------
  171.  *
  172.  * Tm_ExtractArg --
  173.  *    find a name and its value in a list
  174.  *
  175.  * Results:
  176.  *    returns a pointer to the value
  177.  *
  178.  * Side effects:
  179.  *    none
  180.  *--------------------------------------------------------------
  181.  */
  182.  
  183. static char *
  184. Tm_ExtractArg(name, argc, argv)
  185.     char *name;
  186.     int argc;
  187.     char *argv[];
  188. {
  189.    int n;
  190.  
  191.     for (n = 0; n < argc; n++) {
  192.     if (strcmp(name, argv[n]) == 0) {
  193.         if (++n < argc) {
  194.         return argv[n];
  195.         } else {
  196.         return NULL;
  197.         }
  198.     }
  199.     }
  200.     return NULL;
  201. }
  202.  
  203. /*
  204.  *--------------------------------------------------------------
  205.  *
  206.  * Tm_MakeXEvent --
  207.  *
  208.  *    Create an X event that can be used to send to another window
  209.  *
  210.  * Results:
  211.  *    a Tcl result value
  212.  *
  213.  * Side effects:
  214.  *    returns a filled in event in xev
  215.  *
  216.  * Restrictions:
  217.  *    only handles a small number of X types
  218.  *--------------------------------------------------------------
  219.  */
  220. int
  221. Tm_MakeXEvent(w, interp, xev, argc, argv)
  222.     Widget w;
  223.     Tcl_Interp *interp;
  224.     XEvent *xev;
  225.     int argc;
  226.     char **argv;
  227. {
  228.     char *type;
  229.     char *value;
  230.     struct timeval  tp;
  231.     Display *display;
  232.  
  233.  
  234.     /* let them know we made this thing up */
  235.     xev->xany.send_event = True;
  236.  
  237.     /* fill in the rest of the xany values.
  238.      */
  239.     xev->xany.display = display = XtDisplay(w);
  240.     xev->xany.window = XtWindow(w);
  241.  
  242.     if ((type = Tm_ExtractArg("-type", argc, argv)) == NULL) {
  243.         type = "ClientMessage";
  244.     }
  245.  
  246.     if (type[0] == 'B') {
  247.         if (strcmp(type, "ButtonRelease") == 0) {
  248.         xev->type = ButtonRelease;
  249.     } else
  250.     if (strcmp(type, "ButtonPress") == 0) {
  251.         xev->type = ButtonPress;
  252.     } else {
  253.         sprintf(interp->result, "illegal type %s\n", type);
  254.         return TCL_ERROR;
  255.     }
  256.  
  257.     xev->xbutton.time = XtLastTimestampProcessed(display);
  258.  
  259.     if ((value = Tm_ExtractArg("-x", argc, argv)) != NULL) {
  260.         xev->xbutton.x = atoi(value);
  261.     } else {
  262.         xev->xbutton.x = 0;
  263.     }
  264.  
  265.     if ((value = Tm_ExtractArg("-y", argc, argv)) != NULL) {
  266.         xev->xbutton.y = atoi(value);
  267.     } else {
  268.         xev->xbutton.y = 0;
  269.     }
  270.  
  271.     return TCL_OK;
  272.     }
  273.  
  274.     if (type[0] == 'K') {
  275.     unsigned int mask;
  276.     KeySym keysym;
  277.     KeyCode keycode;
  278.  
  279.         if (strcmp(type, "KeyRelease") == 0) {
  280.         xev->type = KeyRelease;
  281.     } else
  282.     if (strcmp(type, "KeyPress") == 0) {
  283.         xev->type = KeyPress;
  284.     } else {
  285.         sprintf(interp->result, "illegal type %s\n", type);
  286.         return TCL_ERROR;;
  287.     }
  288.  
  289.     xev->xkey.time = XtLastTimestampProcessed(display);
  290.  
  291.     if ((value = Tm_ExtractArg("-x", argc, argv)) != NULL) {
  292.         xev->xkey.x = atoi(value);
  293.     } else {
  294.         xev->xkey.x = 0;
  295.     }
  296.  
  297.     if ((value = Tm_ExtractArg("-y", argc, argv)) != NULL) {
  298.         xev->xkey.y = atoi(value);
  299.     } else {
  300.         xev->xkey.y = 0;
  301.     }
  302.  
  303.     if ((value = Tm_ExtractArg("-keysym", argc, argv)) != NULL) {
  304.         if ((keysym = XStringToKeysym(value)) == NoSymbol) {
  305.         sprintf(interp->result, "illegal keysym\"%s\"", value);
  306.         return TCL_ERROR;
  307.         }
  308.         if ((keycode = XKeysymToKeycode(display, keysym)) == 0) {
  309.         sprintf(interp->result, "no keycode for keysym \"%s\"", value);
  310.         return TCL_ERROR;
  311.         }
  312.  
  313.         mask = 0;
  314.         /* is this what we should do? */
  315.         if (keysym >= XK_A && keysym <= XK_Z)
  316.         mask |= ShiftMask;
  317.         
  318.         xev->xkey.keycode = keycode;
  319.         xev->xkey.state = mask;
  320.     } else {
  321.         sprintf(interp->result, "Key event must have a keysym");
  322.         return TCL_ERROR;
  323.     }
  324.  
  325.     return TCL_OK;
  326.     }
  327.  
  328.     if (strcmp(type, "ClientMessage") == 0) {
  329.     return TCL_OK;
  330.     }
  331.  
  332.     sprintf(interp->result, "illegal type %s\n", type);
  333.     return TCL_ERROR;;
  334. }
  335.  
  336. /*
  337.  *--------------------------------------------------------------
  338.  *
  339.  * Tm_EndToken --
  340.  *
  341.  *    return the next char past a string or quoted string
  342.  *    using the actions parameter syntax (Asente & Swick appendix B)
  343.  *
  344.  * Results:
  345.  *    return pointer to next char after string
  346.  *
  347.  *--------------------------------------------------------------
  348.  */
  349.  
  350. char *
  351. Tm_EndToken(s)
  352.     char *s;
  353. {
  354.     if (s == NULL || *s == '\0')
  355.     return s;
  356.  
  357.     /* quoted string */
  358.     if (*s == '"') {
  359.     s++;
  360.     while (*s != '"' && *s != '\0')
  361.         s++;
  362.     if (*s == '"')
  363.         return (s+1);
  364.     else
  365.         return s;
  366.     }
  367.  
  368.     /* unquoted string */
  369.     while (*s != ' ' && *s != '\t' && *s != ',' &&
  370.        *s != '\n' && *s != ')' && *s != '\0')
  371.     s++;
  372.     return s;
  373. }
  374. /*
  375.  *--------------------------------------------------------------
  376.  *
  377.  * Tm_ParseAction --
  378.  *
  379.  *    Parse an action into action name + args
  380.  *
  381.  * Results:
  382.  *    sets the action, options and num_options
  383.  *
  384.  * Side-effects:
  385.  *     none
  386.  *
  387.  * Caveats:
  388.  *    Somehow, this is an ugly piece of code. Tidy it up!
  389.  *--------------------------------------------------------------
  390.  */
  391.  
  392. int
  393. Tm_ParseAction(orig, action, params, num_params)
  394.     char *orig;
  395.     char **action;
  396.     char *params[];
  397.     Cardinal *num_params;
  398. {
  399.     int n = 0;
  400.     char *start, *end;
  401. #   define skipblanks(p) while (isspace(*p)) p++;
  402.  
  403.     skipblanks(orig);
  404.     /* make a new string, and put nulls in it at the end of tokens */
  405.     start = XtNewString(orig);
  406.     *action = end = start;
  407.  
  408.     /* delimit action name */
  409.     while (isalnum(*end) || *end == '_' || *end == '-')
  410.     end++;
  411.     if (*end != '(') {
  412.     fprintf(stderr, "no \"(\" after name\n");
  413.     return TCL_ERROR;
  414.     }
  415.     *end = '\0';
  416.  
  417.     start = end + 1;
  418.     for (n = 0; n < TM_NUM_PARAMS; True) {
  419.     skipblanks(start);
  420.     if (*start == ')')
  421.         break;
  422.  
  423.     /* new token */
  424.     end = Tm_EndToken(start);
  425.  
  426.     /* lose quotes if it was a quoted string */
  427.     if (*start == '"') {
  428.         start++;
  429.         *(end - 1) = '\0';
  430.     }
  431.     params[n++] = start;
  432.  
  433.     if (isspace(*end)) {
  434.         *end++ = '\0';
  435.     }
  436.     skipblanks(end);
  437.     if (*end == '\0') {
  438.         fprintf(stderr, "unterminated action\n");
  439.         return TCL_ERROR;
  440.     }
  441.     if (*end == ')') {
  442.         *end = '\0';
  443.         break;
  444.     }
  445.     if (*end != ',') {
  446.         fprintf(stderr, "no \",\" delimiter\n");
  447.         return TCL_ERROR;
  448.     }
  449.     *end = '\0';
  450.     start = end + 1;
  451.     }
  452.  
  453.     *num_params = n;
  454.     return TCL_OK;
  455. }
  456.