home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / S12659.ZIP / CLIENT.C next >
Text File  |  1989-01-09  |  13KB  |  289 lines

  1. #include "os2.h"
  2. #include "st.h"
  3. #include "client.h"
  4. #include "stdio.h"
  5. #include "string.h"
  6. #include "stdlib.h"
  7.  
  8. /*
  9.  *  This module contains the winprocs for the Graphics Exchange client
  10.  *  application window as well as the DDE anchor window and the DDE
  11.  *  conversation windows.  The support routine, enumhwnd, which is
  12.  *  used to locate DDE conversational windows is implemented here.
  13.  */
  14.  
  15. HWND DDEanchorHWND;    /* handle of DDE anchor window     */
  16. static USHORT winDDEid = ID_DDEANCHOR + 1;
  17.  
  18. /*
  19.  * function enumhwnd(anchor, id)
  20.  *
  21.  * Given a handle of a DDE anchor window and a DD conversation ID,
  22.  * locate the DDE window for that conversation.
  23.  */
  24.  
  25. HWND enumhwnd(anchor, id)
  26. HWND    anchor;          /* handle of the DDE anchor window to be searched */
  27. USHORT  id;              /* DDE conversation to be located                 */
  28. {
  29. HENUM   henum;           /* enumeration handle                             */
  30. HWND    hwndenum;        /* current enumeration window handle              */
  31. HWND    rethwnd;         /* located window handle to be returned           */
  32.  
  33.        rethwnd = (HWND)NULL;
  34.        henum = WinBeginEnumWindows(anchor);
  35.        while (hwndenum = WinGetNextWindow(henum)) {
  36.                if(id == LOUSHORT(hwndenum)){
  37.                    rethwnd = hwndenum;
  38.                    WinLockWindow(hwndenum,FALSE);
  39.                    break;
  40.                }
  41.            WinLockWindow(hwndenum,FALSE);
  42.        }
  43.        WinEndEnumWindows(henum);
  44.        return(rethwnd);
  45. }
  46.  
  47. /*
  48.  *  Function ClientWndProc
  49.  *
  50.  *  This function processes the system and application menu messages
  51.  *  as well as messages from the graphics control child window.  A
  52.  *  conversation_shutting_down (APPM_CONV_CLOSE) message is processed
  53.  *  from the conversational DDE winproc by deleting that
  54.  *  conversation's picture from the graphics control.  The WM_CLOSE message
  55.  *  is processed by terminating ALL conversations related to this
  56.  *  instance of this application.
  57.  */
  58.  
  59. MRESULT APIENTRY ClientWndProc(hwnd,message,lParam1,lParam2)
  60. HWND    hwnd;
  61. USHORT  message;
  62. MPARAM  lParam1;
  63. MPARAM  lParam2;
  64. {
  65. RECTL     rect;           /* window rectangle work space for positioning      */
  66. HPS       hPSW;           /* presentation space for this routine              */
  67. HENUM     henum;          /* enumeration handle                               */
  68. HWND      hwndenum;       /* current enumeration window handle                */
  69. HWND      tohwnd;         /* handle of message recipient                      */
  70. PDDESTRUCT DDEptr;        /* pointers for DDESTRUCT allocation                */
  71.  
  72.     switch (message){
  73.  
  74.         case WM_CREATE:  /* main window initialization - clear all window word memory */
  75.              WinSetWindowULong(hwnd, WW_CLOSE, 0L);
  76.              WinSetWindowULong(hwnd, WW_CONVCOUNT, 0L);
  77.              WinSetWindowULong(hwnd, WW_MASTER_FLAGS, 0L);
  78.              DDEanchorHWND = WinCreateWindow(hwnd, (PSZ)"DDE_Win_P", (PSZ)NULL,
  79.                                     NULL, 0, 0, 0, 0, hwnd, HWND_TOP,
  80.                                     ID_DDEANCHOR, (PVOID)NULL, (PVOID)NULL);
  81.              break;
  82.  
  83.         case WM_PAINT:  /* repaint the window  */
  84.              hPSW = WinBeginPaint(hwnd,NULL,(PRECTL)&rect);
  85.              WinQueryWindowRect( hwnd, &rect );
  86.              WinFillRect( hPSW, &rect, CLR_WHITE);
  87.              WinEndPaint( hPSW );
  88.              break;
  89.  
  90.           case WM_SIZE: /* adjust the window size  */
  91.              WinQueryWindowRect(hwnd, &rect);
  92.                  if (WinWindowFromID(hwnd,ID_GRAPHICS1)) {
  93.                     WinSetWindowPos(WinWindowFromID(hwnd,ID_GRAPHICS1),HWND_TOP,0,0,LOUSHORT(rect.xRight-rect.xLeft),
  94.                        LOUSHORT((rect.yTop-rect.yBottom)),SWP_SIZE|SWP_MOVE);
  95.                  }
  96.              break;
  97.  
  98.         case WM_CLOSE:  /* send WM_DDE_UNADVISE, shutdown all conversations for this applications, then quit */
  99.              if (WinQueryWindowULong(hwnd, WW_CONVCOUNT)) {
  100.                  WinSetWindowULong(hwnd, WW_CLOSE, WinQueryWindowULong(hwnd, WW_CLOSE) | WIN_CLOSING_FLAG);
  101.                  henum = WinBeginEnumWindows(DDEanchorHWND);
  102.                  while (hwndenum = WinGetNextWindow(henum)) {
  103.                          WinSetWindowULong(hwndenum, WW_CONV_FLAGS,
  104.                                             WinQueryWindowULong(hwndenum, WW_CONV_FLAGS) | WIN_TERM_FLAG);
  105.                          tohwnd = (HWND)WinQueryWindowULong(hwndenum, WW_CONV_HWND);
  106.                          DDEptr = st_DDE_Alloc(sizeof(DDESTRUCT) + strlen("Graphics") + 1, "DDEFMT_graphics_data");
  107.                          DDEptr->offszItemName = (USHORT)sizeof(DDESTRUCT);
  108.                          strcpy(DDES_PSZITEMNAME(DDEptr), "Graphics");
  109.                          WinDdePostMsg(tohwnd, hwndenum, WM_DDE_UNADVISE, DDEptr, TRUE);
  110.                          WinDdePostMsg(tohwnd, hwndenum, WM_DDE_TERMINATE, NULL, TRUE);
  111.                          WinLockWindow(hwndenum, FALSE);
  112.                  }
  113.                  WinEndEnumWindows(henum);
  114.              }
  115.              else {
  116.                  WinPostMsg(hwnd,WM_QUIT,0L,0L);  /*  quit if no conversations open  */
  117.              }
  118.              break;
  119.  
  120.          case APPM_CONV_CLOSE: /* decrement conversation count and delete picture  */
  121.              WinSetWindowULong(hwnd, WW_CONVCOUNT, WinQueryWindowULong(hwnd, WW_CONVCOUNT) - 1);
  122.              WinSendMsg(WinWindowFromID(hwnd,ID_GRAPHICS1),IC_DELETEITEM, (MPARAM)LOUSHORT(lParam1), (MPARAM)NULL);
  123.              if (WinQueryWindowULong(hwnd, WW_CLOSE) && !WinQueryWindowULong(hwnd, WW_CONVCOUNT)){
  124.                  WinPostMsg(hwnd,WM_QUIT,0L,0L);
  125.              }
  126.              break;
  127.  
  128.         case WM_DDE_INITIATE:  /* pass to DDE anchor window  */
  129.               WinPostMsg(DDEanchorHWND, message, lParam1, lParam2);
  130.              break;
  131.         default:
  132.             return(WinDefWindowProc(hwnd,message,lParam1,lParam2));
  133.  
  134.     }
  135.     return((MRESULT)0L);
  136. }
  137.  
  138. /*
  139.  *  Function ClientDDEWndProc
  140.  *
  141.  *  This function is the wndproc for the conversational DDE window
  142.  *  and processes the WM_DDE_DATA and WM_DDE_TERMINATE message.  The
  143.  *  WM_DDE_DATA message is processed by adding or replacing the picture
  144.  *  received from the server to the graphics control.  WM_DDE_TERMINATE
  145.  *  sends a WM_DDE_TERMINATE to the server, as APPM_CONV_CLOSE message
  146.  *  to the the client application, and then the conversational
  147.  *  window destroys itself.
  148.  */
  149.  
  150. MRESULT APIENTRY ClientDDEWndProc(hwnd,message,lParam1,lParam2)
  151. HWND    hwnd;
  152. USHORT  message;
  153. MPARAM  lParam1;
  154. MPARAM  lParam2;
  155. {
  156. PDDESTRUCT DDEstrptr, DDEstrPtrAck;  /* pointers for DDESTRUCT allocation */
  157. PGDEDATA   gde_ptr;                  /* pointer for picture data          */
  158.  
  159.  
  160.     DDEstrptr = (PDDESTRUCT)lParam2;
  161.  
  162.     switch (message){
  163.  
  164.         case WM_DDE_DATA:   /* process incoming graphics  */
  165.             gde_ptr = (PGDEDATA)DDES_PABDATA(DDEstrptr);
  166.             gde_ptr->hwnd_idItem = LOUSHORT(hwnd);
  167.  
  168.             if (DDEstrptr->fsStatus && DDE_FRESPONSE) {  /*  add if request  */
  169.                WinSendMsg(WinWindowFromID(WinQueryWindow(hwnd,QW_OWNER,FALSE),ID_GRAPHICS1),IC_INSERTITEM,
  170.                           MPFROMSHORT(ICM_END), (MPARAM)gde_ptr);
  171.             } else {                                     /* replace if advise */
  172.                WinSendMsg(WinWindowFromID(WinQueryWindow(hwnd,QW_OWNER,FALSE),ID_GRAPHICS1), IC_SETITEMSTRUCT,
  173.                                           MPFROMSHORT(gde_ptr->hwnd_idItem), (MPARAM)gde_ptr);
  174.             }
  175.  
  176.             if (DDEstrptr->fsStatus && DDE_FACKREQ) {
  177.                DDEstrPtrAck = st_DDE_Alloc(sizeof(DDESTRUCT) + strlen("Graphics") + 1, "DDEFMT_graphics_data");
  178.                DDEstrPtrAck->offszItemName = (USHORT)sizeof(DDESTRUCT);
  179.                DDEstrPtrAck->fsStatus |= DDE_FACK;
  180.                strcpy(DDES_PSZITEMNAME(DDEstrPtrAck), "Graphics");
  181.                WinDdePostMsg((HWND)lParam1, hwnd, (ULONG)WM_DDE_ACK, DDEstrPtrAck, TRUE);
  182.             }
  183.  
  184.             DosFreeSeg(PDDESTOSEL(DDEstrptr));
  185.  
  186.             break;
  187.  
  188.         case WM_DDE_TERMINATE:  /* post terminate to server, tell client, and die  */
  189.               if (!WinQueryWindowULong(WinQueryWindow(hwnd, QW_OWNER, FALSE), WW_CLOSE)) {
  190.                   WinDdePostMsg((HWND)lParam1, hwnd, WM_DDE_TERMINATE, NULL, TRUE);
  191.               }
  192.                WinPostMsg(WinQueryWindow(hwnd, QW_OWNER, FALSE),APPM_CONV_CLOSE, MPFROMLONG(hwnd), (MPARAM)NULL);
  193.                WinDestroyWindow(hwnd);
  194.  
  195.              break;
  196.  
  197.         case WM_DDE_ACK:  /* unadvise and terminate conversation if server can't process picture  */
  198.               if ((DDEstrptr->fsStatus && ~DDE_FACK) &&
  199.                   (DDEstrptr->fsStatus && DDE_FRESPONSE)) {
  200.                       DDEstrPtrAck = st_DDE_Alloc(sizeof(DDESTRUCT) + strlen("Graphics") + 1, "DDEFMT_graphics_data");
  201.                       DDEstrPtrAck->offszItemName = (USHORT)sizeof(DDESTRUCT);
  202.                       strcpy(DDES_PSZITEMNAME(DDEstrPtrAck), "Graphics");
  203.                       WinDdePostMsg((HWND)lParam1, hwnd, WM_DDE_UNADVISE, DDEstrPtrAck, TRUE);
  204.                       WinDdePostMsg((HWND)lParam1, hwnd, WM_DDE_TERMINATE, NULL, TRUE);
  205.               }
  206.              break;
  207.         default:
  208.             return(WinDefWindowProc(hwnd,message,lParam1,lParam2));
  209.  
  210.     }
  211.     return((MRESULT)0L);
  212. }
  213.  
  214. /*
  215.  *  Function ClientDDEParWndProc
  216.  *
  217.  *  This function is the wndproc for the anchor DDE window and
  218.  *  processes the initialization messages for the client on behalf
  219.  *  of the conversation DDE and the client main winprocs.  The DDE
  220.  *  advise and request for the client are processed here as part of
  221.  *  initialization processing.
  222.  */
  223.  
  224. MRESULT APIENTRY ClientDDEParWndProc(hwnd,message,lParam1,lParam2)
  225. HWND    hwnd;
  226. USHORT  message;
  227. MPARAM  lParam1;
  228. MPARAM  lParam2;
  229. {
  230. PDDESTRUCT DDEstrptr;
  231. PDDEINIT   DDEInitPtr;
  232. HWND       DDEconversationHWND;
  233. char       itoa_buf[24];
  234.  
  235.     DDEInitPtr = (PDDEINIT)lParam2;
  236.     switch (message){
  237.  
  238.         case WM_CREATE:  /* broadcast the initiate message  */
  239.              WinDdeInitiate(hwnd, "", "Graphics_Exchange");
  240.              break;
  241.  
  242.         case WM_DDE_INITIATE: /* reply with a initiate to specific server  */
  243.              if (hwnd != lParam1) {
  244.                  if ((!strcmp("Client", DDEInitPtr->pszAppName)) &&
  245.                     (!strcmp("Graphics_Exchange", DDEInitPtr->pszTopic))) {
  246.                      itoa((int)lParam1, itoa_buf, 10);
  247.                      WinDdeInitiate(hwnd, itoa_buf, "Graphics_Exchange");
  248.                  }
  249.              }
  250.              DosFreeSeg(PDDEITOSEL(DDEInitPtr));
  251.              break;
  252.  
  253.         case WM_DDE_INITIATEACK:  /* establish conversation with server  */
  254.              if( ((!strcmp("Client", DDEInitPtr->pszAppName)) &&
  255.                   (!strcmp("Graphics_Exchange", DDEInitPtr->pszTopic))) ||
  256.                 ((!strlen(DDEInitPtr->pszAppName)) &&
  257.                   (!strcmp("Graphics_Exchange", DDEInitPtr->pszTopic)))) {
  258.                  /* create a window for the conversation  -  child of DDEanchorHWND  */
  259.                  DDEconversationHWND = WinCreateWindow(hwnd, (PSZ)"DDE_Win", (PSZ)NULL,
  260.                                                       WS_VISIBLE, 0, 0, 0, 0,
  261.                                                       WinQueryWindow(hwnd, QW_PARENT, FALSE), HWND_TOP,
  262.                                                       ++winDDEid, (PVOID)NULL, (PVOID)NULL);
  263.                  WinSetWindowULong(DDEconversationHWND, WW_CONV_HWND, (ULONG)lParam1);
  264.                  WinSetWindowULong(WinQueryWindow(hwnd, QW_PARENT, FALSE), WW_CONVCOUNT,
  265.                                    WinQueryWindowULong(WinQueryWindow(hwnd, QW_PARENT, FALSE), WW_CONVCOUNT) + 1);
  266.  
  267.                  /* send a request for initial data */
  268.                  DDEstrptr = st_DDE_Alloc(sizeof(DDESTRUCT) + strlen("Graphics") + 1, "DDEFMT_graphics_data");
  269.                  DDEstrptr->offszItemName = (USHORT)sizeof(DDESTRUCT);
  270.                  strcpy(DDES_PSZITEMNAME(DDEstrptr), "Graphics");
  271.                  WinDdePostMsg((HWND)lParam1, DDEconversationHWND, (ULONG)WM_DDE_REQUEST, DDEstrptr, TRUE);
  272.  
  273.                  /* send an advise to subscribe to receive future data updates */
  274.                  DDEstrptr = st_DDE_Alloc(sizeof(DDESTRUCT) + strlen("Graphics") + 1, "DDEFMT_graphics_data");
  275.                  DDEstrptr->offszItemName = (USHORT)sizeof(DDESTRUCT);
  276.                  strcpy(DDES_PSZITEMNAME(DDEstrptr), "Graphics");
  277.                  WinDdePostMsg((HWND)lParam1, DDEconversationHWND, (ULONG)WM_DDE_ADVISE, DDEstrptr, TRUE);
  278.              }
  279.              /* free the memory */
  280.              DosFreeSeg(PDDEITOSEL(DDEInitPtr));
  281.              break;
  282.  
  283.         default:
  284.             return(WinDefWindowProc(hwnd,message,lParam1,lParam2));
  285.  
  286.     }
  287.     return((MRESULT)0L);
  288. }
  289.