/*	MAIN.C - Window Procedure for GENERIC.EXE Demo
 *  Copyright (C) 1991-1994 Shapeware Corporation. All rights reserved.
 *
 *	Abstract
 *		This file implements the standard Windows message loop and window
 *		procedure for the GENERIC.EXE demo.
 */
 
#include <windows.h>
#include <ole2.h> 
#include <dispatch.h>  

#include "resource.h"

int RunDemo(void);
void ShowAboutDialog(HINSTANCE);
LONG CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApplication(HANDLE);
BOOL InitInstance(HANDLE, int);

HANDLE hInst;

/**************************************************************************
 *+ WinMain
 *
 *	The main message loop and application initialization.
 */
#ifdef __BORLANDC__
#pragma argsused
#endif 
int PASCAL WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpszCmdLine,
	int nCmdShow) 
	{
    MSG msg;
    HRESULT hResult;
    
    if ( hPrevInstance || !InitApplication(hInstance) )
    	return (FALSE);

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

	 hResult = OleInitialize(NULL);
	 if (hResult != NOERROR )
		  return (FALSE);

    while (GetMessage(&msg, NULL, NULL, NULL))
		{
		  TranslateMessage(&msg);
		  DispatchMessage(&msg);
		}
    
	 OleUninitialize();
    
    return (msg.wParam);
	}


/**************************************************************************
 *+ InitApplication
 *
 *	One time initialization to register the window class.
 */
 
BOOL InitApplication(HANDLE hInstance)
	{
    WNDCLASS  wc;

    wc.style = NULL;
    wc.lpfnWndProc = MainWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DISPATCH));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName =  "MainMenu";
    wc.lpszClassName = "GenericDemo";

    return (RegisterClass(&wc));
	}


/**************************************************************************
 *+ InitInstance
 *
 *	Per instance initialization.  Creates the frame window.
 */

BOOL InitInstance(HANDLE hInstance, int nCmdShow)
	{
    HWND hWnd;
     
    hInst = hInstance;
        
    hWnd = CreateWindow("GenericDemo", "Visio Automation Demo",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        NULL, NULL, hInstance, NULL);
     
    if (!hWnd) return (FALSE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    
    return (TRUE);
	}

/**************************************************************************
 *+ MainWndProc
 *
 *	Frame window procedure.  The only special thing it does it run the 
 *	demo when the Run Demo menu entry is selected.
 */

LONG CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
	{

	 switch (message)
	    {
	    case WM_COMMAND:
	        switch (wParam)
		        {
		        case IDM_EXIT:
		            DestroyWindow(hWnd);
		            break;
		            
		        case IDM_RUNDEMO:
						RunDemo();
		            break;
		    
		    	case IDM_ABOUT:
		    		ShowAboutDialog(hInst);
		    		break;        
		    		
		        default:
		            return (DefWindowProc(hWnd, message, wParam, lParam));
		        }
	        
	        break;
	        
		 case WM_DESTROY:
	        PostQuitMessage(0);
	        break;
	            
	    default:
	        return (DefWindowProc(hWnd, message, wParam, lParam));
	    }
    
    return (NULL);
	}
