home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 14 Text / 14-Text.zip / RB3774.ZIP / DMPHONE.ZIP / DMPHONE.C next >
Text File  |  1992-05-07  |  9KB  |  292 lines

  1. //**************************************************************************************** */
  2. //
  3. //  DMPHONE.C - a demo program by Alan Chambers - UK PS Technical Support, on residency
  4. //             at the ITSC, Boca Raton
  5. //
  6. //  Modified 6 May 1992 to work with GA code.
  7. //
  8. //
  9. //  This program is designed to be used with the DMCUST sample program.  A customer
  10. //  can be dragged onto the telephone icon displayed by this program, with the result
  11. //  that a series of beeps, reminiscent of touchtone dialling tones, are sounded.
  12. //
  13. //  The program illustrates the use of a private rendering mechanism, DRM_SHAREMEM, from
  14. //  the target's point of view.
  15. //
  16. //**************************************************************************************** */
  17. //
  18. //  The program displays a telephone icon as using the WC_STATIC class, then subclasses
  19. //  this static window to add function for drag/drop, moving the icon around the desktop,
  20. //  and providing a context (pop-up) menu.
  21. //
  22. /**************************************************************************************** */
  23.  
  24.  
  25. #define INCL_WIN
  26. #define INCL_GPI
  27. #define INCL_DOS
  28.  
  29. #include <os2.h>                        /* PM header file               */
  30. #include "dmphone.h"                      /* Resource symbolic identifiers*/
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <ctype.h>
  35. #include <memory.h>
  36.  
  37.  
  38. #define OBJECTDATASIZE 100
  39. #define DRAGXFERMEMNAME "\\SHAREMEM\\Drgphone.mem"
  40.  
  41.  
  42. typedef struct {
  43.                  char name[30];
  44.                  char address[100];
  45.                  char phone[15];
  46.                  int  credit_limit;
  47.                } 
  48.                  CUSTOMER, *PCUSTOMER;
  49.  
  50. typedef struct{
  51.                 RECORDCORE core;
  52.                 CUSTOMER cust;
  53.               }
  54.                 CONTRECORD, *PCONTRECORD;
  55.  
  56. MRESULT EXPENTRY PhoneSubclassProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  57. void mfbeep(ULONG, ULONG);
  58. void phonedial(PCHAR phone);
  59.  
  60.  
  61. HAB  hab;          
  62. HPOINTER hptrPostIcon, hptrArrow;
  63. PFNWP StaticWinProc;
  64. HWND hwndPhone,
  65.      hwndMenu;
  66.  
  67. INT main (VOID)
  68.   HMQ  hmq;                             /* Message queue handle         */
  69.   QMSG qmsg;                            /* Message from message queue   */
  70.  
  71.   hab = WinInitialize(0);
  72.  
  73.   hmq = WinCreateMsgQueue( hab, 0 );
  74.  
  75.   hptrPostIcon = WinLoadPointer(HWND_DESKTOP, 0, ID_MYPOINTER);
  76.   hptrArrow = WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);
  77.  
  78.   hwndPhone = WinCreateWindow(HWND_DESKTOP, WC_STATIC, "#9999",
  79.                                         WS_VISIBLE | SS_ICON,
  80.                                         10, 10, 64, 64, 
  81.                                         HWND_DESKTOP, HWND_TOP, ID_PHONEICON,
  82.                                         0, 0);
  83.  
  84.   StaticWinProc = WinSubclassWindow(hwndPhone, PhoneSubclassProc);
  85.  
  86.   hwndMenu =  WinLoadMenu(hwndPhone, (ULONG)NULL, RESID_MENU);
  87.  
  88.  
  89.   while(WinGetMsg(hab, &qmsg, 0L, 0, 0))       // message processing loop
  90.      WinDispatchMsg(hab, &qmsg);
  91.  
  92.  
  93.  
  94.   WinDestroyMsgQueue( hmq );             
  95.   WinTerminate( hab );                   
  96.  
  97.  
  98.  
  99. MRESULT EXPENTRY PhoneSubclassProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  100. {
  101.   SWP swp;
  102.   PDRAGTRANSFER pdxfer;
  103.   PCUSTOMER pxfercust;
  104.   PDRAGINFO pdinfo;
  105.   PDRAGITEM pditem;
  106.   ULONG rc;
  107.   static char IconText[20] = "Icon #";
  108.   char curDir[100];
  109.   char msgtext[100];
  110.   static BOOL button2down = FALSE;
  111.   static POINTS oldpos, newpos;
  112.   static BOOL moved = FALSE;
  113.                     
  114.  
  115.   switch( msg )
  116.   {
  117.      case DM_DRAGOVER:
  118.        pdinfo = (PDRAGINFO)mp1; 
  119.        DrgAccessDraginfo(pdinfo);
  120.        pditem = DrgQueryDragitemPtr(pdinfo, 0);
  121.        if(DrgVerifyRMF(pditem, "DRM_SHAREMEM", "DRF_TEXT"))
  122.        {
  123.          DrgFreeDraginfo(pdinfo);
  124.          return(MPFROM2SHORT(DOR_DROP, DO_COPY));
  125.        }
  126.        else
  127.        {
  128.          DrgFreeDraginfo(pdinfo);
  129.          return(MPFROM2SHORT(DOR_NEVERDROP, 0));
  130.        }
  131.        break;
  132.  
  133.  
  134.      case DM_DROP:
  135.        pdinfo = (PDRAGINFO)mp1;
  136.        DrgAccessDraginfo(pdinfo);
  137.        pditem = DrgQueryDragitemPtr(pdinfo, 0);
  138.        
  139.        // allocated a named shared memory object for a customer *pxfercust
  140.  
  141.        rc = DosAllocSharedMem((PPVOID)&pxfercust, DRAGXFERMEMNAME, sizeof(CUSTOMER), PAG_COMMIT | PAG_WRITE | PAG_READ);
  142.  
  143.        // allocate and fill the dxfer structure;
  144.  
  145.        pdxfer = DrgAllocDragtransfer(1);      
  146.  
  147.        pdxfer->cb = sizeof(DRAGTRANSFER);
  148.        pdxfer->hwndClient = hwnd;
  149.        pdxfer->pditem = pditem;
  150.        pdxfer->hstrSelectedRMF = DrgAddStrHandle("<DRM_CUSTOMER,DRF_TEXT>");
  151.        pdxfer->hstrRenderToName = DrgAddStrHandle(DRAGXFERMEMNAME);
  152.        pdxfer->ulTargetInfo = 0;
  153.        pdxfer->usOperation = DO_COPY;
  154.  
  155.        rc = (ULONG)DrgSendTransferMsg(pdinfo->hwndSource, DM_RENDER, (MPARAM)pdxfer, NULL);
  156.  
  157.        if(rc == TRUE)
  158.        {       
  159.           phonedial(pxfercust->phone);
  160.        }
  161.  
  162.  
  163.        DrgFreeDraginfo(pdinfo);
  164.        DrgFreeDragtransfer(pdxfer);
  165.        DosFreeMem((PVOID)pxfercust);
  166.  
  167.        break;           
  168.  
  169.     //*****************************************************************************
  170.     // The next three cases are to detect a user's request for the context menu
  171.     // If he presses button 2, then releases it without moving the mouse
  172.     // the context menu is displayed.  Pressing button 2 and moving before releasing
  173.     // is the way the telephone can be moved about the screen.
  174.     //*****************************************************************************
  175.   
  176.     case WM_BUTTON2DOWN:
  177.        button2down = TRUE;
  178.        moved = FALSE;
  179.        memcpy((PVOID)&oldpos, (PVOID)&mp1, sizeof(POINTS));     //save mouse pos.
  180.        break;
  181.  
  182.     case WM_BUTTON2UP:
  183.        button2down = FALSE;
  184.        if(moved)
  185.        {
  186.          WinSetPointer(HWND_DESKTOP, hptrArrow);
  187.          WinSetCapture(HWND_DESKTOP, (HWND)NULL);
  188.          memcpy((PVOID)&newpos, (PVOID)&mp1, sizeof(POINTS));     //save mouse pos.
  189.          WinQueryWindowPos(hwndPhone, &swp);
  190.          WinSetWindowPos(hwndPhone, HWND_TOP, swp.x + (newpos.x - oldpos.x),
  191.                                               swp.y + (newpos.y - oldpos.y), 
  192.                          swp.cx, swp.cy, SWP_MOVE);       
  193.        }
  194.        else
  195.        {  
  196.          rc = WinPopupMenu(hwndPhone, hwndPhone, hwndMenu, 0, 0, 0, 
  197.                      PU_MOUSEBUTTON1 /*| PU_POSITIONONITEM | PU_HCONSTRAIN | PU_VCONSTRAIN*/ );
  198.        }
  199.     
  200.        break;
  201.  
  202.     case WM_MOUSEMOVE:
  203.        if(button2down)
  204.        {
  205.          if(!moved)
  206.          {
  207.            WinSetPointer(HWND_DESKTOP, hptrPostIcon);
  208.            WinSetCapture(HWND_DESKTOP, hwndPhone);
  209.          }
  210.          moved = TRUE;
  211.        }
  212.        break;
  213.        
  214.     case WM_COMMAND:
  215.       switch (SHORT1FROMMP(mp1))
  216.       {
  217.         case ID_EXITPROG:
  218.           WinPostMsg(hwnd, WM_CLOSE, (MPARAM)0, (MPARAM)0);
  219.           break;
  220.       }
  221.       break;  
  222.  
  223.     case WM_CLOSE:
  224.       WinPostMsg(hwnd, WM_QUIT, (MPARAM)0,(MPARAM)0);     
  225.       break;
  226.    
  227.     default:
  228.       return (MRESULT)StaticWinProc(hwnd, msg, mp1, mp2);
  229.   }
  230.   return (MRESULT)FALSE;
  231.  
  232.  
  233. //****************************************************************************
  234. //
  235. //  The following two functions implement the phone dialling functions.  
  236. //  Unfortunately, OS/2 does not support the production of multiple concurrent
  237. //  tones on the PC speaker.  Since a touchtone phone generates a pair of tones
  238. //  for each number, we can't reproduce this accurately.  For this sample program
  239. //  therefore, all we do is rapidly alternate the two tones in each case.  The
  240. //  frequencies used are correct.
  241. //
  242. //****************************************************************************
  243.  
  244. void phonedial(PCHAR phone)
  245. {  
  246.   char *p;
  247.   ULONG num;
  248.   int hi, lo;
  249.  
  250.   for(p=phone; p != '\0' && (p-phone) < 8; p++)
  251.   {
  252.     if(*p >= '0' && *p <= '9')
  253.     {
  254.       num = (*p - '0');
  255.  
  256.       if(num == 1 || num == 2 || num == 3)
  257.         lo = 697;
  258.       else if(num == 4 || num == 5 || num == 6)
  259.         lo = 770;
  260.       else if(num == 7 || num == 8 || num == 9)
  261.         lo = 852;
  262.       else
  263.         lo = 941;    // zero
  264.  
  265.       if(num == 1 || num == 4 || num == 7)
  266.         hi = 1209;
  267.       else if(num == 2 || num == 5 || num == 8)
  268.         hi = 1336;
  269.       else if(num == 3 || num == 6 || num == 9)
  270.         hi = 1477;
  271.       mfbeep(lo, hi);
  272.     }
  273.   }
  274. }
  275.  
  276.  
  277. #define FLIPTIME 10 
  278. void mfbeep(ULONG f1, ULONG f2)
  279. {
  280.   int i;
  281.    
  282.   for(i=1; i<30/FLIPTIME; i++)
  283.   {
  284.      DosBeep(f1, FLIPTIME);
  285.      DosBeep(f2, FLIPTIME);
  286.   }
  287.   DosSleep(50);
  288. }
  289.