File:  [OS/2 SDKs] / os232sdk / toolkt20 / c / samples / semaph / sem_main.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:30 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

/*==============================================================*\
 *  sem_main.c - Sample PM application                          *
 *      Created 1990, Microsoft, IBM 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 two distinct purposes.  First, it   *
 *  demonstrates the standard menus and dialogs that most PM    *
 *  applications will contain.  The Demo menu calls dialog      *
 *  boxes that demonstrate all of the controls available for    *
 *  use in a dialog box.  There is also a dialog box which      *
 *  demonstrates the setting and removing of control            *
 *  Presentation Parameters.  Finally, the demo menu also has   *
 *  menu items that demonstrate the various types of message    *
 *  boxes that the system supports.                             *
 *                                                              *
 *  This application also 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 nId, bBeep) - Warning box routine       *
 *      MainCommand(mp1, mp2) - WM_COMMAND processing of Main   *
 *                                                              *
\*==============================================================*/

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

#define  LINT_ARGS

#define  INCL_PM
#define  INCL_BASE

#include <os2.h>
#include <stdlib.h>
#include "sem_main.h"
#include "sem_pnt.h"
#include "semaph.h"
#include "sem_help.h"
#include "sem_xtrn.h"

#define RETURN_SUCCESS      0   /* successful return in DosExit */
#define RETURN_ERROR        1   /* error return in DosExit */
#define BEEP_WARN_FREQ      60  /* frequency of warning beep */
#define BEEP_WARN_DUR      100  /* duration of warning beep */
#define MAXWIDTH    80      /* for now */
#define NUMLINES    25      /* for now */

/*--------------------------------------------------------------*\
 *  Global variables                                            *
\*--------------------------------------------------------------*/
HWND hwndMainFrame;                 /* handle to the main frame window */
HWND hwndMain;                      /* handle to the main client window */
HAB hab;                            /* anchor block for the process */
HMQ hmq;                            /* handle to the process' message queue */
CHAR szAppName[MAXAPPNAMELEN];      /* buffer for application name string */

/*--------------------------------------------------------------*\
 *  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: (via DosExit)                                      *
 *          1 - if sucessful execution completed                *
 *          0 - if error                                        *
\****************************************************************/

VOID cdecl main(VOID)
{
    QMSG qmsg;          /* message structure */
    ULONG ctlData;      /* frame control data */

    hab = WinInitialize(0);
    if(!hab)  {
        DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
        DosExit(EXIT_PROCESS, RETURN_ERROR);
    }
       /* find a define for this v */
    hmq = WinCreateMsgQueue(hab, 0);
    if(!hmq)  {
        DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
        WinTerminate(hab);
        DosExit(EXIT_PROCESS, RETURN_ERROR);
    }

    if(!Init())  {
        MessageBox(HWND_DESKTOP, IDMSG_INITFAILED, TRUE);
        DosExit(EXIT_PROCESS, RETURN_ERROR);
    }

    /* create the main window */

    ctlData = FCF_STANDARD & ~FCF_ACCELTABLE;

    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
                        WS_VISIBLE,
                        (PVOID)&ctlData,
                        (PSZ)szAppName,
                        (PSZ)NULL,
                        WS_VISIBLE,
                        (HMODULE)NULL,
                        IDR_SAMPLE,
                        (PHWND)&hwndMain);

    if(!hwndMainFrame)  {
        MessageBox(HWND_DESKTOP, IDMSG_MAINWINCREATEFAILED, TRUE);
        DosExit(EXIT_PROCESS, RETURN_ERROR);
    }

    while(WinGetMsg(hmq, (PQMSG)&qmsg, NULL, NULL, NULL))
        WinDispatchMsg(hmq, (PQMSG)&qmsg);

    DosExit(EXIT_PROCESS, 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 */
{
    FONTMETRICS fntmet;
    HPS         hPS;
    static  SHORT xClient, yClient;
    static  SHORT yChar, yDesc;

    switch(msg)  {
        case WM_CREATE:
            hPS = WinGetPS (hwnd);
            GpiQueryFontMetrics (hPS, (LONG) sizeof fntmet, &fntmet);

            yChar = (SHORT) fntmet.lMaxBaselineExt;
            yDesc = (SHORT) fntmet.lMaxDescender;

            WinReleasePS (hPS);
            break;
         
        case WM_PAINT:
            MainPaint(hwnd);
            break;

        case WM_SIZE:
            xClient = LOUSHORT (mp2);   /* width of window  */
            yClient = HIUSHORT (mp2);   /* height of window */
                                        /* fall thru to ... */
        case WM_USERCHANGED:            /* number of threads changed */
                                        /* so x and y are already known */
            SetRectPositions(xClient, yClient, yChar, yDesc);
            WinInvalidateRect (hwnd, NULL, TRUE);
            break;

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

        case HM_QUERY_KEYS_HELP:
            return (MRESULT)PANEL_HELPKEYS; /* return id of key help panel */
            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, bBeep)                *
 *                                                              *
 *  Purpose: Displays the warning message box with the message  *
 *              given in nIdMsg retrived from the message table *
 *                                                              *
 *  Usage:  Called whever an error occurs and a message wishes  *
 *          to be displayed to the user                         *
 *                                                              *
 *  Method: - Message string is loaded from the process'        *
 *              message table                                   *
 *          - Alarm beep is sounded if desired                  *
 *          - Message box with the message is displayed         *
 *                                                              *
 *  Returns:                                                    *
 *                                                              *
\****************************************************************/

VOID MessageBox(hwndOwner, nIdMsg, bBeep)
HWND hwndOwner;     /* handle of the message box's owner */
SHORT nIdMsg;       /* id if the message in the message table */
BOOL bBeep;         /* if TRUE, beep before message box is displayed */
{
    CHAR szText[MESSAGELEN];

    if(!WinLoadMessage(hab, (HMODULE)NULL, nIdMsg, MESSAGELEN, (PSZ)szText)) {
        WinAlarm(HWND_DESKTOP, WA_ERROR);
        return;
    }

    if(bBeep)
        WinAlarm(HWND_DESKTOP, WA_ERROR);

    WinMessageBox(HWND_DESKTOP,
                  hwndOwner,
                  szText,
                  (PSZ)NULL,
                  1,
                  MB_OK | MB_ERROR);

}   /* 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))  {

#ifdef LATER
        case IDM_FILENEW:
            FileNew(mp2);
            break;

        case IDM_FILEOPEN:
            FileOpen(mp2);
            break;

        case IDM_FILESAVE:
            FileSave(mp2);
            break;

        case IDM_FILESAVEAS:
            FileSaveAs(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_WINDOWTILE:
            WindowTile(mp2);
            break;

        case IDM_WINDOWCASCADE:
            WindowCascade(mp2);
            break;

        case IDM_HELPFORHELP:
            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_FILEPRINT:
            FilePrint(mp2);
            break;

        case IDM_FILEPAGESETUP:
            FilePageSetup(mp2);
            break;

        case IDM_FILEPRINTSETUP:
            FilePrintSetup(mp2);
            break;

        case IDM_HELPHELPFORHELP:
            HelpHelpForHelp(mp2);
            break;

#endif

        case IDM_HELPABOUT:
            HelpAbout(mp2);
            break;

        case IDM_FILEEXIT:
            FileExit(mp2);
            break;


    /*--------------------------------------------------*\
     *      User command processing routine is called   *
     *      here so any ids not procecessed 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.