#define MODULE_DESKTOPS
#if 0

int	toolboxToolFunc(int kobj, int messId)
int	desktop_about(int kobj)
int	desktop_quit(int kobj)
int	desktop_create(int kobj)
int	desktopMITEMfile_loadimgFunc(kobj, messId, argc, pev, trigger)
int	desktopMITEMfile_saveimgFunc(int kobj)
int	desktop_setImageZoom(int kobj)

#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <file_dlg.h>
#include "art.h"
#include "guisub.h"
#include "alert.h"

#include "about.h"
#include "imagewin.h"
#include "colsel.h"
#include "pic.h"
#include "setup.h"
#include "desktop.h"

int	idDesktopSelectiveHyper = -1 ;
int	idBackDialog = -1 ;
int	idDesktopWinAttachHyper = -1 ;
int	idDesktopToolAttachHyper = -1 ;
int	idToolBoxDlg = -1 ;
int	idToolBoxToolTIcon[13] = -1 ;
int	idToolBoxMsg = -1 ;
int	idToolBoxUndoDBtn = -1 ;
int	idDesktopDialog = -1 ;
int	idDesktopMenu = -1 ;
int	idDesktopMItem_System = -1 ;
int	idDesktopSystemMenu = -1 ;
int	idDesktopSystemMItem[3] = -1 ;
int	idDesktopMItem_File = -1 ;
int	idDesktopFileMenu = -1 ;
int	idDesktopFileMItem[4] = -1 ;
int	idDesktopMItem_Image = -1 ;
int	idDesktopImageMenu = -1 ;
int	idDesktopImageMItem[4] = -1 ;
int	idDesktopZoomMenu = -1 ;
int	idDesktopZoomMItem[8] = -1 ;
int	idDesktopMItem_Setup = -1 ;
int	idDesktopExitIcon = -1 ;
int	idDesktopAlertHyper = -1 ;
int	idDesktopHelpIcon = -1 ;
int	idDesktopSkbIcon = -1 ;
int	idDesktopFileDlgHyper = -1 ;
int	idImageClearDlg = -1 ;
int	idImageClearTitleMsg = -1 ;
int	idImageClearMsg[4] = -1 ;
int	idImageClearDBtn[2] = -1 ;
int	idDesktopMItem_Edit = -1 ;
int	idDesktopMItem_Layer = -1 ;
int	idDesktopLayerMenu = -1 ;
int	idDesktopLayerMItem[8] = -1 ;

static int tool_x = 0;

/*--------------------------------------------------------*/
/*                   モジュールの初期化                   */
/*--------------------------------------------------------*/

int		desktop_init(void)
{
	RM_initRadioButton(idToolBoxToolTIcon,INTNUM(idToolBoxToolTIcon),&tool_x);
	return 0;
}

/*--------------------------------------------------------*/
/*                   部品の呼び出し関数                   */
/*--------------------------------------------------------*/

int	toolboxToolFunc(int kobj, int messId)
// idToolBoxToolTIcon[i]:MJ_TICONL40の呼び出し関数
{
	RM_pushRadioButton(kobj,idToolBoxDlg,idToolBoxToolTIcon,
					   INTNUM(idToolBoxToolTIcon),&tool_x);
	return NOERR ;
}

int	ToolBoxUndoDBtnFunc(int kobj)
/*	initDataZDESKTOP:idToolBoxUndoDBtn:MJ_DBUTTONL40の呼び出し関数	*/
{
	imagewin_recoverUndo();
	return NOERR ;
}

int	desktop_about(int kobj)
// desktopMITEMsystem_about:MJ_MITEML40の呼び出し関数
{
	WINCTRL* ctrl;
	MMI_GetControl(&ctrl);
	static char msgbuf[40];
	sprintf(msgbuf, "Free Memory %dK", TL_checkMemory(0)*4);
	MMI_SendMessage(aboutMSG[6], MM_SETMSG, 1, msgbuf);
	static char msgbuf2[40];
	sprintf(msgbuf2,"現在の画面: %d×%d×%s colors",
			ctrl->bound.rdwx+1, ctrl->bound.rdwy+1, 
			(scrPixelSize == 16 ? "32K" : "16M"));
	MMI_SendMessage(aboutMSG[7], MM_SETMSG, 1, msgbuf2);
	RM_moveCenter(aboutALT);
	MMI_SendMessage(aboutALT, MM_ATTACH, 1, idDesktopAlertHyper);
	MMI_SendMessage(aboutALT, MM_SHOW, 0);
	return NOERR ;
}

int	desktop_quit(int kobj)
// desktopMITEMsystem_quit:MJ_MITEML40の呼び出し関数
// desktopICONexit:MJ_ICONL40の呼び出し関数
{
	if (dispCheckMessage("ＡＲＴｅｍｉｓの終了",
			"ARTemis を終了します。よろしいですか？", "終了") == 0)
		MMI_SetHaltFlag(TRUE);
	return NOERR ;
}

int	desktop_create(int kobj)
// desktopMITEMfile_create:MJ_MITEML40の呼び出し関数
{
	desktop_createNewPicture();
	return NOERR ;
}

int	desktopMITEMfile_loadimgFunc(int kobj)
// desktopMITEMfile_loadimg:MJ_MITEML40の呼び出し関数
{
	char pathbuf[_MAX_PATH];
	int ret;
	ret = RM_callSimpleFDG(
			idDesktopFileDlgHyper, idDesktopSelectiveHyper,
			"画像の読込", "読込","取消",
			"*.TIF",FDG_NONE,NULL,pathbuf);
	if (ret != 0)
		return NOERR;
	TIFFINFO info;
	if ((ret = RM_getTIFFinfo(pathbuf, &info)) != NOERR)
	{
		dispAlertMessage("画像の読み込み",
			(ret == TIFFERR_NOMEMORY ? 
				"TIFF読み込み処理に必要なメモリが不足しているので、"
				"読み込むことができません" :
			 ret == TIFFERR_INVALIDFORMAT ?
				"このファイルはTIFFファイルではないので、"
		        "ARTemisで読み込むことはできません" :

				"ファイルをオープンできません")
		);
		return NOERR;
	}
	if (info.pixelsize != 16 && info.pixelsize != 24)
	{
		dispAlertMessage("画像の読み込み",
		                 "このファイルはフルカラーTIFFファイルでも"
		                 "32768色TIFFでもないので、ARTemisで読み込"
		                 "むことはできません");
		return NOERR;
	}
	IMWIN* win = imagewin_new(info.pixelsize, info.wid, info.ht);
	if (win == NULL)
	{
		dispAlertMessage("画像の読み込み",
			"新しい画像ウィンドウ生成に必要なメモリが不足しています。"
			"不要な画像ウィンドウをクローズしてみてください");
		return NOERR;
	}
	imagewin_link(win);
	imagewin_setFileName(win, pathbuf, FALSE);
	imagewin_show(win);
	int csr;
	MG_PushPtr(MOSICON_WAIT, &csr);
	pic_loadTIFF_(win->pic, pathbuf);
	imagewin_updateBuf(win);
	imagewin_updateScr(win);
	MG_PopPtr(csr);
	return NOERR;
}

int	desktopMITEMfile_saveimgFunc(int kobj)
// desktopMITEMfile_saveimg:MJ_MITEML40の呼び出し関数
{
	IMWIN* win = imagewin_getCurrentWin();
	if (win == NULL)
		return NOERR;
	char pathbuf[_MAX_PATH];
	int ret;
	ret = RM_callSimpleFDG(
			idDesktopFileDlgHyper, idDesktopSelectiveHyper,
			"画像の保存", "保存","取消",
			"*.TIF",FDG_TEXT,(win->fname[0]==0 ? NULL : win->fname),pathbuf);
	if (ret != 0)
		return NOERR;
	char drive[_MAX_DRIVE],dir[_MAX_DIR],basename[_MAX_FNAME],ext[_MAX_EXT];
	_splitpath(pathbuf, drive,dir,basename,ext);
	if (basename[0] == 0)
	{
		dispAlertMessage("画像の保存",
		    "保存するファイル名が指定されませんでした");
		goto END;
	}
	_makepath(pathbuf, drive,dir,basename,".TIF");
	FILE *fp = fopen(pathbuf, "rb");
	if (fp != NULL)
	{
		fclose(fp);
		if (dispCheckMessage("画像の保存",
				"同じ名前のファイルがディスクに存在しています。"
				"上書きしますか？", "上書き") != 0)
			goto END;
	}
	int csr;
	MG_PushPtr(MOSICON_WAIT, &csr);
	ret = pic_saveTIFF_(win->pic, pathbuf);
	MG_PopPtr(csr);
	if (ret == NOERR)
		imagewin_setFileName(win, pathbuf, TRUE);
	else if (ret == TIFFERR_NOMEMORY)
	{
		dispAlertMessage("画像の保存",
				"画像の保存処理に必要なメモリが不足しているので、"
				"保存することができません");
	}
	else if (ret == TIFFERR_NODISKSPACE)
	{
		dispAlertMessage("画像の保存",
				"ディスクの空き容量が不足しています");
	}
	else if (ret == TIFFERR_CANNOTOPEN)
	{
		dispAlertMessage("画像の保存",
				"ファイルがオープンできません。"
				"ファイル名に不適切な文字が含まれているのだと思います");
	}
END:
	return NOERR ;
}

int	Desktop_FileCloseProc(int kobj)
// idDesktopFileMItem[3]:MJ_MITEML40の呼び出し関数
{
	IMWIN* win = imagewin_getCurrentWin();
	if (win == NULL)
		return NOERR;
	char buf[250];
	sprintf(buf, "画像 %s のウィンドウをクローズします。"
	        "画像データは破棄されますが、よろしいですか？",
	        (win->fname[0]==0 ? "<名称未定>" : win->fname));
	if (dispCheckMessage("画像ウィンドウを閉じる", buf, "続行") == 0)
	{
		imagewin_unlink(win);
		imagewin_destroy(win);
		if (imagewin_getCurrentWin() == win)
			imagewin_setCurrentWin(NULL);
	}
	return NOERR ;
}

int	Desktop_UndoProc(int kobj)
// idDesktopImageMItem[3]:MJ_MITEML40の呼び出し関数
{
	imagewin_recoverUndo();
	return NOERR ;
}


static int flag_imgclear;

  /* ■画像のクリア■ */

		/*initDataDESKTOP:desktopMITEMfile_clear:MJ_MITEML40の呼び出し関数	*/
	int	desktopMITEMfile_clearFunc(kobj, messId, argc, pev, trigger)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev ;
	int		trigger ;
	{
		EXECDIALOG(idImageClearDlg);
		if (flag_imgclear)
		{
			IMWIN *win;
			if ((win = imagewin_getCurrentWin()) != NULL)
			{
				WINCLIP *clip;
				PIXEL pixel;
				colsel_getPixel(&pixel);
				pic_clear(win->pic, &pixel);
				imagewin_updateBuf(win);
				imagewin_updateScr(win);
			}
		}
		return NOERR ;
	}

  /* ■「画像のクリア」ダイアログメニューのボタン■ */

		/*	initDataDESKTOP:imgclearDBTN[0]:MJ_DBUTTONL40の呼び出し関数	*/
		/*	initDataDESKTOP:imgclearDBTN[1]:MJ_DBUTTONL40の呼び出し関数	*/
	int	imgclearDBTNFunc(kobj, messId, argc, pev, trigger)
	int		kobj ;
	int		messId ;
	int		argc ;
	EVENT	*pev ;
	int		trigger ;
	{
		if (kobj == idImageClearDBtn[0])
			flag_imgclear = 1;
		else
			flag_imgclear = 0;
		MMI_SetHaltFlag(TRUE);
		return NOERR ;
	}

int	desktop_setImageZoom(int kobj)
// idDesktopZoomMItem[i]:MJ_MITEML40の呼び出し関数
{
	int i;
	for (i=0; i<INTNUM(idDesktopZoomMItem); i++)
		if (idDesktopZoomMItem[i] == kobj)
			goto FOUND;
	return NOERR;
  FOUND:
	IMWIN* win = imagewin_getCurrentWin();
	if (win != NULL)
	{
		imagewin_setZoom(win, i+1);
		imagewin_updateTitle(win, TRUE);
	}
	return NOERR ;
}

/*	initDataZDESKTOP:idDesktopMItem_Setup:MJ_MITEML40の呼び出し関数	*/
int	desktop_setup(kobj, messId, argc, pev, trigger)
int		kobj ;
int		messId ;
int		argc ;
EVENT	*pev ;
int		trigger ;
{
	cmd_setup();
	return NOERR ;
}



int		toolbox_getCurrentTool(void)
{
	return tool_x;
}

