home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / startapp.zip / MAIN.C < prev    next >
Text File  |  1998-09-10  |  13KB  |  380 lines

  1. /**************************************************************************
  2.  *  File name  :  main.c
  3.  *
  4.  *  Description:  This application demonstrates the capability to
  5.  *                start another application running in a VDM session.
  6.  *                It also demostrates the capability to terminate this
  7.  *                application and the session.  Note, any other application
  8.  *                started in this session will also be terminated.
  9.  *                DOS and WINOS2 support must be installed to run this
  10.  *                application.
  11.  *
  12.  *                This source file contains the following functions:
  13.  *
  14.  *                main()
  15.  *                MainWndProc(hwnd, msg, mp1, mp2)
  16.  *                MessageBox(hwnd idMsg, fsStyle, fBeep)
  17.  *                MainCommand(mp1, mp2)
  18.  *
  19.  *  Concepts   :  Presentation Manager/WINOS2 process control
  20.  *
  21.  *  API's      :  WinInitialize
  22.  *                DosBeep
  23.  *                WinCreateMsgQueue
  24.  *                WinTerminate
  25.  *                WinCreateStdWindow
  26.  *                WinOpenWindowDC
  27.  *                WinGetMsg
  28.  *                WinDispatchMsg
  29.  *                WinPostMsg
  30.  *                WinLoadMessage
  31.  *                WinAlarm
  32.  *                WinMessageBox
  33.  *
  34.  *    Files    :  OS2.H, MAIN.H, HELP.H, XTRN.H
  35.  *
  36.  *  (c) Copyright IBM Corp. 1991, 1998  All rights reserved.
  37.  *
  38.  *  These sample programs are owned by International Business Machines
  39.  *  Corporation or one of its subsidiaries ("IBM") and are copyrighted and
  40.  *  licensed, not sold.
  41.  *
  42.  *  You may copy, modify, and distribute these sample programs in any
  43.  *  form without payment to IBM,  for any purpose including developing,
  44.  *  using, marketing or distributing programs that include or are
  45.  *  derivative works of the sample programs.
  46.  *
  47.  *  The sample programs are provided to you on an "AS IS" basis, without
  48.  *  warranty of any kind.  IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES,
  49.  *  EITHER EXPRESS OR IMPLIED, INCLUDING , BUT NOT LIMITED TO, THE IMPLIED
  50.  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  51.  *  Some jurisdictions do not allow for the exclusion or limitation of
  52.  *  implied warranties, so the above limitations or exclusions may not
  53.  *  apply to you.  IBM shall not be liable for any damages you suffer
  54.  *  as a result of using, modifying or distributing the sample programs
  55.  *  or their derivatives.
  56.  *************************************************************************/
  57.  
  58. #define  INCL_WINHELP
  59. #define  INCL_WIN
  60.  
  61. #include <os2.h>
  62. #include "main.h"
  63. #include "help.h"
  64. #include "xtrn.h"
  65.  
  66. /*
  67.  *  Global variables
  68.  */
  69. HWND hwndMainFrame = NULLHANDLE;    /* handle to the main frame window     */
  70. HWND hwndMain;                      /* handle to the main client window    */
  71. HWND hwndList;                      /* handle to the main client list box  */
  72. HDC  hdcMain;                       /* handle to the DC of the client      */
  73. HAB  hab;                           /* anchor block for the process        */
  74. HMQ  hmq;                           /* handle to the process' message queue*/
  75. CHAR szAppName[MAXNAMEL];           /* buffer for application name string  */
  76. CHAR szUntitled[MESSAGELEN];        /* buffer for "Untitled" string        */
  77. BOOL fPrintEnabled;                 /* flag to determine if we can print   */
  78. BOOL fHelpEnabled;                  /* flag to determine if help is enabled*/
  79. BOOL fStarted=FALSE;                /* flag to determine if app is started */
  80. /***************************************************************************
  81.  *
  82.  *  Name       : main()
  83.  *
  84.  *  Description: Initializes the PM environment, calls the
  85.  *               initialization routine, creates the main
  86.  *               window, and polls the message queue
  87.  *
  88.  *  Concepts:    - obtains anchor block handle and creates message
  89.  *                    queue
  90.  *               - calls the initialization routine
  91.  *               - creates the main frame window which creates the
  92.  *                    main client window
  93.  *               - polls the message queue via Get/Dispatch Msg loop
  94.  *               - upon exiting the loop, exits
  95.  *
  96.  *  API's      :  WinInitialize
  97.  *                DosBeep
  98.  *                WinCreateMsgQueue
  99.  *                WinTerminate
  100.  *                WinCreateStdWindow
  101.  *                WinOpenWindowDC
  102.  *                WinGetMsg
  103.  *                WinDispatchMsg
  104.  *
  105.  *  Parameters :  [none]
  106.  *
  107.  *  Return     :  0 - if successful execution completed
  108.  *                1 - if error
  109.  *
  110.  *************************************************************************/
  111. int main(VOID)
  112. {
  113.    QMSG qmsg;          /* message structure  */
  114.    ULONG flCtlData;    /* frame control data */
  115.    RECTL rcl;          /* window area points */
  116.  
  117.    hab = WinInitialize(0);
  118.  
  119.    if(NULLHANDLE == hab)
  120.    {
  121.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  122.       return(RETURN_ERROR);
  123.    }
  124.  
  125.    hmq = WinCreateMsgQueue(hab, 0);
  126.  
  127.    if(NULLHANDLE == hmq)
  128.    {
  129.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  130.       WinTerminate(hab);
  131.       return(RETURN_ERROR);
  132.    }
  133.  
  134.    if(!Init())
  135.    {
  136.       MessageBox(HWND_DESKTOP,
  137.                  IDMSG_INITFAILED,
  138.                  MB_OK | MB_ERROR,
  139.                  TRUE);
  140.       return(RETURN_ERROR);
  141.    }
  142.  
  143.    /* NOTE:  clean up from here is handled by the DosExitList processing */
  144.    /* create the main window                                             */
  145.    flCtlData = FCF_STANDARD;
  146.  
  147.    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
  148.                                       WS_VISIBLE,
  149.                                       &flCtlData,
  150.                                       (PSZ)szAppName,
  151.                                       (PSZ)NULL,
  152.                                       WS_VISIBLE,
  153.                                       (HMODULE)NULL,
  154.                                       IDR_MAIN,
  155.                                       (PHWND)&hwndMain);
  156.  
  157.    if(NULLHANDLE == hwndMainFrame)
  158.    {
  159.        MessageBox(HWND_DESKTOP,
  160.                   IDMSG_MAINWINCREATEFAILED,
  161.                   MB_OK | MB_ERROR,
  162.                   TRUE);
  163.        return(RETURN_ERROR);
  164.    }
  165.                                                /* How big is the
  166.                                                 client window? */
  167.    WinQueryWindowRect(hwndMainFrame, &rcl);
  168.                                                /* Make a list-box
  169.                                                  window.       */
  170.    hwndList = WinCreateWindow(hwndMainFrame,   /* Parent       */
  171.          WC_LISTBOX,                           /* Class        */
  172.          "",                                   /* Name         */
  173.          WS_VISIBLE  | LS_NOADJUSTPOS,         /* Style        */
  174.          2, 2,                                 /* x, y         */
  175.          rcl.xRight - 5 , rcl.yTop - 50,       /* cx, cy       */
  176.          hwndMainFrame,                        /* Owner        */
  177.          HWND_TOP,                             /* Behind       */
  178.          WINDOW_LISTBOX,                       /* ID           */
  179.          NULL,                                 /* Control data */
  180.          NULL);                                /* parameters   */
  181.  
  182.    hdcMain = WinOpenWindowDC(hwndMain);
  183.    InitHelp();
  184.  
  185.    /* Get/Dispatch Message loop */
  186.    while(WinGetMsg(hab, (PQMSG)&qmsg, (HWND)NULL, (ULONG)NULL, (ULONG)NULL))
  187.        WinDispatchMsg(hab, (PQMSG)&qmsg);
  188.  
  189.    /* destroy the help instance */
  190.    DestroyHelpInstance();
  191.  
  192.    return(RETURN_SUCCESS);
  193. }   /* End of main   */
  194. /**************************************************************************
  195.  *
  196.  *  Name       : MainWndProc(hwnd, msg, mp1, mp2)
  197.  *
  198.  *  Description:  Processes the messages sent to the main client
  199.  *                window.  This routine processes the basic
  200.  *                messages all client windows should process
  201.  *                and passes all others onto UserWndProc where
  202.  *                the developer can process any others.
  203.  *
  204.  *  Concepts:     Called for each message placed in the main
  205.  *                window's message queue
  206.  *
  207.  *                A switch statement branches to the routines to be
  208.  *                performed for each message processed.  Any messages
  209.  *                not specifically processed are passed to the user's
  210.  *                message processing procedure UserWndProc().
  211.  *
  212.  *  API's      :  WinPostMsg
  213.  *
  214.  *  Parameters :  hwnd = window handle
  215.  *                msg  = message i.d.
  216.  *                mp1  = first message parameter
  217.  *                mp2  = second message parameter
  218.  *
  219.  *  Return     :  value is determined by each message
  220.  *
  221.  *************************************************************************/
  222. MRESULT EXPENTRY MainWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  223. {
  224.    switch(msg)
  225.    {
  226.    case WM_CREATE:
  227.       return(InitMainWindow(hwnd, mp1, mp2));
  228.       break;
  229.  
  230.    case WM_PAINT:
  231.        MainPaint(hwnd);
  232.        break;
  233.  
  234.    case WM_COMMAND:
  235.        MainCommand(mp1, mp2);
  236.        break;
  237.  
  238.    case WM_INITMENU:
  239.        InitMenu(mp1, mp2);
  240.        break;
  241.  
  242.    case HM_QUERY_KEYS_HELP:
  243.        return (MRESULT)PANEL_HELPKEYS;   /* return id of key help panel */
  244.        break;
  245.  
  246.    /*
  247.     *      Any messages not processed are passed on
  248.     *      to the user's window proc.  It is
  249.     *      responsible for passing any messages it
  250.     *      doesn't handle onto WinDefWindowProc()
  251.     */
  252.  
  253.    default:
  254.       return UserWndProc(hwnd, msg, mp1, mp2);
  255.       break;
  256.  
  257.    }
  258.    return (MRESULT)NULL;  /* all window procedures return 0 as a default */
  259. }   /* End of MainWndProc   */
  260.  
  261. /**************************************************************************
  262.  *
  263.  *  Name       : MessageBox(hwndOwner, nIdMsg, fsStyle, fBeep)
  264.  *
  265.  *  Description:  Displays the message box with the message
  266.  *                given in idMsg retrieved from the message table
  267.  *                and using the style flags in fsStyle.
  268.  *
  269.  *  Concepts:     Called whenever a message box is to be displayed
  270.  *
  271.  *                - Message string is loaded from the process'
  272.  *                    message table
  273.  *                - Alarm beep is sounded if desired
  274.  *                - Message box with the message is displayed
  275.  *                - WinMessageBox return value is returned
  276.  *
  277.  *  API's      :  WinLoadMessage
  278.  *                WinAlarm
  279.  *                WinMessageBox
  280.  *
  281.  *  Parameters :  hwndOwner = window handle of the owner
  282.  *                nIdMsg    = message i.d.
  283.  *                fsStyle   = style flags for the message box
  284.  *                fBeep     = should an alarm be sounded?
  285.  *
  286.  *  Return     :  return value from WinMessageBox
  287.  *
  288.  *************************************************************************/
  289. ULONG MessageBox(HWND hwndOwner, ULONG idMsg, ULONG fsStyle, BOOL fBeep)
  290. {
  291.    CHAR szText[MESSAGELEN];
  292.  
  293.    if(!WinLoadMessage(hab,
  294.                      (HMODULE)NULL,
  295.                      idMsg,
  296.                      MESSAGELEN,
  297.                      (PSZ)szText))
  298.    {
  299.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  300.       return MBID_ERROR;
  301.    }
  302.  
  303.    if(fBeep)
  304.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  305.  
  306.    return(WinMessageBox(HWND_DESKTOP,
  307.                         hwndOwner,
  308.                         szText,
  309.                         (PSZ)NULL,
  310.                         MSGBOXID,
  311.                         fsStyle));
  312.  
  313. }   /* End of MessageBox   */
  314. /**************************************************************************
  315.  *
  316.  *  Name       : MainCommand(mp1, mp2)
  317.  *
  318.  *  Description: Calls the appropriate procedures that deal with
  319.  *               the selected menu item.
  320.  *
  321.  *  Concepts:    Routine is called whenever a WM_COMMAND message
  322.  *               is posted to the main window.
  323.  *
  324.  *               A switch statement branches on the id of the
  325.  *               menu item that posted the message and the
  326.  *               appropriate action for that item is taken.  Any
  327.  *               menu ids that are not part of the standard menu
  328.  *               set are passed onto the user defined WM_COMMAND
  329.  *               processing procedure UserCommand.
  330.  *
  331.  *  API's      : [none]
  332.  *
  333.  *  Parameters : mp1 = first message parameter
  334.  *               mp2 = second message parameter
  335.  *
  336.  *  Return     : [none]
  337.  *
  338.  *************************************************************************/
  339. VOID MainCommand(MPARAM mp1, MPARAM mp2)
  340. {
  341.    switch(SHORT1FROMMP(mp1))
  342.    {
  343.    case IDM_START:
  344.       StrApp(mp2);
  345.       break;
  346.    case IDM_TERMIN8:
  347.       StopApp(mp2);
  348.       break;
  349.  
  350.    case IDM_HELPINDEX:
  351.       HelpIndex();
  352.       break;
  353.  
  354.    case IDM_HELPGENERAL:
  355.       HelpGeneral();
  356.       break;
  357.  
  358.    case IDM_HELPUSINGHELP:
  359.       HelpUsingHelp();
  360.       break;
  361.  
  362.    case IDM_HELPKEYS:
  363.       HelpKeys();
  364.       break;
  365.  
  366.    case IDM_HELPTUTORIAL:
  367.       HelpTutorial();
  368.       break;
  369.  
  370.    case IDM_HELPPRODUCTINFO:
  371.       HelpProductInfo();
  372.       break;
  373.  
  374.    default:
  375.       UserCommand(mp1, mp2);
  376.       break;
  377.    }
  378. }   /* End of MainCommand  */
  379. /***************************  End of main.c  ****************************/
  380.