/*----------------------------------------------------------------------------
                           tpvview.c (show tpv file)
                       This file is a part of Topaz systems
                  Copyright: Hisao Kawaura, All rights reserved
                                   1997 - 98


    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

----------------------------------------------------------------------------*/

#include <windows.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include "Parray.h"


HWND hWnd;
static volatile HANDLE thread;
extern Parray *xpa;
extern bool drawline(HDC dc, FILE *in, char *str, METAFILEPICT *mpict, int jobtype,
				 double printmagx, double printmagy, double printshiftx, double printshifty);
int winx = CW_USEDEFAULT;
int winy = CW_USEDEFAULT;
int winw = CW_USEDEFAULT;
int winh = CW_USEDEFAULT;
char wndtitle[1000];

static DWORD WINAPI Winhandle(LPVOID param)
{
	char stemp[10000];
	HDC dc;

	xpa = new Parray;
	
	dc = GetDC(hWnd);
	while(fgets(stemp, sizeof(stemp), stdin) != NULL)
	  drawline(dc, stdin, stemp, NULL, 0, 1.0, 1.0, 0.0, 0.0);
	ReleaseDC(hWnd, dc);
	delete xpa;
	PostMessage(hWnd, WM_QUIT, 0, 0);
	ExitThread(1);
}



LRESULT FAR PASCAL _export WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			return (1);
			break;
	}

	return (DefWindowProc(hWnd, message, wParam, lParam));	
	
}

BOOL InitApplication(HINSTANCE hInstance)
{
	WNDCLASS wc;
	
	wc.style = 0;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance= hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = 0;
	wc.lpszClassName = "tpvview";
	return (RegisterClass(&wc));
}

bool getoptions(LPSTR lpCmdLine)
{
	char *p = lpCmdLine;
	char stemp[1000], stemp3[3];
	char opt;

	while (*p != 0)
	{
		if (*p == '-')
		{
			p++;
			if (strchr("xywht", *p) == NULL)
				return false;
			opt = *p++;
			
			strcpy(stemp, "");
			while(*p != 0 && *p != ' ' && *p != '\n' && *p != '\r')
			{
				stemp3[0] = *p++;
				stemp3[1] = 0;
				strcat(stemp, stemp3);
			}

			switch(opt)
			{
				case 'x':
					winx = atoi(stemp);
					break;
				case 'y':
					winy = atoi(stemp);
					break;
				case 'w':
					winw = atoi(stemp);
					break;
				case 'h':
					winh = atoi(stemp);
					break;
				case 't':
					strcpy(wndtitle, stemp);
					break;
				case 'v':
					fprintf(stderr, "wtpvview Ver. 1.00\n");;
					fprintf(stderr, "Written by Hisao Kawaura\n");
					fprintf(stderr, "-x(x position)\n");
					fprintf(stderr, "-y(y position)\n");
					fprintf(stderr, "-w(width)\n");
					fprintf(stderr, "-h(height)\n");
					fprintf(stderr, "-t(window title)\n");
					return false;
					break;
			}
		}
		p++;
	}
	return true;
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hWnd = CreateWindow(	"tpvview",
						 	"tpvview",
	 						WS_OVERLAPPEDWINDOW,
	 						winx,
	 						winy,
	 						winw,
	 						winh,
	 						NULL,
	 						NULL,
	 						hInstance,
	 						NULL);

	if (hWnd == NULL) return (FALSE);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	return (TRUE);

}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	MSG msg;
	DWORD dw, retthread;
	char stemp[10000];

	strcpy(wndtitle, "");

	if (hPrevInstance == NULL)
	{
		if (!InitApplication(hInstance))
			return FALSE;
	}

	if (!getoptions(lpCmdLine))
		exit(1);

	if (!InitInstance(hInstance, nCmdShow)) return FALSE;

	SetWindowText(hWnd, wndtitle);

	thread = CreateThread(0, 0, Winhandle, 0, 0, &dw);

	while (GetMessage(&msg, NULL, 0, 0))
 	{
 		TranslateMessage(&msg);
 		DispatchMessage(&msg);
 	}

    WaitForSingleObject(thread, INFINITE);
    CloseHandle(thread);
	exit (0);

}


syntax highlighted by Code2HTML, v. 0.9.1