home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / devnews / vol2 / sample3 / sty_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-27  |  8.1 KB  |  256 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.         return FALSE;
  106.     }
  107.     /*
  108.      *so we can do some testing without the VDD being installed
  109.      */
  110.     if(fUseDeviceDriver)
  111.     {
  112.          /*
  113.           *make sure the device driver is
  114.           *installed
  115.           */
  116.  
  117.          if(OpenVirtQueue() )
  118.          {
  119.               MessageBox(HWND_DESKTOP,
  120.                         IDMSG_VIRTDDNOTLOADED,
  121.                         MB_OK | MB_ERROR,
  122.                         TRUE);
  123.               return(FALSE);
  124.          }
  125.          /*
  126.           *start off our read thread
  127.           */
  128.           if(StartReadThread() )
  129.           {
  130.                return(FALSE);
  131.           }
  132.     }
  133.  
  134.     /*
  135.      * Add any command line processing here
  136.      */
  137.     return TRUE;
  138. }               /* End of Init()          */
  139.  
  140. /*********************************************************************
  141.  *  Name: InitMainWindow
  142.  *
  143.  *  Description : Performs initialization functions required
  144.  *                when the main window can be created.
  145.  *
  146.  *  Concepts : Performs initialization functions required
  147.  *             when the main window is created.  Called once
  148.  *             during the WM_CREATE processing when the main
  149.  *             window is created.
  150.  *
  151.  *
  152.  *  API's : WinQueryWindowRect
  153.  *          WinCreateWindow
  154.  *
  155.  *  Parameters : hwnd - client window handle
  156.  *               mp1 - First message parameter
  157.  *               mp2 - Second message parameter
  158.  *
  159.  *  Returns: TRUE  - initialization failed
  160.  *           FALSE - initialization is successful
  161.  *
  162.  ****************************************************************/
  163. MRESULT InitMainWindow(HWND hwnd, MPARAM mp1, MPARAM mp2)
  164. {
  165.     RECTL rcl;
  166.  
  167.     UpdateTitleText(((PCREATESTRUCT)PVOIDFROMMP(mp2))->hwndParent);
  168.  
  169.     WinQueryWindowRect(hwnd, (PRECTL)&rcl);
  170.  
  171.     /* create MLE window the same size as the client */
  172.  
  173.     hwndMLE = WinCreateWindow(hwnd, WC_MLE, (PSZ)NULL,
  174.                               MLS_HSCROLL | MLS_VSCROLL | WS_VISIBLE,
  175.                               rcl.xLeft,  rcl.yBottom,
  176.                               rcl.xRight, rcl.yTop,
  177.                               hwnd, HWND_TOP, ID_MLE, NULL, NULL);
  178.     if(!hwndMLE)
  179.         return (MRESULT)TRUE;
  180.  
  181.     /*
  182.      * Set default colors
  183.      */
  184.  
  185.     WinSendMsg(hwndMLE, MLM_SETTEXTCOLOR, MPFROMLONG(lClrDefaultForeground),
  186.                                                                         NULL);
  187.     WinSendMsg(hwndMLE, MLM_SETBACKCOLOR, MPFROMLONG(lClrDefaultBackground),
  188.                                                                         NULL);
  189.     WinSendMsg(hwndMLE, MLM_RESETUNDO, NULL, NULL);
  190.  
  191.     /* return FALSE to continue window creation, TRUE to abort it */
  192.     return (MRESULT)FALSE;
  193.  
  194.     /*
  195.      *  This routine currently doesn't use the mp1 and mp2 parameters so
  196.      *  it is referenced here to prevent an 'Unreferenced Parameter'
  197.      *  warning at compile time
  198.      */
  199.     mp1;
  200.     mp2;
  201. }        /* End of InitMainWindow() */
  202.  
  203. /*********************************************************************
  204.  *  Name:  ExitProc
  205.  *
  206.  *  Description : Cleans up certain resources when the
  207.  *                application terminates
  208.  *
  209.  *  Concepts : Global resources, such as the main window an
  210.  *             message queue, are destroyed and any system
  211.  *             resources used are freed
  212.  *
  213.  *  API's : WinIsWindow
  214.  *          WinDestroyWindow
  215.  *          WinDestroyMsgQueue
  216.  *          WinTerminate
  217.  *          DosExitList
  218.  *
  219.  *  Parameters : USHORT -  termination code
  220.  *
  221.  *  Returns:  Returns EXLST_EXIT to the DosExitList handler
  222.  *
  223.  ****************************************************************/
  224. VOID APIENTRY ExitProc(USHORT usTermCode)
  225. {
  226.  /* destroy the main window if it exists                       */
  227.     if(WinIsWindow(hab, hwndMainFrame))
  228.         WinDestroyWindow(hwndMainFrame);
  229.  
  230.     /*
  231.      *      Any other system resources used
  232.      *      (e.g. memory or files) should be freed here
  233.      */
  234.  
  235.     WinDestroyMsgQueue(hmq);
  236.  
  237.     WinTerminate(hab);
  238.     /*
  239.      *clean up with
  240.      *our vdd
  241.      */
  242.     CloseVirtQueue();
  243.  
  244.     /*
  245.      * Termination complete
  246.      */
  247.     DosExitList(EXLST_EXIT, (PFNEXITLIST)ExitProc);
  248.  
  249.     /*
  250.      * This routine currently doesn't use the usTermCode parameter so
  251.      *  it is referenced here to prevent an 'Unreferenced Parameter'
  252.      *  warning at compile time
  253.      */
  254.     usTermCode;
  255. }                                  /*     End of ExitProc()           */
  256.