home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / getkey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-09  |  2.9 KB  |  168 lines

  1. /*
  2.  *    getkey.c  --  input function(s) for mouse and keyboard with menu.
  3.  *              Use in graphics mode only.
  4.  *
  5.  *    Menus start at (0, 0).
  6.  *
  7.  *    2 july 1989  Olle.
  8.  */
  9.  
  10. #include <graphics.h>
  11. #include <conio.h>
  12. #include "gmtc.h"
  13. #include "getkey.h"
  14.  
  15. /* Mouse button character for gotkey(). (Should have setup function.) */
  16. #define EDIT_CH ' '
  17.  
  18. /* current menu */
  19. static menudescr *menup;
  20.  
  21. /* kbhit() doesn't seem to detect characters from ungetch(), use a local flag */
  22. static int haskey;
  23.  
  24. int getkey( struct gm_status *gp )
  25. {
  26. register int *cp;
  27. register char **s;
  28. register int x;
  29.  
  30. /* wait for a button or a key */
  31. do
  32.     {
  33.     if (kbhit() || haskey)
  34.         {
  35.         haskey = 0;
  36.         return (getch());
  37.         }
  38.     gm_getpos( gp );
  39.     }
  40. while (!gp -> gm_pbutton);
  41.  
  42. /* a button, check the menu */
  43. if (menup) if (gp -> gm_ypos < menup -> row)
  44.     {
  45.     /* in the menu area, which entry ? */
  46.         x = gp -> gm_xpos;
  47.     for (s = menup -> item, cp = menup -> col; *s; ++s, ++cp)
  48.         {
  49.         if (x < *cp)
  50.                        return (*s[0]);
  51.             }
  52.  
  53.     /* loop fall-through means outside the menu */
  54.     }
  55.  
  56. /* zero means a button */
  57. return (0);
  58. }
  59.  
  60. void showmenu( menudescr *mp )
  61. {
  62. register char *s;
  63. register int i;
  64.  
  65. /* show the menu and fill in the coordinates */
  66. moveto( 0, 0 );
  67.  
  68. for (i = 0; s = mp -> item[i]; ++i)
  69.     {
  70.     /* gputs() clears the background; don't do that */
  71.     /*gputs( " " ); */
  72.     /*gputs( s ); */
  73.     outtext( " " );
  74.     outtext( s );
  75.  
  76.     /* graphic mode coordinate */
  77.     mp -> col[i] = getx();
  78.     }
  79.  
  80. /* save the position of the end of the last item */
  81. mp -> col[i] = getx();
  82. mp -> row = textheight( "A" );
  83.  
  84. /* remember the menu */
  85. menup = mp;
  86. }
  87.  
  88. void nomenu()
  89. {
  90. register int i;
  91. struct fillsettingstype fis;
  92.  
  93. /* remove the menu (if there is one) */
  94. if (!menup)
  95.     return;
  96.  
  97. /* (this is as gputs()) */
  98. /* assume TOP_TEXT & LEFT_TEXT justification */
  99.  
  100. /* get fill settings */
  101. getfillsettings( &fis );
  102.  
  103. /* setup for character erase */
  104. setfillstyle( SOLID_FILL, getbkcolor() );
  105.  
  106. /* find the end x of the last menu item */
  107. for (i = 0; menup -> item[i]; ++i);
  108.  
  109. /* fill with the background color */
  110. bar( 0, 0, menup -> col[i]  - 1, textheight( "A" ) - 1 );
  111.  
  112. /* restore fill settings */
  113. setfillstyle( fis.pattern, fis.color );
  114.  
  115. /* and now, there is no menu */
  116. menup = 0;
  117. }
  118.     
  119. void wbup()
  120. {
  121. struct gm_status gs;
  122.  
  123. /* wait for all buttons to be up */
  124. do
  125.     {
  126.     gm_getpos( &gs );
  127.     }
  128. while (gs.gm_pbutton);
  129.  
  130. /* clear the button press counters */
  131. gm_press( GM_LEFT, &gs );
  132. gm_press( GM_RIGHT, &gs );
  133. }
  134.  
  135. int gotkey()
  136. {
  137. struct gm_status gs;
  138.  
  139. /* check keyboard and mouse */
  140. if (kbhit()) return (1);
  141.  
  142. /* kbhit() fix */
  143. if (haskey)
  144.     {
  145.     return (1);
  146.     }
  147.  
  148. /* if a button was pressed make it look like EDIT_CH */
  149. gm_press( GM_LEFT, &gs );
  150. if (gs.gm_count > 0)
  151.     {
  152.     ungetch( EDIT_CH );
  153.     ++haskey;
  154.     return (1);
  155.     }
  156.  
  157. gm_press( GM_RIGHT, &gs );
  158. if (gs.gm_count > 0)
  159.     {
  160.     ungetch( EDIT_CH );
  161.     ++haskey;
  162.     return (1);
  163.     }
  164.  
  165. return (0);
  166. }
  167.  
  168.