home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / os2tk21j / c / samples / style / sty_init.c__ / sty_init.c
Encoding:
C/C++ Source or Header  |  1993-03-12  |  7.5 KB  |  225 lines

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