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 / EXECUTE.C < prev    next >
Text File  |  1991-10-15  |  10KB  |  417 lines

  1. /***************************************************************************/
  2. byte Do_Query_Window(query)
  3.  
  4. Menu *query;
  5.  
  6. {
  7.     Win[win_index].event = QUERY;
  8.  
  9.     if ( !Windowfy_Menu( query ) )
  10.         return(0);
  11.     Display_Menu(NULL);
  12.     Select(current_item);
  13.  
  14.     for (;;){
  15.         Wait_For_Event();
  16.  
  17.         switch ( event.action ){
  18.             case GOTO_ITEM:
  19.                 Select( Number_To_Item( event.data ) );
  20.                 break;
  21.             case MOVE_UP:
  22.                 Select(Move_Up());
  23.                 break;
  24.             case MOVE_DOWN:
  25.                 Select(Move_Down());
  26.                 break;
  27.             case GOTO_END:
  28.                   Select(End_Item());
  29.                 break;
  30.             case GOTO_TOP:
  31.                   Select(Home_Item());
  32.                 break;
  33.             case FUNCTION_KEY:
  34.                 if (function_key[ event.data ]->first_line->text_line[0] == '%' ||    
  35.                     Compare(function_key[ event.data ]->first_line->text_line,"#ask"))
  36.                     break;    
  37.                 current_item = function_key[ event.data ];
  38.                 Win[win_index].event = FUNCTION_KEY+current_item->item_number;
  39.                 Execute_Item();
  40.                 break;
  41.             case ESCAPE:
  42.                 return(0);
  43.             case DO_ITEM:
  44.                 return(current_item->item_number);
  45.         }
  46.     }
  47. }
  48.  
  49. /***************************************************************************/
  50. void Exit_Query()
  51.  
  52. {
  53.     Item *yes,*no;
  54.     Menu *exit_query;
  55.  
  56.     if (shadow) Remove_Shadow(left,right,top,bottom);
  57.     Display_Menu(current_item);
  58.  
  59.     l_len = 12;
  60.     exit_query = Allocate_Menu();
  61.     strcpy(exit_query->title,"Exit Menu?");
  62.  
  63.     yes = Allocate_Item();
  64.     strcpy(yes->title,"Yes");
  65.     no = Allocate_Item();
  66.     strcpy(no->title,"No");
  67.  
  68.     exit_query->first_item=yes;
  69.     yes->next_item=no;
  70.     yes->last_item=NULL;
  71.     no->last_item=yes;
  72.     no->next_item=NULL;
  73.  
  74.             /* make items choosable */
  75.     yes->first_line= current_item->first_line;
  76.     no->first_line= current_item->first_line;
  77.     yes->item_number=1;
  78.     no->item_number=2;
  79.     
  80.     exit_query->number_of_items = 2;
  81.     exit_query->title_length    = 10;
  82.     exit_query->longest_title   = 10;
  83.     exit_query->offset_x = CENTER;
  84.     exit_query->offset_y = CENTER;
  85.     exit_query->palette  = message_palette;
  86.  
  87.     if ( Do_Query_Window(exit_query) == 1){
  88.         Clean_Up();
  89.         exit(1);
  90.     }
  91.  
  92.     free(current_menu->first_item->next_item);
  93.     free(current_menu->first_item);
  94.     free(current_menu);
  95.  
  96.     Remove_Window (1);
  97. }
  98.  
  99. /**************************************************************************/
  100. void Choice_Query(place,temp)
  101.  
  102. char *place,temp[];
  103.  
  104. {
  105.     Menu *choice_query;
  106.     Item *item=NULL;
  107.     Linked_Text *line = current_text;
  108.     byte i,j,longest, value;
  109.      
  110.     l_len = MAX_LINE;
  111.     choice_query = Allocate_Menu();
  112.  
  113.     for (i = 0; *place != ',' && *place!= '\0';i++)
  114.         choice_query->title[i] = *(place++);
  115.     choice_query->title[i] = '\0';
  116.     longest = choice_query->title_length = strlen(choice_query->title);
  117.  
  118.     if (*place != '\0') place++;
  119.     choice_query->offset_x = Get_Value(place,default_x);
  120.     place = After_Comma(place);
  121.     choice_query->offset_y = Get_Value(place,default_y);
  122.     place = After_Comma(place);
  123.     choice_query->palette = Get_Palette(place,current_menu->palette);
  124.     
  125.     line = line->next_line;
  126.  
  127.     i=0;
  128.     while (line != NULL && line->text_line[0] == '"'){
  129.         if (item == NULL){
  130.             item = Allocate_Item();
  131.             choice_query->first_item = item;
  132.             item->last_item = NULL;
  133.             item->next_item = NULL;
  134.         }
  135.         else{
  136.             item->next_item = Allocate_Item();
  137.             item->next_item->last_item = item;
  138.             item = item->next_item;
  139.             item->next_item = NULL;
  140.         }
  141.  
  142.                 /* read title from quotes */
  143.         for (j=1; line->text_line[j] != '"' && line->text_line[j] != '\0'; j++)
  144.             item->title[j-1] = line->text_line[j];
  145.         item->title[j-1] = '\0';
  146.  
  147.         if (longest < (byte)(j-1) )
  148.             longest = (byte)(j-1) ;
  149.  
  150.         item->item_number = ++i;
  151.  
  152.                     /* make it a chooseable option */
  153.         item->first_line = line;    
  154.  
  155.         line = line->next_line;
  156.     }
  157.  
  158.     choice_query->number_of_items = i;
  159.     choice_query->longest_title   = longest;
  160.  
  161.     value = Do_Query_Window(choice_query);
  162.  
  163.     while (item->last_item != NULL){
  164.         item = item->last_item;
  165.         free (item->next_item);
  166.     }
  167.  
  168.     free(item);
  169.     free(choice_query);
  170.  
  171.     if ( value == 0){
  172.         *temp = LEAVE_QUERY_MODE;
  173.         return;
  174.     }
  175.  
  176.     line = current_text;              /* find chosen line */
  177.     for (;value>0;value--){
  178.         line = line->next_line;
  179.     }
  180.  
  181.     strcpy( temp, After_Equals(line->text_line) );
  182.  
  183.     while ( line->text_line[0] == '"')
  184.         line = line->next_line;
  185.  
  186.     current_text = line;
  187. }
  188.  
  189. /**************************************************************************/
  190. void Get_Key_String(string,x,y,color)
  191.  
  192. char string[];
  193. byte x,y,color;
  194.  
  195. {
  196.     byte input=0,length=0;
  197.     
  198.     *string = '\0';
  199.     Show_Cursor();
  200.  
  201.     for(;;){
  202.         Put_Cursor( (byte) (x+length) ,y);
  203.         input = getch();
  204.  
  205.                     /* deal with extended key codes */
  206.         while (input == 0){
  207.             input = getch();
  208.                     /* change back arrow or del to back space */
  209.             if (input == 75 || input == 83 ) 
  210.                 input = 8;
  211.             else{
  212.                 if (input >58 && input < 69){
  213.                     input -= 58;
  214.                     if (function_key[input] != NULL &&
  215.                         function_key[ input ]->first_line->text_line[0] != '%' &&
  216.                         !Compare(function_key[ input ]->first_line->text_line,"#ask") ){
  217.                             current_item = function_key[ input ];
  218.                             Win[win_index].event = FUNCTION_KEY+current_item->item_number;
  219.                             Execute_Item();
  220.                     }
  221.                 }            
  222.                 input = getch();
  223.             }
  224.         }
  225.  
  226.         switch (input){
  227.             case 27:
  228.                 *string = LEAVE_QUERY_MODE;
  229.             case 13:
  230.                  Hide_Cursor();
  231.                 return;
  232.             case 8:
  233.                 if (length){
  234.                     length--;
  235.                     string[length] = '\0';
  236.                     Draw( (byte)(x+length),y,' ',color);
  237.                 }
  238.                 break;
  239.             default:
  240.                 if (length <56){                
  241.                     string[length] = input;
  242.                     Draw( (byte)(x+length),y,input,color);
  243.                     length++;
  244.                     string[length] = '\0';
  245.                 }
  246.                 break;
  247.         }
  248.     } 
  249. }    
  250.  
  251. /**************************************************************************/
  252. void String_Query(place,temp)
  253.  
  254. char *place,temp[];
  255.  
  256. {
  257.     Menu *string_query;
  258.     Item *item;
  259.     Linked_Text *line = current_text;
  260.  
  261.     byte i=0;
  262.  
  263.     l_len = 10;
  264.     item = Allocate_Item();
  265.     item->first_line = current_item->first_line;
  266.     item->next_item = NULL;
  267.     strcpy(item->title," ");
  268.  
  269.     l_len = 60;
  270.     string_query = Allocate_Menu();
  271.     for (i = 0; *place != ',' && *place!= '\0';i++)
  272.         string_query->title[i] = *(place++);
  273.     string_query->title[i] = '\0';
  274.  
  275.     place = After_Comma(place);
  276.     string_query->offset_x = Get_Value(place,(byte)(max_screen_x>1-30) );
  277.     place = After_Comma(place);
  278.     string_query->offset_y = Get_Value(place,(byte)(max_screen_y>1+6) );
  279.     place = After_Comma(place);
  280.     string_query->palette = Get_Palette(place,current_menu->palette);
  281.  
  282.     string_query->title_length = i;
  283.     string_query->longest_title = 56;
  284.     string_query->number_of_items = 1;
  285.     string_query->first_item = item;
  286.  
  287.     if ( Windowfy_Menu(string_query) ){
  288.         Display_Menu(NULL);
  289.         Get_Key_String( temp, (byte)(left+2), (byte)(top+3), 
  290.             (byte)(palette[string_query->palette][TEXT] | palette[string_query->palette][BACK]<<4) );
  291.     }
  292.     else
  293.         temp[0] = LEAVE_QUERY_MODE;
  294.     free(item);
  295.     free(string_query);
  296.     current_text = current_text->next_line;
  297. }
  298.  
  299. /**************************************************************************/
  300. void Make_Batch( void )
  301.  
  302. {
  303.     register char *place;
  304.     register byte i;
  305.     Vars vars[MAX_VARS];
  306.     char temp[MAX_LINE];
  307.     byte var_number;
  308.     FILE *Temp_Bat;    
  309.  
  310.                 /* initialize variables */
  311.     current_text = current_item->first_line;
  312.     var_number = 0;
  313.  
  314.                 /* read in variables */
  315.     while ( Compare(current_text->text_line,"#ask ") ){
  316.         if (var_number == MAX_VARS)
  317.             Critical_Error(MEMORY,"Too many variables,");
  318.  
  319.                 /* mark place in text list */
  320.         vars[var_number].text =    current_text; 
  321.  
  322.                 /* find var.name and record it */
  323.         place = After_Space(current_text->text_line);
  324.         for (i=1; *place != ' ' && *place != ',' && *place != '\0'; i++ )
  325.             temp[i] = *(place++);
  326.         place = After_Comma(place);
  327.         temp[0] = '%';
  328.         temp[i] = '%';
  329.         temp[++i] = '\0';
  330.         vars[var_number].name = strdup(temp);
  331.  
  332.                 /* get value in temp[] */
  333.         if (current_text->next_line->text_line[0] == '"') 
  334.             Choice_Query(place,temp);
  335.         else String_Query(place,temp);
  336.  
  337.                 /* remove query window */
  338.         if (shadow) Remove_Shadow(left,right,top,bottom);
  339.         Restore_Screen(Win[win_index].storage,left,right,top,bottom);
  340.         Last_Window_Globals();    
  341.  
  342.                 /* did user request to leave? */
  343.         if (*temp == LEAVE_QUERY_MODE ){
  344.             free(vars[var_number].name);
  345.             for (var_number--;var_number!=255;var_number--){
  346.                 free(vars[var_number].name);
  347.                 free(vars[var_number].value);
  348.             }
  349.             return;
  350.         }
  351.         else{
  352.             vars[var_number].value = strdup(temp);
  353.             var_number++;
  354.         }    
  355.     }
  356.  
  357.             /**********************/
  358.             /* we're gonna do it! */
  359.             /**********************/
  360.  
  361.     Clean_Up();
  362.             /* read file name from enviroment into temp */
  363.     for (i=0; *(temp_bat+i) != '\0';i++)
  364.         temp[i] = *(temp_bat+i);
  365.     temp[i]='\0';
  366.  
  367.             /* open file to write */
  368.     if ( ( Temp_Bat = fopen(temp, "w" ) ) == NULL ){
  369.         Error_Box("Unable to create temporary batch file.","Aborting");
  370.         return;
  371.     }
  372.  
  373.             /* write file header */
  374.      fputs("REM   This is a temporary file for DougMenu\n",Temp_Bat);
  375.     fputs("REM   return: ",Temp_Bat);
  376.     for (i=0;i<=win_index;i++){
  377.         fputc(Win[i].item->item_number+64,Temp_Bat);
  378.         if (i<win_index)
  379.             fputc(Win[i].event+64,Temp_Bat);
  380.     }
  381.     fputc('\n',Temp_Bat);
  382.  
  383.             /* Write the batch file */
  384.     while (current_text != NULL){
  385.         place = current_text->text_line;
  386.  
  387.             /* Check for special Commands */
  388.         if (Compare(place,"#exit") ){
  389.             fputs("set t=\n",Temp_Bat);
  390.             fclose (Temp_Bat);
  391.             exit(2);
  392.         }
  393.  
  394.             /* check for variable names */
  395.         while (*place != '\0'){
  396.             if (*place == '%'){
  397.                 for (i=0;i<var_number;i++){
  398.                     if (Compare(place,vars[i].name)){
  399.                         fputs(vars[i].value,Temp_Bat);
  400.                         place += strlen(vars[i].name);
  401.                         break;
  402.                     }
  403.                 }
  404.             }
  405.             if (*place != '\0'){
  406.                 fputc(*place,Temp_Bat);
  407.                 place++;
  408.             }
  409.         }
  410.         fputc('\n',Temp_Bat);
  411.         current_text = current_text->next_line;
  412.     }    
  413.  
  414.     fclose (Temp_Bat);
  415.     exit(0);
  416. }
  417.