home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 337_01 / hlp_menu.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  6KB  |  281 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   HLP_MENU.C   ***************************/
  4.  
  5. /*
  6. link with:
  7. makehelp.c
  8. hlp_io.c
  9. */
  10.  
  11. #include "mydef.h"
  12.  
  13. #include "help.h"
  14. #include <stdio.h>
  15.  
  16. #if defined QUICKC
  17.  
  18. #include <malloc.h>
  19. #include <memory.h>
  20.  
  21. #endif
  22.  
  23. #if defined TURBOC
  24.  
  25. #include <alloc.h>
  26. #include <mem.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. #endif
  31.  
  32. /* these functions create the menus used by makehelp.c */
  33.  
  34. void main_menu(void)   /* the main menu */
  35. {
  36. extern struct screen_structure scr;
  37. extern struct window_structure w[];
  38.  
  39. struct bar_struc main_menu [5]={
  40.     "Browse" ,"",browse,0,
  41.     "Add" ,"",add,0,
  42.     "New-file" ,"",file,0,
  43.     "Quit","",NULL,1,
  44.     "\0"
  45.   };
  46.  
  47.   status(0);
  48.  
  49.  bar_menu(main_menu,scr.inverse,scr.normal);
  50.  };
  51.  
  52.  
  53. int browse(void)     /* page browsing menu */
  54. {
  55. extern struct hlp help;
  56. extern struct screen_structure scr;
  57. extern struct window_structure w[];
  58.  
  59. int current_page=0;
  60. int done=FALSE;
  61. int last_load;
  62. int ret_code;
  63.  
  64.   struct bar_struc browse_menu [5]={
  65.     "Next " ,     "", NULL,1,
  66.     " Previous ", "", NULL,2,
  67.     " Edit " ,    "", NULL,3,
  68.     "Quit ",      "", NULL,4,
  69.     "\0"
  70.   };
  71.  
  72. if (help.number_pages==0) return(0);
  73.  
  74.   while(!done){
  75.     alt_screen(ON);
  76.     win_pop_top(help.edit);
  77.     scr.current=win_what_attr(help.edit);
  78.     load_page(current_page);
  79.      status(current_page+1);
  80.      win_pop_top(2);
  81.      alt_screen(OFF);
  82.  
  83.      ret_code=bar_menu(browse_menu,scr.inverse,scr.normal);
  84.  
  85.      switch(ret_code){
  86.        case 0: break;
  87.        case 1: current_page++;break;
  88.        case 2: current_page--;break;
  89.        case 3: cls(); win_pop_top(help.edit);
  90.          for(;;){
  91.  
  92.              /* get edit window attribute */
  93.           scr.current=win_what_attr(help.edit);
  94.            edit(1,1);  /* edit window */
  95.  
  96.            /* pop up the menu and status windows*/
  97.            win_pop_top(help.status);
  98.            win_pop_top(help.menu);
  99.            ret_code=verify_save();
  100.              if (ret_code==0)break;
  101.  
  102.               if(ret_code==1){
  103.                 save_page(current_page);
  104.                 break;
  105.               }
  106.  
  107.               if(ret_code==2)win_pop_top(help.edit);
  108.               if(ret_code==3) break;
  109.             }/* end for(;;) */
  110.              break;
  111.  
  112.        case 4: done=TRUE;
  113.  
  114.      }  /* end switch */
  115.  
  116.   if (current_page==help.number_pages)
  117.       current_page=help.number_pages-1;
  118.   if (current_page<0)current_page=0;
  119.  
  120.   } /* end while(!done)  */
  121.  
  122.  win_cls(help.edit);win_redraw_all();
  123.  status(0);  /* update status window */
  124.  return(0);
  125. }
  126.  
  127.  
  128. int file(void)      /* get the name of a new help file */
  129. {
  130. extern struct hlp help;
  131. extern struct screen_structure scr;
  132. extern struct window_structure w[];
  133.  
  134.  char old_name[80];
  135.  strcpy(old_name,help.filename);
  136.  get_name(help.filename);
  137.  if((strcmp(old_name,help.filename))!=0){
  138.    /* delete old edit window, create new one */
  139.    win_delete(help.edit);
  140.    help.edit= win_make(2,3,help.width,help.height,STD_FRAME,
  141.                       "Edit: Then 'Esc' ",scr.normal,scr.normal);
  142.      win_cls(help.status); /* clear status window */
  143.      status(0);            /* create new one */
  144.  }
  145.  return(0);
  146. }
  147.  
  148.  
  149. int verify_save(void)   /* verify the user wants to save the page */
  150. {
  151. extern struct hlp help;
  152. extern struct screen_structure scr;
  153. extern struct window_structure w[];
  154.  
  155. int return_code;
  156.  /* no function pointers, we want return codes only */
  157.   struct bar_struc main_menu [4]={
  158.     "Save",  "", NULL,1,
  159.     "Edit",  "", NULL,2,
  160.     "Quit" , "", NULL,3,
  161.  
  162.     "\0"
  163.   };
  164.  
  165.  return(bar_menu(main_menu,scr.inverse,scr.normal));
  166. }
  167.  
  168.  
  169. int add(void)   /* add a new help page */
  170. {
  171. extern struct hlp help;
  172. extern struct screen_structure scr;
  173. extern struct window_structure w[];
  174.  
  175. int done=FALSE;
  176. char *ptr=NULL;
  177. char *temp;
  178. char far *scrn_ptr;
  179. char ch;
  180. int i,j;
  181. int return_code;
  182.  
  183. cls();
  184.  
  185. win_pop_top(help.edit);
  186. scr.current=win_what_attr(help.edit);
  187.  
  188. /* allocate space, allow for \0 terminator */
  189. ptr=(char *)malloc ((help.width*help.height)*sizeof(char));
  190. status(help.number_pages+1);
  191. win_pop_top(help.edit);
  192. edit(1,1);
  193.  
  194. while(!done){
  195.   status(help.number_pages+1);
  196.   win_pop_top(help.menu);
  197.  
  198.   return_code=verify_save();   /* save file?*/
  199.  
  200.    if ( return_code==1){   /* save help page to file */
  201.      win_pop_top(help.edit);
  202.      /* scan the help window for characters */
  203.  
  204.       temp=ptr;    /* set temp pointer = pointer */
  205.  
  206.       for(i=0;i<help.height;i++){  /* get each row */
  207.  
  208.        scrn_ptr=(char far *)(scr.buffer+(scr.top+i-1)*
  209.                 (scr.columns*2)+2*(scr.left-1));
  210.  
  211.         for(j=0;j<help.width;j++){
  212.          *temp=*scrn_ptr;
  213.          temp++;scrn_ptr+=2;
  214.         }
  215.  
  216.        }
  217.        /* save image to file */
  218.       append(help.filename,ptr,(help.width*help.height*
  219.              sizeof(char)));
  220.       help.number_pages++;
  221.       ch=' ';
  222.  
  223.      status(help.number_pages+1);
  224.      win_pop_top(help.menu);
  225.  
  226.      ceol(1,1);
  227.      print(1,1, "Add another page ");
  228.      scr.bold_caps=TRUE;
  229.      print_here("Y/N? ");
  230.      scr.bold_caps=FALSE;
  231.      while(ch != 'Y' && ch !='N'){
  232.       ch=toupper(getch());
  233.      }
  234.  
  235.      if(ch=='N') done=TRUE;
  236.  
  237.      else{               /* edit a new page */
  238.         cls();
  239.         win_pop_top(help.edit);
  240.         cls();
  241.         edit(1,1);
  242.      }
  243.    } /* end return code==1 */
  244.  
  245.     if(return_code==2) {
  246.      win_pop_top(help.edit);
  247.      edit(1,1);
  248.     }
  249.     if(return_code==3){
  250.      done=TRUE;
  251.  
  252.     }
  253.  
  254. } /* end while !done */
  255.  
  256. if(ptr!=NULL)free(ptr);
  257. win_cls(help.menu);
  258. win_cls(help.edit);
  259. win_redraw_all(); status(0); return(0);
  260.  
  261. }
  262.  
  263. void status(page)        /* display the status line */
  264. {
  265. extern struct hlp help;
  266. extern struct  screen_structure scr;
  267. extern struct window_structure w[];
  268.  
  269.  char string[20];
  270.  win_pop_top(help.status);
  271.  ceol(1,1);
  272.  scr.current=win_what_attr(help.status);
  273.  print(1,1,help.filename);
  274.  sprintf(string,"Page %3i of %d",page,help.number_pages);
  275.  
  276.  if(page>help.number_pages)
  277.   strcpy(string,"NEW-PAGE");
  278.  print(60,1,string);
  279.  win_pop_top(help.menu);
  280. }
  281.