/*===========================================
          DolphMorph（ドルフモーフ）

      モーフィング＆変形アニメ作成ソフト

  モーフィングアルゴリズム: EAST 1994
  インターフェース作成:     松内 良介 1994
===========================================*/
/*
	ファイル関係のちょっとした処理

	void	RM_putblock32k(char *buf, int x,int y,int width,int height)
*/

#include <stdio.h>
#include <stdefs.h>
#include <egb.h>
#include <malloc.h>
#include <stdlib.h>
#include <msdos.cf>
#include <memory.h>
#include <interrup.cf>
#include <math.h>
#include <dos.h>

#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <file_dlg.h>
#include <fnt.h>

#include "egbmac.h"
#include "wgbmac.h"

#include "morph.h"
#include "desktop.h"
#include "guisub.h"

int resetfattr(char *fname)
{
	if (_dos_setfileattr(fname, _A_ARCH | _A_NORMAL) == 0)
		return 0;
	else
		return -1;
}

int fexist(char *fname)
{
	unsigned int b;
	if (_dos_getfileattr(fname, &b) == 0)
		return 1;
	else
		return 0;
}

void trans_fname_ext(char *buf, char *fname, char *newext)
/*
	ファイル名 fname の拡張子を newext に置き換えたファイル名を返す
*/
{
	static char path_buf[_MAX_PATH];
	char drive[_MAX_DRIVE], dir[_MAX_DIR], basename[_MAX_FNAME], ext[_MAX_EXT];
	_splitpath(fname, drive, dir, basename, ext);
	_makepath(buf, drive, dir, basename, newext);
}

void add_fname_ext(char *buf, char *fname, char *newext)
/*
	ファイル名 fname に拡張子が無い場合、newext を拡張子として加える
*/
{
	char drive[_MAX_DRIVE], dir[_MAX_DIR], basename[_MAX_FNAME], ext[_MAX_EXT];
	_splitpath(fname, drive, dir, basename, ext);
	if (ext[0] == '\0')
		_makepath(buf, drive, dir, basename, newext);
	else
		_makepath(buf, drive, dir, basename, ext);
}

/*--------------------------------------------------------*/
/*                     裏画面への描画                     */
/*--------------------------------------------------------*/

	void RM_putblock32k(char *buf, int x,int y,int width,int height)
	{
		typedef struct {
			short	x1,y1,x2,y2;
		} VIEW;
		VIEW view;
		EGB_writePage(guiEgbPtr, 1);
		WINCTRL *pCtrl;
		MMI_GetControl(&pCtrl);
		if (pCtrl->clip == NULL)
		{
			view.x1 = 0;
			view.y1 = 0;
			view.x2 = 319;
			view.y2 = 239;
			EGB_viewport(guiEgbPtr, (char*)&view);
			EGB_PUTBLOCK(guiEgbPtr, buf, x,y, width,height,1);
		}
		else
		{
			WINCLIP *pClip;
			Rect imageRect;
			imageRect.left  = x*2;
			imageRect.up    = y*2;
			imageRect.right = x*2 + width*2 - 1;
			imageRect.down  = y*2 + width*2 - 1;
			for (pClip = pCtrl->clip;  pClip != NULL;  pClip=pClip->nextClip)
			{
				Rect clipRect;
				clipRect = *(Rect*)&pClip->fr;
				if (SectRect(&clipRect, &imageRect))
				{
					view.x1 = (pClip->fr.lupx) / 2;
					view.y1 = (pClip->fr.lupy) / 2;
					view.x2 = (pClip->fr.rdwx+1) / 2;
					view.y2 = (pClip->fr.rdwy+1) / 2;
					EGB_viewport(guiEgbPtr, (char*)&view);
					EGB_PUTBLOCK(guiEgbPtr, buf, x,y, width,height, 1);
				}
			}
		}
		EGB_writePage(guiEgbPtr, 0);
		view.x1 = 0;
		view.y1 = 0;
		view.x2 = 639;
		view.y2 = 479;
		EGB_viewport(guiEgbPtr, (char*)&view);
	}

/*--------------------------------------------------------*/
/*             アラートとしてウィンドウを表示             */
/*--------------------------------------------------------*/

	void MORPH_dispWinAlert(int idWin)
	{
		RM_moveCenter(idWin);
		MTL_setFlagObj(idDesktopSelectiveHyper, MS_UNSELECT) ;
		MMI_SendMessage(idWin, MM_ATTACH, 1, idDesktopAlertHyper);
		MMI_SendMessage(idWin, MM_SHOW, 0);
	}
	
	void MORPH_eraseWinAlert(int idWin)
	{
		MMI_SendMessage(idWin, MM_ERASE, 0);
		MMI_SendMessage(idWin, MM_DETACH, 0);
		MTL_resetFlagObj(idDesktopSelectiveHyper, ~MS_UNSELECT) ;
	}
