home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / MNMCHKEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  3.9 KB  |  140 lines

  1. /**
  2. *
  3. * Name        MNMCHKEY - Match a key to a specification.
  4. *
  5. * Synopsis    presult = mnmchkey (pmenu, pkey, ch, scan, perror);
  6. *
  7. *        BKEYMAP *presult    Pointer to the key in the
  8. *                    menu's key list which matches
  9. *                    ch and scan, or NIL for
  10. *                    failure.
  11. *        BMENU *pmenu        Pointer to menu in which to
  12. *                    search key list.  Needed if we
  13. *                    get to the end of the key list
  14. *                    and have to wrap around.
  15. *        BKEYMAP *pkey        Pointer to key in list to start
  16. *                    search at, or NIL to start at
  17. *                    first key.
  18. *        int    ch,        Character code and scan code to
  19. *               scan        search for in key list.
  20. *        int   *pcode        Pointer to variable to return
  21. *                    error code in.  NIL if error code
  22. *                    should not be returned.
  23. *
  24. * Description    MNMCHKEY searches from a given point in the key
  25. *        list of a menu for a keymap entry matching a
  26. *        particular specification.
  27. *
  28. *        It only matches enabled keys (keys without the
  29. *        MN_DISABLE bit set).
  30. *
  31. *        It skips keys with the same row, col, and action
  32. *        fields as those in pkey.  This enables menus
  33. *        such as the following to work:
  34. *
  35. *            +--------+
  36. *            | Quit   |    <-- bound to "Qq"
  37. *            | Create |    <-- bound to "Cc"
  38. *            | Close  |    <-- bound to "Cc"
  39. *            +--------+
  40. *
  41. *        The way this works is that the highlight bar moves
  42. *        to "Create" the first time upper or lower-case "C"
  43. *        is pressed, and then alternates between "Create"
  44. *        and "Close" as "C"'s are pressed.
  45. *
  46. * Returns    presult     Pointer to BKEYMAP found, or
  47. *                NIL if failure.
  48. *
  49. * Version    6.00 (C)Copyright Blaise Computing Inc.  1987,1989
  50. *
  51. **/
  52.  
  53.  
  54. #include <bkeybrd.h>
  55. #include <bmenu.h>
  56.  
  57. #define TRUE 1
  58. #define FALSE 0
  59.  
  60.  
  61. const BKEYMAP *mnmchkey (pmenu, pkey, ch, scan, pcode)
  62. const BMENU   *pmenu;
  63. const BKEYMAP *pkey;
  64. int     ch, scan;
  65. int    *pcode;
  66. {
  67.     BKEYMAP *pstart = (pkey == NIL) ? (pmenu->pkeys) : (pkey);
  68.     BKEYMAP *qkey   = pstart;
  69.     BKEYMAP *rkey   = NIL;
  70.     BKEYMAP *skey   = NIL;
  71.     int      done   = FALSE;
  72.     int      first  = TRUE;
  73.  
  74.     if (pcode != NIL)
  75.     *pcode = WN_NO_ERROR;
  76.  
  77.         /* Exit immediately if there is no key list.        */
  78.     if (pstart == NIL)
  79.     return (NIL);
  80.  
  81.         /* Check each key list entry to see if it matches   */
  82.         /* the given specification.                */
  83.     for (;
  84.      !done;
  85.      qkey = qkey->next)
  86.     {
  87.     if (qkey == NIL)
  88.         qkey = pmenu->pkeys;
  89.  
  90.     if ((qkey == pstart) && !first)
  91.         done = TRUE;
  92.  
  93.         /* Check key signature.                 */
  94.     if (qkey->signature != MN_KEY_SIGN)
  95.     {
  96.         if (pcode != NIL)
  97.         *pcode = MN_BAD_KEY;
  98.  
  99.         wnreterr (MN_BAD_KEY);
  100.     }
  101.  
  102.         /* If *qkey is not disabled,                */
  103.     if (( !(qkey->action & MN_DISABLE))           &&
  104.         ( !(qkey->action & MN_TEMP_DISABLE))       &&
  105.         /* and the character and scan codes match the ones  */
  106.         /* we are looking for, save a pointer to it.        */
  107.         ((qkey->ch == ch) && (qkey->scan == scan)))
  108.  
  109.         skey = qkey;
  110.  
  111.         /* If *qkey is not disabled,                */
  112.     if (( !(qkey->action & MN_DISABLE))           &&
  113.         ( !(qkey->action & MN_TEMP_DISABLE))       &&
  114.         /* and the character and scan codes match the ones  */
  115.         /* we are looking for,                    */
  116.         ((qkey->ch == ch) && (qkey->scan == scan)) &&
  117.         /* and at least one of the following is true:        */
  118.         /* 1. There was no previous key;            */
  119.         ((pkey == NIL)            ||
  120.         /* 2. We went all the way around the list and the   */
  121.         /*    key we were given at first is the only one    */
  122.         /*    which matches the specification;            */
  123.          ((qkey == pkey) && !first)     ||
  124.         /* 3. At least one component of the key we are        */
  125.         /*    looking at is different from that of the key  */
  126.         /*    we were given to start with.            */
  127.          (pkey->row    != qkey->row)    ||
  128.          (pkey->col    != qkey->col)    ||
  129.          (pkey->action != qkey->action)))
  130.     {
  131.         rkey = qkey;
  132.         done = TRUE;
  133.     }
  134.  
  135.     first = FALSE;
  136.     }
  137.  
  138.     return ((rkey == NIL) ? skey : rkey);
  139. }
  140.