home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dmenu1.zip / DM10SRC.ZIP / DOUGMENU.C next >
Text File  |  1991-10-15  |  8KB  |  346 lines

  1. #include "stdio.h"
  2. #include "conio.h"
  3. #include "string.h"
  4. #include "malloc.h"
  5.  
  6. #include "menu.h"
  7.  
  8. #include "setup.c"
  9. #include "parse.c"
  10. #include "draw.c"
  11. #include "input.c"
  12. #include "execute.c"
  13.  
  14.  
  15.  
  16. /***************************************************************************/
  17. void Critical_Error(error,error_string)
  18.  
  19. char error_string[];
  20. error_type error;
  21.  
  22. {
  23.     Clean_Up();
  24.     printf("\nA critical error has occured.");
  25.     switch(error){
  26.         case MEMORY:
  27.             printf("\nOut of memory durring %s.\n",error_string);
  28.             break;
  29.         case FILE_OPEN:
  30.             printf("\nUnable to open file %s\n",current_menu);
  31.             break;
  32.         case SYNTAX:
  33.             printf("\n%s (%u) Syntax Error.\n",current_file,line_count);
  34.             break;
  35.         case VALUE:
  36.             printf("\n%s (%u) Illegal or Missing Value.\n",current_file,line_count);
  37.             break;
  38.         case POSITION:
  39.             printf("\n%s (%u) Unexpected Structure.\n",current_file,line_count);
  40.             break;
  41.         case DISPLAY:
  42.             printf("\nUnable to display first menu.\n");
  43.             break;
  44.         case ENVIROMENT:
  45.             printf("\n%s\n",error_string);
  46.             break;
  47.     }
  48.     exit(1);
  49. }
  50.  
  51. /***************************************************************************/
  52. void Last_Window_Globals(void)
  53.  
  54. {
  55.     win_index--;
  56.  
  57.     current_menu = Win[win_index].menu;
  58.     current_item = Win[win_index].item;
  59.     top       = Win[win_index].top;
  60.     bottom = Win[win_index].bottom;
  61.     left   = Win[win_index].left;
  62.     right  = Win[win_index].right;
  63.     width  = Win[win_index].width;
  64.     height = Win[win_index].height;
  65. }
  66.  
  67. /***************************************************************************/
  68. boolean Windowfy_Menu(menu)
  69.  
  70. Menu *menu;
  71.  
  72. /*  sets h,w,t,b,l,r, current_item and style */
  73. /*  sets nothing and returns NO if error */
  74.  
  75. {
  76.     if (menu == NULL){
  77.          current_item = Win[win_index].item; 
  78.         return(NO);
  79.     }
  80.     if (win_index == MAX_RECURSION){
  81.         Error_Box("Only 10 windows can open at a time.","The next menu cannot be displayed.");
  82.         current_item = Win[win_index].item;
  83.         return(NO);
  84.     }
  85.  
  86.     win_index++;
  87.     current_menu = menu;
  88.     current_item = Home_Item();
  89.  
  90.     if (current_item == NULL){
  91.         Error_Box("Unable to display menu.","The menu has no options.");
  92.         Last_Window_Globals();
  93.         return(NO);
  94.     }
  95.  
  96.     height = current_menu->number_of_items +3;
  97.     width  = current_menu->longest_title +3;
  98.  
  99.     if (height > (byte)(max_screen_y-4) || width >= max_screen_x){
  100.         Error_Box("Unable to display menu.","The menu is too large.");
  101.         Last_Window_Globals();
  102.         return(NO);
  103.     }
  104.  
  105.     left    = current_menu->offset_x;
  106.     top     = current_menu->offset_y;
  107.  
  108.     if (left == CENTER)
  109.             left   = (byte)( (max_screen_x - width)/2 );
  110.     if (top == CENTER)
  111.             top    = (byte)( (max_screen_y - height)/2 +1);
  112.  
  113.     else if (cascade && current_menu->offset_x == Win[0].left &&
  114.              current_menu->offset_y == Win[0].top){
  115.         left   = (byte) ( Win[0].left + (win_index) * cascade_x );
  116.         top    = (byte) ( Win[0].top + (win_index) * cascade_y );
  117.     }    
  118.  
  119.     if (top <3)
  120.         top = 3;
  121.  
  122.     right = left + width;
  123.     if (right >= (byte)(max_screen_x-1) ){
  124.         right = (byte)(max_screen_x-2);
  125.         left = (byte)(right - width);
  126.     }
  127.  
  128.     bottom = top + height;
  129.     if (bottom > (byte)(max_screen_y-1) ){
  130.         bottom = (byte)(max_screen_y-1);
  131.         top = (byte)(bottom - height);
  132.     }
  133.  
  134.     Win[win_index].menu       = current_menu;
  135.     Win[win_index].item    = current_item;
  136.     Win[win_index].top     = top;
  137.     Win[win_index].left    = left;
  138.     Win[win_index].right   = right;
  139.     Win[win_index].width   = width;
  140.     Win[win_index].bottom  = bottom;
  141.     Win[win_index].height  = height;
  142.     if (win_index) Win[win_index].storage = Store_Screen(left,right,top,bottom);
  143.  
  144.         /* force mouse reset */
  145.     last_x = 0;
  146.  
  147.     return(YES);
  148. }
  149.  
  150. /***************************************************************************/
  151. Item *Number_To_Item(number)
  152.  
  153. byte number;
  154.  
  155. {
  156.     Item *item = current_item;
  157.  
  158.     if (number > current_menu->number_of_items)
  159.         return ( current_item );
  160.     
  161.     while (item->item_number < number)
  162.         item = item->next_item;
  163.     while (item->item_number > number) 
  164.         item = item->last_item;
  165.  
  166.     return(item);
  167. }
  168.  
  169. /***************************************************************************/
  170. Item *Home_Item()
  171.  
  172. {
  173.     Item *item = current_menu->first_item;
  174.     
  175.     while (item->first_line == NULL){
  176.         item = item->next_item;
  177.  
  178.     /* if no selectable items */
  179.         if (item == NULL)
  180.             return(NULL);
  181.     }
  182.     return(item);
  183. }
  184.  
  185. /***************************************************************************/
  186. Item *End_Item()
  187.  
  188. {
  189.     Item *item = current_item;
  190.  
  191.     while (item->next_item != NULL)
  192.         item = item->next_item;
  193.  
  194.     while (item->first_line == NULL)
  195.         item = item->last_item;
  196.  
  197.     return(item);
  198. }
  199.  
  200. /***************************************************************************/
  201. Item *Move_Up()
  202. {
  203.     Item *item = current_item->last_item;
  204.  
  205.     if (item == NULL)
  206.         return( End_Item() );
  207.     while (item->first_line == NULL){
  208.         if (item->last_item == NULL)
  209.             return( End_Item() );
  210.         item = item->last_item;        
  211.     }
  212.     return(item);
  213. }
  214.  
  215. /***************************************************************************/
  216. Item *Move_Down()
  217. {
  218.     Item *item = current_item->next_item;
  219.  
  220.     if (item == NULL)
  221.         return( Home_Item () );
  222.  
  223.     while (item->first_line == NULL){
  224.         if (item->next_item == NULL)
  225.             return( Home_Item () );
  226.         item = item->next_item;        
  227.     }
  228.     return(item);
  229. }
  230.  
  231. /***************************************************************************/
  232. void Remove_Window(number_to_remove)
  233.  
  234. byte number_to_remove;
  235.  
  236. {
  237.     if (shadow) Remove_Shadow(left,right,top,bottom);
  238.  
  239.     while ( number_to_remove-- > 0){
  240.         Restore_Screen(Win[win_index].storage,left,right,top,bottom);
  241.         Last_Window_Globals();    
  242.     }
  243.  
  244.     Display_Menu(NULL);
  245.     Select(current_item);
  246.  
  247.         /* force mouse refresh */
  248.     last_x = 0;
  249. }
  250.  
  251. /***************************************************************************/
  252. Menu *Find_Menu(menu_to_find)
  253.  
  254. char menu_to_find[];
  255.  
  256. {
  257.     Menu *menu_to_check = first_menu;
  258.     menu_to_find++;                              /* chop off '%' */
  259.   
  260.     while ( strcmpi(menu_to_find,menu_to_check->title) )
  261.         if ((menu_to_check = menu_to_check->next_menu) == NULL){
  262.             Error_Box("Unable to locate menu labeled:",menu_to_find);    
  263.             return(NULL);
  264.         }
  265.     return (menu_to_check);
  266. }
  267.  
  268. /**************************************************************************/
  269. void Execute_Item()
  270.  
  271. {
  272.     Display_Menu( Win[win_index].item );
  273.     if (shadow) Remove_Shadow(left,right,top,bottom);
  274.  
  275.     if ( current_item->first_line->text_line[0] == '%')
  276.         Windowfy_Menu( Find_Menu( current_item->first_line->text_line ) ); 
  277.     else
  278.         if ( Compare(current_item->first_line->text_line,"#display") )
  279.             Display_Item_Text(); 
  280.         else
  281.             Make_Batch();
  282.  
  283.     Display_Menu( NULL );
  284.     Select( current_item );
  285. }
  286.  
  287. /***************************************************************************/
  288. void main( argc, argv )
  289.           
  290. int argc;
  291. char *argv[];
  292.  
  293. {
  294.     Break_Off();
  295.     Display_Title_Screen();
  296.     Get_Video_Info();
  297.     Set_Up_Enviroment();
  298.     Set_Up_Mouse();
  299.     Parse( argv[1] );
  300.     Set_Up_Screen();
  301.     Hide_Cursor();
  302.  
  303.         /* clear key board buffer */
  304.     while (kbhit())
  305.         getch();
  306.  
  307.     for (;;){
  308.         Wait_For_Event();
  309.  
  310.         switch ( event.action ){
  311.             case GOTO_ITEM:
  312.                 Select( Number_To_Item( event.data ) );
  313.                 break;
  314.             case MOVE_UP:
  315.                 Select(Move_Up());
  316.                 break;
  317.             case MOVE_DOWN:
  318.                 Select(Move_Down());
  319.                 break;
  320.             case GOTO_END:
  321.                   Select(End_Item());
  322.                 break;
  323.             case GOTO_TOP:
  324.                   Select(Home_Item());
  325.                 break;
  326.             case ESCAPE:
  327.                 if (win_index == 0){
  328.                     if (exitable)
  329.                         Exit_Query();
  330.                 }
  331.                 else
  332.                     Remove_Window(event.data);
  333.                 break;
  334.             case DO_ITEM:
  335.                 Win[win_index].event = DO_ITEM;
  336.                 Execute_Item();
  337.                 break;
  338.             case FUNCTION_KEY:
  339.                 current_item = function_key[ event.data ];    
  340.                 Win[win_index].event = FUNCTION_KEY+current_item->item_number;
  341.                 Execute_Item();
  342.                 break;
  343.         }
  344.     }
  345. }
  346.