/*
 * stopquit.c
 *
 * This module provides a window to enabled the user to confirm a quit action if they decide to
 * try and exit us whilst we're fetching.
 *
 * It also handles the 'Stop' button on the status window, but confirmation is not requested in
 * this case
 *
 * JJH, 22/05/98
 *
 * $Id: stopquit,v 1.11 2002/01/05 18:47:14 joseph Exp $
 *
 */

#include <stdlib.h>
#include "memleak.h"

#include "wimplib.h"
#include "wimpclib.h"

#include "err.h"
#include "stopquit.h"
#include "IconNames.h"

static int stopquit_winhan = -1; /* handle of our window */
static int stopquit_task; /* task handle of the taskmanager? */
static stopquit_t stopquit_action; /* the action we'll take when the user presses the confirmation button */

/* this is the handler to be called when we want to cause the current fetch to stop / program to quit */
static stopquit_func_t stopquit_func;
/* and this is a pointer we pass to it */
static void *stopquit_handle;

stopquit_t stopquit_pending = stopquit_Null; /* true if there's a stop/quit event that hasn't been actioned yet */

/* One time initialisation */
void stopquit_init( int winhan )
{
  stopquit_winhan = winhan;
}

/* This handles when the user confirms they do/don't want to quit after they tried to
 * quit in the middle of a transfer. */
/* returns:
 *   0 : click wasn't for us
 *   1 : click was for us
 */
int stopquit_click( int winhan, int icon, int but )
{
  if ( stopquit_winhan != winhan ) return 0;

  if ( (icon != QUIT_QUIT && icon != QUIT_CANCEL) || but != Wimp_MouseButtonSelect )
    return 1;

  if ( icon == QUIT_QUIT )
    stopquit_handler( stopquit_action );

  wimp_close_window( &stopquit_winhan );

  return 1;
}


/* Open Quit box, storing stopquit_t and task for later user
 * action : action to be taken when user pressed confirmation button
 * task   : taskhandle of taskmanager, if it's a shutdown
 */
void stopquit_show(stopquit_t action, int task)
{
  if ( stopquit_winhan == -1 ) return;
  if ( wimpc_openwindowcentre( stopquit_winhan, 0 ) < 0 ) return;
  stopquit_action = action;
  stopquit_task = task;
}

/* close our window */
void stopquit_hide()
{
  if ( stopquit_winhan == -1 ) return;
  wimp_close_window( &stopquit_winhan );
}

#include "log.h"
#include "swis.h"
/* Take pending action, called after the user has confirmed it, and the fetch/send module has closed itself down */
void stopquit_act(stopquit_t action)
{
  WimpKeyPressedEvent event;

  stopquit_pending = stopquit_Null;

  switch (action)
  {
  case stopquit_Quit:
    exit(0);
  case stopquit_Shutdown:
    E(wimp_get_caret_position(&event.caret));
    event.key_code = 0x1fc;	/* scF12 */
    E(wimp_send_message(Wimp_EKeyPressed, &event, stopquit_task, 0, 0));
    exit(0);
/*    E( _swix( Wimp_ProcessKey, _IN(0), 0x1fc scF12 ) ); */
    break;
  }
}


/* called when stop button in status win is pressed, or quit button in quit window pressed */
void stopquit_handler( stopquit_t action )
{
  stopquit_pending = action;

  if ( stopquit_func )
    (*stopquit_func)( action, stopquit_handle );

  /* otherwise, the action is delayed until the next call to event_poll.
     This enables the foreground task to close down properly before the
     action is carried out.
   */
}


/* this procedure is called to register the handler that should be called when we get a stopquit_handle call */
void stopquit_registerhandler( stopquit_func_t func, void *handle )
{
  stopquit_func   = func;
  stopquit_handle = handle;
}

/* this procedure deregister's the handler - eg. at end of fetch */
void stopquit_deregister( void )
{
  stopquit_func   = NULL;
}
