Advertisement
Guest User

menu1.c

a guest
Mar 11th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <process.h>
  6.  
  7. struct menuitem {
  8.     char *name;
  9.     char *exe;
  10.     struct menuitem *next;
  11. };
  12.  
  13. void fill(char far *video, int start, int end, int ch, int color) {
  14.     int i;
  15.     for(i=start;i<end;i+=2) {
  16.         *(video+i) = ch;
  17.         *(video+i+1) = color;
  18.     }
  19. }
  20.  
  21. void fillscreen(char far *video) {
  22.     fill(video, 0, 159, 0, 0);
  23.     fill(video, 160, 3999, 177, 0x0B);
  24. }
  25.  
  26. void rectangle(char far *video, int x1, int y1, int x2, int y2, int ch, int color) {
  27.     int i;
  28.     for(i=y1*160;i<y2*160;i+=160) {
  29.         fill(video, x1*2+i, x2*2+i, ch, color);
  30.     }
  31. }
  32.  
  33. void bar(char far *video, int x1, int y1, int x2, int y2, int ch, int color) {
  34.     rectangle(video, x1, y1, x2, y2, ch, color);
  35.     rectangle(video, x2, y1+1, x2+2, y2, 177, 0x08);
  36.     rectangle(video, x1+2, y2, x2+2, y2+1, 177, 0x08);
  37. }
  38.  
  39. void frame(char far *video, int x1, int y1, int x2, int y2, int color) {
  40.     int topleft, topright, botleft, botright;
  41.     topleft = x1*2+y1*160;
  42.     *(video+topleft) = 218;
  43.     *(video+topleft+1) = color;
  44.     rectangle(video, x1+1,y1, x2-1, y1+1, 196, color);
  45.     topright = (x2-1)*2+y1*160;
  46.     *(video+topright) = 191;
  47.     *(video+topright+1) = color;
  48.     botleft = x1*2+(y2-1)*160;
  49.     *(video+botleft) = 192;
  50.     *(video+botleft+1) = color;
  51.     rectangle(video, x1+1, y2-1, x2-1, y2, 196, color);
  52.     botright = (x2-1)*2+(y2-1)*160;
  53.     *(video+botright) = 217;
  54.     *(video+botright+1) = color;
  55.     rectangle(video, x1, y1+1, x1+1, y2-1, 179, color);
  56.     rectangle(video, x2-1, y1+1, x2, y2-1, 179, color);
  57. }
  58.  
  59. void barframe(char far *video, int x1, int y1, int x2, int y2, int color) {
  60.     bar(video, x1, y1, x2, y2, 32, 0x70);
  61.     frame(video, x1, y1, x2, y2, color);
  62.     rectangle(video, x1+1, y1+1, x2-1, y2-1, 32, 0x1E);
  63. }
  64.  
  65. void echo(char far *video, int x, int y, char *text) {
  66.     int i;
  67.     for(i=0; i<256; i++) {
  68.         if(text[i]==0) break;
  69.         *(video+x*2+y*160+i*2) = text[i];
  70.     }
  71. }
  72.  
  73. void printmenu(char far *video, char ** menulist, int items, int offset) {
  74.     int i, j, k, end;
  75.     end = items - offset;
  76.     if(end>12) end = 12;
  77.     for(i=0; i<end; i++) {
  78.         for(j=0; i<40; j++) {
  79.             if(menulist[offset+i][j]==0) break;
  80.             *(video+22*2+(6+i)*160+j*2) = menulist[offset+i][j];
  81.         }
  82.         for(k=j; k<40; k++) *(video+22*2+(6+i)*160+k*2) = ' ';
  83.     }
  84. }
  85.  
  86. void highlight(char far *video, int y, int color) {
  87.      int i;
  88.     for(i=41; i<122; i+=2) *(video+i+(y+6)*160) = color;
  89. }
  90.  
  91. int strquote(char *dest, char *src, int maxlen) {
  92.     int count = 1;
  93.     if(!dest) return 0;
  94.     while((*src != 0) && maxlen >0) {
  95.         switch(*src) {
  96.             case '\\':
  97.             case '\"':
  98.             case '\'':
  99.                 *dest++ = '\\';
  100.                 count ++;
  101.             default:
  102.                 *dest++ = *src;
  103.                 count ++;
  104.         }
  105.         ++src;
  106.         --maxlen;
  107.     }
  108.     if(dest) *dest = 0;
  109.     return count;
  110. }
  111.  
  112. int main(void) {
  113.     char far *video;
  114.     FILE *fp;
  115.     char line[255];
  116.     struct menuitem *headitem = NULL;
  117.     struct menuitem *curitem = NULL;
  118.     char **menulist;
  119.     char **exelist;
  120.     int items=0;
  121.     int offset = 0;
  122.     int hl_item = 0;
  123.     int sel = 0;
  124.     int ch1 = 0;
  125.     int ch2 = 0;
  126.     int i = 0;
  127.     char errst[3];
  128.     _setcursortype(_NOCURSOR);
  129.     video = (char far *)0xB8000000;
  130.     fillscreen(video);
  131.     barframe(video, 14, 0, 66, 3, 0x79);
  132.     barframe(video, 17, 5, 64, 19, 0x7E);
  133.     bar(video, 15, 21, 67, 23, 32, 0x70);
  134.     echo(video, 26, 1, "ГЛАВНОЕ   М Е Н Ю   ПОЛЬЗОВАТЕЛЯ");
  135.     echo(video, 31, 21, "Выбор нужного раздела:");
  136.     echo(video, 22, 22, "клавишами: ( \x18 ), ( \x19 ) и нажмите Enter");
  137.  
  138.     fp = fopen("menu.txt", "r");
  139.     if (fp == NULL) {
  140.         perror("Error while opening menu.txt\n");
  141.         exit(EXIT_FAILURE);
  142.     }
  143.     while( fgets(line, 254, fp) != NULL ) {
  144.         char *k, *v;
  145.         char tmpline[255];
  146.         int klen, vlen;
  147.         struct menuitem *tmp;
  148.         tmp = malloc(sizeof(struct menuitem));
  149.         tmp->next = NULL;
  150.         if(!tmp) { printf("malloc error"); exit(1); }
  151.         if(curitem==NULL) { curitem = tmp; headitem = tmp; }
  152.         else { curitem->next = tmp; curitem = tmp; }
  153.         k = strtok(line, "=");
  154.         v = strtok(NULL, "=");
  155.         klen=strlen(k)+1;
  156.         if(klen>40) { klen=40; }
  157.         curitem->name = malloc(klen*sizeof(char));
  158.         if (!curitem->name) { printf("malloc error"); exit(1); }
  159.         strncpy(curitem->name, k, 40);
  160.         if(klen==40) curitem->name[39] = 0;
  161.         vlen=strquote(tmpline, v, 214);
  162.         if(vlen>214) { vlen=214; }
  163.         curitem->exe=malloc(vlen*sizeof(char));
  164.         if (!curitem->exe) { printf("malloc error"); exit(1); }
  165.         strncpy(curitem->exe, tmpline, 214);
  166.         if(vlen==214) curitem->exe[213] = 0;
  167.         items++;
  168.     }
  169.     fclose(fp);
  170.     menulist = malloc(items*sizeof(char *));
  171.     exelist = malloc(items*sizeof(char *));
  172.     curitem = headitem;
  173.     for(i=0;i<items;i++) {
  174.         menulist[i] = curitem->name;
  175.         exelist[i] = curitem->exe;
  176.         curitem = curitem->next;
  177.     }
  178.     printmenu(video, menulist, items, offset);
  179.     highlight(video, sel, 0x4F);
  180.     while (ch1 != 27) {
  181.         ch1 = getch();
  182.         ch2 = 0;
  183.         switch(ch1) {
  184.           case 0xE0:
  185.           case 0:
  186.             ch2 = getch();
  187.             highlight(video, hl_item, 0x1E);
  188.             switch(ch2) {
  189.                 case 72: if(sel != 0) {
  190.                              sel--;
  191.                              if(hl_item != 0) hl_item--;
  192.                              else {
  193.                                  offset--;
  194.                                  printmenu(video, menulist, items, offset);
  195.                              }
  196.                          } else {
  197.                              sel=items-1;
  198.                              if(items<12) { offset=0; hl_item=sel; }
  199.                              else {
  200.                                  offset=items-12; hl_item=11;
  201.                                  printmenu(video, menulist, items, offset);
  202.                              }
  203.                          }
  204.                          break;
  205.                 case 80: if(sel < items-1) {
  206.                              sel++;
  207.                              if(hl_item < 11) hl_item++;
  208.                              else {
  209.                                  offset++;
  210.                                  printmenu(video, menulist, items, offset);
  211.                              }
  212.                          } else {
  213.                              sel=0; hl_item=0; offset=0;
  214.                              printmenu(video, menulist, items, offset);
  215.                          }
  216.                          break;
  217.                 default:
  218.                          break;
  219.             }
  220.             highlight(video, hl_item, 0x4F);
  221.             break;
  222.           case 13:
  223.           case 32:
  224.             _setcursortype(_NORMALCURSOR);
  225.             execlp(exelist[sel], exelist[sel], NULL);
  226.             _setcursortype(_NOCURSOR);
  227.             itoa(errno, errst,10);
  228.             echo(video, 32,24, "ERROR NUMBER:  ");
  229.             echo(video, 47,24, errst);
  230.             getch();
  231.             fill(video, 3840, 3999, 177, 0x0B);
  232.             break;
  233.           default:
  234.             //printf("%u", ch1);
  235.             break;
  236.         }
  237.     }
  238.     curitem=headitem;
  239.     while(curitem!=NULL) {
  240.         struct menuitem *tmp;
  241.         tmp = curitem;
  242.         curitem = curitem->next;
  243.         free(tmp->name);
  244.         free(tmp->exe);
  245.         free(tmp);
  246.     }
  247.     free(menulist);
  248.     free(exelist);
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement