home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / drgwps.zip / commwin.c < prev    next >
C/C++ Source or Header  |  1993-08-02  |  22KB  |  512 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  commwin.c              AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  08-01-93                                           *
  5.  *                                                                   *
  6.  * DESCRIPTION:                                                      *
  7.  *                                                                   *
  8.  *   This module contains the code for the object window used by the *
  9.  *   agent object to communicate with the PM window.                 *
  10.  *                                                                   *
  11.  * CALLABLE FUNCTIONS:                                               *
  12.  *                                                                   *
  13.  *   CommwinCreate                                                   *
  14.  *   CommwinDestroy                                                  *
  15.  *                                                                   *
  16.  * HISTORY:                                                          *
  17.  *                                                                   *
  18.  *  08-01-93 - Started coding.                                       *
  19.  *                                                                   *
  20.  *  Rick Fishman                                                     *
  21.  *  Code Blazers, Inc.                                               *
  22.  *  4113 Apricot                                                     *
  23.  *  Irvine, CA. 92720                                                *
  24.  *  CIS ID: 72251,750                                                *
  25.  *                                                                   *
  26.  *********************************************************************/
  27.  
  28. #pragma strings(readonly)
  29.  
  30. /*********************************************************************/
  31. /*------- Include relevant sections of the OS/2 header files --------*/
  32. /*********************************************************************/
  33.  
  34. #define  INCL_DOSPROCESS
  35. #define  INCL_WINERRORS
  36. #define  INCL_WINFRAMEMGR
  37. #define  INCL_WINPROGRAMLIST
  38. #define  INCL_WINWINDOWMGR
  39. #define  INCL_WINWORKPLACE
  40.  
  41. /**********************************************************************/
  42. /*----------------------------- INCLUDES -----------------------------*/
  43. /**********************************************************************/
  44.  
  45. #include <os2.h>
  46. #include <process.h>
  47. #include <stdarg.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51.  
  52. #pragma info( none )
  53. #include "drgagent.ih"
  54. #pragma info( restore )
  55.  
  56. #include "wppgm.h"
  57. #include "common.h"
  58.  
  59. /*********************************************************************/
  60. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  61. /*********************************************************************/
  62.  
  63. #define THREAD_STACKSIZE    58000       // Stacksize for secondary thread
  64.  
  65. #define INSTDATA( hwnd )    ((PINSTANCE) WinQueryWindowPtr( hwnd, 0 ))
  66.  
  67. #define ANCHOR( hwnd )      (WinQueryAnchorBlock( hwnd ))
  68. #define HWNDERR( hwnd )     (ERRORIDERROR( WinGetLastError( ANCHOR(hwnd) ) ))
  69. #define HABERR( hab )       (ERRORIDERROR( WinGetLastError( hab ) ))
  70.  
  71. /**********************************************************************/
  72. /*---------------------------- STRUCTURES ----------------------------*/
  73. /**********************************************************************/
  74.  
  75. typedef struct _INSTANCE
  76. {
  77.     ULONG cb;
  78.     M_DrgAgent *somClass;
  79.     DrgAgent   *somObject;
  80.     HWND       hwndDrag;
  81.  
  82. } INSTANCE, *PINSTANCE;
  83.  
  84. /**********************************************************************/
  85. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  86. /**********************************************************************/
  87.  
  88. void DragInfoRequest( ULONG ulItemID, HWND hwndRequestor );
  89. MRESULT FormatDragItem( HWND hwndObject, PDRAGITEM pDragItem );
  90. void SetSomObject( HWND hwndObject, HOBJECT hObj );
  91. MRESULT BeenDroppedOn( HWND hwndObject, SOMAny *somObject );
  92. extern VOID _Optlink CommThread( void * );
  93.  
  94. FNWP wpComm;
  95.  
  96. /**********************************************************************/
  97. /*------------------------ GLOBAL VARIABLES --------------------------*/
  98. /**********************************************************************/
  99.  
  100. HWND hwndComm;
  101.  
  102. /**********************************************************************/
  103. /*-------------------------- CommwinCreate ---------------------------*/
  104. /*                                                                    */
  105. /*  CREATE THE OBJECT WINDOW THAT WILL COMMUNICATE WITH PM WINDOWS.   */
  106. /*                                                                    */
  107. /*  PARMS: pointer to class object                                    */
  108. /*                                                                    */
  109. /*  NOTES: The object window is put on a separate thread because the  */
  110. /*         thread that we are now on is transitory and could be       */
  111. /*         destroyed at anytime without us knowing about it.          */
  112. /*                                                                    */
  113. /*         Remember - we're in WPS-land now!                          */
  114. /*                                                                    */
  115. /*  RETURNS: nothing                                                  */
  116. /*                                                                    */
  117. /*--------------------------------------------------------------------*/
  118. /**********************************************************************/
  119. void CommwinCreate( void *somClass )
  120. {
  121.     if( -1 == _beginthread( CommThread, NULL, THREAD_STACKSIZE, somClass ) )
  122.     {
  123.         DosBeep( 1000, 1000 );
  124.         printf( "_beginthread failed in CommwinCreate\n" );
  125.     }
  126. }
  127.  
  128. const char pszWindowClass[] = "CommWindow";
  129.  
  130. extern VOID _Optlink CommThread( void *somClassIn )
  131. {
  132.     PINSTANCE pi;
  133.     HAB       hab = WinInitialize( 0 );
  134.     HMQ       hmq = WinCreateMsgQueue( hab, 0 );
  135.     QMSG      qmsg;
  136.  
  137.     pi = (PINSTANCE) malloc( sizeof( INSTANCE ) );
  138.  
  139.     if( pi )
  140.     {
  141.         pi->cb = sizeof( INSTANCE );
  142.         pi->somClass = somClassIn;
  143.         pi->somObject = NULL;
  144.  
  145.         WinRegisterClass( hab, pszWindowClass, wpComm, 0, sizeof( PVOID ) );
  146.  
  147.         hwndComm = WinCreateWindow( HWND_OBJECT, pszWindowClass, NULL, 0, 0, 0,
  148.                                     0, 0, NULLHANDLE, HWND_TOP, 1, pi, NULL );
  149.         if( hwndComm )
  150.         {
  151.             while( WinGetMsg( hab, &qmsg, NULLHANDLE, 0, 0 ) )
  152.                 WinDispatchMsg( hab, &qmsg );
  153.  
  154.             WinDestroyWindow( hwndComm );
  155.         }
  156.         else
  157.             Msg( "WinCreateWindow failed with rc(%X)", HABERR( hab ) );
  158.     }
  159.     else
  160.         Msg( "Out of memory in CommThread!" );
  161.  
  162.     WinDestroyMsgQueue( hmq );
  163.     WinTerminate( hab );
  164.  
  165.     _endthread();
  166. }
  167.  
  168.  
  169. /**********************************************************************/
  170. /*------------------------- CommwinDestroy ---------------------------*/
  171. /*                                                                    */
  172. /*  DESTROY THE OBJECT WINDOW THAT COMMUNICATES WITH PM WINDOWS.      */
  173. /*                                                                    */
  174. /*  PARMS: nothing                                                    */
  175. /*                                                                    */
  176. /*  NOTES:                                                            */
  177. /*                                                                    */
  178. /*  RETURNS: nothing                                                  */
  179. /*                                                                    */
  180. /*--------------------------------------------------------------------*/
  181. /**********************************************************************/
  182. void CommwinDestroy()
  183. {
  184.     WinPostMsg( hwndComm, WM_QUIT, NULL, NULL );
  185. }
  186.  
  187.  
  188. /**********************************************************************/
  189. /*----------------------------- wpComm -------------------------------*/
  190. /*                                                                    */
  191. /*  COMM WINDOW PROCEDURE                                             */
  192. /*                                                                    */
  193. /*  PARMS: standard winproc parms                                     */
  194. /*                                                                    */
  195. /*  NOTES:                                                            */
  196. /*                                                                    */
  197. /*  RETURNS: standard winproc return value                            */
  198. /*                                                                    */
  199. /*--------------------------------------------------------------------*/
  200. /**********************************************************************/
  201. MRESULT EXPENTRY wpComm( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  202. {
  203.     switch( msg )
  204.     {
  205.         case WM_CREATE:
  206.         {
  207.             PINSTANCE pi = (PINSTANCE) mp1;
  208.  
  209.             WinSetWindowPtr( hwnd, 0, pi );
  210.  
  211.             // Create the shared-memory heap used to allocate chunks of memory
  212.             // that can be used to pass information between the WPS object and
  213.             // the PM window.
  214.  
  215.             HeapCreate();
  216.  
  217.             // Create the shared memory that will contain information shared by
  218.             // both the PM window and the WPS object.
  219.  
  220.             CommdataCreate();
  221.  
  222.             // Set our window handle into the shared memory so the PM window
  223.             // can use it.
  224.  
  225.             if( pCommData )
  226.                 pCommData->hwndComm = hwnd;
  227.             break;
  228.         }
  229.  
  230.         case WM_DESTROY:
  231.         {
  232.             PINSTANCE pi = INSTDATA( hwnd );
  233.  
  234.             if( pi )
  235.             {
  236.                 free( pi );
  237.                 WinSetWindowPtr( hwnd, 0, NULL );
  238.             }
  239.  
  240.             // Destroy the shared memory and shared heap.
  241.  
  242.             CommdataDestroy();
  243.             HeapDestroy();
  244.             break;
  245.         }
  246.  
  247.         case UM_FORMAT_DRAGITEM:
  248.  
  249.             // This is sent by the PM window to allow us to format a DRAGITEM
  250.             // structure. It has preallocated the DRAGITEM struct in the
  251.             // shared heap.
  252.  
  253.             return FormatDragItem( hwnd, (PDRAGITEM) mp1 );
  254.  
  255.         case UM_SET_HOBJECT:
  256.  
  257.             // This is sent by the PM window so that we know the HOBJECT that
  258.             // we are communicating with.
  259.  
  260.             SetSomObject( hwnd, (HOBJECT) mp1 );
  261.             return 0;
  262.  
  263.         case UM_DRAGINFO_REQUEST:
  264.  
  265.             // This is sent to us by the PM window when a WPS object is dropped
  266.             // on it and it needs more info about that object.
  267.  
  268.             DragInfoRequest( (ULONG) mp1, (HWND) mp2 );
  269.             return 0;
  270.  
  271.         case UM_STARTING_DRAG:
  272.  
  273.             // The PM window is giving us its window handle so we can use it
  274.             // if its icon is dropped on a WPS object and we need to inform it.
  275.  
  276.             INSTDATA( hwnd )->hwndDrag = (HWND) mp1;
  277.             return 0;
  278.  
  279.         case UM_DROP:
  280.  
  281.             // This is sent to us from the WPS object when it gets a
  282.             // wpDroppedOnObject method call.
  283.  
  284.             return BeenDroppedOn( hwnd, (SOMAny *) mp1 );
  285.  
  286.         case UM_DROP_ENDCONVERSATION:
  287.         {
  288.             ULONG  ulItemID = LONGFROMMP( mp1 );
  289.             SOMAny *somObject = OBJECT_FROM_PREC( ulItemID );
  290.  
  291.             // This is sent to us from the PM window when it is done with a
  292.             // drop.
  293.  
  294.             _wpEndConversation( somObject, ulItemID, DMFL_TARGETSUCCESSFUL );
  295.             return 0;
  296.         }
  297.     }
  298.  
  299.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  300. }
  301.  
  302. /**********************************************************************/
  303. /*------------------------- DragInfoRequest --------------------------*/
  304. /*                                                                    */
  305. /*  A WINDOW HAS REQUESTED DRAG INFORMATION. SUPPLY IT.               */
  306. /*                                                                    */
  307. /*  PARMS: DRAGITEM.ulItemID,                                         */
  308. /*         window handle of the window that requested the info        */
  309. /*                                                                    */
  310. /*  NOTES: Populate a shared memory block with information about the  */
  311. /*         DRAGITEM whose ulItemID was passed to us. We allocate the  */
  312. /*         block of memory from the shared heap. The PM window will   */
  313. /*         free that block when it is done with it.                   */
  314. /*                                                                    */
  315. /*         We only get a lot of information if this is a WPProgram    */
  316. /*         object. Obviously you can get more information on other    */
  317. /*         objects. This is just used to show how to do it.           */
  318. /*                                                                    */
  319. /*  RETURNS: nothing                                                  */
  320. /*                                                                    */
  321. /*--------------------------------------------------------------------*/
  322. /**********************************************************************/
  323. void DragInfoRequest( ULONG ulItemID, HWND hwndRequestor )
  324. {
  325.     SOMAny *somObject = OBJECT_FROM_PREC( ulItemID );
  326.  
  327.     if( somObject )
  328.     {
  329.         POBJECTDATA pObjData = HeapAlloc( sizeof( OBJECTDATA ) );
  330.  
  331.         if( pObjData )
  332.         {
  333.             memset( pObjData, 0, sizeof( OBJECTDATA ) );
  334.             strcpy( pObjData->szClassName,
  335.                     _somGetName( _somGetClass( somObject ) ) );
  336.             strcpy( pObjData->szTitle, _wpQueryTitle( somObject ) );
  337.             if( _somRespondsTo( somObject,
  338.                                 SOM_IdFromString( "wpQueryProgDetails" ) ) )
  339.             {
  340.                 PPROGDETAILS ppd = NULL;
  341.                 ULONG        cbProgDetails = 0;
  342.  
  343.                 _wpQueryProgDetails( somObject, ppd, &cbProgDetails );
  344.                 ppd = (PPROGDETAILS) SOMMalloc( cbProgDetails );
  345.                 if( cbProgDetails && ppd &&
  346.                     _wpQueryProgDetails( somObject, ppd, &cbProgDetails ) )
  347.                 {
  348.                     pObjData->fPgmObject = TRUE;
  349.                     if( ppd->pszExecutable )
  350.                         strcpy( pObjData->szExeName,    ppd->pszExecutable );
  351.                     if( ppd->pszParameters )
  352.                         strcpy( pObjData->szParms,      ppd->pszParameters );
  353.                     if( ppd->pszStartupDir )
  354.                         strcpy( pObjData->szStartupDir, ppd->pszStartupDir );
  355.                 }
  356.                 else
  357.                     pObjData->fPgmObject = FALSE;
  358.  
  359.                 if( ppd )
  360.                     SOMFree( ppd );
  361.             }
  362.             else
  363.                 pObjData->fPgmObject = FALSE;
  364.             WinPostMsg( hwndRequestor, UM_DRAGINFO, MPFROMP( pObjData ), NULL );
  365.         }
  366.         else
  367.             Msg( "HeapAlloc failed in DragInfoRequest!\n" );
  368.     }
  369.     else
  370.         Msg( "No somObject supplied!" );
  371. }
  372.  
  373. /**********************************************************************/
  374. /*------------------------- FormatDragItem ---------------------------*/
  375. /*                                                                    */
  376. /*  A WINDOW HAS REQUESTED THAT WE FORMAT A DRAG ITEM. BASICALLY IT   */
  377. /*  WANTS THE ulItemID FILLED IN.                                     */
  378. /*                                                                    */
  379. /*  PARMS: object window handle,                                      */
  380. /*         pointer to a pre-allocated DRAGITEM structure              */
  381. /*                                                                    */
  382. /*  NOTES:                                                            */
  383. /*                                                                    */
  384. /*  RETURNS: an MRESULT (this was a WinSent message                   */
  385. /*                                                                    */
  386. /*--------------------------------------------------------------------*/
  387. /**********************************************************************/
  388. MRESULT FormatDragItem( HWND hwndObject, PDRAGITEM pDragItem )
  389. {
  390.     PINSTANCE pi = INSTDATA( hwndObject );
  391.     BOOL      fSuccess = TRUE;
  392.  
  393.     if( pi->somObject )
  394.     {
  395.         if( !_wpFormatDragItem( pi->somObject, pDragItem ) )
  396.         {
  397.             fSuccess = FALSE;
  398.             Msg( "wpFormatDragitem failed!" );
  399.         }
  400.     }
  401.     else
  402.     {
  403.         fSuccess = FALSE;
  404.         Msg( "FormatDragItem somObject not set at this point!" );
  405.     }
  406.  
  407.     return (MRESULT) fSuccess;
  408. }
  409.  
  410. /**********************************************************************/
  411. /*-------------------------- SetSomObject ----------------------------*/
  412. /*                                                                    */
  413. /*  SET THE WINDOW WORD SOMOBJECT POINTER FROM AN HOBJECT SENT TO US  */
  414. /*  VIA A PM MESSAGE FROM A PM WINDOW.                                */
  415. /*                                                                    */
  416. /*  PARMS: object window handle,                                      */
  417. /*         HOBJECT of an object created by the PM window.             */
  418. /*                                                                    */
  419. /*  NOTES:                                                            */
  420. /*                                                                    */
  421. /*  RETURNS: nothing                                                  */
  422. /*                                                                    */
  423. /*--------------------------------------------------------------------*/
  424. /**********************************************************************/
  425. void SetSomObject( HWND hwndObject, HOBJECT hObj )
  426. {
  427.     PINSTANCE    pi = INSTDATA( hwndObject );
  428.     DrgAgentData *somThis;
  429.  
  430.     pi->somObject = _wpclsQueryObject( pi->somClass, hObj );
  431.  
  432.     somThis = DrgAgentGetData( pi->somObject );
  433.  
  434.     _hwndComm = hwndObject;
  435. }
  436.  
  437. /**********************************************************************/
  438. /*-------------------------- BeenDroppedOn ---------------------------*/
  439. /*                                                                    */
  440. /*  WE'VE BEEN DROPPED ON ANOTHER OBJECT. PROCESS IT.                 */
  441. /*                                                                    */
  442. /*  PARMS: object window handle,                                      */
  443. /*         the somObject pointer that we were dropped on              */
  444. /*                                                                    */
  445. /*  NOTES:                                                            */
  446. /*                                                                    */
  447. /*  RETURNS: return message to WPS object                             */
  448. /*                                                                    */
  449. /*--------------------------------------------------------------------*/
  450. /**********************************************************************/
  451. MRESULT BeenDroppedOn( HWND hwndObject, SOMAny *somObject )
  452. {
  453.     PINSTANCE pi = INSTDATA( hwndObject );
  454.     HOBJECT   hObj = _wpQueryHandle( somObject );
  455.  
  456.     // We get this from the WPS object and we need to route it to the PM
  457.     // window after translating the somObject pointer to an HOBJECT. PM
  458.     // windows can deal with HOBJECT's but not with somObjects.
  459.  
  460.     WinSendMsg( pi->hwndDrag, UM_DROP, MPFROMLONG( hObj ), NULL );
  461.  
  462.     return (MRESULT) TRUE;
  463. }
  464.  
  465. /**********************************************************************/
  466. /*------------------------------- Msg --------------------------------*/
  467. /*                                                                    */
  468. /*  DISPLAY A MESSAGE TO THE USER IN A PM MESSAGE BOX.                */
  469. /*                                                                    */
  470. /*  PARMS: standard printf parameters                                 */
  471. /*                                                                    */
  472. /*  NOTES:                                                            */
  473. /*                                                                    */
  474. /*  RETURNS: nothing                                                  */
  475. /*                                                                    */
  476. /*--------------------------------------------------------------------*/
  477. /**********************************************************************/
  478.  
  479. #define MESSAGE_SIZE    1024
  480.  
  481. void Msg( PSZ szFormat,... )
  482. {
  483.     PSZ     szMsg;
  484.     va_list argptr;
  485.  
  486.     if( (szMsg = (PSZ) malloc( MESSAGE_SIZE )) == NULL )
  487.     {
  488.         DosBeep( 1000, 1000 );
  489.         return;
  490.     }
  491.  
  492.     va_start( argptr, szFormat );
  493.  
  494.     vsprintf( szMsg, szFormat, argptr );
  495.  
  496.     va_end( argptr );
  497.  
  498.     szMsg[ MESSAGE_SIZE - 1 ] = 0;
  499.  
  500.     WinAlarm( HWND_DESKTOP, WA_WARNING );
  501.  
  502.     WinMessageBox(  HWND_DESKTOP, HWND_DESKTOP, szMsg,
  503.                     "DrgPmWps DragAgent object",
  504.                     1, MB_OK | MB_MOVEABLE );
  505.  
  506.     free( szMsg );
  507. }
  508.  
  509. /*************************************************************************
  510.  *                     E N D     O F     S O U R C E                     *
  511.  *************************************************************************/
  512.