// GptWin.cpp
// copyright 1992 Pittsburgh Supercomputing Center
#include "gpt.h"


char CGMWindow::szClass[]       = "gptCGM";
char CGMWindow::szMenu[]        = "gptMenuCGM";
//HMENU CGMWindow::hMenu          = NULL;
HMENU CGMWindow::hWindow        = NULL;
//HMENU CGMWindow::hMenuWindow    = NULL;

// ******************* CGMWindow Declarations *****************
void CGMWindow::DoCaption (LPSTR mesg)
	  {
	  char szCaption [80] ;

	  wsprintf (szCaption, "%s - %s", (LPSTR) mesg,
					(LPSTR) (FObj->GetFileName() )) ;

	  SetWindowText (hWndThis, szCaption) ;
	  }




// *** CGMWindow::WndProc
long CGMWindow::WndProc(HWND hWnd, WORD iMessage, WORD wParam,
																					 LONG lParam )
	{
	char tmp[256];
	this->iMessage = iMessage;
	this->wParam   = wParam;
	this->lParam   = lParam;
	switch (iMessage)
		{
		case WM_CREATE:
		hWndThis = hWnd;
		SBars = new Scroller(this, DrawRect.right, DrawRect.bottom);

		DoCaption("pcGPlot");
		return 0;



		case WM_RBUTTONDOWN:     // user initated debug break
		return 0;

		case WM_INITMENUPOPUP:
		if (lParam == CREATE_POP)
			{
			if ( CgmObj->IsValidCgm() )
				{
				EnableMenuItem (wParam, IDM_SAVE , MF_ENABLED) ;
				EnableMenuItem (wParam, IDM_META , MF_ENABLED) ;
				}
			else
				{
				EnableMenuItem (wParam, IDM_SAVE , MF_GRAYED) ;
				EnableMenuItem (wParam, IDM_META , MF_GRAYED) ;
				}
			}
		if (lParam == CGM_MENU_POS	)
			{
			if ( CgmObj->NextPageExists() )
				EnableMenuItem (wParam, IDM_NEXT , MF_ENABLED) ;
			else
				EnableMenuItem (wParam, IDM_NEXT , MF_GRAYED) ;
			if ( CgmObj->PrevPageExists() )
				EnableMenuItem (wParam, IDM_PREVIOUS , MF_ENABLED) ;
			else
				EnableMenuItem (wParam, IDM_PREVIOUS , MF_GRAYED) ;
			}


		return 0;

		case WM_PAINT:
		hWindow = hWndThis;
		Paint();
		return 0;

		case WM_SIZE:
		Size();
		break;

		case WM_VSCROLL:
		case WM_HSCROLL:
		SBars->Scroll();
		break;

		case WM_COMMAND:
		switch (wParam)
			{
			FileObjectPt FObjSave;
			case IDM_ERRORS:
			if (!ErrMsg1)
				OKMsgBox("*** GPlot Errors ***", "No Errors" );
			else
				OKMsgBox("*** GPlot Errors ***", "%s\n%s", ErrMsg1, ErrMsg2 );
			return 0;

			case IDM_NEXT:    // Via Menu or accel key Shift-F2
			if (CgmObj->NextPage())InvalidateRect(hWndThis, NULL, TRUE) ;
			SBars->SetPos(0,0);
			return 0;

			case IDM_PREVIOUS:  // Via Menu or accel key Shift-F1
			if (CgmObj->PreviousPage()) InvalidateRect(hWndThis, NULL, TRUE);
			SBars->SetPos(0,0);
			return 0;
/* ****
			case IDM_INDEX:
//			if ( !CgmObj || !CgmObj->IsValidCgm() )
//				{
//				OKMsgBox("*** !!! ***", "CGM Not Valid\nCannot Index" );
//				return 0;
//				}
			IndexObjectPt IObj = new IndexObject(CgmObj);
			IObj->IndexCgm(hWndThis);
			WORD NewPage = IObj->GetPage();
			if (NewPage != NO_PAGE_SELECTED && NewPage != CgmObj->GetPicNum() )
				{
				CgmObj->NewPage(NewPage);
				InvalidateRect(hWndThis, NULL, TRUE) ;
				}

			delete IObj;
			return 0;
************ */
			case IDM_INDEX:
			//case IDM_PAGE_SEL:
			if ( !CgmObj || !CgmObj->IsValidCgm() )
				{
				OKMsgBox("*** !!! ***", "CGM Not Valid\nCannot Index" );
				return 0;
				}
			PageObjectPt PObj = new PageObject(CgmObj);
			PObj->PageCgm(hWndThis);
			WORD SelPage = PObj->GetPage();
			if (SelPage != NO_PAGE_SELECTED && SelPage != CgmObj->GetPicNum() )
				{
				CgmObj->NewPage(SelPage);
				InvalidateRect(hWndThis, NULL, TRUE) ;
				}

			delete PObj;
			return 0;

			case IDM_SAVE:           // Open file for clear text cgm output
			FObjSave = new FileObject();
			// Do dialog for opening CGC
			strcpy(tmp, this->FObj->GetFileName());
			ChangeExt( tmp, ".CGC");
			FObjSave->DialogFileSetup(
								"&Convert Picture to Clear Text CGM File :",
								"",
								tmp,
								".CGC",
								0x4010);
			if (FObjSave->OpenWrite( hWndThis))  // Clear Output File opened
				{
				HourGlass(hWnd);   // Turn on HourGlass
				CgmObj->MakeClearText(FObjSave);
				HourGlass(hWnd);
				}
			delete FObjSave;
			return 0;

			case IDM_META:           // Open file for metafile output
			FObjSave = new FileObject();
			// Do dialog for opening MET
			strcpy(tmp, this->FObj->GetFileName());
			ChangeExt( tmp, ".MET");
			FObjSave->DialogFileSetup(
					"&Save Picture as Windows Metafile File :",
					"",
					tmp,
					".MET", 0x4010);
			if (FObjSave->OpenWrite( hWndThis))  // Metafile File opened
				{
				HourGlass(hWnd);   // Turn on HourGlass
				CgmObj->WriteMetaFile(FObjSave);
				HourGlass(hWnd);
				}
			delete FObjSave;
			return 0;


			default: return 0;
			}
		break;

		case WM_MDIACTIVATE:
		if (wParam == TRUE)	Activate();
		else DeActivate();
		return 0;

		case WM_QUERYENDSESSION:
		case WM_CLOSE:
		SendMessage (GetParent (hWnd), WM_MDIRESTORE, hWnd, 0L) ;
		if (IDOK != MessageBox(hWnd,"OK to close window??","CGM Close",
								  MB_ICONQUESTION | MB_OKCANCEL)) return 0;
		break;

		case WM_NCDESTROY:
		delete this;
		break;
		}
	return DefMDIChildProc( hWnd, iMessage, wParam, lParam );
	}



void CGMWindow::Size()
	{
	LONG lSize = this->lParam;
	WORD wType = this->wParam;
	cxClient = LOWORD(lSize);
	cyClient = HIWORD(lSize);
	if (SBars) SBars->NewSize(cxClient, cyClient);
	}




void CGMWindow::Paint( void )
	{
	HANDLE hBrush;
	HDC hdcMeta;
	PAINTSTRUCT ps;
/* ***************
	if (hmf)
		{
		HDC hdc = BeginPaint(hWndThis, &ps);
		//HourGlass(hWnd);
		SetMapMode(ps.hdc, MM_ANISOTROPIC);
		SetWindowExt(ps.hdc, DrawRect.right, DrawRect.bottom);
		SetViewportExt(ps.hdc, DrawRect.right, DrawRect.bottom);
		SetWindowOrg(ps.hdc,+SBars->getnHscrollPos(),+SBars->getnVscrollPos());
		PlayMetaFile(ps.hdc,hmf);
		EndPaint(hWndThis, &ps);
		//HourGlass(hWnd);
		return;
		}

************************ */

	// if new cgm
	if (!CgmObj)
		{
		CgmObj = new cgmObject(this, FObj);
		HourGlass(hWndThis);
		CgmObj->GPlotInit();
		HourGlass(hWndThis);
		if (!CgmObj->IsValidCgm())
			{
			//EnableMenuItem (hMenu, IDM_PAGE_SEL , MF_GRAYED) ;
			EnableMenuItem (hMenu, IDM_INDEX , MF_GRAYED) ;
			}
		else
			{
			EnableMenuItem (hMenu, IDM_INDEX , MF_ENABLED) ;
			//EnableMenuItem (hMenu, IDM_PAGE_SEL , MF_ENABLED) ;
			}
		if (errors)
			 EnableMenuItem (hMenu, IDM_ERRORS , MF_ENABLED) ;
		else
			 EnableMenuItem (hMenu, IDM_ERRORS , MF_GRAYED) ;

		DrawMenuBar(Appl::hWndFrame);
		}
	if ( !CgmObj->IsValidCgm() )
		{
		// If the DrawMenuBar goes after Begin, End Paint
		//   then flickers when window moved so body mostly off screeen
		BeginPaint(hWndThis, &ps);
		EndPaint(hWndThis,&ps);
		return;
		}

	BOOL newPicture = FALSE;
	if ( !CgmObj->IsSamePic())
		{
		HourGlass(hWndThis);
		CgmObj->ZapLastPic();
		CgmObj->ReadCurPic();
		HourGlass(hWndThis);
		//GetClientRect( hWndThis, (LPRECT) &rect );
		CgmObj->SetLastCur();
		char page[150];
		CgmObj->GetPageText((PSTR)&page);
		DoCaption(page);
		newPicture = TRUE;
		}
	CgmObj->DisplayCurPic(hWndThis, &DrawRect, SBars);
	if (!CgmObj->NextPageExists());   //will set indexed if at last  page
	if (newPicture)
		{
		if (errors)
			 EnableMenuItem (hMenu, IDM_ERRORS , MF_ENABLED) ;
		else
			 EnableMenuItem (hMenu, IDM_ERRORS , MF_GRAYED) ;

		 DrawMenuBar(Appl::hWndFrame);
		 }

	}

// *** CGMWindow constructor
// Constructor
CGMWindow::CGMWindow(FileObjectPt myfile )
	{
	MDICREATESTRUCT mdicreate;
	pPalette = NULL;              // May not need to keep around
	hPal = NULL;
	SBars = NULL;
	errors = 0;
	severe_error = FALSE;
	ErrMsg1 = NULL;
	ErrMsg2 = NULL;
	DrawRect.top    =   DrawRect.left = 0;
	DrawRect.right  =   GetSystemMetrics(SM_CXSCREEN);
	DrawRect.bottom =   GetSystemMetrics(SM_CYSCREEN)
								- GetSystemMetrics(SM_CYCAPTION)
								- GetSystemMetrics(SM_CYMENU);

	FObj = myfile;
	// FObj->DialogFileInfo( );
	CgmObj = NULL;    // gplot has not "opened" yet
	hmf = NULL;    // never been painted
	hMenu = ::LoadMenu( Appl::hInstance,szMenu);
	hMenuWindow =
			 GetSubMenu( hMenu,CGM_MENU_POS);

	mdicreate.szClass = szClass;
	mdicreate.szTitle = "CGM" ;
	mdicreate.hOwner  = Appl::hInstance ;
	mdicreate.x       = CW_USEDEFAULT ;
	mdicreate.y       = CW_USEDEFAULT ;
	mdicreate.cx      = CW_USEDEFAULT ;
	mdicreate.cy      = CW_USEDEFAULT ;
	mdicreate.style   = WS_VSCROLL | WS_HSCROLL; // | WS_MAXIMIZE;
	//mdicreate.lParam  = (LONG) this ;
	SetObjectPt(this, (DWORD *)&mdicreate.lParam );
	HWND hWndChild = SendMessage (Appl::hWndClient, WM_MDICREATE, 0,
							(LONG) (LPMDICREATESTRUCT) &mdicreate) ;
	if ( ! hWndChild ) exit( FALSE );

	}

// CGMWindow Destructor
CGMWindow::~CGMWindow( void )
	{
	BOOL hoho;
	if (hmf) DeleteMetaFile(hmf);
	delete SBars;
	delete FObj;    // also closes open file
	HourGlass(hWndThis);
	delete CgmObj;
	HourGlass(hWndThis);

	DeleteSz(ErrMsg1);
	DeleteSz(ErrMsg2);
	if (hPal) hoho = DeleteObject(hPal);
	delete pPalette;

	}

void  CGMWindow::LoadMenu(HANDLE hInstance)
	{
	//hMenu = ::LoadMenu( hInstance,szMenu);
	//hMenuWindow =
	//		 GetSubMenu( hMenu,CGM_MENU_POS);
	}

// *** CGMWindow::Activate
void CGMWindow::Activate( void )
	{
	SendMessage(Appl::hWndClient, WM_MDISETMENU,0,
														  MAKELONG( hMenu, hMenuWindow));
	DrawMenuBar(Appl::hWndFrame);
		if (hPal)
			{
			HDC hDC = GetDC(hWindow);
			SelectPalette(hDC, hPal,0);
			RealizePalette(hDC);
			//InvalidateRect(hWnd, (LPRECT)NULL, 1);
			ReleaseDC(hWindow, hDC);
			}
	}

// *** CGMWindow::DeActivate
void CGMWindow::DeActivate( void )
	{
	Appl::theFrame->InitMenu();
	DrawMenuBar(Appl::hWndFrame);
	}

// *** CGMWindow::Register - static window class register
void CGMWindow::Register( void )
	{
	WNDCLASS wndclass;   // Structure used to register Windows class.

	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = ::gptWndProc;     // the global one
	wndclass.cbClsExtra    = 0;
	// Reserve extra bytes for each instance of the window;
	// we will use these bytes to store a pointer to the C++
	// (CGMWindow) object corresponding to the window.
	// the size of a 'this' pointer depends on the memory model.
	wndclass.cbWndExtra    = sizeof( CGMWindow * );
	wndclass.hInstance     = Appl::hInstance;
	wndclass.hIcon         = LoadIcon( Appl::hInstance,"gptChild" );
	wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
	wndclass.lpszMenuName  = "gptMenuCGM";
	wndclass.lpszClassName = szClass;

	if ( ! RegisterClass( &wndclass ) )	exit( FALSE );
	}

