/*
	ARTemis (Graphic Editor for FM-TOWNS)
	(c) MATSUUCHI Ryosuke 1992

	mainmenu.c
*/

// #define DEBUG

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <egb.h>
#include <mos.h>

#include "ge.h"
#include "plt16.h"
#include "imageman.h"
#include "dispman.h"
#include "mask.h"
#include "colspace.h"

/*--------------------------------------------------------*/
/*             パレットとＲＧＢとＨＬＳの取扱             */
/*--------------------------------------------------------*/

static int  __plt[PLTNUM];
static deci __h[PLTNUM];

void plt_init(void)
{
	static	int		_plt32K[] =
		{ 0, 0, GRB(21, 0, 0), GRB(31,20,10),
		  GRB( 0, 0,31), GRB(13, 5,31), GRB( 0,22, 0), GRB(23, 3,31),
		  GRB( 6,31, 0), GRB(16,31, 5), GRB(31,31, 0), GRB(23,31,19),
		  GRB(10, 0, 0), GRB( 0,31,31), GRB(14,14,14), GRB(31,31,31) };
	memcpy(__plt, _plt32K, sizeof(int)*16);
	int i;
	for (i=0; i<PLTNUM; i++)
		__h[i] = -1;
}

int plt_getcode(int pltcode)
{
	return __plt[pltcode];
}

void plt_setcode(int pltcode, int colcode)
{
	__plt[pltcode] = colcode;
	deci h,l,s;
	#define C colcode
	rgb_hls(getR(C),getG(C),getB(C), &h,&l,&s);
	#undef C
	if(s>0)
		__h[pltcode] = h;
}

#define P	__plt[plt]

void plt_getRGB(int plt, int *r, int *g, int *b)
{
	*r=getR(P), *g=getG(P), *b=getB(P);
}

void plt_setRGB(int plt,int r,int g,int b)
{
	P = GRB(g,r,b);
	deci h,l,s;
	rgb_hls(r,g,b,&h,&l,&s);
	if(s>0)
		__h[plt] = h;
}

void plt_getHLS(int plt,deci *h,deci *l,deci *s)
{
	int r,g,b;
	r=getR(P),g=getG(P),b=getB(P);
	rgb_hls(r,g,b,h,l,s);
	if(__h[plt]>=0)
		*h = __h[plt];
}

void plt_setHLS(int plt,deci h,deci l,deci s)
{
	int r,g,b;
	hls_rgb(h,l,s,&r,&g,&b);
	P = GRB(g,r,b);
	__h[plt] = h;
}

void plt_setH(int plt, deci _h)
{
	int r,g,b;
	deci h,l,s;
	r=getR(P),g=getG(P),b=getB(P);
	rgb_hls(r,g,b,&h,&l,&s);
	h = _h;
	hls_rgb(h,l,s,&r,&g,&b);
	P = GRB(g,r,b);
	__h[plt] = _h;
}

void plt_setL(int plt, deci _l)
{
	int r,g,b;
	deci h,l,s;
	r=getR(P),g=getG(P),b=getB(P);
	rgb_hls(r,g,b,&h,&l,&s);
	l = _l;
	hls_rgb((__h[plt]>=0 ? __h[plt] : h),l,s,&r,&g,&b);
	P = GRB(g,r,b);
}

void plt_setS(int plt, deci _s)
{
	int r,g,b;
	deci h,l,s;
	r=getR(P),g=getG(P),b=getB(P);
	rgb_hls(r,g,b,&h,&l,&s);
	s = _s;
	hls_rgb((__h[plt]>=0? __h[plt] : h),l,s,&r,&g,&b);
	P = GRB(g,r,b);
}

#undef P

/*--------------------------------------------------------*/
/*                 カラーバー（RGB/HLS）                  */
/*--------------------------------------------------------*/

#include "colbar.md"

static	SCROLLBAR	*_sp;
static	int			_pltcode;

void colbar_disp(int x, int y, SCROLLBAR *sp, int pltcode)
{
	_pltcode = pltcode;
	int initcode[3];
	if (colmodel == MODEL_RGB)
		plt_getRGB(pltcode,&initcode[0],&initcode[1],&initcode[2]);
	else
	{
		int h,l,s;
		plt_getHLS(pltcode,&h,&l,&s);
		initcode[0] = _lim(DI(99 * h),0,99);
		initcode[1] = _lim(DI(99 * l),0,99);
		initcode[2] = _lim(DI(99 * s),0,99);
	}
	int mx,my;
	menu_getmenuxy(NULL, &mx, &my, NULL, NULL);
	_sp = sp;
	int i;
	for (i=0; i<3; i++)
	{
		#define S	_sp[i]
		S = RGBHLSbar[i];
		menu_scrollbar_setxy(&S,x+i*15,y);
		if (colmodel == MODEL_RGB)
			menu_scrollbar_setallsize(&S,31+5),
			menu_scrollbar_setdispsize(&S,5);
		else
			menu_scrollbar_setallsize(&S,99+16),
			menu_scrollbar_setdispsize(&S,16);
		menu_scrollbar_setvar(&S,initcode[i]);
		menu_drawscrollbar(&S);
		int tx,ty,tlen;
		menu_scrollbar_getxy(&S,&tx,&ty,&tlen);
		ty += tlen+1+13;
		grboxfill(tx,ty,12,12,menu_plt(COL_menu),DrawNORMAL);
		char *s[2][3] = {{"Ｒ","Ｇ","Ｂ"},{"Ｈ","Ｌ","Ｓ"}};
		ARTputstr12(tx,ty,(colmodel==MODEL_RGB?s[0][i]:s[1][i]),
					menu_plt(COL_menuString),menu_plt(COL_menu));
		#undef S
	}
}

void colbar_erase(void)
{
	int i;
	for (i=0; i<3; i++)
	{
		#define S	_sp[i]
		S = RGBHLSnullbar[i];
		#undef S
	}
}

void colbar_makeup(void)
{
	#define SETBV(n,a)   menu_scrollbar_setvar(&_sp[n],a)
	if (colmodel == MODEL_RGB)
	{
		int r,g,b;
		plt_getRGB(_pltcode, &r,&g,&b);
		SETBV(0,r);
		SETBV(1,g);
		SETBV(2,b);
	}
	else
	{
		deci h,l,s;
		plt_getHLS(_pltcode, &h,&l,&s);
		SETBV(0,_lim(DI(99*h),0,99));
		SETBV(1,_lim(DI(99*l),0,99));
		SETBV(2,_lim(DI(99*s),0,99));
	}
	menu_makeupscrollbar(&_sp[0]);
	menu_makeupscrollbar(&_sp[1]);
	menu_makeupscrollbar(&_sp[2]);
	#undef BV
}

void colbar_setpltcode(int pltcode)
{
	_pltcode = pltcode;
	colbar_makeup();
}

int colbar_getpltcode(void)
{
	return _pltcode;
}

void colbar_attachfunc(void (*func)())	// 更新関数の登録
{
	int i;
	for (i=0; i<3; i++)
	{
		#define S	_sp[i]
		menu_scrollbar_setfunc(&S, func);
		#undef S
	}
}

void colbar_getRGB(int *r, int *g, int *b)
{
	#define BV(n)	menu_scrollbar_getvar(&_sp[n])
	if (colmodel == MODEL_RGB)
		*r = BV(0),
		*g = BV(1),
		*b = BV(2);
	else
	{
		deci h,l,s;
		h = _lim((ID(BV(0))+FD(0.5)) / 99, 0, ID(1));
		l = _lim((ID(BV(1))+FD(0.5)) / 99, 0, ID(1));
		s = _lim((ID(BV(2))+FD(0.5)) / 99, 0, ID(1));
		hls_rgb(h,l,s,r,g,b);
	}
	#undef BV
}

/*--------------------------------------------------------*/
/*                  メインメニューの定義                  */
/*--------------------------------------------------------*/

#if 0
enum {
	itemPenSample=0,	itemView,		itemColList,
	itemForeCol,		itemBackCol,	itemCmdColspace,	itemMixrate,
	itemIcons,

	itemCmdPFLine,		itemCmdPFPset,	itemCmdLine,		itemCmdBlot,
	itemCmdSpray,		itemCmdBoxfill,	itemCmdPoly,

	itemCmdPaint,		itemCmdGoshi,	itemCmdSmooth,		itemCmdCopy,
	itemCmdCopyPoly,	itemCmdZoom,	itemCmdZoomPoly,

	itemCmdDiffuse,		itemCmdDiffuseA,	itemCmdSharp,
	itemCmdCopyBoxHturn,	itemCmdCopyBoxVturn,
	itemCmdCopyPolyHturn,	itemCmdCopyPolyVturn,
	
	itemCmdSand,		itemCmdSandA,	itemCmdColchg,
	itemCmdRotateBox,	itemCmdRotatePoly,
	itemCmdSave,		itemCmdLoad,

	itemCmdUndo,		itemCmdView,		itemCmdPat,		itemCmdSetup,
	itemCmdMask,		itemCmdTest,		itemCmdExit,

	/* itemCmdAccent, itemCmdPlay, itemCmdRec, itemCmdRecStop, */
	itemEND
};
#endif

#define	COLBARX	176
#define	COLBARY	24

static void disp_mainmenu(), erase_mainmenu();
static void touchRGBbars(),touchMixbar();
static void touchRGBbars();

#include "mainmenu.md"

#define	BAR(n)	&mainmenu_bars[n]
#define	MAINMIP(n)	(&mainmenu.itemlist[(n)])	/* MAIN Menu Item Pointer */

static	void	drawViewCsr(int col),eraseViewCsr();
static	int		drawCols();
static	void	eraseCols(), drawColCsr();
void cols_init(int *col1,int *col2, MENU *menu, int itemcol1, int itemcol2);


static void disp_mainmenu()
{
	int ix,iy,ixlen,iylen;
	menu_getbuttonxy(&mainmenu, itemColList, &ix, &iy, NULL, NULL);
	drawPltList(ix,iy);
	drawPltCsr(pltnum);
	drawViewCsr(White);
	menu_getbuttonxy(&mainmenu, itemPenSample, &ix, &iy, &ixlen,&iylen);
	grboxfill(ix,iy,ixlen,iylen,menu_plt(White),DrawNORMAL);
	grboxline(ix,iy,ixlen,iylen,menu_plt(Black),DrawNORMAL);
	pen_display(getcurpen(),ix+(ixlen-32)/2, iy+(iylen-32)/2);
	cols_init(&forecol,&backcol, &mainmenu, itemForeCol, itemBackCol);
	colbar_disp(COLBARX, COLBARY, BAR(0), pltnum);
	drawCols();
	drawColCsr();
	colbar_attachfunc(touchRGBbars);
}


static void erase_mainmenu()
{
	colbar_erase();
	erasePltList();
	eraseViewCsr();
	eraseCols();
}

/*--------------------------------------------------------*/
/*              メインメニューの更新ルーチン              */
/*--------------------------------------------------------*/


// ● ビューカーソルの描画･消去

static bool viewcsr_on = NO;
static int  viewcsr_x, viewcsr_y, viewcsr_xlen, viewcsr_ylen;
static int  viewcsr_col;


static void eraseViewCsr()
{
	if (!viewcsr_on)
		return;
	int ix,iy;
	menu_getbuttonxy(&mainmenu, itemView, &ix, &iy, NULL, NULL);
	grboxline(ix + viewcsr_x, iy + viewcsr_y,
			  viewcsr_xlen, viewcsr_ylen, DMgetmenuplt(Black), DrawNORMAL);
	viewcsr_on = NO;
}


static void getviewcsrpossize(int *x, int *y, int *xlen, int *ylen)
{
	int ix,iy,ixlen,iylen;
	menu_getbuttonxy(&mainmenu, itemView, &ix, &iy, &ixlen, &iylen);
	int dispx,dispy,dispxlen,dispylen;
	DMimage_getdispxy(&dispx,&dispy);
	DMimage_getdispxylen(&dispxlen,&dispylen);
	*x = ix + dispx * ixlen / EIMgetxsize();
	*y = iy + dispy * iylen / EIMgetysize();
	*xlen = _max(1,dispxlen * ixlen / EIMgetxsize());
	*ylen = _max(1,dispylen * iylen / EIMgetysize());
	return;
}


static void drawViewCsr(int col)
{
	int ix,iy,ixlen,iylen;
	menu_getbuttonxy(&mainmenu, itemView, &ix, &iy, &ixlen, &iylen);
	// 以下の５行で、ビューカーソルの表示位置・大きさを計算する
	int x,y,xlen,ylen,dispx,dispy,dispxlen,dispylen;
	DMimage_getdispxy(&dispx,&dispy);
	DMimage_getdispxylen(&dispxlen,&dispylen);
	x = dispx * ixlen / EIMgetxsize();
	y = dispy * iylen / EIMgetysize();
	xlen = _max(1,dispxlen * ixlen / EIMgetxsize());
	ylen = _max(1,dispylen * iylen / EIMgetysize());
	if (/* ビューカーソルの表示を変更する必要がないならば */
	    viewcsr_on && viewcsr_x == x && viewcsr_y == y &&
	    viewcsr_xlen == xlen && viewcsr_ylen == ylen && viewcsr_col == col)
	{
	 	return;
	}
	eraseViewCsr();
	grboxline(ix + x, iy + y, xlen, ylen, DMgetmenuplt(col), DrawNORMAL);
	viewcsr_x = x;
	viewcsr_y = y;
	viewcsr_xlen = xlen;
	viewcsr_ylen = ylen;
	viewcsr_col = col;
	viewcsr_on = YES;
}

static void touchRGBbars(void)
{
	int r,g,b;
	colbar_getRGB(&r,&g,&b);
	plt_setRGB(pltnum, r,g,b);
	makeupPltList();
	if (cols_getcsrpos() == 0)
		forecol = plt_getcode(pltnum);
	else
		backcol = plt_getcode(pltnum);
	makeupCols();
}

static void touchMixbar(void)
{
	mixrate = menu_scrollbar_getvar(BAR(3));
}

/*--------------------------------------------------------*/
/*                  メニューの項目を実行                  */
/*--------------------------------------------------------*/


extern void commandSave(),	 commandLoad(),	 commandCopy(), commandView();
extern void commandSetup(),	 commandPaint(), commandLine(), commandSpray();
extern void commandPFline(), commandPFPset(),setPen(),		commandBoxfill();
extern void commandPattern(),commandAccent(),commandZoom(), commandUndo();
extern void commandBlot(),   commandDiffuse(),commandSand();
extern void cmd_polygon(),   commandGoshi(),  commandMsPlay(),commandMsRec();
extern void commandMsRecStop();
extern void commandDiffuseArea(),commandSandArea(),commandSmooth();
extern void commandRotatePoly(),commandCopyPoly();
extern void commandZoomPoly(),commandRotateBox();
extern void commandColchg(),commandSharp();
extern void commandCopyHturn(),commandCopyVturn();
extern void commandCopyPolyHturn(),commandCopyPolyVturn();
extern void commandMask();
extern void commandColspace();
extern void commandFreeTest();


static int execMainMenuItems(int itemnum)
// 返値: -1 itemnum に該当する項目がなかった
//		  0 該当する項目が存在したので、それを実行した
{
	DEBUG_MSG("execMainMenuItems begin");

	typedef struct
	{
		#ifdef DEBUG
		char *name;			// デバッグ用のコマンド名文字列
		#endif
		short int item;		// 対応するアイテム番号(-1 : エンドマーク)
		char type;			// 0=メニューを消して、画面を保存して実行
							// 1=メニューを消し、画面を保存しないで実行
							// 2=メニューを消さずに実行
		void (*func)();		// 実行する関数
	} EXEC;

	static EXEC execlist[] =
	{
		#ifdef DEBUG
			#define	EE(a,b,c,d)		{ a,b,c,d }
		#else
			#define EE(a,b,c,d)		{ b,c,d }
		#endif
		EE( "セーブ",		itemCmdSave,		0,	commandSave		),
		EE( "ロード",		itemCmdLoad,		0,	commandLoad		),
		EE( "コピー",		itemCmdCopy,		1,	commandCopy		),
		EE( "ビュー",		itemCmdView,		1,	commandView		),
		EE( "設定",			itemCmdSetup,		1,	commandSetup	),
		EE("閉領域フィル",	itemCmdPaint,		1,	commandPaint	),
		EE( "直線",			itemCmdLine,		1,	commandLine		),
		EE( "スプレー",		itemCmdSpray,		1,	commandSpray	),
		EE( "自由曲線",		itemCmdPFLine,		1,	commandPFline	),
		EE( "自由点描",		itemCmdPFPset,		1,	commandPFPset	),
		EE( "ペン先編集",	itemPenSample,		1,	setPen			),
		EE( "矩形フィル",	itemCmdBoxfill,		1,	commandBoxfill	),
		EE("パターン編集",	itemCmdPat,			1,	commandPattern	),
		// EE("アクセント",	itemCmdAccent,		0,	commandAccent	),
		EE( "拡大縮小",		itemCmdZoom,		1,	commandZoom		),
		EE( "アンドゥー",	itemCmdUndo,		1,	commandUndo		),
		EE( "にじみペン",	itemCmdBlot,		1,	commandBlot		),
		EE( "ぼかしペン",	itemCmdDiffuse,		1,	commandDiffuse	),
		EE( "砂ペン",		itemCmdSand,		1,	commandSand		),
		EE("ポリゴンフィル",itemCmdPoly,		1,	cmd_polygon		),
		EE( "こすりペン",	itemCmdGoshi,		1,	commandGoshi	),
		// EE( "MSPLAY",	itemCmdPlay,		2,	commandMsPlay	),
		// EE( "MSREC",		itemCmdRec,			2,	commandMsRec	),
		// EE( "MSRECSTOP",	itemCmdRecStop,		2,	commandMsRecStop),
		EE( "領域ぼかし",	itemCmdDiffuseA,	1,	commandDiffuseArea),
		EE( "領域砂化",		itemCmdSandA,		1,	commandSandArea ),
		EE( "領域平滑",		itemCmdSmooth,		1,	commandSmooth ),
		EE( "矩形回転",		itemCmdRotateBox,	1,	commandRotateBox ),
		EE( "多角形回転",	itemCmdRotatePoly,	1,	commandRotatePoly ),
		EE( "多角形拡大",	itemCmdZoomPoly,	1,	commandZoomPoly ),
		EE( "多角形コピー",	itemCmdCopyPoly,	1,	commandCopyPoly ),
		EE( "色彩変換",		itemCmdColchg,		1,	commandColchg ),
		EE( "シャープ化",	itemCmdSharp,		1,	commandSharp ),
		EE( "左右反転BOX",	itemCmdCopyBoxHTurn,  1,commandCopyHturn ),
		EE( "上下反転BOX",	itemCmdCopyBoxVTurn,  1,commandCopyVturn ),
		EE( "左右反転POLY",	itemCmdCopyPolyHTurn, 1,commandCopyPolyHturn ),
		EE( "上下反転POLY",	itemCmdCopyPolyVTurn, 1,commandCopyPolyVturn ),
		EE( "マスク",		itemCmdMask,		1, commandMask ),
		EE( "テスト",		itemCmdTest,		1, commandFreeTest ),
		EE( "色空間",		itemCmdColSpace,	1, commandColspace ),
		EE( "",-1,-1,NOFNC )
		#undef EE
	};

	EXEC *ep;
	for (ep = execlist;  ep->item != -1;  ep++)
	{
		if (ep->item == itemnum)
		{
			switch (ep->type)
			{
			case 0:
				#ifdef DEBUG
				sprintf(debugmsg, "(execMainMenuItems) type 0 command:[%s]",
						ep->name);
				DEBUG_MSG(debugmsg);
				#endif
				DEBUG_MSG("これよりメニューを消去");
				menu_erase();
				DEBUG_MSG("これより編集バッファを退避");
				EIMbackup();
				DEBUG_MSG("退避完了");
				break;
			case 1:
				#ifdef DEBUG
				sprintf(debugmsg, "(execMainMenuItems) type 1 command:[%s]",
						ep->name);
				DEBUG_MSG(debugmsg);
				#endif
				menu_erase();
			}
			if (ep->func != NOFNC)
				(*ep->func)();
			if (cols_getcsrpos() == 0)
				forecol = plt_getcode(pltnum);
			else
				backcol = plt_getcode(pltnum);
			switch (ep->type)
			{
			case 0:
				menu_disp(&mainmenu);
				break;
			case 1:
				menu_disp(&mainmenu);
			}
			DEBUG_MSG("execMainMenuItems end");
			return 0;
		}
	}
	DEBUG_MSG("execMainMenuItems end");
	return -1;
}




/*--------------------------------------------------------*/
/*                view box によるスクロール               */
/*--------------------------------------------------------*/


static void touch_view(void)
{
	int ix,iy,ixlen,iylen;
	menu_getbuttonxy(&mainmenu, itemView, &ix, &iy, &ixlen, &iylen);
	int dispxlen,dispylen;
	DMimage_getdispxylen(&dispxlen,&dispylen);
	int vx,vy,vxlen,vylen;
	getviewcsrpossize(&vx,&vy,&vxlen,&vylen);
	if (!(vx <= ms.x && ms.x < vx+vxlen && vy <= ms.y && ms.y < vy+vylen))
	{
		int dispx, dispy;
		dispx = ((ms.x - vxlen / 2 - ix) * EIMgetxsize() + ixlen / 2) / ixlen;
		dispy = ((ms.y - vylen / 2 - iy) * EIMgetysize() + iylen / 2) / iylen;
		DMimage_limitdispxy(dispx,dispy,&dispx,&dispy);
		DMimage_setdispxy(dispx,dispy);
		getviewcsrpossize(&vx,&vy,&vxlen,&vylen);
	}
	int rx = ms.x - vx, ry = ms.y - vy;
	drawViewCsr(Yellow);
	while (ms.btn1 == OFFON || ms.btn1 == ON)
	{
		int predispx, predispy, dispx, dispy;
		DMimage_getdispxy(&predispx,&predispy);
		DMdispcsr(ms.x,ms.y);
		for (;;)
		{	ms_get(&ms);
			if (ms.dx!=0||ms.dy!=0||(ms.btn1==ONOFF||ms.btn1==OFF)||
			    ms.btn2!=OFF||key_chk()!=0) break; }
		DMerasecsr();
		limitCsrPos();
		dispx = ((ms.x - rx - ix) * EIMgetxsize() + ixlen / 2) / ixlen;
		dispy = ((ms.y - ry - iy) * EIMgetysize() + iylen / 2) / iylen;
		DMimage_limitdispxy(dispx,dispy,&dispx,&dispy);
		if (dispx != predispx || dispy != predispy)
			DMimage_setdispxy(dispx,dispy);
		drawViewCsr(Yellow);
	}
	limitCsrPos();
	drawViewCsr(White);
}


/*--------------------------------------------------------*/
/*                         メイン                         */
/*--------------------------------------------------------*/


void do_mainmenu(void)
{
	// mainmenu.x0 = ((DMimage_getzoomrate()==1? 512: 640) - mainmenu.xlen) / 2;
	// mainmenu.y0 = (480                                  - mainmenu.ylen) / 2;
	menu_scrollbar_setvar(BAR(3),mixrate);
	// menu_dispxy(-2,-2,&mainmenu);
	menu_dispxy(-3,-3,&mainmenu);
	int prex,prey;	// 自由曲線描画のための前座標保存変数
	for (;;)
	{
		DMdispcsr(ms.x,ms.y);
		drawViewCsr(White);
		for (;;)
		{
			ms_get(&ms);
			if (ms.dx!=0||ms.dy!=0||ms.btn1!=OFF||ms.btn2!=OFF||key_chk()!=0)
				break;
		}
		DMerasecsr();
		scrollForCsr(1,1);
		if (ms.btn1 == OFFON)
		{
			int a;  int ax,ay;  SCROLLBAR *sbarp;
			a = menu_where(ms.x,ms.y,&mainmenu, &ax,&ay,&sbarp);
			prex = prey = -1;
			switch (a)
			{
			case OutOfMenu:
				prex = DMimage_getx(ms.x), prey = DMimage_gety(ms.y);
				EIMpset(prex,prey,forecol,DrawNORMAL);
				break;
			case itemColList:
				drawPltCsr(pltnum);	// erase
				pltnum = ax/16 + (ay/16)*4;
				// makeupRgbBar();
				drawPltCsr(pltnum);	// draw
				// drawColCsr();
				// eraseCols();
				colbar_setpltcode(pltnum);
				if (cols_getcsrpos() == 0)
					forecol = plt_getcode(pltnum);
				else
					backcol = plt_getcode(pltnum);
				makeupCols();
				// drawCols();
				// drawColCsr();
				break;
			case itemForeCol:
				drawColCsr();
				cols_setcsrpos(0);
				drawColCsr();
				break;
			case itemBackCol:
				drawColCsr();
				cols_setcsrpos(1);
				drawColCsr();
				break;
/*
			case itemRgbBar:
				touchRgbBar(ax,ay,ms.btn1);
				if (cols_getcsrpos() == 0)
					forecol = plt_getcode(pltnum);
				else
					backcol = plt_getcode(pltnum);
				makeupCols();
				break;
*/
			case itemCmdExit:
				menu_erase();
				extern int commandExit();
				if (commandExit() == 0)
					goto EXIT;
				menu_disp(&mainmenu);
				break;
			case itemScrollBar:
				menu_touchscrollbar(sbarp, ax,ay);
				if (cols_getcsrpos() == 0)
					forecol = plt_getcode(pltnum);
				else
					backcol = plt_getcode(pltnum);
				makeupCols();
				break;
			case itemMoveMenu:
				menu_move();
				break;
			case itemView:
				touch_view();
				break;
			default:
				execMainMenuItems(a);
			}
		}
		else if (ms.btn1 == ON)
		{
			int a;  int ax,ay;
			a = menu_where(ms.x,ms.y,&mainmenu, &ax,&ay,NULL);
			if (a != OutOfMenu)
				prex = prey = -1;
			switch (a)
			{
			case OutOfMenu:
				{
					int nx,ny;
					nx = DMimage_getx(ms.x);
					ny = DMimage_gety(ms.y);
					if (prex >= 0 && (prex!=nx || prey!=ny))
					{
						// printf("line:%d,%d,%d,%d\n",prex,prey,nx,ny);
						EIMline(prex,prey,nx,ny,forecol,DrawNORMAL);
					}
					else
						EIMpset(nx,ny,forecol,DrawNORMAL);
					prex = nx;
					prey = ny;
				}
				break;
/*
			case itemRgbBar:
				touchRgbBar(ax,ay,ms.btn1);
				if (cols_getcsrpos() == 0)
					forecol = plt_getcode(pltnum);
				else
					backcol = plt_getcode(pltnum);
				makeupCols();
*/
			}
		}
		else if (ms.btn2 == OFFON)
		{
			int a;
			a = menu_where(ms.x,ms.y,&mainmenu, NULL,NULL,NULL);
			if (a == OutOfMenu)
			{
				int px,py;
				px = DMimage_getx(ms.x),  py = DMimage_gety(ms.y);
				drawPltCsr(pltnum);	// erase
				if (mode == MODE32K)
					plt_setcode(pltnum, EIMpoint(px,py));
				else
					pltnum = EIMpoint(px,py);
				colbar_setpltcode(pltnum);
				makeupPltList();
				// makeupRgbBar();
				drawPltCsr(pltnum);	// draw
				drawColCsr();
				if (cols_getcsrpos() == 0)
					forecol = plt_getcode(pltnum);
				else
					backcol = plt_getcode(pltnum);
				makeupCols();
				drawColCsr();
			}
		}
	}
EXIT:
	return;
}

/* end of mainmenu.c */
