/*
*********************************************************************
*
*	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: SUMMRDLG.CPP
*
*	Purpose: Dialog box response function for summary information editing 
*
*********************************************************************
*/


#include <windows.h>

#include "resource.h"
#include "main.h"


BOOL WINAPI SummaryDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

	static LPSTORAGE pSubStg ;
	static LPSTREAM pTitle, pAuthor ;

	switch (message)
	{

/*
Open substorage and streams where author and title live. Read them in and
set them into the edit controls.  Hang on to objects until done.
*/

		case WM_INITDIALOG:
		{

			char buf [256] ; DWORD nActual ;
			LPSTORAGE pMainStg = (LPSTORAGE) lParam ;

			pMainStg->OpenStorage (L"Summary Info", NULL, 
				STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
				NULL, 0, 
				&pSubStg) ;
			
			pSubStg->OpenStream (L"Author", NULL, 
				STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
				0, &pAuthor) ;

			pSubStg->OpenStream (L"Title", NULL, 
				STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
				0, &pTitle) ;

			pAuthor->Read (buf, sizeof(buf), &nActual) ;
			SetDlgItemText (hDlg, IDC_EDIT2, buf) ;

			pTitle->Read (buf, sizeof(buf), &nActual) ;
			SetDlgItemText (hDlg, IDC_EDIT1, buf) ;

	
			SetFocus (GetDlgItem (hDlg, IDC_EDIT1)) ;
			SendDlgItemMessage (hDlg, IDC_EDIT1, EM_SETSEL, 0, -1) ;
			return FALSE ;
		}

		case WM_COMMAND:
		{

			char buf [256] ;  DWORD nActual ;

/*
If user clicked OK, then check each edit control.  If its contents have
changed, fetch them and write them into the respective streams.  Then release
all storages and streams
*/

			if (LOWORD(wParam) == IDOK)
			{

				if (SendDlgItemMessage (hDlg, IDC_EDIT2, EM_GETMODIFY, 0, 0))
				{
					LARGE_INTEGER ZeroL = {0, 0} ;	ULARGE_INTEGER Size ;

					GetDlgItemText (hDlg, IDC_EDIT2, buf, sizeof(buf)) ;

					Size.LowPart = lstrlen (buf) + 1 ;
					Size.HighPart = 0 ;

					pAuthor->Seek(ZeroL, STREAM_SEEK_SET, NULL) ; 
					pAuthor->Write (buf, Size.LowPart, &nActual) ;
					pAuthor->SetSize (Size) ;
				}
	
				if (SendDlgItemMessage (hDlg, IDC_EDIT1, EM_GETMODIFY, 0, 0))
				{
					LARGE_INTEGER ZeroL = {0, 0} ;	ULARGE_INTEGER Size ;

					GetDlgItemText (hDlg, IDC_EDIT1, buf, sizeof(buf)) ;

					Size.LowPart = lstrlen (buf) + 1 ;
					Size.HighPart = 0 ;

					pTitle->Seek(ZeroL, STREAM_SEEK_SET, NULL) ; 
					pTitle->Write (buf, Size.LowPart, &nActual) ;
					pTitle->SetSize (Size) ;
				}
			
				pAuthor->Release( ) ;
				pTitle->Release( ) ;
				pSubStg->Release( ) ;

				EndDialog (hDlg, IDOK) ;
				return TRUE ;
			}
			else  if (LOWORD (wParam) == IDCANCEL)
			{
				EndDialog (hDlg, IDCANCEL) ;
				pAuthor->Release( ) ;
				pTitle->Release( ) ;
				pSubStg->Release( ) ;
			}

			return FALSE ;
		}
	}

	return FALSE ;
}
