	        /*********************************
		 *                               *
		 *   Visual Shell v1.17  10/92   *
		 *                               *
		 *     by Torsten Jürgeleit      *
		 *                               *
		 *        display part 2         *
		 *                               *
		 *********************************/

	/* Includes */

#include "includes.h"
#include "imports.h"
#include "protos.h"

	/* Install string gadget in status line and get input */

   BYTE *
get_input(USHORT mode, BYTE *default_input)
{
   struct Gadget      *gad = &gadget;
   struct StringInfo  *sinfo = (struct StringInfo *)gad->SpecialInfo;
   struct IntuiText   *itext = gad->GadgetText;
   BYTE   *prompt, *buffer = (BYTE *)sinfo->Buffer;
   USHORT max_input_len, prompt_width;

   /* Init prompt text */
   event_mode = EVENT_MODE_IGNORE;
   print_fkey_text(FKEY_MODE_NONE);
   switch (mode) {
      case INPUT_MODE_FREQ_ENTRY :
      case INPUT_MODE_TREQ_ENTRY :
	 prompt        = "Speed search";
	 max_input_len = MAX_SEARCH_STRING_LEN;
	 break;
      case INPUT_MODE_VIEW_LINE :
      case INPUT_MODE_HISTORY_LINE :
	 prompt        = "Jump to line";
	 max_input_len = 10;
	 break;
      case INPUT_MODE_VIEW_SEARCH :
	 prompt        = "Search for ($ for hex num)";
	 max_input_len = MAX_SEARCH_STRING_LEN;
	 break;
      case INPUT_MODE_HISTORY_SEARCH :
	 prompt        = "Search for";
	 max_input_len = MAX_SEARCH_STRING_LEN;
	 break;
      case INPUT_MODE_FILE_PATTERN :
	 prompt        = "New file pattern";
	 max_input_len = MAX_PATTERN_LEN;
	 break;
      case INPUT_MODE_SELECT_PATTERN_MARK :
	 prompt        = "Select pattern for mark";
	 max_input_len = MAX_PATTERN_LEN;
	 break;
      case INPUT_MODE_SELECT_PATTERN_UNMARK :
	 prompt        = "Select pattern for unmark";
	 max_input_len = MAX_PATTERN_LEN;
	 break;
      case INPUT_MODE_CD :
	 prompt        = "New directory";
	 max_input_len = MAX_GADGET_BUFFER_LEN;
	 break;
      case INPUT_MODE_USER_FUNCTION :
	 prompt        = "New user function";
	 max_input_len = MAX_USER_FUNCTION_LEN;
	 break;
      case INPUT_MODE_COPY :
	 prompt        = "Copy to";
	 max_input_len = MAX_GADGET_BUFFER_LEN;
	 break;
      case INPUT_MODE_RENAME_MOVE :
	 prompt        = "Rename or move to";
	 max_input_len = MAX_GADGET_BUFFER_LEN;
	 break;
      case INPUT_MODE_MAKE_DIR :
	 prompt        = "Make new dir";
	 max_input_len = MAX_GADGET_BUFFER_LEN;
	 break;
      case INPUT_MODE_FIND :
	 prompt        = "Find pattern";
	 max_input_len = MAX_PATTERN_LEN;
	 break;
      default :
	 prompt        = "Input";
	 max_input_len = MAX_GADGET_BUFFER_LEN;
	 break;
   }
   prompt_width = strlen(prompt) * 8 + 8;

   /* Init string gadget */
   gad->LeftEdge = prompt_width + BORDER_LEFT;
   gad->TopEdge  = BORDER_TOP;
   gad->Width    = vsh_width - prompt_width - BORDER_LEFT - BORDER_RIGHT;
   gad->Height   = 9;
   if (mode == INPUT_MODE_VIEW_LINE || mode == INPUT_MODE_HISTORY_LINE) {
      gad->Activation = LONGINT;
   } else {
      gad->Activation = 0;
   }
   itext->LeftEdge = -prompt_width;
   itext->IText    = (UBYTE *)prompt;

   /* Init gadget input buffer */
   sinfo->DispPos  = 0;
   sinfo->MaxChars = max_input_len;
   if (default_input) {

      /* Default input text too long? */
      if (strlen(default_input) >= (max_input_len - 1)) {

	 /* Truncate default input text */
	 *(default_input + max_input_len - 1) = '\0';
	 DisplayBeep(NULL);
      }
      strcpy(buffer, default_input);
      sinfo->BufferPos = strlen(default_input);
   } else {
      *buffer          = '\0';
      sinfo->BufferPos = 0;
   }
   *(buffer + max_input_len - 1) = '\0';

   /* Clear status line, change screen font and install string gadget */
   SetAPen(con_rport, (LONG)COLOR0);
   RectFill(con_rport, (LONG)(BORDER_LEFT - 2), (LONG)BORDER_TOP, (LONG)
	     (vsh_width - BORDER_RIGHT + 1), (LONG)(BORDER_TOP_HIDDEN - 3));
   SetFont(&con_window->WScreen->RastPort, con_font);   /* need console 8x8 font for string gadgets */
   AddGadget(con_window, gad, -1L);
   RefreshGadgets(gad, con_window, NULL);
   ActivateGadget(gad, con_window, NULL);

   /* Input action loop */
   event_mode = EVENT_MODE_INPUT;
   do {
      if (mode == INPUT_MODE_FREQ_ENTRY) {
	 switch (action) {
	    case VSH_ACTION_SPEED_SEARCH :
	       jump_to_freq_entry(buffer, JUMP_MODE_FIRST);
	       break;
	    case VSH_ACTION_SHIFT_GADGET_END_KEY :
	       jump_to_freq_entry(buffer, JUMP_MODE_NEXT);
	       action = VSH_ACTION_NONE;
	       break;
	 }
      }
      if (mode == INPUT_MODE_TREQ_ENTRY) {
	 switch (action) {
	    case VSH_ACTION_SPEED_SEARCH :
	       jump_to_treq_entry(buffer, JUMP_MODE_FIRST);
	       break;
	    case VSH_ACTION_SHIFT_GADGET_END_KEY :
	       jump_to_treq_entry(buffer, JUMP_MODE_NEXT);
	       action = VSH_ACTION_NONE;
	       break;
	 }
      }
      Wait(SIGF_ACTION);
   } while (action != VSH_ACTION_GADGET_END_KEY && action != VSH_ACTION_ESC);

   /* Remove string gadget and restore old screen font */
   RemoveGadget(con_window, &gadget);
   SetFont(&con_window->WScreen->RastPort, old_wb_font);

   /* Abort ? */
   if (action == VSH_ACTION_ESC || *buffer == '\0') {

      /* Restore function key texts ? */
      switch (mode) {
	 case INPUT_MODE_FREQ_ENTRY :
	 case INPUT_MODE_TREQ_ENTRY :
	 case INPUT_MODE_FILE_PATTERN :
	 case INPUT_MODE_SELECT_PATTERN_MARK :
	 case INPUT_MODE_SELECT_PATTERN_UNMARK :
	 case INPUT_MODE_CD :
	 case INPUT_MODE_USER_FUNCTION :
	 case INPUT_MODE_COPY :
	 case INPUT_MODE_RENAME_MOVE :
	 case INPUT_MODE_MAKE_DIR :
	    change_fkey_text();
	    break;
	 case INPUT_MODE_VIEW_LINE :
	 case INPUT_MODE_VIEW_SEARCH :
	 case INPUT_MODE_HISTORY_LINE :
	 case INPUT_MODE_HISTORY_SEARCH :
	 case INPUT_MODE_FIND :
	    break;
      }

      /* Clear break signal */
      if (action == VSH_ACTION_ESC) {
	 SetSignal(0L, (LONG)SIGBREAKF_CTRL_C);
      }
      buffer = NULL;
   }
   action = VSH_ACTION_NONE;
   return(buffer);
}
	/* Get answer (yes/no/all) */

   USHORT
get_answer(USHORT mode, BYTE *text, USHORT default_answer)
{
   struct Gadget      *gad = &gadget;
   struct StringInfo  *sinfo = &gadget_info;
   struct IntuiText   *itext = &gadget_text;
   BYTE   *format, *default_input, prompt[MAX_LINE_LEN],
	  *path = &line2_buffer[0], *buffer = &gadget_buffer[0];
   USHORT len, answer;
   BOOL   keepon = TRUE;

   /* Init prompt text */
   event_mode = EVENT_MODE_IGNORE;
   switch (mode) {
      case ANSWER_MODE_COPY_NO_READ :
	 format = "Read protection set for %s - copy (y/n/a)?";
	 build_limited_path_name(path, NULL, text, max_line_len - 39 - 4,
						  LIMITED_PATH_MODE_NORMAL);
	 break;
      case ANSWER_MODE_COPY_OVERWRITE :
	 format = "Destination %s exists - overwrite (y/n/a)?";
	 build_limited_path_name(path, NULL, text, max_line_len - 42 - 4,
						  LIMITED_PATH_MODE_NORMAL);
	 break;
      case ANSWER_MODE_DELETE_START :
	 if (text) {
	    format = "Delete %ld entries (y/n)?";
	 } else {
	    format = "Delete current entry (y/n)?";
	 }
	 path   = NULL;
	 break;
      case ANSWER_MODE_DELETE_NO_DELETE :
	 format = "Delete protection set for %s - delete (y/n/a)?";
	 build_limited_path_name(path, NULL, text, max_line_len - 43 - 4,
						  LIMITED_PATH_MODE_NORMAL);
	 break;
      case ANSWER_MODE_QUIT :
	 format = "Quit Visual Shell (y/n)?";
	 path   = NULL;
	 break;
   }
   if (path) {
      SPrintf(&prompt[0], format, path);
   } else {
      SPrintf(&prompt[0], format, text);
   }
   len = strlen(&prompt[0]);

   /* Init string gadget */
   gad->LeftEdge   = len * 8 + 8 + BORDER_LEFT;
   gad->TopEdge    = BORDER_TOP;
   gad->Width      = vsh_width - len * 8 - 8 - BORDER_LEFT - BORDER_RIGHT;
   gad->Height     = 9;
   gad->Activation = 0;

   itext->LeftEdge = -(len * 8 + 8);
   itext->IText    = (UBYTE *)&prompt[0];

   /* Init gadget input buffer */
   switch (default_answer) {
      case ANSWER_TYPE_YES :
	 default_input = "y";
	 break;
      case ANSWER_TYPE_NO :
	 default_input = "n";
	 break;
      case ANSWER_TYPE_ALL :
	 default_input = "a";
	 break;
      default :
	 default_input = NULL;
	 break;
   }
   if (default_input) {
      strcpy(buffer, default_input);
      len = strlen(default_input);
   } else {
      *buffer = '\0';
      len     = 0;
   }
   sinfo->DispPos   = 0;
   sinfo->MaxChars  = 2;
   sinfo->BufferPos = len;

   /* Clear status line, change screen font and install string gadget */
   SetAPen(con_rport, (LONG)COLOR0);
   RectFill(con_rport, (LONG)(BORDER_LEFT - 2), (LONG)BORDER_TOP, (LONG)
	     (vsh_width - BORDER_RIGHT + 1), (LONG)(BORDER_TOP_HIDDEN - 3));
   SetFont(&con_window->WScreen->RastPort, con_font);   /* need console 8x8 font for string gadgets */
   AddGadget(con_window, gad, -1L);
   RefreshGadgets(gad, con_window, NULL);
   ActivateGadget(gad, con_window, NULL);

   /* Input action loop */
   do {
      event_mode = EVENT_MODE_INPUT;   /*ANSWER;*/
      Wait(SIGF_ACTION);

      /* Check action */
      if (action == VSH_ACTION_ESC) {

	 /* Clear break signal */
	 SetSignal(0L, (LONG)SIGBREAKF_CTRL_C);
	 answer = ANSWER_TYPE_ESC;
	 keepon = FALSE;
      } else {
	 if (action == VSH_ACTION_GADGET_END_KEY ||
				action == VSH_ACTION_SHIFT_GADGET_END_KEY) {
	    /* Check answer */
	    switch (toupper(*buffer)) {
	       case 'Y' :
		  answer = ANSWER_TYPE_YES;
		  keepon = FALSE;
		  break;
	       case 'N' :
		  answer = ANSWER_TYPE_NO;
		  keepon = FALSE;
		  break;
	       case 'A' :
		  switch (mode) {
		     case ANSWER_MODE_DELETE_START :
		     case ANSWER_MODE_QUIT :
			DisplayBeep(NULL);
			ActivateGadget(gad, con_window, NULL);
			break;
		     default :
			answer = ANSWER_TYPE_ALL;
			keepon = FALSE;
			break;
		  }
		  break;
	       default :
		  DisplayBeep(NULL);
		  ActivateGadget(gad, con_window, NULL);
		  break;
	    }
	 }
      }
   } while (keepon == TRUE);

   /* Remove string gadget and restore old screen font */
   RemoveGadget(con_window, &gadget);
   SetFont(&con_window->WScreen->RastPort, old_wb_font);
   action = VSH_ACTION_NONE;
   return(answer);
}
	/* Print directory info to specified filerequester */

   VOID
print_dir_info(struct FileRequest  *freq1)
{
   struct FileRequest  *freq2 = (freq1 == &file_req[0] ? &file_req[1] :
							      &file_req[0]);
   struct Info  *info = &freq2->fr_Info;
   USHORT left   = freq1->fr_Display.d_LeftEdge,
	  top    = freq1->fr_Display.d_TopEdge,
	  vlines = freq1->fr_Display.d_VisibleLines, vpos, len;
   BYTE   *line = &line1_buffer[0];

   if (show_flag && freq1->fr_Mode == FREQ_MODE_INFO) {
      SetAPen(con_rport, (LONG)COLOR0);
      RectFill(con_rport, (LONG)left, (LONG)top, (LONG)(left +
			    MAX_FREQ_LINE_WIDTH), (LONG)(top + vlines * 8));
      if (! info->i_Valid) {   /* valid info data ? */
	 display_text_centered(COLOR2, COLOR3, left, (USHORT)(top + vlines * 
			  4), "No directory selected !", MAX_FREQ_LINE_LEN);
      } else {
	 vpos = top + (vlines - MAX_INFO_LINES) * (USHORT)4;
	 display_text_centered(COLOR1, COLOR2, left, vpos,
			     "Infos about current disk", MAX_FREQ_LINE_LEN);
	 len = (USHORT)(strchr(&freq2->fr_DirName[0], ':') -
						     &freq2->fr_DirName[0]);
	 strncpy(line, &freq2->fr_DirName[0], (size_t)len);
	 *(line +  len) = '\0';
	 display_text_centered(COLOR3, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Volume name : %s", &info->i_VolumeName[0]);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Disk state  : %s", (info->i_DiskState ==
			  ID_WRITE_PROTECTED ? "Read only" : "Read/Write"));
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Block size  : %ld", info->i_BytesPerBlock);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Total bytes : %ld", info->i_TotalSize);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Free bytes  : %ld", info->i_FreeSize);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Soft errors : %ld", info->i_NumSoftErrors);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 display_text_centered(COLOR1, COLOR0, left, vpos += (USHORT)8,
					   (BYTE *)NULL, MAX_FREQ_LINE_LEN);
	 display_text_centered(COLOR1, COLOR2, left, vpos += (USHORT)8,
			      "Infos about current dir", MAX_FREQ_LINE_LEN);
	 build_limited_path_name(line, NULL, &freq2->fr_DirName[0],
			       MAX_FREQ_LINE_LEN, LIMITED_PATH_MODE_NORMAL);
	 display_text_centered(COLOR3, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Dirs        : %ld", info->i_Dirs);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Files       : %ld", info->i_Files);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
	 SPrintf(line, "Used bytes  : %ld", info->i_FileSizes);
	 display_text_left(COLOR1, COLOR0, left, vpos += (USHORT)8,
						   line, MAX_FREQ_LINE_LEN);
      }
   }
}
	/* Refresh directory info for the other filerequester */

   VOID
refresh_dir_info(struct FileRequest  *freq1)
{
   struct FileRequest  *freq2 = (freq1 == &file_req[0] ? &file_req[1] :
							      &file_req[0]);

   if (show_flag && freq2->fr_Mode == FREQ_MODE_INFO) {
      print_dir_info(freq2);
   }
}
	/* Print about page */

   VOID
print_about_page(VOID)
{
   USHORT i, color, left_offset, top_offset = BORDER_TOP +
			  MAX_OUTPUT_HEIGHT / 2 - MAX_ABOUT_LINES * 4 - 6;
   BYTE   *text;

   SetAPen(con_rport, (LONG)COLOR2);
   RectFill(con_rport, (LONG)BORDER_LEFT, (LONG)BORDER_TOP, (LONG)
		      (vsh_width - BORDER_RIGHT - 1), (LONG)(cli_vpos - 4));
   for (i = 0; i < MAX_ABOUT_LINES; i++) {
      if (text = about_line[i]) {
	 if (text == (BYTE *)-1L) {
	    text  = compile_date;
	    color = 1;
	 } else {
	    color = *text++ - '0';
	 }
	 left_offset = (vsh_width - strlen(text) * 8) / 2;   /* center line */
	 display_text(color, COLOR2, left_offset, (USHORT)(top_offset + i *
								  8), text);
      }
   }
   text        = prompt_line[3];   /* print prompt line */
   color       = *text++ - '0';
   left_offset = (vsh_width - strlen(text) * 8) / 2;
   display_text(color, COLOR2, left_offset, (USHORT)(cli_vpos - 3 - 2 * 8),
								      text);
}
	/* Refresh about page */

   VOID
refresh_about_page(VOID)
{
   print_about_page();
   draw_line(COLOR1, (USHORT)2, (USHORT)(cli_vpos - 2), (USHORT)
				   (vsh_width - 3), (USHORT)(cli_vpos - 2));
   draw_line(COLOR1, (USHORT)2, (USHORT)(vsh_height - FKEY_HEIGHT), (USHORT)
		       (vsh_width - 3), (USHORT)(vsh_height - FKEY_HEIGHT));
   print_fkey_text(FKEY_MODE_QUIT_ONLY);
}
	/* Print help page */

   BOOL
print_help_page(USHORT page_num)
{
   USHORT save_page_num = page_num, max_lines  = MAX_OUTPUT_HEIGHT / 8 - 3,
	  top_offset = MAX_OUTPUT_HEIGHT / 2 - (max_lines - 1) * 4;
   SHORT  i, color, indent, left_offset;
   BYTE   c, *text, **line = &help_line[0], *last_head_line = NULL;
   BOOL   next_page, last_page = FALSE;

   SetAPen(con_rport, (LONG)COLOR2);
   RectFill(con_rport, (LONG)BORDER_LEFT, (LONG)BORDER_TOP, (LONG)
		      (vsh_width - BORDER_RIGHT - 1), (LONG)(cli_vpos - 4));
   /* search start of selected help page */
   while (page_num && last_page == FALSE) {
      for (i = 0, next_page = FALSE; i < max_lines && last_page == FALSE &&
						  next_page == FALSE; i++) {
	 if (!(text = *line++)) {   /* valid help line ? */
	    last_page = TRUE;
	 } else {
	    if ((c = *text++) == 'H') {   /* new head line ? */
	       last_head_line = text;
	       if (i > 0) {   /* new head line already at start of new page ? */
		  next_page = TRUE;
	       }
	    } else {
	       if (i == 0) {   /* new page ? */
		  c = 'H';   /* print first old head line */
		  *line--;
	       }
	    }
	    if (next_page == FALSE) {   /* start new page ? */
	       if (c == 'H') {   /* head line ? */
		  i++;   /* one empty line after head line */
	       } else {   /* normal line */
		  if (c == 'K') {   /* keyboard type specified ? */
		     if (vsh_keyboard_type != (*text++ - '0')) {   /* wrong keyboard ? */
			i--;   /* once again the same line */
		     }
		  }
	       }
	    }
	    if (! *line) {   /* no more help lines ? */
	       last_page = TRUE;
	    }
	 }
      }
      if (last_page == FALSE) {   /* skip next page ? */
	 page_num--;
      }
   }
   /* print selected help page */
   for (i = 0, next_page = FALSE; i < max_lines && last_page == FALSE &&
						  next_page == FALSE; i++) {
      if (!(text = *line++)) {   /* valid help line ? */
	 last_page = TRUE;
      } else {
	 if ((c = *text++) == 'H') {   /* new head line ? */
	    last_head_line = text;
	    if (i > 0) {   /* new head line already at start of new page ? */
	       next_page = TRUE;
	    }
	 } else {
	    if (i == 0) {   /* new page ? */
	       text = last_head_line;   /* print first old head line */
	       c    = 'H';
	       *line--;
	    }
	 }
	 if (next_page == FALSE) {   /* start new page ? */
	    if (c == 'H') {   /* head line ? */
	       color  = 3;
	       indent = 0;
	    } else {   /* normal help line */
	       if (c == 'K') {   /* keyboard type specified ? */
		  if (vsh_keyboard_type == (*text++ - '0')) {   /* right keyboard ? */
		     color  = 1;
		     indent = *text++ - '0';
		  } else {
		     color = -1;   /* don't print this help line */
		     i--;   /* once again the same line */
		  }
	       } else {
		  color  = 1;
		  indent = c - '0';
	       }
	    }
	    if (color != -1) {
	       left_offset = (vsh_width - MIN_VSH_WIDTH) / 2 + (3 +
								indent) * 8;   /* left justify text */
	       display_text(color, COLOR2, left_offset, (USHORT)
						(top_offset + i * 8), text);
	    }
	    if (c == 'H') {
	       i++;   /* one empty line after head line */
	    }
	 }
	 if (! *line) {   /* no more help lines ? */
	    last_page = TRUE;
	 }
      }
   }
   if (last_page == FALSE) {   /* print prompt line */
      if (save_page_num) {
	 text = prompt_line[1];
      } else {
	 text = prompt_line[0];
      }
   } else {
      text = prompt_line[2];
   }
   color       = *text++ - '0';
   left_offset = (vsh_width - strlen(text) * 8) / 2;   /* center line */
   display_text(color, COLOR2, left_offset, (USHORT)(cli_vpos - 3 - 2 * 8),
								      text);
   return(last_page);
}
	/* Refresh help page */

   BOOL
refresh_help_page(USHORT page_num)
{
   BOOL last_page;

   last_page = print_help_page(page_num);
   draw_line(COLOR1, (USHORT)2, (USHORT)(cli_vpos - 2), (USHORT)
				   (vsh_width - 3), (USHORT)(cli_vpos - 2));
   draw_line(COLOR1, (USHORT)2, (USHORT)(vsh_height - FKEY_HEIGHT), (USHORT)
		       (vsh_width - 3), (USHORT)(vsh_height - FKEY_HEIGHT));
   print_fkey_text(FKEY_MODE_QUIT_ONLY);
   return(last_page);
}
