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

  1. /**************************************************************************
  2.  *  File name  :  init.c
  3.  *
  4.  *  Description:  This module contains the code for application
  5.  *                initialization, as well as the code for exit list
  6.  *                processing
  7.  *
  8.  *                This source file contains the following functions:
  9.  *
  10.  *                Init()
  11.  *                InitMainWindow(hwnd, mp1, mp2)
  12.  *                ExitProc(usTermCode)
  13.  *
  14.  *  Concepts   :  initialization
  15.  *
  16.  *  API's      :  DosExitList
  17.  *                DosExit
  18.  *                WinLoadString
  19.  *                WinRegisterClass
  20.  *                WinIsWindow
  21.  *                WinDestroyWindow
  22.  *                WinDestroyMsgQueue
  23.  *                WinTerminate
  24.  *
  25.  *  Required
  26.  *    Files    :  OS2.H, STRING.H, MAIN.H, XTRN.H
  27.  *
  28.  *  Copyright (C) 1991 IBM Corporation
  29.  *
  30.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  31.  *      sample code created by IBM Corporation. This sample code is not
  32.  *      part of any standard or IBM product and is provided to you solely
  33.  *      for  the purpose of assisting you in the development of your
  34.  *      applications.  The code is provided "AS IS", without
  35.  *      warranty of any kind.  IBM shall not be liable for any damages
  36.  *      arising out of your use of the sample code, even if they have been
  37.  *      advised of the possibility of such damages.                                                    *
  38.  *************************************************************************/
  39.  
  40. /*
  41.  *  Include files, macros, defined constants, and externs
  42.  */
  43.  
  44. #define  INCL_WINWINDOWMGR
  45. #define  INCL_DOSPROCESS
  46.  
  47. #include <os2.h>
  48. #include <string.h>
  49. #include "main.h"
  50. #include "xtrn.h"
  51.  
  52. /*
  53.  *  Global variables
  54.  */
  55.  
  56.  
  57. /*
  58.  *  Entry point declarations
  59.  *
  60.  
  61. /**************************************************************************
  62.  *
  63.  *  Name       : Init()
  64.  *
  65.  *  Description:  Performs initialization functions required
  66.  *                before the main window can be created.
  67.  *
  68.  *  Concepts:     Called once before the main window is created.
  69.  *
  70.  *                - Installs the routine ExitProc into the
  71.  *                  DosExitList chain
  72.  *                - registers all window classes
  73.  *                - performs any command line processing
  74.  *
  75.  *  API's      :  DosExitList
  76.  *                DosExit
  77.  *                WinLoadString
  78.  *                WinRegisterClass
  79.  *
  80.  *  Parameters :  [none]
  81.  *
  82.  *  Return     :  TRUE = initialization is successful
  83.  *                FALSE = initialization failed
  84.  *
  85.  *************************************************************************/
  86. BOOL Init(VOID)
  87. {
  88.  
  89.    /* Add ExitProc to the exit list to handle the exit processing.  If
  90.     * there is an error, then terminate the process since there have
  91.     * not been any resources allocated yet.
  92.     */
  93.    if(DosExitList(EXLST_ADD, (PFNEXITLIST)ExitProc))
  94.    {
  95.       MessageBox(HWND_DESKTOP,
  96.                  IDMSG_CANNOTLOADEXITLIST,
  97.                  MB_OK | MB_ERROR,
  98.                  TRUE);
  99.       DosExit(EXIT_PROCESS, RETURN_ERROR);
  100.    }
  101.  
  102.    /* load application name from resource file */
  103.    if(0 == WinLoadString(hab, (HMODULE)0, IDS_APPNAME, MAXNAMEL, szAppName))
  104.       return FALSE;
  105.  
  106.    /* load "untitled" string */
  107.    if(!WinLoadString(hab, (HMODULE)0, IDS_UNTITLED, MESSAGELEN, szUntitled))
  108.        return FALSE;
  109.  
  110.    /* register the main client window class */
  111.    if(!WinRegisterClass(hab,
  112.                        (PSZ)szAppName,
  113.                        (PFNWP)MainWndProc,
  114.                        CS_SIZEREDRAW | CS_CLIPCHILDREN,
  115.                        0L))
  116.        return FALSE;
  117.  
  118.    /* If you wish to create a thread for background processing, define
  119.     *  the BACKGROUND_THREAD constant
  120.     *  the routine commented out here.  The routines for the background
  121.     *  thread are in the thrd.c file.
  122.     */
  123.  
  124. #ifdef BACKGROUND_THREAD
  125.    /* see main.h for comment on using a background thread */
  126.  
  127.    if(!CreateBackgroundThread())
  128.        return FALSE;
  129.  
  130. #endif
  131.  
  132.     /*
  133.      *      Add any command line processing here.
  134.      */
  135.  
  136.  
  137.    return TRUE;
  138. }  /* End of Init   */
  139.  
  140. /**************************************************************************
  141.  *
  142.  *  Name       : InitMainWindow(hwnd, mp1, mp2)
  143.  *
  144.  *  Description: Performs initialization functions required
  145.  *               when the main window is created.
  146.  *
  147.  *  Concepts:    Called once during the WM_CREATE processing when
  148.  *               the main window is created.
  149.  *
  150.  *  API's      : [none]
  151.  *
  152.  *  Parameters :  hwnd = window handle
  153.  *                mp1  = first message parameter
  154.  *                mp2  = second message parameter
  155.  *
  156.  *  Return     :   value to be returned from the WM_CREATE message:
  157.  *                TRUE =  window creation should stop
  158.  *                FALSE = window creation should continue
  159.  *
  160.  *************************************************************************/
  161. MRESULT InitMainWindow(HWND hwnd, MPARAM mp1, MPARAM mp2)
  162. {
  163.    UpdateTitleText(((PCREATESTRUCT)PVOIDFROMMP(mp2))->hwndParent);
  164.  
  165.    /* return FALSE to continue window creation, TRUE to abort it */
  166.    return (MRESULT)FALSE;
  167.  
  168.  
  169.    /* This routine currently doesn't use the hwnd, mp1, and mp2 parameters
  170.     *  so they are referenced here to prevent an 'Unreferenced Parameter'
  171.     *  warning at compile time.
  172.     */
  173.  
  174.    hwnd;
  175.    mp1;
  176.    mp2;
  177. }   /* End of InitMainWindow   */
  178.  
  179.  
  180. /**************************************************************************
  181.  *
  182.  *  Name       : ExitProc(usTermCode)
  183.  *
  184.  *  Description: Cleans up certain resources when the application
  185.  *               terminates.
  186.  *
  187.  *  Concepts:    Routine is called by DosExitList when the
  188.  *               application exits.
  189.  *
  190.  *               Global resources, such as the main window and
  191.  *               message queue, are destroyed and any system
  192.  *               resources used are freed.
  193.  *
  194.  *  API's      : WinIsWindow
  195.  *               WinDestroyWindow
  196.  *               WinDestroyMsgQueue
  197.  *               WinTerminate
  198.  *               DosExitList
  199.  *
  200.  *  Parameters :  usTermCode = termination code number
  201.  *
  202.  *  Return    :   Returns EXLST_EXIT to the DosExitList handler
  203.  *
  204.  *************************************************************************/
  205. VOID ExitProc(USHORT usTermCode)
  206. {
  207.     /* destroy the main window if it exists */
  208.    if(WinIsWindow(hab, hwndMainFrame))
  209.       WinDestroyWindow(hwndMainFrame);
  210.  
  211.     /*
  212.      *      Any other system resources used
  213.      *      (e.g. memory or files) should be freed here.
  214.      */
  215.  
  216.  
  217.    WinDestroyMsgQueue(hmq);
  218.  
  219.    WinTerminate(hab);
  220.  
  221.    DosExitList(EXLST_EXIT, (PFNEXITLIST)NULL);    /* termination complete */
  222.  
  223.  
  224.     /* This routine currently doesn't use the usTermCode parameter so
  225.      *  it is referenced here to prevent an 'Unreferenced Parameter'
  226.      *  warning at compile time
  227.      */
  228.  
  229.    usTermCode;
  230. }   /* End of ExitProc    */
  231. /***************************  End of init.c  ***************************/
  232.