File:  [OS/2 SDKs] / os232sdk / toolkt20 / c / samples / template / main.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:31 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1990, HEAD
Microsoft OS/2 SDK 2.0 05-30-1990

/*==============================================================*\
 *  Main.c - Sample PM application main source file
 *
 *      Created 1989, 1990 Microsoft Corp.
 *
 *	DISCLAIMER OF WARRANTIES.  The following [enclosed] code is 
 *	sample code created by Microsoft Corporation and/or IBM 
 *	Corporation. This sample code is not part of any standard 
 *	Microsoft or IBM product and is provided to you solely for 
 *	the purpose of assisting you in the development of your 
 *	applications.  The code is provided "AS IS", without 
 *	warranty of any kind.  Neither Microsoft nor IBM shall be 
 *	liable for any damages arising out of your use of the sample 
 *	code, even if they have been advised of the possibility of 
 *	such damages.
 *
 *--------------------------------------------------------------
 *
 *  This application serves as a template than can be
 *  easily modified by an application developer.  The source
 *  files are organized so that the overhead code that should
 *  be in all applications is located in the same files so
 *  that these files do not need to be modified.  The routines
 *  that deal with application specific code are also located
 *  in their own modules.  An application developer need only
 *  change these files in order to modify this template for
 *  his application.
 *
 *--------------------------------------------------------------
 *
 *  This source file contains the following functions:
 *
 *      main() - main routine
 *      MainWndProc(hwnd, msg, mp1, mp2) - main window procedure
 *      MessageBox(hwnd idMsg, fsStyle, fBeep) - Message box routine
 *      MainCommand(mp1, mp2) - WM_COMMAND processing of Main
 *
\*==============================================================*/

/*--------------------------------------------------------------*\
 *  Include files, macros, defined constants, and externs
\*--------------------------------------------------------------*/

#define  INCL_WINHELP

#include <os2.h>
#include "main.h"
#include "help.h"
#include "xtrn.h"


/*----
 * Define these constants when th corresponding components (Help manager,
 *  etc.) are implimented in the product
 *---
/* #define PRINT_DLGS_ENABLED */


/*--------------------------------------------------------------*\
 *  Global variables
\*--------------------------------------------------------------*/
HWND hwndMainFrame = NULL;          /* handle to the main frame window */
HWND hwndMain;                      /* handle to the main client window */
HDC  hdcMain;                       /* handle to the DC of the client */
HAB  hab;                           /* anchor block for the process */
HMQ  hmq;                           /* handle to the process' message queue */
CHAR szAppName[MAXNAMEL];           /* buffer for application name string */
CHAR szUntitled[MESSAGELEN];        /* buffer for "Untitled" string */
BOOL fPrintEnabled;                 /* flag to determine if we can print */
BOOL fHelpEnabled;                  /* flag to determine if help is enabled */

/*--------------------------------------------------------------*\
 *  Entry point declarations
\*--------------------------------------------------------------*/

/****************************************************************\
 *  Main routine
 *--------------------------------------------------------------
 *
 *  Name:   main()
 *
 *  Purpose: Initializes the PM environment, calls the
 *              initialization routine, creates the main
 *              window,  and polls the message queue
 *
 *  Usage:
 *
 *  Method:
 *          - obtains anchor block handle and creates message
 *              queue
 *          - calls the initialization routine
 *          - creates the main frame window which creates the
 *              main client window
 *          - polls the message queue via Get/Dispatch Msg loop
 *          - upon exiting the loop, exits
 *
 *  Returns:
 *          1 - if sucessful execution completed
 *          0 - if error
\****************************************************************/
BOOL cdecl main(VOID)
{
    QMSG qmsg;          /* message structure */
    ULONG flCtlData;    /* frame control data */

    hab = WinInitialize(0);

    if(!hab)  {
        DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
        return(RETURN_ERROR);
    }

    hmq = WinCreateMsgQueue(hab, 0);

    if(!hmq)  {
        DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
        WinTerminate(hab);
        return(RETURN_ERROR);
    }

    if(!Init())  {
        MessageBox(HWND_DESKTOP,
                   IDMSG_INITFAILED,
                   MB_OK | MB_ERROR,
                   TRUE);

        return(RETURN_ERROR);
    }

    /* NOTE:  clean up from here is handled by the DosExitList processing */


    /* create the main window */
    flCtlData = FCF_STANDARD;

    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
                                       WS_VISIBLE,
                                       (PULONG)&flCtlData,
                                       (PSZ)szAppName,
                                       (PSZ)NULL,
                                       WS_VISIBLE,
                                       (HMODULE)NULL,
                                       IDR_MAIN,
                                       (PHWND)&hwndMain);

    if(hwndMainFrame == NULL)  {
        MessageBox(HWND_DESKTOP,
                   IDMSG_MAINWINCREATEFAILED,
                   MB_OK | MB_ERROR,
                   TRUE);
        return(RETURN_ERROR);
    }

    hdcMain = WinOpenWindowDC(hwndMain);

    InitHelp();

    /* Get/Dispatch Message loop */
    while(WinGetMsg(hmq, (PQMSG)&qmsg, NULL, NULL, NULL))
        WinDispatchMsg(hmq, (PQMSG)&qmsg);

    /* destroy the help instance */
    DestroyHelpInstance();

#ifdef BACKGROUND_THREAD
    /* see main.h for comment on using a background thread */
    DestroyBackgroundThread();
#endif


    return(RETURN_SUCCESS);

}   /* main() */


/****************************************************************\
 *  Main client window procedure
 *--------------------------------------------------------------
 *
 *  Name:   MainWndProc(hwnd, msg, mp1, mp2)
 *
 *  Purpose: Processes the messages sent to the main client
 *              window.  This routine processes the basic
 *              messages all client windows should process
 *              and passes all others onto UserWndProc where
 *              the developer can process any others.
 *
 *  Usage:  Called for each message placed in the main
 *          window's message queue
 *
 *  Method: a switch statement branches to the routines to be
 *          performed for each message processed.  Any messages
 *          not specifically process are passed to the user's
 *          message processing procedure UserWndProc().
 *
 *  Returns:  Return values are determined by each message
 *
\****************************************************************/
MRESULT EXPENTRY MainWndProc(hwnd, msg, mp1, mp2)
HWND hwnd;      /* handle of window */
USHORT msg;     /* id of message */
MPARAM mp1;     /* first message parameter */
MPARAM mp2;     /* second message parameter */
{

    switch(msg)  {
        case WM_CREATE:
            return(InitMainWindow(hwnd, mp1, mp2));
            break;

        case WM_PAINT:
            MainPaint(hwnd);
            break;

        case WM_COMMAND:
            MainCommand(mp1, mp2);
            break;

        case WM_INITMENU:
            InitMenu(mp1, mp2);
            break;

        case HM_QUERY_KEYS_HELP:
            return (MRESULT)PANEL_HELPKEYS;   /* return id of key help panel */
            break;

        case TM_THREADINITFAILED:
            /* message is received if the background thread initialization
                fails.  A message box is displayed and the application is
                terminated */
            MessageBox(HWND_DESKTOP,
                       IDMSG_INITFAILED,
                       MB_OK | MB_ERROR,
                       TRUE);

            WinPostMsg(hwnd, WM_CLOSE, NULL, NULL);
            break;

    /*--------------------------------------------------*\
     *      Any messages not processed are passed on
     *      to the user's window proc.  It is
     *      responsible for passing any messages it
     *      doesn't handle onto WinDefWindowProc()
    \*--------------------------------------------------*/

        default:
            return(UserWndProc(hwnd, msg, mp1, mp2));
            break;

    }
    return 0L;      /* all window procedures should return 0 as a default */

}   /* MainWndProc() */

/****************************************************************\
 *  Message Box procedure
 *--------------------------------------------------------------
 *
 *  Name:   MessageBox(hwndOwner, nIdMsg, fsStyle, fBeep)
 *
 *  Purpose: Displays the message box with the message
 *              given in idMsg retrieved from the message table
 *              and using the style flags in fsStyle
 *
 *  Usage:  Called whenever a MessageBox is to be displayed
 *
 *  Method: - Message string is loaded from the process'
 *              message table
 *          - Alarm beep is sounded if desired
 *          - Message box with the message is displayed
 *          - WinMessageBox return value is returned
 *
 *  Returns: return value from WinMessageBox()
 *
\****************************************************************/
SHORT MessageBox(hwndOwner, idMsg, fsStyle, fBeep)
HWND hwndOwner;     /* handle of the message box's owner */
SHORT idMsg;        /* id if the message in the message table */
SHORT fsStyle;      /* style of the message box */
BOOL fBeep;         /* if TRUE, beep before message box is displayed */
{
    CHAR szText[MESSAGELEN];

    if(!WinLoadMessage(hab,
                      (HMODULE)NULL,
                      idMsg,
                      MESSAGELEN,
                      (PSZ)szText)) {

        WinAlarm(HWND_DESKTOP, WA_ERROR);
        return MBID_ERROR;
    }

    if(fBeep)  {
        WinAlarm(HWND_DESKTOP, WA_ERROR);
    }

    return(WinMessageBox(HWND_DESKTOP,
                         hwndOwner,
                         szText,
                         (PSZ)NULL,
                         MSGBOXID,
                         fsStyle));

}   /* MessageBox() */

/****************************************************************\
 *  Main window WM_COMMAND processing procedure
 *--------------------------------------------------------------
 *
 *  Name:   MainCommand(mp1, mp2)
 *
 *  Purpose: Calls the appropriate procedures that deal with
 *              the selected menu item.
 *
 *  Usage:  Routine is called whenever a WM_COMMAND message
 *          is posted to the main window.
 *
 *  Method: a switch statement branches on the id of the
 *          menu item that posted the message and the
 *          appropriate action for that item is taken.  Any
 *          menu ids that are not part of the standard menu
 *          set are passed onto the user defined WM_COMMAND
 *          processing procedure.
 *
 *  Returns:
 *
\****************************************************************/
VOID MainCommand(mp1, mp2)
MPARAM mp1;     /* first parameter of WM_COMMAND message */
MPARAM mp2;     /* second parameter of WM_COMMAND message */
{

    switch(SHORT1FROMMP(mp1))  {

        case IDM_FILENEW:
            FileNew(mp2);
            break;

        case IDM_FILEOPEN:
            FileOpen(mp2);
            break;

        case IDM_FILESAVE:
            FileSave(mp2);
            break;

        case IDM_FILESAVEAS:
            FileSaveAs(mp2);
            break;

#ifdef PRINT_DIALOGS_ENABLED

        case IDM_FILEPRINT:
            FilePrint(mp2);
            break;

        case IDM_FILEPAGESETUP:
            FilePageSetup(mp2);
            break;

        case IDM_FILEPRINTSETUP:
            FilePrintSetup(mp2);
            break;

#endif  /* PRINT_DLGS_ENABLED */

        case IDM_FILEEXIT:
            FileExit(mp2);
            break;

        case IDM_EDITUNDO:
            EditUndo(mp2);
            break;

        case IDM_EDITCUT:
            EditCut(mp2);
            break;

        case IDM_EDITCOPY:
            EditCopy(mp2);
            break;

        case IDM_EDITPASTE:
            EditPaste(mp2);
            break;

        case IDM_EDITCLEAR:
            EditClear(mp2);
            break;

        case IDM_HELPHELPFORHELP:
            HelpHelpForHelp(mp2);
            break;

        case IDM_HELPEXTENDED:
            HelpExtended(mp2);
            break;

        case IDM_HELPKEYS:
            HelpKeys(mp2);
            break;

        case IDM_HELPINDEX:
            HelpIndex(mp2);
            break;

        case IDM_HELPTUTORIAL:
            HelpTutorial(mp2);
            break;

        case IDM_HELPABOUT:
            HelpAbout(mp2);
            break;



    /*--------------------------------------------------*\
     *      User command processing routine is called
     *      here so any ids not processed here can be
     *      processed
    \*--------------------------------------------------*/
        default:
            UserCommand(mp1, mp2);
            break;
    }

}   /* MainCommand() */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.