/*                                                                                          */
//  NAME = HELPDEMO.C    
//  Turbo C++ version of a Windows program to demonstrate how
//  to call the Microsoft Help Engine, WINHELP.EXE.  This
//  program creates a window w/menu bar having two options -
//  Exit and Help.  Click on Help to activate the sample help file
//  using WINHELP.
//  Programmers:  Keith E. Bugg and Jack Tackett
//

#include "helpdemo.h"
#include "context.h"        // indices for help subjects
long FAR PASCAL _export DemoProc(HWND hWnd, 
WORD message, WORD wParam, LONG lParam)
{
        
        switch (message)         // process messages here
        {
                case WM_PAINT:              // message to update screen
                        dopaint(hWnd);             // function to repaint screen
                        break;


                case WM_KEYDOWN:    // trap F1 function key
                        switch(wParam)
                        {
                                case VK_F1: // go to main index
                                        WinHelp(hWnd,"HELPDEMO.HLP", HELP_CONTENTS,0);
                                        break;
                                default:
                                        break;
                        }
                        break;

        case WM_COMMAND:    // check for system message
                switch(wParam)
                {
                        case GEN_EXIT:      // Exit from Menu bar
                           PostQuitMessage(0);
                           break;

                case SC_MINIMIZE:   
                         // system menu click on minimize
                        ShowWindow(hWnd,SW_SHOWMINIMIZED);
                        break;

                case SC_MAXIMIZE:   
                        // system menu click on maximize
                        ShowWindow(hWnd,SW_SHOWMAXIMIZED);
                        break;

                case SC_RESTORE:    
                        // system menu click on restore
                                ShowWindow(hWnd,SW_SHOW);
                                break;

                        //
                        //  this case brings up the table of contents
                        //

                case HELP_TOPICS:
                        WinHelp(hWnd,"HELPDEMO.HLP", HELP_CONTENTS,0);
                                                break;
                        //
                        //  next cases are for specific topics
                        //
                case HELP_MERCURY:
                        WinHelp(hWnd,"HELPDEMO.HLP",HELP_CONTEXT,GO_MERCURY);
                        break;

                case HELP_VENUS:
                        WinHelp(hWnd,"HELPDEMO.HLP",HELP_CONTEXT,GO_VENUS);
                        break;

                case HELP_EARTH:
                        WinHelp(hWnd,"HELPDEMO.HLP",HELP_CONTEXT,GO_EARTH);
                        break;

                case HELP_MARS:
                        WinHelp(hWnd,"HELPDEMO.HLP",HELP_CONTEXT,GO_MARS);
                        break;

                default:
                        break;
                }
                break;
                
                case WM_CLOSE:     // User wants to Exit via sys menu
                case WM_QUIT:       // QUIT & DESTROY messages 
                case WM_DESTROY:
                         PostQuitMessage(0);
                         break;

                default:            // message of no interest to program..
                         return (DefWindowProc(hWnd, message, 
                           wParam, lParam));
        }               // end switch on all messages

        return (NULL);
}       /* end MainWndProc()    */

#pragma argsused        // TURBO C++ pragma
/**************************************************************/
int PASCAL WinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance,  LPSTR lpCmdLine,
         int nCmdShow)
{
        
         WNDCLASS  wc;
         static HWND hWnd;
         MSG msg;
        
        if (!hPrevInstance) // Other instances of app running?
        {
                // Fill in window class structure with parameters that 
                // describe the main window.

                wc.style = CS_HREDRAW | CS_VREDRAW  ;   // Class style(s).
                //  Declare function to retrieve messages for this class
                wc.lpfnWndProc = (long (FAR PASCAL*)())DemoProc;

                wc.cbClsExtra = 0;          // No per-class extra data.
                wc.cbWndExtra = 0;          // No per-window extra data.
                wc.hInstance = hInstance;   // Application that owns the class.
                wc.hIcon = LoadIcon(hInstance, "HELPDEMO");
                wc.hCursor = LoadCursor(NULL, IDC_ARROW);
                //
                // create a cyan background
                //
                wc.hbrBackground = CreateSolidBrush(RGB(100,255,255)); 

                wc.lpszMenuName = "mainmenu";
                wc.lpszClassName = "HELPDEMO";    // Name used for CreateWindow()

                /* Register the window class and return success/failure code. */

                if(!RegisterClass(&wc))
                        return 0;                   // FAILED TO REGISTER!!

                /* Create a main window for this application instance.  */

                hWnd = CreateWindow(
                        "HELPDEMO",               // See RegisterClass() call.
                        "HELPDEMO Development",   // Text for window title bar.
                        WS_MAXIMIZE | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME
                        | WS_SYSMENU | WS_OVERLAPPEDWINDOW,   // Window style.
                        0,                      // Default horizontal position.
                        0,                      // Default vertical position.
                        600,                    // Default width.
                        400,                    // Default height.
                        NULL,                   // Overlapped windows have no parent.
                        NULL,                   // No menu bar
                        hInstance,              // This instance owns this window.
                        NULL                    // Pointer not needed.
                        );               

                if (!hWnd)   /* If window could not be created, return "failure" */
                {        
                        MessageBeep(0);
                        MessageBox(hWnd,"Could not create window!","ERROR",MB_OK);
                        return 0;
                }
        

                /* Make the window visible; update its client area   */

                nCmdShow= SW_SHOWNORMAL;     // Show window normal MAX size
                ShowWindow(hWnd,nCmdShow ); // Show the window
                UpdateWindow(hWnd);         // Sends WM_PAINT message
                
        }
        /* Acquire and dispatch messages until a WM_QUIT message is received. */

        while (GetMessage(&msg, // message structure
                NULL,               // handle of window receiving the message
                NULL,               // lowest message to examine
                NULL))              // highest message to examine
        {
                TranslateMessage(&msg); // Translates virtual key codes
                DispatchMessage(&msg);  // Dispatches message to window
        }
        return (msg.wParam);    // Returns the value from PostQuitMessage

}               /* end WinMain()    */

void dopaint(HWND hWnd)         // handles any Windows PAINT messages
{
        PAINTSTRUCT ps;             // Paint Struct for BeginPaint call
        HDC hDC;

        BeginPaint(hWnd,&ps);
        hDC= GetDC(hWnd);           // get handle to display context
                
        SetBkMode(hDC,TRANSPARENT);
        SetTextColor(hDC,RGB(0,0,0));       /* write instructions in black  */
        TextOut(hDC,100,310,
                "Click on Help to see a demonstration of WINHELP.EXE...",54 
);

        EndPaint(hWnd,&ps);                 // END OF THE PAINT PROCEDURE
        ReleaseDC(hWnd,hDC);                // release the Display Context

}       // end dopaint()

//
//  end HELPDEMO.c
//
