/*
*********************************************************************
*
*	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: HELLODLG.CPP
*
*	Purpose: Dialog box response function.  Gets commands from user and
*		calls OLE Automation methods in the local CHelloDriver proxy object.
*
*********************************************************************
*/

#include <windows.h> 
#include "resource.h"
#include "hellodrv.h"


extern CHelloDriver chd ;
HWND hMainDlg ;

BOOL WINAPI HelloDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{

	switch (msg)
	{
		case WM_INITDIALOG:
		{
			hMainDlg = hDlg ;
			return TRUE ;
		}

		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
/*
User clicked OK.  Close dialog and terminate app.
*/
				case IDOK:
				{
					EndDialog (hDlg, 0) ;
					return TRUE ;
				}

/*
User clicked "Say Hello" button.  Call driver object's SayHowdy( )
method, which uses the IDispatch interface to control the server's
automation object and invoke its "SayHello( )" method.
*/
				case ID_SAYHELLO:
				{
					chd.SayHowdy ( ) ;
					return TRUE ;
				}
				
/*
User clicked "Get String" button.  Call driver object's GetString( ) 
method, which uses the IDispatch interface to read the value of the
server object's "HelloMessage" property.  Convert the returned
string from wide characters to ANSI and place in edit control. 
*/
				case ID_GETSTRING:       
				{
					char out [128] ;
					WCHAR wout[128] ;
					
					chd.GetString (wout) ;
				
					WideCharToMultiByte (CP_ACP, 0, wout,  wcslen(wout)+1, out, sizeof(out), NULL, NULL) ;
					SetDlgItemText (hDlg, IDC_EDIT1, out) ;
					return TRUE;
				}
/*
User clicked "Set String" button. Fetch the desired string from the
edit control where the user has typed it and conver from ANSI into the
wide characters required by OLE. Call driver object's SetString( ) 
method, which uses the IDispatch interface to set the value of the
server object's "HelloMessage" property.  
*/

				case ID_SETSTRING:
				{  
					char out [128] ;
					WCHAR wout [128] ;

					GetDlgItemText (hDlg, IDC_EDIT2, out, sizeof(out)) ;
					MultiByteToWideChar (CP_ACP, 0, out, lstrlen(out)+1, wout, sizeof(wout)) ;
					
					chd.SetString (wout) ;
					return TRUE ;
				}

/*
User clicked "Bad Set String" button. Invoke the driver object's 
"BadSetString( )" method, which tries to set the automation server
object's "HelloMessage" property but makes intentional errors so as
to test and demonstrate error handling in OLE.
*/
				case ID_BADSETSTRING:
				{
					chd.BadSetString ( ) ;
					return TRUE ;
				}	
			}
			
			return FALSE ;
		}
		
		default:
		{
			return FALSE ;
		}
	}
	
}
