home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / template.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1998-04-20  |  13KB  |  421 lines

  1. /**************************************************************************
  2.  *  File name  :  main.c
  3.  *
  4.  *  Description:  This application serves as a template than can be
  5.  *                modified by an application developer.  The source
  6.  *                files are organized so that the overhead code, which
  7.  *                should be in all applications, is located in the same
  8.  *                files, so that these files do not need to be modified.
  9.  *                The routines that deal with application-specific code
  10.  *                are also locatedin their own modules.  An application
  11.  *                developer need only change these files in order to
  12.  *                modify this template for his application.
  13.  *
  14.  *                This source file contains the following functions:
  15.  *
  16.  *                main()
  17.  *                MainWndProc(hwnd, msg, mp1, mp2)
  18.  *                MessageBox(hwnd idMsg, fsStyle, fBeep)
  19.  *                MainCommand(mp1, mp2)
  20.  *
  21.  *  Concepts   :  Presentation Manager
  22.  *
  23.  *  API's      :  WinInitialize
  24.  *                DosBeep
  25.  *                WinCreateMsgQueue
  26.  *                WinTerminate
  27.  *                WinCreateStdWindow
  28.  *                WinOpenWindowDC
  29.  *                WinGetMsg
  30.  *                WinDispatchMsg
  31.  *                WinPostMsg
  32.  *                WinLoadMessage
  33.  *                WinAlarm
  34.  *                WinMessageBox
  35.  *
  36.  *    Files    :  OS2.H, MAIN.H, HELP.H, XTRN.H
  37.  *
  38.  *  Copyright (C) 1991 IBM Corporation
  39.  *
  40.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  41.  *      sample code created by IBM Corporation. This sample code is not
  42.  *      part of any standard or IBM product and is provided to you solely
  43.  *      for  the purpose of assisting you in the development of your
  44.  *      applications.  The code is provided "AS IS", without
  45.  *      warranty of any kind.  IBM shall not be liable for any damages
  46.  *      arising out of your use of the sample code, even if they have been
  47.  *      advised of the possibility of such damages.                                                    *
  48.  *************************************************************************/
  49.  
  50. /*
  51.  *  Include files, macros, defined constants, and externs
  52.  */
  53.  
  54. #define  INCL_WINHELP
  55.  
  56. #include <os2.h>
  57. #include "main.h"
  58. #include "help.h"
  59. #include "xtrn.h"
  60.  
  61.  
  62. /*
  63.  *  Global variables
  64.  */
  65. HWND hwndMainFrame = NULLHANDLE;    /* handle to the main frame window */
  66. HWND hwndMain;                      /* handle to the main client window */
  67. HDC  hdcMain;                       /* handle to the DC of the client */
  68. HAB  hab;                           /* anchor block for the process */
  69. HMQ  hmq;                           /* handle to the process' message queue */
  70. CHAR szAppName[MAXNAMEL];           /* buffer for application name string */
  71. CHAR szUntitled[MESSAGELEN];        /* buffer for "Untitled" string */
  72. BOOL fPrintEnabled;                 /* flag to determine if we can print */
  73. BOOL fHelpEnabled;                  /* flag to determine if help is enabled */
  74.  
  75. /*
  76.  *  Entry point declarations
  77.  */
  78.  
  79. /**************************************************************************
  80.  *
  81.  *  Name       : main()
  82.  *
  83.  *  Description: Initializes the PM environment, calls the
  84.  *               initialization routine, creates the main
  85.  *               window,  and polls the message queue
  86.  *
  87.  *  Concepts:    - obtains anchor block handle and creates message
  88.  *                    queue
  89.  *               - calls the initialization routine
  90.  *               - creates the main frame window which creates the
  91.  *                    main client window
  92.  *               - polls the message queue via Get/Dispatch Msg loop
  93.  *               - upon exiting the loop, exits
  94.  *
  95.  *  API's      :  WinInitialize
  96.  *                DosBeep
  97.  *                WinCreateMsgQueue
  98.  *                WinTerminate
  99.  *                WinCreateStdWindow
  100.  *                WinOpenWindowDC
  101.  *                WinGetMsg
  102.  *                WinDispatchMsg
  103.  *
  104.  *  Parameters :  [none]
  105.  *
  106.  *  Return     :  0 - if successful execution completed
  107.  *                1 - if error
  108.  *
  109.  *************************************************************************/
  110. int main(VOID)
  111. {
  112.    QMSG qmsg;          /* message structure */
  113.    ULONG flCtlData;    /* frame control data */
  114.  
  115.    hab = WinInitialize(0);
  116.  
  117.    if(NULLHANDLE == hab)
  118.    {
  119.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  120.       return(RETURN_ERROR);
  121.    }
  122.  
  123.    hmq = WinCreateMsgQueue(hab, 0);
  124.  
  125.    if(NULLHANDLE == hmq)
  126.    {
  127.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  128.       WinTerminate(hab);
  129.       return(RETURN_ERROR);
  130.    }
  131.  
  132.    if(!Init())
  133.    {
  134.       MessageBox(HWND_DESKTOP,
  135.                  IDMSG_INITFAILED,
  136.                  MB_OK | MB_ERROR,
  137.                  TRUE);
  138.       return(RETURN_ERROR);
  139.    }
  140.  
  141.    /* NOTE:  clean up from here is handled by the DosExitList processing */
  142.  
  143.  
  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.  
  166.    hdcMain = WinOpenWindowDC(hwndMain);
  167.  
  168.    InitHelp();
  169.  
  170.    /* Get/Dispatch Message loop */
  171.    while(WinGetMsg(hab, (PQMSG)&qmsg, (HWND)NULL, (ULONG)NULL, (ULONG)NULL))
  172.        WinDispatchMsg(hab, (PQMSG)&qmsg);
  173.  
  174.    /* destroy the help instance */
  175.    DestroyHelpInstance();
  176.  
  177. #ifdef BACKGROUND_THREAD
  178.     /* see main.h for comment on using a background thread */
  179.    DestroyBackgroundThread();
  180. #endif
  181.  
  182.    return(RETURN_SUCCESS);
  183. }   /* End of main   */
  184.  
  185.  
  186. /**************************************************************************
  187.  *
  188.  *  Name       : MainWndProc(hwnd, msg, mp1, mp2)
  189.  *
  190.  *  Description:  Processes the messages sent to the main client
  191.  *                window.  This routine processes the basic
  192.  *                messages all client windows should process
  193.  *                and passes all others onto UserWndProc where
  194.  *                the developer can process any others.
  195.  *
  196.  *  Concepts:     Called for each message placed in the main
  197.  *                window's message queue
  198.  *
  199.  *                A switch statement branches to the routines to be
  200.  *                performed for each message processed.  Any messages
  201.  *                not specifically process are passed to the user's
  202.  *                message processing procedure UserWndProc().
  203.  *
  204.  *  API's      :  WinPostMsg
  205.  *
  206.  *  Parameters :  hwnd = window handle
  207.  *                msg  = message i.d.
  208.  *                mp1  = first message parameter
  209.  *                mp2  = second message parameter
  210.  *
  211.  *  Return     :  value is determined by each message
  212.  *
  213.  *************************************************************************/
  214. MRESULT EXPENTRY MainWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  215. {
  216.    switch(msg)
  217.    {
  218.    case WM_CREATE:
  219.       return(InitMainWindow(hwnd, mp1, mp2));
  220.       break;
  221.  
  222.    case WM_PAINT:
  223.        MainPaint(hwnd);
  224.        break;
  225.  
  226.    case WM_COMMAND:
  227.        MainCommand(mp1, mp2);
  228.        break;
  229.  
  230.    case WM_INITMENU:
  231.        InitMenu(mp1, mp2);
  232.        break;
  233.  
  234.    case HM_QUERY_KEYS_HELP:
  235.        return (MRESULT)PANEL_HELPKEYS;   /* return id of key help panel */
  236.        break;
  237.  
  238.    case TM_THREADINITFAILED:
  239.        /*  The message is received if the background thread
  240.         *  initialization fails.
  241.         *  A message box is displayed and the application is
  242.         *  terminated.
  243.         */
  244.        MessageBox(HWND_DESKTOP,
  245.                   IDMSG_INITFAILED,
  246.                   MB_OK | MB_ERROR,
  247.                   TRUE);
  248.  
  249.        WinPostMsg(hwnd, WM_CLOSE, NULL, NULL);
  250.        break;
  251.  
  252.    /*
  253.     *      Any messages not processed are passed on
  254.     *      to the user's window proc.  It is
  255.     *      responsible for passing any messages it
  256.     *      doesn't handle onto WinDefWindowProc()
  257.     */
  258.  
  259.    default:
  260.       return UserWndProc(hwnd, msg, mp1, mp2);
  261.       break;
  262.  
  263.    }
  264.    return (MRESULT)NULL;  /* all window procedures return 0 as a default */
  265. }   /* End of MainWndProc   */
  266.  
  267. /**************************************************************************
  268.  *
  269.  *  Name       : MessageBox(hwndOwner, nIdMsg, fsStyle, fBeep)
  270.  *
  271.  *  Description:  Displays the message box with the message
  272.  *                given in idMsg retrieved from the message table
  273.  *                and using the style flags in fsStyle.
  274.  *
  275.  *  Concepts:     Called whenever a message box is to be displayed
  276.  *
  277.  *                - Message string is loaded from the process'
  278.  *                    message table
  279.  *                - Alarm beep is sounded if desired
  280.  *                - Message box with the message is displayed
  281.  *                - WinMessageBox return value is returned
  282.  *
  283.  *  API's      :  WinLoadMessage
  284.  *                WinAlarm
  285.  *                WinMessageBox
  286.  *
  287.  *  Parameters :  hwndOwner = window handle of the owner
  288.  *                nIdMsg    = message i.d.
  289.  *                fsStyle   = style flags for the message box
  290.  *                fBeep     = should an alarm be sounded?
  291.  *
  292.  *  Return     :  return value from WinMessageBox
  293.  *
  294.  *************************************************************************/
  295. ULONG MessageBox(HWND hwndOwner, ULONG idMsg, ULONG fsStyle, BOOL fBeep)
  296. {
  297.    CHAR szText[MESSAGELEN];
  298.  
  299.    if(!WinLoadMessage(hab,
  300.                      (HMODULE)NULL,
  301.                      idMsg,
  302.                      MESSAGELEN,
  303.                      (PSZ)szText))
  304.    {
  305.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  306.       return MBID_ERROR;
  307.    }
  308.  
  309.    if(fBeep)
  310.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  311.  
  312.    return(WinMessageBox(HWND_DESKTOP,
  313.                         hwndOwner,
  314.                         szText,
  315.                         (PSZ)NULL,
  316.                         MSGBOXID,
  317.                         fsStyle));
  318.  
  319. }   /* End of MessageBox   */
  320.  
  321. /**************************************************************************
  322.  *
  323.  *  Name       : MainCommand(mp1, mp2)
  324.  *
  325.  *  Description: Calls the appropriate procedures that deal with
  326.  *               the selected menu item.
  327.  *
  328.  *  Concepts:    Routine is called whenever a WM_COMMAND message
  329.  *               is posted to the main window.
  330.  *
  331.  *               A switch statement branches on the id of the
  332.  *               menu item that posted the message and the
  333.  *               appropriate action for that item is taken.  Any
  334.  *               menu ids that are not part of the standard menu
  335.  *               set are passed onto the user defined WM_COMMAND
  336.  *               processing procedure.
  337.  *
  338.  *  API's      : [none]
  339.  *
  340.  *  Parameters : mp1 = first message parameter
  341.  *               mp2 = second message parameter
  342.  *
  343.  *  Return     : [none]
  344.  *
  345.  *************************************************************************/
  346. VOID MainCommand(MPARAM mp1, MPARAM mp2)
  347. {
  348.    switch(SHORT1FROMMP(mp1))
  349.    {
  350.    case IDM_FILENEW:
  351.       FileNew(mp2);
  352.       break;
  353.  
  354.    case IDM_FILEOPEN:
  355.       FileOpen(mp2);
  356.       break;
  357.  
  358.    case IDM_FILESAVE:
  359.       FileSave(mp2);
  360.       break;
  361.  
  362.    case IDM_FILESAVEAS:
  363.       FileSaveAs(mp2);
  364.       break;
  365.  
  366.    case IDM_EDITUNDO:
  367.       EditUndo(mp2);
  368.       break;
  369.  
  370.    case IDM_EDITCUT:
  371.       EditCut(mp2);
  372.       break;
  373.  
  374.    case IDM_EDITCOPY:
  375.       EditCopy(mp2);
  376.       break;
  377.  
  378.    case IDM_EDITPASTE:
  379.       EditPaste(mp2);
  380.       break;
  381.  
  382.    case IDM_EDITCLEAR:
  383.       EditClear(mp2);
  384.       break;
  385.  
  386.    case IDM_HELPINDEX:
  387.       HelpIndex();
  388.       break;
  389.  
  390.    case IDM_HELPGENERAL:
  391.       HelpGeneral();
  392.       break;
  393.  
  394.    case IDM_HELPUSINGHELP:
  395.       HelpUsingHelp();
  396.       break;
  397.  
  398.    case IDM_HELPKEYS:
  399.       HelpKeys();
  400.       break;
  401.  
  402.    case IDM_HELPTUTORIAL:
  403.       HelpTutorial();
  404.       break;
  405.  
  406.    case IDM_HELPPRODUCTINFO:
  407.       HelpProductInfo();
  408.       break;
  409.  
  410.    /*
  411.     *      User command processing routine is called
  412.     *      here so any ids not processed here can be
  413.     *      processed.
  414.     */
  415.    default:
  416.       UserCommand(mp1, mp2);
  417.       break;
  418.    }
  419. }   /* End of MainCommand  */
  420. /***************************  End of main.c  ****************************/
  421.