/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*	File Name: MAINWIND.CPP
*
*	Purpose: Main window response function for moniker server example
*
*********************************************************************
*/

/*
*****************************************************************************
System header files
*****************************************************************************
*/

#include <windows.h>		    /* required for all Windows applications */


/*
*****************************************************************************
Application header files
*****************************************************************************
*/

#include "main.h"
#include "resource.h"
#include "cperfile.h"
#include "cfiletextdata.h"
#include "cmonikerdata.h"

/*
*****************************************************************************
Function prototype files
*****************************************************************************
*/

/*
*****************************************************************************
Global Variables defined here
*****************************************************************************
*/

//WCHAR WideFileName [512] ;
CFileTextData *pFileDataObj = NULL ;
UINT cfLinkSource ;
                               
/*
*****************************************************************************
EXTERNS ofglobal variables defined in other files
*****************************************************************************
*/

extern HWND	hMainWnd ;
extern HINSTANCE hInst ;
extern GUID CLSID_MonikerSv ;


HWND hEdit ;

long WINAPI MainWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{


	switch (message)
	{

		case WM_CREATE:
		{   
			cfLinkSource = RegisterClipboardFormat ("Link Source") ;

			hEdit = CreateWindow ("edit", "", 
				WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | WS_BORDER ,
				0, 0, 0, 0, 
				hWnd, (HMENU) 1, hInst, NULL) ;
			SetFocus (hEdit) ;
   			return 0 ;	
		}



		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case ID_FILE_EXIT:
				{	
					SendMessage (hWnd, WM_CLOSE, 0, 0) ;
					return 0 ;
				}

				case ID_FILE_OPENME:
				{
/*
Get name of file from user, convert to wide char
*/
					OPENFILENAME of ;
					char FileName [256] ;
					WCHAR WideFileName [256] ;

					ZeroMemory (&of, sizeof(of)) ;
					FileName [0] = 0x00 ;
					
					of.lStructSize = sizeof (of) ;
					of.hwndOwner = hWnd ;
					of.lpstrFile = FileName ;
					of.nMaxFile = sizeof (FileName) ;
					of.lpstrFilter = "All Files\0*.*\0" ;

					if (GetOpenFileName (&of)) 
					{
						MultiByteToWideChar (CP_ACP, 0, FileName, -1, WideFileName, 80) ; 

/*
Release previous data object, if any.  Instantiate new data object.  
Call its IPersistFile::Load( ) method to read it in from the specified file. 
*/
						if (pFileDataObj)
						{
							pFileDataObj->Release( ) ;
						}

						pFileDataObj = new CFileTextData () ;

						pFileDataObj->Load (WideFileName, 0) ;
					}
					return 0 ;
				}

				case ID_FILE_SAVEAS:
				{
/*
Get name of file from user, convert to wide char
*/
					OPENFILENAME of ;
					char FileName [256] ;
					WCHAR WideFileName [256] ;

					FileName [0] = 0x00 ;
					ZeroMemory (&of, sizeof(of)) ;
					
					of.lStructSize = sizeof (of) ;
					of.hwndOwner = hWnd ;
					of.lpstrFile = FileName ;
					of.nMaxFile = sizeof (FileName) ;
					of.lpstrFilter = "All Files\0*.*\0" ;

					if (GetSaveFileName (&of)) 
					{
						MultiByteToWideChar (CP_ACP, 0, FileName, -1, WideFileName, 80) ; 
/*
Get text from edit control, create data object based on text.
*/
						char buf [4096] ;
						int length ;

						length = GetWindowText (hEdit, buf, sizeof(buf)) ;
						CFileTextData *pd = new CFileTextData (buf, length+1) ;
						

/*
Tell object to save itself into the specified file name
*/

						pd->Save (WideFileName, TRUE) ;

/*
Release previous text data object, if any.  Create new one based
one newly-opened file.
*/
						if (pFileDataObj)
						{
							pFileDataObj->Release( ) ;
						}

						pFileDataObj = pd ;
					}

					return 0 ;
				}


/*
Copy File moniker link source to Clipboard.  Instantiate data
transfer object containing moniker, place on Clipboard.
*/
				
				case ID_EDIT_COPYFILELINK:
				{
					LPMONIKER pFileMoniker ;
					LPDATAOBJECT pData ;

					CreateFileMoniker (pFileDataObj->m_wFileName, &pFileMoniker) ;

					pData = new CMonikerData (pFileMoniker) ;
					
					HRESULT hr = OleSetClipboard (pData) ;

					pFileMoniker->Release( ) ;
					pData->Release ( ) ;
					return 0 ;
				}

/*
Copy File!Item moniker to Clipboard.  Create file moniker. Get first and
last selected character from edit control, create item moniker based
on that. Create composite moniker from file and item monikers.  Instantiate
data transfer object containing moniker, place on Clipboard.
*/
				case ID_EDIT_COPYFILEITEMLINK:
				{
					LPMONIKER pFileMoniker, pItemMoniker, pCompositeMoniker ;
					LPDATAOBJECT pData ;
					char out [256] ; WCHAR wout [256] ;

					CreateFileMoniker (pFileDataObj->m_wFileName, &pFileMoniker) ;

					DWORD dwSel = SendMessage (hEdit, EM_GETSEL, 0, 0) ;
					wsprintf (out, "%d:%d", LOWORD(dwSel), HIWORD(dwSel)) ;
					MultiByteToWideChar (CP_ACP, 0, out, -1, wout, 80) ; 

						
					CreateItemMoniker (L"!", wout, &pItemMoniker) ;
					CreateGenericComposite (pFileMoniker, pItemMoniker, &pCompositeMoniker) ;

					pData = new CMonikerData (pCompositeMoniker) ;
					
					HRESULT hr = OleSetClipboard (pData) ;

					pFileMoniker->Release ( ) ;
					pItemMoniker->Release ( ) ;
					pCompositeMoniker->Release( ) ;
					pData->Release ( ) ;

					return 0 ;
				}

				case ID_EDIT_COPYDATAONLY:
				{
					OleSetClipboard (pFileDataObj) ;
					return 0 ;
				}
			}

			return 0 ;
		}

		case WM_INITMENU:
		{
			if (pFileDataObj)
			{
				EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYFILELINK, MF_ENABLED) ;
				EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYDATAONLY, MF_ENABLED) ;
				DWORD sel = SendMessage (hEdit, EM_GETSEL, NULL, NULL) ;

				if (LOWORD(sel) != HIWORD(sel))
				{
					EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYFILEITEMLINK, MF_ENABLED) ;
				}
				else
				{
 					EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYFILEITEMLINK, MF_GRAYED) ;
				}
			}
			else
			{
				EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYDATAONLY, MF_GRAYED) ;
				EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYFILELINK, MF_GRAYED) ;
				EnableMenuItem ((HMENU)wParam, ID_EDIT_COPYFILEITEMLINK, MF_GRAYED) ;
			}
			return 0 ;
		}

		case WM_SIZE:
		{
			MoveWindow (hEdit, 5, 5, LOWORD(lParam) - 10, HIWORD(lParam)-10, TRUE) ;
			return 0 ;
		}

/*
Shutdown time.  
*/

		case WM_CLOSE:
		{
			if (pFileDataObj)
			{
				pFileDataObj->Release( ) ;
			}

			DestroyWindow (hWnd) ;
			return 0 ;
		}

	
		case WM_DESTROY:
		{           
		  	PostQuitMessage (0) ;
		  	return 0 ;
		}
		
	}
       
	return DefWindowProc(hWnd, message, wParam, lParam) ;
	
}

