home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / OS2 / MEMSIZ.ZIP / OBJECT.C < prev    next >
C/C++ Source or Header  |  1991-10-17  |  13KB  |  354 lines

  1. /*************************************************************** OBJECT.C
  2.  *                                                                      *
  3.  *                    Object Processor Definitions                      *
  4.  *                                                                      *
  5.  ************************************************************************/
  6.  
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #include <os2.h>
  10. #include <stdlib.h>
  11.  
  12. #include "object.h"
  13.  
  14. #define TRUE  1
  15. #define FALSE 0
  16.  
  17. #define NOT !
  18. #define OR  ||
  19. #define AND &&
  20.  
  21.  
  22. /************************************************************************
  23.  *                                                                      *
  24.  *      Initialize PM Environment at program start                      *
  25.  *                                                                      *
  26.  ************************************************************************/
  27.  
  28. VOID ObjInitialize
  29. (
  30.   HAB *phAB,
  31.   HMQ *phMQ,
  32.   HELPINIT *pHelpInit,
  33.   HWND *phwndHelp
  34. )
  35. {
  36.  /***********************************************************************
  37.   * Initialize PM mode and create the application message queue.        *
  38.   ***********************************************************************/
  39.  
  40.   *phAB = WinInitialize ( 0 ) ;
  41.   *phMQ = WinCreateMsgQueue ( *phAB, 0 ) ;
  42.  
  43.  /***********************************************************************
  44.   * Register the general object class.                                  *
  45.   ***********************************************************************/
  46.  
  47.   WinRegisterClass ( *phAB, CLASS_OBJECT,
  48.     ObjMessageProcessor, CS_SIZEREDRAW | CS_MOVENOTIFY, sizeof(PVOID) ) ;
  49.  
  50.  /***********************************************************************
  51.   * If help instance to be set up, do it.                *
  52.   ***********************************************************************/
  53.  
  54.   if ( pHelpInit )
  55.   {
  56.     *phwndHelp = WinCreateHelpInstance ( *phAB, pHelpInit ) ;
  57.  
  58.     if ( *phwndHelp == NULL )
  59.     {
  60.       WinMessageBox ( HWND_DESKTOP, HWND_DESKTOP,
  61.     "ERROR: Unable to create help instance.",
  62.     "Window", 0, MB_ENTER ) ;
  63.     }
  64.   }
  65. }
  66.  
  67. /************************************************************************
  68.  *                                                                      *
  69.  *      Create an Object                                                *
  70.  *                                                                      *
  71.  ************************************************************************/
  72.  
  73. HWND ObjCreateObject
  74. (
  75.   HWND hwndOwner,
  76.   SHORT idWindow,
  77.   PCLASS pClass,
  78.   PCHAR szTitle,
  79.   HWND hwndHelp
  80. )
  81. {
  82.  /***********************************************************************
  83.   *                     Local Declarations                              *
  84.   ***********************************************************************/
  85.  
  86.   FRAMECDATA fcdata ;
  87.   HWND hwndClient ;
  88.   HWND hwndFrame = hwndOwner ;
  89.  
  90.  /***********************************************************************
  91.   * If called for, create a frame window.  Return if unable to do so.   *
  92.   ***********************************************************************/
  93.  
  94.   if ( pClass->fFrame )
  95.   {
  96.     fcdata.cb = sizeof(fcdata) ;
  97.     fcdata.flCreateFlags = pClass->FrameData.flCreateFlags ;
  98.     fcdata.hmodResources = pClass->FrameData.hmodResources ;
  99.     fcdata.idResources     = pClass->FrameData.idResources ;
  100.  
  101.     hwndFrame = WinCreateWindow ( hwndOwner, WC_FRAME, "",
  102.       pClass->FrameData.flStyle, 0, 0, 0, 0, hwndOwner, HWND_TOP,
  103.       idWindow, &fcdata, NULL ) ;
  104.  
  105.     if ( hwndFrame == NULL )
  106.     {
  107.       return ( NULL ) ;
  108.     }
  109.  
  110.     hwndOwner = hwndFrame ;
  111.     idWindow = FID_CLIENT ;
  112.   }
  113.  
  114.  /***********************************************************************
  115.   * Create client window.  If this fails, destroy frame and return.     *
  116.   ***********************************************************************/
  117.  
  118.   hwndClient = WinCreateWindow ( hwndOwner, CLASS_OBJECT, "",
  119.     pClass->flStyle, 0, 0, 0, 0, hwndOwner, HWND_BOTTOM,
  120.     idWindow, pClass, NULL ) ;
  121.  
  122.   if ( hwndClient == NULL )
  123.   {
  124.     if ( pClass->fFrame )
  125.     {
  126.       WinDestroyWindow ( hwndFrame ) ;
  127.     }
  128.     return ( NULL ) ;
  129.   }
  130.  
  131.  /***********************************************************************
  132.   * If window text specified, set it now.                *
  133.   ***********************************************************************/
  134.  
  135.   if ( szTitle )
  136.   {
  137.     WinSetWindowText ( pClass->fFrame ? hwndFrame : hwndClient, szTitle ) ;
  138.   }
  139.  
  140.  /***********************************************************************
  141.   * Associate the help instance with the frame window.            *
  142.   ***********************************************************************/
  143.  
  144.   WinSetWindowUShort ( pClass->fFrame ? hwndFrame : hwndClient, QWS_ID, idWindow ) ;
  145.   if ( hwndHelp )
  146.   {
  147.     WinAssociateHelpInstance ( hwndHelp, pClass->fFrame ? hwndFrame : hwndClient ) ;
  148.   }
  149.  
  150.  /***********************************************************************
  151.   * Return handle to frame.                                             *
  152.   ***********************************************************************/
  153.  
  154.   return ( pClass->fFrame ? hwndFrame : hwndClient ) ;
  155. }
  156.  
  157. /************************************************************************
  158.  *                                                                      *
  159.  *      Execute all Objects                                             *
  160.  *                                                                      *
  161.  ************************************************************************/
  162.  
  163. VOID ObjExecuteAll ( HAB hAB )
  164. {
  165.  /***********************************************************************
  166.   *                     Local Declarations                              *
  167.   ***********************************************************************/
  168.  
  169.   QMSG qmsg ;
  170.  
  171.  /***********************************************************************
  172.   * Wait for and process messages to the window's queue.  Terminate     *
  173.   *   when the WM_QUIT message is received.                             *
  174.   ***********************************************************************/
  175.  
  176.   while ( WinGetMsg ( hAB, &qmsg, NULL, 0, 0 ) )
  177.   {
  178.     WinDispatchMsg ( hAB, &qmsg ) ;
  179.   }
  180. }
  181.  
  182. /************************************************************************
  183.  *                                                                      *
  184.  *      Destroy a generalized Object                                    *
  185.  *                                                                      *
  186.  ************************************************************************/
  187.  
  188. VOID ObjDestroy ( HWND hwnd )
  189. {
  190.  /***********************************************************************
  191.   * Destroy window.                                                     *
  192.   ***********************************************************************/
  193.  
  194.   WinDestroyWindow ( hwnd ) ;
  195. }
  196.  
  197. /************************************************************************
  198.  *                                                                      *
  199.  *      Clean up PM Environment prior to program completion.            *
  200.  *                                                                      *
  201.  ************************************************************************/
  202.  
  203. VOID ObjCleanup ( HAB hAB, HMQ hMQ, HWND hwndHelp )
  204. {
  205.  /***********************************************************************
  206.   * If help instance has been created, get rid of it.            *
  207.   ***********************************************************************/
  208.  
  209.   if ( hwndHelp )
  210.   {
  211.     WinDestroyHelpInstance ( hwndHelp ) ;
  212.   }
  213.  
  214.  /***********************************************************************
  215.   * Discard all that was requested of the system and terminate.         *
  216.   ***********************************************************************/
  217.  
  218.   WinDestroyMsgQueue ( hMQ ) ;
  219.   WinTerminate ( hAB ) ;
  220. }
  221.  
  222. /************************************************************************
  223.  *                                                                      *
  224.  *      General Object Procedure - Process messages to object           *
  225.  *                                                                      *
  226.  ************************************************************************/
  227.  
  228. MRESULT EXPENTRY ObjMessageProcessor
  229. (
  230.   HWND hwnd,
  231.   USHORT msg,
  232.   MPARAM mp1,
  233.   MPARAM mp2
  234. )
  235. {
  236.  /***********************************************************************
  237.   *                             Declarations                            *
  238.   ***********************************************************************/
  239.  
  240.   USHORT cNumberLeft ;
  241.   MRESULT mr ;
  242.   PCLASS pClass ;
  243.   PVOID pData ;
  244.   PMETHOD pMethod ;
  245.   POBJECT pObject ;
  246.   SEL selData ;
  247.   SEL selObject ;
  248.  
  249.  /***********************************************************************
  250.   * Perform pre-processing of WM_CREATE message.                        *
  251.   ***********************************************************************/
  252.  
  253.   if ( msg == WM_CREATE )
  254.   {
  255.     pClass = (PCLASS) PVOIDFROMMP ( mp1 ) ;
  256.     DosAllocSeg ( sizeof(OBJECT), &selObject, SEG_NONSHARED ) ;
  257.     DosAllocSeg ( pClass->cDataSize, &selData, SEG_NONSHARED ) ;
  258.     pObject = MAKEP ( selObject, 0 ) ;
  259.     pObject->selObject = selObject ;
  260.     pObject->selData = selData ;
  261.     pObject->pClass = pClass ;
  262.     WinSetWindowPtr ( hwnd, 0, pObject ) ;
  263.   }
  264.  
  265.  /***********************************************************************
  266.   * Get object's class information and private data pointer.            *
  267.   ***********************************************************************/
  268.  
  269.   pObject = (POBJECT) WinQueryWindowPtr ( hwnd, 0 ) ;
  270.   pData = MAKEP ( pObject->selData, 0 ) ;
  271.   pClass = pObject->pClass ;
  272.  
  273.  /***********************************************************************
  274.   * Process messages according to object's class method table.          *
  275.   ***********************************************************************/
  276.  
  277.   pMethod = pClass->pMethods ;
  278.   cNumberLeft = pClass->cMethods ;
  279.   while ( ( cNumberLeft ) AND ( pMethod->Action != msg ) )
  280.   {
  281.     pMethod ++ ;
  282.     cNumberLeft -- ;
  283.   }
  284.  
  285.   if ( cNumberLeft )
  286.   {
  287.     mr = pMethod->pFunction ( hwnd, msg, mp1, mp2, pData ) ;
  288.   }
  289.   else
  290.   {
  291.     mr = pClass->BaseObjectProcessor ( hwnd, msg, mp1, mp2 ) ;
  292.   }
  293.  
  294.  /***********************************************************************
  295.   * Perform post-processing of WM_DESTROY message.                      *
  296.   ***********************************************************************/
  297.  
  298.   if ( msg == WM_DESTROY )
  299.   {
  300.     DosFreeSeg ( pObject->selData ) ;
  301.     DosFreeSeg ( pObject->selObject ) ;
  302.   }
  303.  
  304.  /***********************************************************************
  305.   * Return result from message processor.                               *
  306.   ***********************************************************************/
  307.  
  308.   return ( mr ) ;
  309. }
  310.  
  311. /************************************************************************
  312.  *                                                                      *
  313.  *      Generalized Message Processor                                   *
  314.  *                                                                      *
  315.  ************************************************************************/
  316.  
  317. MRESULT EXPENTRY GeneralMessageProcessor
  318.   HWND hwnd, 
  319.   USHORT msg, 
  320.   MPARAM mp1, 
  321.   MPARAM mp2,
  322.   PMETHOD pMethods,
  323.   SHORT cMethods,
  324.   MRESULT (EXPENTRY *pDefaultMessageProcessor) (HWND,USHORT,MPARAM,MPARAM),
  325.   PVOID pData
  326. )
  327. {
  328.  /***********************************************************************
  329.   * Search class method table for message to be processed.              *
  330.   ***********************************************************************/
  331.  
  332.   while ( ( cMethods ) AND ( pMethods->Action != msg ) )
  333.   {
  334.     pMethods ++ ;
  335.     cMethods -- ;
  336.   }
  337.  
  338.  /***********************************************************************
  339.   * If message was found, return through specified processing function. *
  340.   ***********************************************************************/
  341.  
  342.   if ( cMethods )
  343.   {
  344.     return ( pMethods->pFunction ( hwnd, msg, mp1, mp2, pData ) ) ;
  345.   }
  346.  
  347.  /***********************************************************************
  348.   * If not, return through the default message processing function.     *
  349.   ***********************************************************************/
  350.  
  351.   return ( pDefaultMessageProcessor ( hwnd, msg, mp1, mp2 ) ) ;
  352. }
  353.