/*
 * treport.c
 *
 * Displays a error messages to the user in a small dialogue
 *
 * Multiple unacknowledged errors cause it to use blue icons rather than red ones
 *
 * $Id: treport,v 1.13 2000/01/02 18:28:05 joseph Exp $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "swis.h"

#include "wimplib.h"

#include "err.h"
#include "log.h"
#include "treport.h"
#include "macros.h"
#include "IconNames.h"

#include "wimpclib.h"

/* These aren't exported from WinEd. You can't set validation strings for indirected spriteonly icons. */
#define treport_Graphic1 1
#define treport_Graphic2 2




static int treport_winhan = -1; /* window handle */
int treport_pending = 0; /* number of unacked errors */


void treport_close( int winhan )
{
  if ( winhan == treport_winhan )
    treport_pending = 0;
}

void treport_click( int winhan, int icon, int button )
{
  if ( winhan != treport_winhan ) return;

  if ( icon == treport_Dismiss && button & Wimp_MouseButtonSelect )
  {
    E_CHECK( wimp_close_window( &treport_winhan ) );
    treport_pending = 0;
  }
}

/* returns :
 *   1 - we handed the key press
 *   0 - we didn't
 */
int treport_keyhan( int key )
{
  if ( key == 0x1b && treport_pending ) /* esc */
  {
    E_CHECK( wimp_close_window( &treport_winhan ) );
    treport_pending = 0;
    return 1;
  }

  return 0;
}

void treport(const char *msg, int level)
{
  if (msg == NULL)
  {
    msg = "(null)";
  }

  _kernel_oswrch( 0x07 );

  xsyslog_logmessage(log_NAME, msg, level);

  wimpc_seticontext( treport_winhan, treport_Message, msg );

#ifndef FEATURE_LEAVETREPORTICONS
  if ( treport_pending )
  {
    /* use a blue icon if we've already displayed an error and it's got acknowledged */
    wimpc_seticontext( treport_winhan, treport_Graphic1, "&popstar" );
    wimpc_seticontext( treport_winhan, treport_Graphic2, "&popstar" );
  }
  else
  {
    wimpc_seticontext( treport_winhan, treport_Graphic1, "!popstar" );
    wimpc_seticontext( treport_winhan, treport_Graphic2, "!popstar" );
  }
#endif
  treport_pending++;
  wimpc_openwindowcentre( treport_winhan, 0 );
}

void treport_init( int winhan )
{
  treport_winhan = winhan;
}
