/*===========================================
          DolphMorph（ドルフモーフ）

      モーフィング＆変形アニメ作成ソフト

  モーフィングアルゴリズム: EAST 1994
  インターフェース作成:     松内 良介 1994
===========================================*/
#if 0

	alert.c
		警告メッセージ、確認メッセージの表示

	static	void	setTitleAndMessage(int idTitleMsg, char *title,
									   int *idDispMsg, int msgNum, char *msg)

			void	dispAlertMessage(char *title, char *msg)
			void	alert_noMemory(char *title)
			int		dispCheckMessage(char *title, char *msg, char *okmsg)

			int		AlertOkDBtnFunc(kobj, messId, argc, pev, trigger)
			int		CheckAlertOkDBtnFunc(kobj, messId, argc, pev, trigger)
			int		CheckAlertCancelDBtnFunc(kobj, messId, argc, pev, trigger)
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <ctype.h>

#include "art.h"
#include "guisub.h"
#include "desktop.h"

#define	_iskanji(c)	(((c)&0xff) >= 0x81 && ((c)&0xff)<=0x9f || \
					 ((c)&0xff) >= 0xe0 && ((c)&0xff)<=0xfc)
#define	_iskanji1(kcode)	((((kcode)>>8)&0xff) <= 0x97)

/*--------------------------------------------------------*/
/*                        部品ＩＤ                        */
/*--------------------------------------------------------*/

	int	idAlertDlg = -1 ;
	int	idAlertTitleMsg = -1 ;
	int	idAlertOkDBtn = -1 ;
	int	idAlertMsg[3] = -1 ;
	int	idCheckAlertDlg = -1 ;
	int	idCheckAlertTitleMsg = -1 ;
	int	idCheckAlertMsg[3] = -1 ;
	int	idCheckAlertOkDBtn = -1 ;
	int	idCheckAlertOkMsg = -1 ;
	int	idCheckAlertCancelDBtn = -1 ;
	int	idCheckAlertCancelMsg = -1 ;
	int	idBigAlertDlg = -1 ;
	int	idBigAlertTitleMsg = -1 ;
	int	idBigAlertOkDBtn = -1 ;
	int	idBigAlertMsg[3] = -1 ;

/*--------------------------------------------------------*/
/*               タイトル、表示文字列の設定               */
/*--------------------------------------------------------*/

	static char msgbuf[3][100];	// [3] で決め打ちはよくない…
	static char titlebuf[100];

	static void setTitleAndMessage(int idTitleMsg, char *title,
									int *idDispMsg, int msgNum, char *msg)
	{
		int idx;
	  /* タイトル文字列の設定 */
		titlebuf[0] = 0;
		if (title != NULL)
			strncpy(titlebuf, title, 63);
		MMI_SendMessage(idTitleMsg, MM_SETMSG, 1, title);
	  /* 表示文字列の設定 */
		int width;
		FRAME fr;
		RM_getFrame(idDispMsg[0], &fr);
		width = (fr.rdwx - fr.lupx + 1) / 6;
		for (idx=0; idx<msgNum; idx++)
		{
			memset(msgbuf[idx], ' ', width);
			msgbuf[idx][width] = '\0';
		}
		if (msg == NULL)
			goto NOMSG;
		int clm;
		idx = 0;
		clm = 0;
		char *sp;
		sp = msg;
		while (*sp != '\0' && idx < msgNum)
		{
			if (*sp == '\n' || clm >= width-1)
			{
				idx++;
				clm = 0;
				if (*sp == '\n')
					sp++;
			}
			else if (!_iskanji(*sp))
				msgbuf[idx][clm++] = *sp++;
			else
			{
				msgbuf[idx][clm++] = *sp++;
				msgbuf[idx][clm++] = *sp++;
			}
		}
	   NOMSG:;
		for (idx=0; idx<msgNum; idx++)
			MMI_SendMessage(idDispMsg[idx], MM_SETMSG, 1, msgbuf[idx]);
	}

/*--------------------------------------------------------*/
/*                   警告メッセージ表示                   */
/*--------------------------------------------------------*/

	static void dispAlertMessageSub(int idAlert)
	{
		int moscsr;
		MG_PushPtr(MOSICON_ARROW, &moscsr);
		EXECDIALOG(idAlert);
		MG_PopPtr(moscsr);
	}

	void dispAlertMessage(char *title, char *msg)
	{
		setTitleAndMessage(idAlertTitleMsg, title,
						   idAlertMsg, INTNUM(idAlertMsg), msg);
		dispAlertMessageSub(idAlertDlg);
	}

	void dispBigAlertMessage(char *title, char *msg)
	{
		setTitleAndMessage(idBigAlertTitleMsg, title,
						   idBigAlertMsg, INTNUM(idBigAlertMsg), msg);
		dispAlertMessageSub(idBigAlertDlg);
	}

	void alert_noMemory(char *title)
	{
		dispAlertMessage(title, "メモリ容量不足です");
	}

/*--------------------------------------------------------*/
/*                   確認メッセージ表示                   */
/*--------------------------------------------------------*/

	static int nCheck;

	int dispCheckMessage(char *title, char *msg, char *okmsg)
	{
		int idx;
		static char OkMsg[20];
		if (okmsg != NULL)
			strncpy(OkMsg, okmsg, 19);
		else
			strcpy(OkMsg, "続行");
		int moscsr;
		MG_PushPtr(MOSICON_ARROW, &moscsr);
		MMI_SendMessage(idCheckAlertOkMsg, MM_SETMSG, 1, OkMsg);
		setTitleAndMessage(idCheckAlertTitleMsg, title,
						   idCheckAlertMsg, INTNUM(idCheckAlertMsg), msg);
		EXECDIALOG(idCheckAlertDlg);
		MG_PopPtr(moscsr);
		return nCheck;
	}

/*--------------------------------------------------------*/
/*                   部品の呼び出し関数                   */
/*--------------------------------------------------------*/

	/*	initDataZALERT:idAlertOkDBtn:MJ_DBUTTONL40の呼び出し関数	*/
	int	AlertOkDBtnFunc(kobj, messId, argc, pev, trigger)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev ;
	int		trigger ;
	{
		MMI_SetHaltFlag(TRUE);
		return NOERR ;
	}

	/*	initDataZALERT:idBigAlertOkDBtn:MJ_DBUTTONL40の呼び出し関数	*/
	int	BigAlertOkDBtnFunc(kobj, messId, argc, pev, trigger)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev ;
	int		trigger ;
	{
		MMI_SetHaltFlag(TRUE);
		return NOERR ;
	}

	/*	initDataZALERT:idCheckAlertOkDBtn:MJ_DBUTTONL40の呼び出し関数	*/
	int	CheckAlertOkDBtnFunc(kobj, messId, argc, pev, trigger)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev ;
	int		trigger ;
	{
		nCheck = 0;
		MMI_SetHaltFlag(TRUE);
		return NOERR ;
	}

	/*	initDataZALERT:idCheckAlertCancelDBtn:MJ_DBUTTONL40の呼び出し関数	*/
	int	CheckAlertCancelDBtnFunc(kobj, messId, argc, pev, trigger)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev ;
	int		trigger ;
	{
		nCheck = -1;
		MMI_SetHaltFlag(TRUE);
		return NOERR ;
	}
