/* Copyright (c) 1996 by Lele Gaifax.  All Rights Reserved
 *
 * This file is part of Nothing (yet).
 *
 * $RCSfile: AlertPanel.m,v $
 * $Revision: 1.1.1.1 $
 * $Date: 1996/09/30 06:06:48 $
 *
 * Created Wed Sep  4 15:27:34 1996.
 */

#include <appkit/appkit.h>
#include "AlertPanel.h"

#define CLASS_VERSION 0

@implementation AlertPanel

+ (void) initialize
{
  if (self == [AlertPanel class])
    {
      [AlertPanel setVersion:CLASS_VERSION];
    }
}

- initWithTitle:(const char *) title
{
  return [self initWithTitle:title message:NULL];
}

- initWithTitle:(const char *) title
	message:(const char *) msg
{
  return [self initWithTitle:title
	       message:msg
	       defaultButton:NULL];
}

- initWithTitle:(const char *) title
	message:(const char *) msg
  defaultButton:(const char *) defButton
{
  return [self initWithTitle:title
	       message:msg
	       defaultButton:defButton
	       altButton:NULL];
}

- initWithTitle:(const char *) title
	message:(const char *) msg
  defaultButton:(const char *) defButton
      altButton:(const char *) altButton
{
  return [self initWithTitle:title
	       message:msg
	       defaultButton:defButton
	       altButton:altButton
	       otherButton:NULL];
}

- initWithTitle:(const char *) title
	message:(const char *) msg
  defaultButton:(const char *) defButton
      altButton:(const char *) altButton
    otherButton:(const char *) otherButton
{
  [super init];

  mainTitle = NXCopyStringBuffer (title);
  if (msg)
    message = NXCopyStringBuffer (msg);
  if (defButton)
    defaultButtonTitle = NXCopyStringBuffer (defButton);
  if (altButton)
    alternateButtonTitle = NXCopyStringBuffer (altButton);
  if (otherButton)
    otherButtonTitle = NXCopyStringBuffer (otherButton);

  return self;
}

- (void) setAction:(id <PythonObject>) anAction
{
  if ([anAction callableCheck])
    action = anAction;
  else
    NXLogError ("action should be a callable object");
}

- (enum _alert_panel_status) status
{
  return status;
}

- (enum _alert_panel_status) run
{
  if (status == ALERT_PANEL_READY)
    {
      [Application new];
      
      switch (NXRunAlertPanel (mainTitle,
			       message,
			       defaultButtonTitle,
			       alternateButtonTitle,
			       otherButtonTitle))
	{
	case NX_ALERTDEFAULT:
	  status = ALERT_PANEL_DEFAULT;
	  break;
	  
	case NX_ALERTALTERNATE:
	  status = ALERT_PANEL_ALTERNATE;
	  break;
	  
	case NX_ALERTOTHER:
	  status = ALERT_PANEL_OTHER;
	  break;
	}

      if (action)
	[action callableCallFunction:"i", status];
    }
  
  return status;
}  

- (const char *) buttonTitle
{
  switch (status)
    {
    case ALERT_PANEL_READY:
      return "None";

    case ALERT_PANEL_DEFAULT:
      return defaultButtonTitle;

    case ALERT_PANEL_ALTERNATE:
      return alternateButtonTitle;

    case ALERT_PANEL_OTHER:
      return otherButtonTitle;
    }
}

@end /* AlertPanel class implementation */
