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

  1. //****************************************************************************************
  2. //
  3. //  DMORDER.C - a demo program written by Franco Federico and Alan Chambers
  4. //              whilst on a residency at the ITSC, Boca Raton
  5. //
  6. //  Updated to work with GA code, 6 May 1992
  7. //
  8. //  This program will display an order form when the appropriate action bar selection
  9. //  is made.  Customers, dragged from the DMCUST sample program, can be dropped onto
  10. //  this order form, with the result that the customer's details are entered into
  11. //  the appropriate fields.
  12. //
  13. //  The code for the window and dialog box, and the technique used for subclassing
  14. //  were written by Franco, the drag/drop code added by Alan.
  15. //
  16. //**************************************************************************************** */
  17.  
  18. #define INCL_GPI
  19. #define INCL_DOS
  20. #define INCL_WIN
  21.  
  22. #include <os2.h>
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <stddef.h>
  29. #include <process.h>
  30. #include <memory.h>
  31. #include <sys\types.h>
  32. #include <sys\stat.h>
  33. #include <bsememf.h>
  34.  
  35. #define WC_NAME_FRAME            "#1"
  36. #define WC_NAME_COMBOBOX         "#2"
  37. #define WC_NAME_BUTTON           "#3"
  38. #define WC_NAME_MENU             "#4"
  39. #define WC_NAME_STATIC           "#5"
  40. #define WC_NAME_ENTRYFIELD       "#6"
  41. #define WC_NAME_LISTBOX          "#7"
  42. #define WC_NAME_SCROLLBAR        "#8"
  43. #define WC_NAME_TITLEBAR         "#9"
  44. #define WC_NAME_MLE              "#10"
  45. #define WC_NAME_APPSTAT          "#16"
  46. #define WC_NAME_KBDSTAT          "#17"
  47. #define WC_NAME_PECIC            "#18"
  48. #define WC_NAME_DBE_KKPOPUP      "#19"
  49. #define WC_NAME_SPINBUTTON       "#32"
  50. #define WC_NAME_CONTAINER        "#37"
  51. #define WC_NAME_SLIDER           "#38"
  52. #define WC_NAME_VALUESET         "#39"
  53. #define WC_NAME_NOTEBOOK         "#40"
  54.  
  55. #define DRAGXFERMEMNAME  "\\SHAREMEM\\DMORDXFER.DAT"
  56.  
  57. #include "dmorder.h"
  58. #include "dmorddlg.h"
  59.  
  60.  
  61. #define DebugBox(title, text)  WinMessageBox(HWND_DESKTOP,HWND_DESKTOP, \
  62.                                (PSZ) text , (PSZ) title, 0, \
  63.                                MB_OK | MB_INFORMATION )
  64.  
  65. typedef struct {
  66.                  char name[30];
  67.                  char address[100];
  68.                  char phone[15];
  69.                } 
  70.                  CUSTOMER, *PCUSTOMER;
  71.  
  72.  
  73. typedef struct _SUBCLASSDATA {
  74.         HWND  hwnd;
  75.         PFNWP pSubWinProc;
  76.         PFNWP pPreviousWinProc;
  77.         CHAR  szClassName[40];
  78.         HWND  hwndSurrogate;
  79.         PVOID pWindowData;
  80.         BOOL  fEmphasis;
  81.         struct _SUBCLASSDATA *pNext;
  82.        } 
  83.          SUBCLASSDATA, *PSUBCLASSDATA;
  84.  
  85. MRESULT EXPENTRY OrderDlgProc(HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2);
  86. MRESULT EXPENTRY OrderSubWinProc(HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2);
  87. MRESULT EXPENTRY MainWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  88.  
  89. PSUBCLASSDATA GetSubClassData(HWND hwnd, PFNWP pSubWinProc);
  90. VOID SubClassWindow(HWND hwnd, PFNWP pSubWinProc, PVOID pData, HWND  hwndSurrogate );
  91. VOID FreeUnwantedSubClassData(VOID);
  92. VOID DrawEmphasis(HWND hwnd);
  93.  
  94. HWND hwndCliArea;
  95. HWND hwndMLEWindow;
  96.  
  97. HAB  hab;
  98. CHAR szMsgString[200];
  99.  
  100. PSUBCLASSDATA pSubClassDataFirst = NULL;
  101. PSUBCLASSDATA pSubClassDataLast  = NULL;
  102.  
  103. main ()
  104. {
  105.   HMQ  hmq;
  106.   HWND hwndFrame;
  107.   QMSG qmsg;
  108.   ULONG flCreate;
  109.  
  110.   hab = WinInitialize(0);
  111.  
  112.   hmq = WinCreateMsgQueue( hab, 0 );
  113.  
  114.   WinRegisterClass( hab, (PSZ)"MainWindow",
  115.                         (PFNWP)MainWindowProc,
  116.                         CS_SIZEREDRAW, 0 );
  117.  
  118.   flCreate = FCF_STANDARD & ~FCF_ACCELTABLE & ~FCF_ICON;
  119.  
  120.   hwndFrame = WinCreateStdWindow( HWND_DESKTOP, 0,
  121.                                   &flCreate, "MainWindow",
  122.                                   "Order Form Sample", 0,
  123.                                   (HMODULE)0L, ID_FRAME_WND,
  124.                                   &hwndCliArea ) ;
  125.  
  126.   strcpy(szMsgString,"To take an order, select \"File\"/\"Take order\".");
  127.  
  128.  
  129.   while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  130.      WinDispatchMsg( hab, &qmsg );
  131.  
  132.  
  133.   WinDestroyWindow(hwndFrame);
  134.   WinDestroyMsgQueue( hmq );
  135.   WinTerminate( hab );
  136. }
  137.  
  138.  
  139.  
  140. MRESULT EXPENTRY MainWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  141. {
  142.  
  143.   switch( msg )
  144.   {
  145.       case WM_CREATE:
  146.       {
  147.         WinSetWindowPos( WinQueryWindow(hwnd, QW_PARENT),  HWND_TOP, 150, 250, 400, 120,
  148.                    SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW);
  149.       }
  150.       break;
  151.  
  152.  
  153.       case WM_COMMAND:
  154.        {
  155.          switch (SHORT1FROMMP(mp1))
  156.          {
  157.            case MID_TAKE_ORDER:
  158.  
  159.                 WinDlgBox(HWND_DESKTOP,
  160.                           HWND_DESKTOP,
  161.                           OrderDlgProc,
  162.                           0L,
  163.                           ID_DLG_ORDERFORM,
  164.                           (PVOID)NULL);
  165.  
  166.                 FreeUnwantedSubClassData();
  167.  
  168.              break;
  169.  
  170.            case MID_OTHER:
  171.  
  172.              WinPostMsg(WinQueryWindow(HWND_DESKTOP,QW_PREV), WM_COMMAND, MPFROMSHORT(705) ,(MPARAM)0);
  173.              DosBeep(300,200);
  174.              WinPostMsg(WinQueryWindow(HWND_DESKTOP,QW_PREV), WM_USER+1659, MPFROMSHORT(705) ,(MPARAM)0);
  175.              DosBeep(300,200);
  176.              WinPostMsg(WinQueryWindow(HWND_DESKTOP,QW_PREV), WM_USER+1660, MPFROMSHORT(705) ,(MPARAM)0);
  177.              DosBeep(300,200);
  178.  
  179.              break;
  180.  
  181.            default:
  182.              return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  183.          }
  184.       }
  185.       break;
  186.  
  187.     case WM_PAINT:
  188.       {
  189.          HPS    hps;
  190.          RECTL  rc;
  191.  
  192.          hps = WinBeginPaint( hwnd, 0L, &rc );
  193.  
  194.          WinFillRect(hps, &rc, CLR_WHITE);
  195.          WinQueryWindowRect(hwnd,&rc);
  196.  
  197.          WinDrawText(hps, (LONG)strlen( szMsgString ),
  198.                      szMsgString, &rc, CLR_RED,
  199.                      CLR_WHITE, DT_CENTER | DT_VCENTER);
  200.  
  201.          WinEndPaint( hps );
  202.       }
  203.       break;
  204.  
  205.     default:
  206.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  207.   }
  208.   return (MRESULT)FALSE;
  209. }
  210.  
  211.  
  212. MRESULT EXPENTRY OrderDlgProc(HWND hwndDlg, ULONG msg,
  213.                                   MPARAM mp1, MPARAM mp2)
  214. {
  215.  
  216.  switch (msg)
  217.  {
  218.     case WM_USER+1:
  219.     /* Subclass the dialog and all it's children */
  220.        {
  221.           HWND   hwndChild;       /* current dialog child      */
  222.           HENUM  henum;           /* enumeration handle        */
  223.  
  224.           SubClassWindow( hwndDlg, OrderSubWinProc, NULL , (HWND)0 );
  225.  
  226.           henum = WinBeginEnumWindows(hwndDlg);
  227.  
  228.           while ( (hwndChild = WinGetNextWindow(henum)) )
  229.           {
  230.              SubClassWindow( hwndChild, OrderSubWinProc, NULL, hwndDlg);
  231.           }
  232.        }
  233.        break;
  234.  
  235.     case WM_INITDLG:
  236.        WinPostMsg(hwndDlg, WM_USER+1, (MPARAM)0, (MPARAM)0 );
  237.        break;
  238.  
  239.     case WM_COMMAND:
  240.        {
  241.           switch (SHORT1FROMMP(mp1))
  242.           {
  243.             case DID_OK:
  244.               {
  245.                  //
  246.               }
  247.               return (MRESULT) TRUE;
  248.               break;
  249.  
  250.             case DID_CANCEL:
  251.               WinDismissDlg(hwndDlg,DID_CANCEL);
  252.               break;
  253.           }
  254.        }
  255.        return (MRESULT) TRUE;
  256.  }
  257.  return (WinDefDlgProc(hwndDlg, msg, mp1, mp2) );
  258. }
  259.  
  260.  
  261.  
  262. VOID SubClassWindow(HWND hwnd, PFNWP pSubWinProc, PVOID pData, HWND  hwndSurrogate )
  263. {
  264.    PFNWP  pOldWinProc;   /*Old window procedure */
  265.  
  266.    PSUBCLASSDATA pSubClassData;
  267.  
  268.    pOldWinProc = WinSubclassWindow(hwnd, pSubWinProc);
  269.  
  270.    /* allocate our subclass data and fill it  */
  271.  
  272.    pSubClassData = (PSUBCLASSDATA) malloc( sizeof(SUBCLASSDATA) );
  273.  
  274.    pSubClassData->hwnd = hwnd;
  275.    pSubClassData->pPreviousWinProc = pOldWinProc;
  276.    pSubClassData->pWindowData = pData;
  277.    pSubClassData->hwndSurrogate = hwndSurrogate;
  278.    pSubClassData->fEmphasis = FALSE;
  279.    pSubClassData->pSubWinProc = pSubWinProc;
  280.  
  281.    WinQueryClassName(hwnd,
  282.                      sizeof(pSubClassData->szClassName),
  283.                      pSubClassData->szClassName);
  284.  
  285.    pSubClassData->pNext = NULL;
  286.  
  287.    /* Now add to our linked list */
  288.  
  289.    if(pSubClassDataLast == NULL)
  290.    {
  291.       pSubClassDataFirst = pSubClassData;
  292.       pSubClassDataLast  = pSubClassData;
  293.    }
  294.    else
  295.    {
  296.       pSubClassDataLast->pNext = pSubClassData;
  297.       pSubClassDataLast = pSubClassData;
  298.    }
  299. }
  300.  
  301. PSUBCLASSDATA GetSubClassData(HWND hwnd, PFNWP pSubWinProc)
  302. {
  303.    PSUBCLASSDATA pSubClassData;
  304.  
  305.    pSubClassData = pSubClassDataFirst;
  306.  
  307.    for(;;)
  308.    {
  309.       if( pSubClassData->hwnd == hwnd && pSubClassData->pSubWinProc == pSubWinProc)
  310.       {
  311.          break; /* Found a hwnd match */
  312.       }
  313.  
  314.       pSubClassData = pSubClassData->pNext;
  315.  
  316.       if( pSubClassData == NULL)
  317.       {
  318.          break; /* Not found returns NULL */
  319.       }
  320.    }
  321.  
  322.    return (pSubClassData);
  323. }
  324.  
  325. VOID FreeUnwantedSubClassData(VOID)
  326. {
  327.  
  328.    PSUBCLASSDATA pSubClassData;
  329.    PSUBCLASSDATA pDeleteable;
  330.  
  331.    if(pSubClassDataFirst != NULL)
  332.    {
  333.       pSubClassData = pSubClassDataFirst;
  334.  
  335.       for(;;)
  336.       {
  337.          if( pSubClassData->pNext == NULL)
  338.          {
  339.             pSubClassDataLast = pSubClassData;
  340.             break;
  341.          }
  342.  
  343.          if(! WinIsWindow((HAB)0, (pSubClassData->pNext)->hwnd ) )
  344.          {
  345.             /* Window no longer exists so let us delete this entry   */
  346.             /* To keep the simple the first entry never gets deleted */
  347.             pDeleteable = pSubClassData->pNext;
  348.             pSubClassData->pNext = pDeleteable->pNext;
  349.  
  350.             free(pDeleteable);
  351.          }
  352.          else
  353.          {
  354.             pSubClassData = pSubClassData->pNext;
  355.          }
  356.       }
  357.    }
  358. }
  359.  
  360.  
  361.  
  362. MRESULT EXPENTRY OrderSubWinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  363. {
  364.   PSUBCLASSDATA pSubClassData;
  365.   PDRAGINFO pdinfo;
  366.   PDRAGITEM pditem;
  367.   PDRAGTRANSFER pdxfer;
  368.   PCUSTOMER pxfercust;
  369.   ULONG rc;
  370.  
  371.   pSubClassData = GetSubClassData(hwnd, OrderSubWinProc );
  372.  
  373.  
  374.   switch( msg )
  375.   {
  376.     case DM_DRAGLEAVE:
  377.        {
  378.           if(pSubClassData->hwndSurrogate)
  379.           {
  380.              break;
  381.           }
  382.           else
  383.           {
  384.              DrawEmphasis(hwnd);
  385.              pSubClassData->fEmphasis = FALSE;
  386.              return ((MRESULT)(*(pSubClassData->pPreviousWinProc))(hwnd, msg, mp1, mp2));
  387.           }
  388.        }
  389.        break;
  390.  
  391.     case DM_DRAGOVER:
  392.        {
  393.           if(pSubClassData->hwndSurrogate)
  394.           {
  395.              return( WinSendMsg(pSubClassData->hwndSurrogate, msg, mp1, mp2) );
  396.           }
  397.           else
  398.           {
  399.              if( pSubClassData->fEmphasis == FALSE)
  400.              {
  401.                 DrawEmphasis(hwnd);
  402.                 pSubClassData->fEmphasis = TRUE;
  403.              }
  404.  
  405.              pdinfo = (PDRAGINFO)mp1;
  406.              DrgAccessDraginfo(pdinfo);
  407.              pditem = DrgQueryDragitemPtr(pdinfo, 0);
  408.              if(DrgVerifyRMF(pditem, "DRM_SHAREMEM", "DRF_TEXT"))
  409.              {
  410.                 DrgFreeDraginfo(pdinfo);
  411.                 return(MRFROM2SHORT(DOR_DROP, DO_COPY));
  412.              }
  413.              else
  414.              {
  415.                DrgFreeDraginfo(pdinfo);
  416.                return(MPFROM2SHORT(DOR_NEVERDROP, 0));
  417.              }
  418.              return ((MRESULT)(*(pSubClassData->pPreviousWinProc))(hwnd, msg, mp1, mp2));
  419.  
  420.           }
  421.        }
  422.        break;
  423.  
  424.    case DM_DROP:
  425.        {
  426.           if(pSubClassData->hwndSurrogate)
  427.           {
  428.              return( WinSendMsg(pSubClassData->hwndSurrogate, msg, mp1, mp2) );
  429.           }
  430.           else
  431.           {
  432.              pdinfo = (PDRAGINFO)mp1;
  433.              DrgAccessDraginfo(pdinfo);
  434.              pditem = DrgQueryDragitemPtr(pdinfo, 0);
  435.              
  436.              rc = DosAllocSharedMem((PPVOID)&pxfercust, DRAGXFERMEMNAME, sizeof(CUSTOMER), PAG_COMMIT | PAG_WRITE | PAG_READ);
  437.           
  438.              pdxfer = DrgAllocDragtransfer(1);      
  439.           
  440.              pdxfer->cb = sizeof(DRAGTRANSFER);
  441.              pdxfer->hwndClient = hwnd;
  442.              pdxfer->pditem = pditem;
  443.              pdxfer->hstrSelectedRMF = DrgAddStrHandle("DRM_CUSTOMER");
  444.              pdxfer->hstrRenderToName = DrgAddStrHandle(DRAGXFERMEMNAME);
  445.              pdxfer->ulTargetInfo = 0;
  446.              pdxfer->usOperation = DO_COPY;
  447.           
  448.              rc = (ULONG)DrgSendTransferMsg(pdinfo->hwndSource, DM_RENDER, (MPARAM)pdxfer, NULL);
  449.           
  450.              if(rc == TRUE)
  451.              {       
  452.                  WinSetWindowText(WinWindowFromID(hwnd, ID_EF_NAME), pxfercust->name);
  453.                  WinSetWindowText(WinWindowFromID(hwnd, ID_EF_ADDRESS), pxfercust->address);
  454.                  WinSetWindowText(WinWindowFromID(hwnd, ID_EF_TELEPHONE), pxfercust->phone);
  455.                  DosBeep(2000,20); DosSleep(20);DosBeep(2000,20); DosSleep(20); DosBeep(2000,20); DosSleep(20); DosBeep(2000,20); DosSleep(20); 
  456.           
  457.              }
  458.           
  459.           
  460.              DrgFreeDraginfo(pdinfo);
  461.              DrgFreeDragtransfer(pdxfer);
  462.              DosFreeMem((PVOID)pxfercust);
  463.            }
  464.        }
  465.        break;           
  466.  
  467.     default:
  468.       return ((MRESULT)(*(pSubClassData->pPreviousWinProc))(hwnd, msg, mp1, mp2));
  469.       break;
  470.   }
  471.   return (MRESULT)FALSE;
  472.  
  473. }
  474.  
  475. VOID DrawEmphasis(HWND hwnd)
  476. {
  477.    RECTL  rc;
  478.    POINTL ptl;
  479.    HPS    hps;
  480.  
  481.    hps = DrgGetPS(hwnd);
  482.  
  483.    WinQueryWindowRect(hwnd,&rc);
  484.  
  485.    GpiSetMix(hps, FM_XOR);
  486.    GpiSetColor(hps, CLR_BACKGROUND);
  487.  
  488.    ptl.x = rc.xLeft + 4L; ptl.y = rc.yBottom  + 4L;
  489.    GpiMove(hps, &ptl);
  490.    ptl.x = rc.xLeft + 4L; ptl.y = rc.yTop  - 4L;
  491.    GpiLine(hps, &ptl);
  492.    ptl.x = rc.xRight - 4L; ptl.y = rc.yTop  - 4L;
  493.    GpiLine(hps, &ptl);
  494.    ptl.x = rc.xRight - 4L; ptl.y = rc.yBottom  + 4L;
  495.    GpiLine(hps, &ptl);
  496.    ptl.x = rc.xLeft + 4L; ptl.y = rc.yBottom  + 4L;
  497.    GpiLine(hps, &ptl);
  498.  
  499.    DrgReleasePS(hps);
  500. }
  501.  
  502.