/*====================================================
                      ARTemis
                   (version 1.3)
             FM-TOWNS 用ペイントツール

                 by 松内 良介 1994
====================================================*/
/*
	imgbtn.c  :  イメージボタン型部品・実現モジュール
	
	外部関数:
		int MMI_initImageBtn(void)
*/

#define	MODULE_IMGBTN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include "art.h"
#include "imgbtn.h"

int		MJ_IMGBTN = -1;

extern int	ML_DspDrawBtnL40(int kobj, int messId, int argc);

/*	表示処理	*/

	static int ML_IMGBTNdisp(kobj, messId, argc)
	int		kobj ;
	int		messId ;
	int		argc ;
	{
		register IMGBTN *pbtn ;  IMGBTN btn ;
		ML_DspDrawBtnL40(kobj, messId, argc);
		btn = *(pbtn = ((IMGBTN *)(TL_getObjectPtr(kobj)->data))) ;
		if (btn.func == (int(*)())NULL)
			return NOERR;
	  /* 部品の位置を得る */
		MG_CorrectPositionL40(&(btn.fr), (HYPER *)pbtn) ;
	  /* 最描画する必要があるか? */
		if (MMI_CheckUpdateFrame(&(btn.fr)) && btn.func!=0)
		{
			POINT new,old;
			WINCLIP *clip;
			MG_mosDisp(2) ;
			WIN_beginUpDateObj(kobj, &clip);
			new.x = new.y = 0;
			MG_PushOrigin(&new, &old);
			if (btn.func != (int(*)())NULL)
				(*btn.func)(kobj, MM_SHOW, 0);
			MG_PopOrigin(&old);
			WIN_endUpDateObj(clip);
			MG_mosDisp(3) ;
		}
	  /* おわり */
		return NOERR ;
	}

/*	呼び出し関数の登録  */
	
	static int ML_IMGBTNsetexec(kobj, messId, argc, func)
	int		kobj ;
	int		messId ;
	int		argc ;
	int		(*func)();
	{
		IMGBTN *plbl ;
		plbl = (IMGBTN *) ( TL_getObjectPtr(kobj) -> data ) ;
		plbl->func = func ;
		return NOERR ;
	}

/* マウスボタンが押されたときの呼び出し関数 */

	static int ML_IMGBTNmouseon(kobj, messId, argc, pev)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev;
	{
		IMGBTN *plbl ;
		plbl = (IMGBTN *) ( TL_getObjectPtr(kobj) -> data ) ;
	  /* 属性MS_BTLEFTL40,MS_BTRIGHTL40 のいずれも非設定だったり、
	     あるいはボタンが左右とも押されていない場合はなにもしない */
		if (((plbl->atr &(MS_BTLEFTL40|MS_BTRIGHTL40)) & 
										((pev->shift) << 22)) == 0)
			return NOERR ;
	  /* 実行関数の起動 */
		if (plbl->func != (int(*)())NULL)
			(*plbl->func)(kobj, MM_MOUSEON, 1, pev);
		return NOERR ;
	}

/* メッセージに対する関数の対応 */

	#define	NMET_IMGBTN	3
	
	static int (*MF_IMGBTNmethod[NMET_IMGBTN])() = {
		ML_IMGBTNdisp,
		ML_IMGBTNmouseon,
		ML_IMGBTNsetexec
	} ;
	
	static char *MM_IMGBTNmessage[NMET_IMGBTN] = {
		MT_SHOW,
		MT_MOUSEON,
		MT_SETEXEC
	};

/* 初期化関数 */

	int MMI_initImageBtn(void)
	{
		MJ_IMGBTN = MMI_AddType(MJ_HYPER, NMET_IMGBTN,
								sizeof(IMGBTN),
								MF_IMGBTNmethod,
								MM_IMGBTNmessage) ;
		if (MJ_IMGBTN < NOERR)
			return MJ_IMGBTN ;
		return NOERR ;
	}

/* プロトタイプの生成 */

	#define ALIGN	4
	#define OFFSET(type) \
	        (sizeof(MMIPACKET) + (sizeof(type)+ALIGN-1)/ALIGN*ALIGN)

	int IMGBTN_makePrototype(int *objId)
	{
		static int tempId;
		static MMIINIT initDataIMGBTN = { "MmiInit",   1, 0 } ;
		static MMIPACKET d001 = { &tempId, NULL, &MJ_IMGBTN,
		                          OFFSET(IMGBTN), MS_NONE };
		static IMGBTN    d001d = { MS_EVMOSONL40 | MS_BTLEFTL40,
		                           0,0,47,47, 0,0,0,
		                           MS_NONEL40, (int(*)())NULL };
		int ret;
		if ((ret = MMI_Init(&initDataIMGBTN)) < NOERR)
			return ret ;
		*objId = tempId;
		return NOERR;
	}

