/*********************************
*  INPUT  09/29/90
*  Source file for STV
*  © Copyright 1990 Timm Martin
*  All Rights Reserved Worldwide
**********************************/

#include <exec/types.h>
#include <functions.h>
#include <intuition/intuition.h>
#include "func.h"
#include "main.h"

/**************
*  INPUT KEYS
***************/

/*
This procedure handles keypresses.  It returns SELECT_PREV, SELECT_NEXT,
SELECT_STOP, or SELECT_SOME based on the key that was pressed.
*/

SELECT input_keys( struct IntuiMessage *imessage )
{
  SELECT file = SELECT_SOME;

  if (imessage->Qualifier & IEQUALIFIER_LSHIFT ||
      imessage->Qualifier & IEQUALIFIER_RSHIFT)
  {
    switch (imessage->Code)
    {
      case /* CursorUp    */ 0x4C: end_up();    break;
      case /* CursorDown  */ 0x4D: end_down();  break;
    }
  }
  else if (imessage->Qualifier & IEQUALIFIER_LALT ||
           imessage->Qualifier & IEQUALIFIER_RALT)
  {
    switch (imessage->Code)
    {
      case /* CursorUp    */ 0x4C: page_up();    break;
      case /* CursorDown  */ 0x4D: page_down();  break;
    }
  }
  else
  {
    switch (imessage->Code)
    {
      case /* CursorUp    */ 0x4C: line_up( YES );     break;
      case /* CursorDown  */ 0x4D: line_down( YES );   break;
      case /* SpaceBar    */ 0x40: page_down();        break;
      case /* 'S' key     */ 0x21: search();           break;
      case /* 'A' key     */ 0x20:
      case /* RETURN      */ 0x44:
      case /* ENTER       */ 0x43: search_again();     break;
      case /* 'N' key     */ 0x36: file = SELECT_NEXT; break;
      case /* 'P' key     */ 0x19: file = SELECT_PREV; break;
      case /* 'Q' key     */ 0x10:
      case /* ESCape      */ 0x45: file = SELECT_STOP; break;
    }
  }
  return (file);
}

/****************
*  INPUT WINDOW
*****************/

/*
This function handles all user input while the window is up, returning either
SELECT_PREV, SELECT_NEXT, or SELECT_STOP if the user wants to view the
previous file, the next file, or end the program.
*/

SELECT input_window( SELECT prev, SELECT next )
{
  SELECT file = SELECT_SOME;
  struct IntuiMessage *imessage;

  CLEAR_POINTER;

  while (file == SELECT_SOME)
  {
    WAIT_FOR_INPUT;
    while (WINDOW_INPUT || SCROLLING || PROPPING)
    {
      if (imessage)
      {
        /* only do this file the file hasn't already been selected */
        if (file == SELECT_SOME)
        {
          switch (imessage->Class)
          {
            case RAWKEY:
              file = input_keys( imessage );
              break;
            case MOUSEBUTTONS:
              SCROLLING = imessage->Code == SELECTDOWN;
              break;
            case MENUPICK:
              file = menu_pick( MENUNUM( imessage->Code ),
                                ITEMNUM( imessage->Code ) );
              break;
            case GADGETDOWN:
              PROPPING = YES;
              break;
            case CLOSEWINDOW:
              file = SELECT_STOP;
              break;
            case REFRESHWINDOW:
              refresh_display();
              break;
          } /* switch message class*/
        }
        ReplyMsg( (struct Message *)imessage );
      } /* if window input */

      else if (SCROLLING)
        scroll_display();

      else /* PROPPING */
        gadget_current();

    } /* while window input */

    /* check to make sure there is a previous or next file */
    if ((file == SELECT_PREV && prev == SELECT_NONE) ||
        (file == SELECT_NEXT && next == SELECT_NONE))
      file = SELECT_SOME;

  } /* while continue inputs */

  SET_POINTER;

  /* return which file want to read next */
  return (file);
}
