home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / semaph / sem_main.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  14KB  |  305 lines

  1. /*static char *SCCSID = "@(#)sem_main.c    6.16 92/02/19";*/
  2. /*==============================================================*\
  3.  *  SEM_MAIN.C - Sample PM application                          *
  4.  *     (C) Copyright IBM Corporation 1992.                      *
  5.  *--------------------------------------------------------------*
  6.  *                                                              *
  7.  *  This application serves two distinct purposes.  First, it   *
  8.  *  demonstrates the standard menus and dialogs that most PM    *
  9.  *  applications will contain.  The Demo menu calls dialog      *
  10.  *  boxes that demonstrate all of the controls available for    *
  11.  *  use in a dialog box.  There is also a dialog box which      *
  12.  *  demonstrates the setting and removing of control            *
  13.  *  Presentation Parameters.  Finally, the demo menu also has   *
  14.  *  menu items that demonstrate the various types of message    *
  15.  *  boxes that the system supports.                             *
  16.  *                                                              *
  17.  *  This application also serves as a template than can be      *
  18.  *  easily modified by an application developer.  The source    *
  19.  *  files are organized so that the overhead code that should   *
  20.  *  be in all applications is located in the same files so      *
  21.  *  that these files do not need to be modified.  The routines  *
  22.  *  that deal with application specific code are also located   *
  23.  *  in their own modules.  An application developer need only   *
  24.  *  change these files in order to modify this template for     *
  25.  *  his application.                                            *
  26.  *                                                              *
  27.  *--------------------------------------------------------------*
  28.  *                                                              *
  29.  *  This source file contains the following functions:          *
  30.  *                                                              *
  31.  *   main() - main routine                                      *
  32.  *   MainWndProc(hwnd, msg, mp1, mp2) - main window procedure   *
  33.  *   MessageBox(hwnd nId, bBeep) - Warning box routine          *
  34.  *   MainCommand(mp1, mp2) - WM_COMMAND processing of Main      *
  35.  *                                                              *
  36. \*==============================================================*/
  37.  
  38. /*--------------------------------------------------------------*\
  39.  *  Include files, macros, defined constants, and externs       *
  40. \*--------------------------------------------------------------*/
  41. #include "semaph.h"
  42.  
  43. /****************************************************************\
  44.  *  Main routine                                                *
  45.  *--------------------------------------------------------------*
  46.  *                                                              *
  47.  *  Name:   main()                                              *
  48.  *                                                              *
  49.  *  Purpose: Initializes the PM environment, calls the          *
  50.  *              initialization routine, creates the main        *
  51.  *              window,  and polls the message queue            *
  52.  *                                                              *
  53.  *  Usage:                                                      *
  54.  *                                                              *
  55.  *  Method:                                                     *
  56.  *          - obtains anchor block handle and creates message   *
  57.  *              queue                                           *
  58.  *          - calls the initialization routine                  *
  59.  *          - creates the main frame window which creates the   *
  60.  *              main client window                              *
  61.  *          - polls the message queue via Get/Dispatch Msg loop *
  62.  *          - upon exiting the loop, exits                      *
  63.  *                                                              *
  64.  *  Returns: (via DosExit)                                      *
  65.  *          1 - if sucessful execution completed                *
  66.  *          0 - if error                                        *
  67. \****************************************************************/
  68. INT main(VOID)
  69. {
  70.    QMSG qmsg;          /* message structure */
  71.    ULONG ctlData;      /* frame control data */
  72.  
  73.    strcpy(szWindowText,"Sample");
  74.    cNumUsers=NUMUSERS;
  75.    fSemaphoreStarted=FALSE;
  76.    fSemaphoreWasStarted=FALSE;
  77.    fAutoMode=FALSE;
  78.    ulTimeout = TIMEOUTPERIOD;
  79.  
  80.    hab = WinInitialize(0);
  81.    if(!hab) {
  82.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  83.       return(RETURN_ERROR);
  84.    }
  85.    hmq = WinCreateMsgQueue(hab, 0);
  86.    if(!hmq) {
  87.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  88.       WinTerminate(hab);
  89.       return(RETURN_ERROR);
  90.    }
  91.    if(!Init()) {
  92.       MessageBox(HWND_DESKTOP, IDMSG_INITFAILED, TRUE);
  93.       DosExit(EXIT_PROCESS, RETURN_ERROR);
  94.    }
  95.                                          /* create the main window */
  96.    ctlData = FCF_STANDARD;
  97.    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
  98.                                       WS_VISIBLE,
  99.                                       (PVOID)&ctlData,
  100.                                       (PSZ)szAppName,
  101.                                       (PSZ)NULL,
  102.                                       WS_VISIBLE,
  103.                                       (HMODULE)NULL,
  104.                                       IDR_RESOURCE,
  105.                                       (PHWND)&hwndMain);
  106.  
  107.    if(!hwndMainFrame)
  108.    {
  109.        MessageBox(HWND_DESKTOP, IDMSG_MAINWINCREATEFAILED, TRUE);
  110.        DosExit(EXIT_PROCESS, RETURN_ERROR);
  111.    }
  112.    InitHelp();
  113.  
  114.    while(WinGetMsg(hab, (PQMSG)&qmsg, (HWND)0, 0, 0))
  115.        WinDispatchMsg(hab, (PQMSG)&qmsg);
  116.                                           /* destroy the help instance */
  117.    DestroyHelpInstance();
  118.                                     /* will normally be put in ExitProc */
  119.    DosExit(EXIT_PROCESS, RETURN_SUCCESS);
  120.    return 0L;
  121. }
  122.  
  123. /****************************************************************\
  124.  *  Main client window procedure                                *
  125.  *--------------------------------------------------------------*
  126.  *                                                              *
  127.  *  Name:   MainWndProc(hwnd, msg, mp1, mp2)                    *
  128.  *                                                              *
  129.  *  Purpose: Processes the messages sent to the main client     *
  130.  *              window.  This routine processes the basic       *
  131.  *              messages all client windows should process      *
  132.  *              and passes all others onto UserWndProc where    *
  133.  *              the developer can process any others.           *
  134.  *                                                              *
  135.  *  Usage:  Called for each message placed in the main          *
  136.  *          window's message queue                              *
  137.  *                                                              *
  138.  *  Method: a switch statement branches to the routines to be   *
  139.  *          performed for each message processed.  Any messages *
  140.  *          not specifically process are passed to the user's   *
  141.  *          message processing procedure UserWndProc().         *
  142.  *                                                              *
  143.  *  Returns:  Return values are determined by each message      *
  144.  *                                                              *
  145. \****************************************************************/
  146. MRESULT EXPENTRY MainWndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  147. {
  148.     FONTMETRICS  fntmet;
  149.     HPS          hPS;
  150.     static SHORT xClient, yClient;
  151.     static SHORT yChar, yDesc;
  152.     MRESULT sRC;
  153.  
  154.     switch(msg)
  155.     {
  156.        case WM_CREATE:
  157.            hPS = WinGetPS (hwnd);
  158.            GpiQueryFontMetrics (hPS, (LONG) sizeof fntmet, &fntmet);
  159.  
  160.            yChar = fntmet.lMaxBaselineExt;
  161.            yDesc = fntmet.lMaxDescender;
  162.  
  163.            WinReleasePS (hPS);
  164.            break;
  165.  
  166.        case WM_PAINT:
  167.            MainPaint(hwnd);
  168.            break;
  169.  
  170.        case WM_SIZE:
  171.            xClient = LOUSHORT (mp2);   /* width of window  */
  172.            yClient = HIUSHORT (mp2);   /* height of window */
  173.            WinPostMsg (hwndMain, WM_USERCHANGED, (MPARAM)0, (MPARAM)0);
  174.            break;
  175.                                        /* fall thru to ... */
  176.        case WM_USERCHANGED:            /* number of threads changed */
  177.                                        /* so x and y are already known */
  178.            SetRectPositions(xClient, yClient, yChar, yDesc);
  179.            WinInvalidateRect (hwnd, NULL, TRUE);
  180.            break;
  181.  
  182.        case WM_COMMAND:
  183.            MainCommand(hwnd, mp1, mp2);
  184.            break;
  185.  
  186.         /*-----------------------------------------------*\
  187.          *   Any messages not processed are passed on    *
  188.          *   to the user's window proc.  It is           *
  189.          *   responsible for passing any messages it     *
  190.          *   doesn't handle onto WinDefWindowProc()      *
  191.         \*-----------------------------------------------*/
  192.        case HM_QUERY_KEYS_HELP:
  193.            return ((MRESULT)PANEL_KEYSHELP);
  194.  
  195.        default:
  196.            sRC = WinDefWindowProc(hwnd, msg, mp1, mp2);
  197.            return sRC;
  198.  
  199.     }
  200.     return (MRESULT)0L;
  201.                   /* all window procedures should return 0 as a default */
  202. }                                                      /* MainWndProc() */
  203.  
  204. /****************************************************************\
  205.  *  Main window WM_COMMAND processing procedure                 *
  206.  *--------------------------------------------------------------*
  207.  *                                                              *
  208.  *  Name:   MainCommand(hwnd, mp1, mp2)                         *
  209.  *                                                              *
  210.  *  Purpose: Calls the appropriate procedures that deal with    *
  211.  *              the selected menu item.                         *
  212.  *                                                              *
  213.  *  Usage:  Routine is called whenever a WM_COMMAND message     *
  214.  *          is posted to the main window.                       *
  215.  *                                                              *
  216.  *  Method: a switch statement branches on the id of the        *
  217.  *          menu item that posted the message and the           *
  218.  *          appropriate action for that item is taken.  Any     *
  219.  *          menu ids that are not part of the standard menu     *
  220.  *          set are passed onto the user defined WM_COMMAND     *
  221.  *          processing procedure.                               *
  222.  *                                                              *
  223.  *  Returns:                                                    *
  224.  *                                                              *
  225. \****************************************************************/
  226. VOID MainCommand(HWND hwnd, MPARAM mp1, MPARAM mp2 )
  227. {
  228.     switch(SHORT1FROMMP(mp1))
  229.     {
  230.         case IDM_GENERALHELP:
  231.            HelpExtended(mp2);
  232.            break;
  233.  
  234.         case IDM_USINGHELP:
  235.            HelpHelpForHelp(mp2);
  236.            break;
  237.  
  238.         case IDM_TUTORIAL:
  239.            HelpKeys(mp2);
  240.            break;
  241.  
  242.         case IDM_HELPINDEX:
  243.            HelpIndex(mp2);
  244.            break;
  245.  
  246.         case IDM_HELPPRODUCTINFO:
  247.            HelpAbout(mp2);
  248.            break;
  249.  
  250.     /*--------------------------------------------------*\
  251.      *      User command processing routine is called   *
  252.      *      here so any ids not procecessed here can be *
  253.      *      processed                                   *
  254.     \*--------------------------------------------------*/
  255.         default:
  256.             UserCommand(mp1, mp2);
  257.             break;
  258.     }
  259.     return;
  260. }                                             /* MainCommand() */
  261.  
  262. /****************************************************************\
  263.  *  Message Box procedure                                       *
  264.  *--------------------------------------------------------------*
  265.  *                                                              *
  266.  *  Name:   MessageBox(hwndOwner, nIdMsg, bBeep)                *
  267.  *                                                              *
  268.  *  Purpose: Displays the warning message box with the message  *
  269.  *              given in nIdMsg retrived from the message table *
  270.  *                                                              *
  271.  *  Usage:  Called whever an error occurs and a message wishes  *
  272.  *          to be displayed to the user                         *
  273.  *                                                              *
  274.  *  Method: - Message string is loaded from the process'        *
  275.  *              message table                                   *
  276.  *          - Alarm beep is sounded if desired                  *
  277.  *          - Message box with the message is displayed         *
  278.  *                                                              *
  279.  *  Returns:                                                    *
  280.  *                                                              *
  281. \****************************************************************/
  282. VOID MessageBox( HWND hwndOwner, /* handle of the message box's owner      */
  283.                  LONG nIdMsg,   /* id if the message in the message table */
  284.                  BOOL bBeep ) /* TRUE, beep before message box is displayed*/
  285. {
  286.    CHAR szText[MESSAGELEN];
  287.  
  288.    if(!WinLoadMessage(hab, (HMODULE)NULL, nIdMsg, MESSAGELEN, (PSZ)szText))
  289.    {
  290.        WinAlarm(HWND_DESKTOP, WA_ERROR);
  291.        return;
  292.    }
  293.  
  294.    if(bBeep)
  295.        WinAlarm(HWND_DESKTOP, WA_ERROR);
  296.  
  297.    WinMessageBox(HWND_DESKTOP,
  298.                  hwndOwner,
  299.                  szText,
  300.                  (PSZ)NULL,
  301.                  IDM_MSGBOX,
  302.                  MB_OK | MB_ERROR);
  303.    return;
  304. }                                                        /* MessageBox() */
  305.