#include <v_lib.h>

#ifdef	EMULATE_GDI

/*
 *	Using metafiles: portable mode.
 */
#define	rgbSet(c,r,g,b) (c)[0]=(r); (c)[1]=(g); (c)[2]=(b);

void	metaRect(vhandle_t meta,
	vec_t x1,vec_t y1,vec_t x2,vec_t y2,u8_t r,u8_t g,u8_t b)
{
	vec3_t v;
	color4b_t c;
	vec2_t st={0,0};
	gl_tl_callbacks_p ref_api;
	ref_api=metaGetRefAPI(meta);
	rgbSet(c,r,g,b);
	/* draw quadrilateral */
	ref_api->begin_polygon(meta,4);
	vec3Set(v,x1,y1,0);
	ref_api->polygon_vertex(meta,v,c,st);
	vec3Set(v,x2,y1,0);
	ref_api->polygon_vertex(meta,v,c,st);
	vec3Set(v,x2,y2,0);
	ref_api->polygon_vertex(meta,v,c,st);
	vec3Set(v,x1,y2,0);
	ref_api->polygon_vertex(meta,v,c,st);
}

/* Implementation for metaTextOut() is not too hard, too ... :) */

int main()
{
	int i;
	vhandle_t meta;
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
#ifdef	NO_CLIPBOARD
	meta=metaCreateRendererEx("out.wmf",
		150,150,			/* size of picture in millimeters */
		1000.0f,1000.0f,	/* logical size of view */
		0x7FFF,0x7FFF);
#else	/* !NO_CLIPBOARD */
	meta=metaCreateRendererEx("//./clipboard",
		150,150,			/* size of picture in millimeters */
		1000.0f,1000.0f,	/* logical size of view */
		0x7FFF,0x7FFF);
#endif	/* !NO_CLIPBOARD */
	if(meta == NULL)
		return -1;
	srand(timeTickCount());
	for(i=10;i<500;i+=50)
		metaRect(meta,i,i,1000-i,1000-i,
			128 + rand() % 128,128 + rand() % 128,128 + rand() % 128);
	metaDestroyRenderer(meta);
	memPrintStats();
#ifndef	NO_CLIPBOARD
	puts("Start winword (or whatever) and select 'Edit|Paste'.");
#endif	/* !NO_CLIPBOARD */
	return 0;
}

#else	/* !EMULATE_GDI */

#ifdef	NO_CLIPBOARD
#error	"In v_conf.h comment NO_CLIPBOARD and recompile"
#endif	/* NO_CLIPBOARD */

#include <../source/PAL/oshead.h>

/*
 *	Using metafiles: native Win32 mode.
 */
int main()
{
	HDC dc;
	vhandle_t meta;
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	meta=metaCreateRendererEx("//./clipboard",
		150,150,	/* size in millimeters */
		1.0f,1.0f,	/* logical size of view not used; we are using: */
		1000,1000);	/* raw metafile extents */
	if(meta == NULL)
		return -1;
	dc=metaGetHandleNP(meta);
	metaChangeFontNP(meta,100,0,0,0,"Times New Roman Cyr");
	SelectObject(dc,GetStockObject(GRAY_BRUSH));
	Rectangle(dc,1,1,999,999);
	SelectObject(dc,GetStockObject(LTGRAY_BRUSH));
	Rectangle(dc,100,100,900,900);
	SetBkMode(dc,TRANSPARENT);
	SetTextColor(dc,RGB(255,255,0));
	TextOut(dc,0,0,"[_Привет_]",10);
	metaChangeFontNP(meta,100,90,0,0,"Times New Roman Cyr");
	TextOut(dc,0,1000,"[_Привет_]",10);
	metaChangeFontNP(meta,100,180,0,0,"Times New Roman Cyr");
	TextOut(dc,1000,1000,"[_Привет_]",10);
	metaChangeFontNP(meta,100,270,0,0,"Times New Roman Cyr");
	TextOut(dc,1000,0,"[_Привет_]",10);
	metaDestroyRenderer(meta);
	memPrintStats();
	puts("Start winword (or whatever) and select 'Edit|Paste'.");
	return 0;
}

#endif	/* !EMULATE_GDI */
