home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_01.ZIP / MENU.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-02  |  3.1 KB  |  104 lines

  1. /*MENU.C - Menu program from "Microsoft C Programming for the   */
  2. /*IBM" by Robert Lafore, page267. Use the up and down arrow     */
  3. /*keys to move in the menu and the INSert ket to print the menu */
  4. /*item.                                                         */
  5. /****************************************************************/
  6.  
  7. #define TRUE 1
  8. #define NUM 5                 /*Number of menu items*/
  9. #define CLEAR "\x1B[2J"       /*Clear screen*/
  10. #define ERASE "\x1B[K"        /*Erase line*/
  11. #define NORMAL "\x1B[0m"      /*Normal attribute*/
  12. #define REVERSE "\x1B[7m"     /*Reverse video attribute*/
  13. #define HOME "\x1B[1;1f"      /*Cursor to top left*/
  14. #define BOTTOM "\x1B[20;1f"   /*Cursor to lower left*/
  15. #define U_ARRO 72             /*up-arrow key*/
  16. #define D_ARRO 80             /*down-arrow key*/
  17. #define INSERT 82             /*"Ins" key*/
  18.  
  19. main()
  20. {
  21. static char *items[NUM] = {         /*menu items*/
  22.             "Open",
  23.             "Close",
  24.             "Save",
  25.             "Print",
  26.             "Quit"      };
  27. int curpos;                /*position of selected item*/
  28. int code;
  29. printf(CLEAR);             /*clear screen*/
  30. curpos = 0;                /*select top of menu*/
  31.  
  32.    while (TRUE) {
  33.       display(items, NUM, curpos);     /*display menu*/
  34.       code = getcode();                /*check keyboard*/
  35.       switch (code) {         /*act on key pressed*/
  36.                case U_ARRO:
  37.                   if (curpos > 0)
  38.                      --curpos;
  39.                      break;
  40.                case D_ARRO:
  41.                   if (curpos < NUM - 1)
  42.                      ++curpos;
  43.                      break;
  44.                case INSERT:
  45.                   action(curpos);
  46.                   break;
  47.       }
  48.    }
  49. }
  50.  
  51. /* display() */
  52. /* displays menu */
  53. display(arr, size, pos)
  54. char *arr[];
  55. int size, pos;
  56. {
  57. int j;
  58.  
  59.    printf(HOME);                    /*cursor to top left */
  60.    for(j = 0; j < size; j++) {      /*for each menu item*/
  61.       if(j == pos)                  /*if selected*/
  62.          printf(REVERSE);           /*print in reverse video*/
  63.       printf("%s\n", *(arr + j));   /*print item*/
  64.       printf(NORMAL);               /*restore normal attribute*/
  65.    }
  66.    printf(BOTTOM);                  /*cursor to lower left*/
  67. }
  68.  
  69. /* getcode() */
  70. /* gets keyboard code */
  71. getcode()
  72. {
  73. int key;
  74.  
  75.    while(getch() != 0)              /*wait for initial 0*/
  76.       ;                             /*ignores normal keys*/
  77.    return(getch());                 /*return code*/
  78. }
  79.  
  80. /* action() */
  81. /* performs action based on cursor position */
  82. action(pos)
  83. int pos;
  84. {
  85.    printf(ERASE);                   /*erase lower line*/
  86.    switch(pos) {
  87.             case 0:                 /*calls to routines*/
  88.                printf("Open");
  89.                break;
  90.             case 1:                 /*could be inserted here*/
  91.                printf("Close");
  92.                break;
  93.             case 2:
  94.                printf("Save");
  95.                break;
  96.             case 3:
  97.                printf("Print");
  98.                break;
  99.             case 4:
  100.                exit();
  101.    }
  102. }
  103.  
  104.