home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / msngr / bmap.c next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  14.0 KB  |  529 lines

  1. /****************************** MODULE Header ******************************\
  2. * Module Name:  bmap.c - Messenger application - bitmap module
  3. *
  4. * Created: 8/1/89  sanfords
  5. *
  6. * Copyright (c) 1988, 1989  Microsoft Corporation
  7. \***************************************************************************/
  8.  
  9. #include "msngr.h"
  10. #include "string.h"
  11. #ifdef SLOOP
  12. #define BITMAPINFOHEADER2 BITMAPINFOHEADER
  13. #define PBITMAPINFOHEADER2 PBITMAPINFOHEADER
  14. #define BITMAPINFO2 BITMAPINFO
  15. #define PBITMAPINFO2 PBITMAPINFO
  16. #endif
  17.  
  18. typedef struct _CDATA {
  19.     RECTL rcl;
  20.     HBITMAP hbm;
  21.     BOOL fSelect;
  22.     BOOL fSelecting;
  23.     NPUSERLIST npUL;
  24. } CDATA;
  25. typedef CDATA *NPCDATA;
  26.  
  27.  
  28. typedef struct _PKT {
  29.     HBITMAP hbm;
  30.     SHORT cx;
  31.     SHORT cy;
  32.     char szName[MAX_NAMESTR + 1];
  33. } PKT;
  34. typedef PKT *NPPKT;
  35.  
  36. extern HWND hwndMsngr;
  37. extern SHORT cyText;
  38. extern HAB hab;
  39. extern HSZ hszAppName;
  40. extern HSZ hszEmailName;
  41. extern char szEmailName[];
  42. extern HWND hwndLB;
  43. extern ITEMLIST msgTopicItemList[];
  44.  
  45.  
  46.  
  47. /*
  48.  * local procs
  49.  */
  50. MRESULT bmpInit(HWND hwnd, NPPKT ppktInit);
  51. MRESULT sndBmapInit(HWND hwnd, NPUSERLIST pUserItem);
  52. BOOL sndBmap(NPCDATA pcd);
  53. HBITMAP SnapRegion(HPS hps, PRECTL prcl);
  54. void DrawRgn(HPS hps, PRECTL prcl);
  55. void SortRect(PRECTL prcl, PRECTL prclSorted);
  56. HDC CreateDC(PSZ lpszDriver, HDC hdcCompat);
  57.  
  58. #define max(a,b)    (((a) > (b)) ? (a) : (b))
  59.  
  60. /*
  61.  * file globals
  62.  */
  63. ATOM fmtBmapPkt;
  64. HPOINTER hptrSelBmap = 0;
  65. HPOINTER hptrBmap = 0;
  66.  
  67. void InitBmapModule()
  68. {
  69.     fmtBmapPkt = WinAddAtom(WinQuerySystemAtomTable(), SZBMAPDATA);
  70.     hptrSelBmap = WinLoadPointer(HWND_DESKTOP, (HMODULE)NULL, IDR_SELBMAP);
  71.     hptrBmap = WinLoadPointer(HWND_DESKTOP, NULL, IDD_GETBITMAP);
  72.  
  73. void CloseBmapModule()
  74. {
  75.     WinDeleteAtom(WinQuerySystemAtomTable(), fmtBmapPkt);
  76.     WinDestroyPointer(hptrSelBmap);
  77.     WinDestroyPointer(hptrBmap);
  78. }
  79.  
  80. HDMGDATA bmpXfer(pXferInfo)
  81. PXFERINFO pXferInfo;
  82. {
  83.     PBYTE pbuf;
  84.     PKT pkt;
  85.     PBITMAPINFO pbmi;
  86.     HDC hdc;
  87.     HPS hpsMem;
  88.     SIZEL size;
  89.  
  90.     if (pXferInfo->usFmt != fmtBmapPkt)
  91.         return(DDE_NOTPROCESSED);
  92.  
  93.     if (pXferInfo->usType == XTYP_POKE) {
  94.         /*
  95.          * we have bitmap bits...stick them into pkt.hbm.
  96.          */
  97.         pbuf = DdeAccessData(pXferInfo->hDmgData);
  98.         
  99.         DdeCopyBlock(pbuf, (PBYTE)&pkt.szName[0], MAX_NAMESTR + 1L);
  100.         pbmi = (PBITMAPINFO)(pbuf + MAX_NAMESTR + 1);
  101.  
  102.         pkt.cx = pbmi->cx;
  103.         pkt.cy = pbmi->cy;
  104.         size.cx = (LONG)pkt.cx;
  105.         size.cy = (LONG)pkt.cy;
  106.         hdc = CreateDC((PSZ)"MEMORY", (HDC)NULL);
  107.         hpsMem = GpiCreatePS(hab, hdc, &size,
  108.                 PU_ARBITRARY | GPIT_NORMAL | GPIA_ASSOC );
  109.         pkt.hbm = GpiCreateBitmap(hpsMem, (PBITMAPINFOHEADER2)pbmi, CBM_INIT,
  110.                 (PBYTE)&pbmi->argbColor[1 << pbmi->cBitCount],
  111.                 (PBITMAPINFO2)pbmi);
  112.         GpiAssociate(hpsMem, NULL);
  113.         GpiDestroyPS(hpsMem);
  114.         DevCloseDC(hdc);
  115.         DdeFreeData(pXferInfo->hDmgData);
  116.         
  117.         WinLoadDlg(HWND_DESKTOP, hwndMsngr, BmpDlgProc, 0L, IDD_GETBITMAP,
  118.                 (PVOID)&pkt);
  119.         return(1);
  120.     }
  121.     return(0);
  122. }
  123.  
  124.  
  125. /*
  126.  * This is the proc used for receiving a bitmap
  127.  */
  128. MRESULT EXPENTRY BmpDlgProc(hwnd, msg, mp1, mp2)
  129. HWND hwnd;
  130. USHORT msg;
  131. MPARAM mp1;
  132. MPARAM mp2;
  133. {
  134.     HBITMAP hbm;
  135.     HPS hps;
  136.     WRECT wrc;
  137.     
  138.     switch(msg) {
  139.     case WM_INITDLG:
  140.         return(bmpInit(hwnd, (NPPKT)(SHORT)mp2));
  141.         break;
  142.  
  143.     case WM_DESTROY:
  144.         if (hbm = ((NPPKT)WinQueryWindowUShort(hwnd, QWS_USER))->hbm)
  145.             GpiDeleteBitmap(hbm);
  146.         WinFreeMem(hheap, (NPBYTE)WinQueryWindowUShort(hwnd, QWS_USER),
  147.                 sizeof(PKT));
  148.         break;
  149.  
  150.     case WM_WINDOWPOSCHANGED:
  151.         /*
  152.          * hide the OK button when minimized since it messes up the icon.
  153.          */
  154.         if ((LONG)mp2 & AWP_MINIMIZED)
  155.             WinShowWindow(WinWindowFromID(hwnd, MBID_OK), FALSE);
  156.         else if ((LONG)mp2 & AWP_RESTORED)
  157.             WinShowWindow(WinWindowFromID(hwnd, MBID_OK), TRUE);
  158.         return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  159.         break;
  160.  
  161.     case WM_COMMAND:
  162.         WinDestroyWindow(hwnd);
  163.         break;
  164.         
  165.     case WM_PAINT:
  166.         WinDefDlgProc(hwnd, msg, mp1, mp2);
  167.         /*
  168.          * draw the bitmap just above the OK button.
  169.          */
  170.         hps = WinGetPS(hwnd);
  171.         WinQueryWindowRect(WinWindowFromID(hwnd, MBID_OK), (PRECTL)&wrc);
  172.         WinMapWindowPoints(WinWindowFromID(hwnd, MBID_OK), hwnd, (PPOINTL)&wrc, 2);
  173.         wrc.yBottom = wrc.yTop + cyText / 2;
  174.         hbm = ((NPPKT)WinQueryWindowUShort(hwnd, QWS_USER))->hbm;
  175.         WinDrawBitmap(hps, hbm, (PRECTL)NULL, (PPOINTL)&wrc, 0L, 0L, DBM_NORMAL);
  176.         WinReleasePS(hps);
  177.         break;
  178.  
  179.     default:
  180.         return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  181.     }
  182.     return(0);
  183. }
  184.  
  185.  
  186. MRESULT bmpInit(hwnd, ppktInit)
  187. HWND hwnd;
  188. NPPKT ppktInit;
  189. {
  190.     char szTitle[MAX_TITLESTR];
  191.     WRECT wrc;
  192.     NPPKT ppkt;
  193.     SHORT cxMin;
  194.  
  195.     if (!(ppkt = (NPPKT)WinAllocMem(hheap, sizeof(PKT))))
  196.         return(1);    
  197.     *ppkt = *ppktInit;
  198.     WinSetWindowUShort(hwnd, QWL_USER, (USHORT)ppkt);
  199.     /*
  200.      * This is required because currently, automatic ICON resource loading
  201.      * is not supported for dialogs.
  202.      */
  203.     WinSendMsg(hwnd, WM_SETICON, (MPARAM)hptrBmap, 0L);
  204.     /*
  205.      * Set up title.
  206.      */
  207.     WinQueryWindowText(hwnd, MAX_TITLESTR, (PSZ)szTitle);
  208.     lstrcat(szTitle, szTitle, ppkt->szName);
  209.     WinSetWindowText(hwnd, (PSZ)szTitle);
  210.     /*
  211.      * resize the dialog so the bitmap just fits.
  212.      */
  213.     WinQueryWindowRect(hwnd, (PRECTL)&wrc);
  214.     cxMin = wrc.xRight;
  215.     WinQueryWindowRect(WinWindowFromID(hwnd, MBID_OK), (PRECTL)&wrc);
  216.     WinMapWindowPoints(WinWindowFromID(hwnd, MBID_OK), hwnd, (PPOINTL)&wrc, 2);
  217.     WinSetWindowPos(hwnd, NULL, 0, 0, max(wrc.xLeft * 2 + ppkt->cx, cxMin),
  218.             wrc.yTop + ppkt->cy + cyText +
  219.             (SHORT)WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR), SWP_SIZE);
  220.     return(0);
  221. }
  222.  
  223.  
  224.  
  225. MRESULT EXPENTRY SendBitmapDlgProc(hwnd, msg, mp1, mp2)
  226. HWND hwnd;
  227. USHORT msg;
  228. MPARAM mp1;
  229. MPARAM mp2;
  230. {
  231.     NPCDATA pcd;
  232.     POINTL pt;
  233.     HPS hps;
  234.  
  235.     pcd = (NPCDATA)WinQueryWindowUShort(hwnd, QWS_USER);
  236.     
  237.     switch (msg) {
  238.     case WM_INITDLG:
  239.         return(sndBmapInit(hwnd, (NPUSERLIST)(SHORT)mp2));
  240.         break;
  241.  
  242.     case WM_DESTROY:
  243.         WinFreeMem(hheap, (NPBYTE)pcd, sizeof(CDATA));
  244.         break;
  245.         
  246.     case WM_COMMAND:
  247.         switch (LOUSHORT(mp1)) {
  248.         case IDC_SENDBITMAP:
  249.             if (sndBmap(pcd))
  250.                 WinDismissDlg(hwnd, 0);
  251.             break;
  252.             
  253.         case MBID_CANCEL:                    
  254.             WinDismissDlg(hwnd, 0);
  255.             break;
  256.             
  257.         case IDC_SELECT:
  258.             pcd->fSelect = TRUE;
  259.             WinSetCapture(HWND_DESKTOP, hwnd);
  260.             break;
  261.         }
  262.         break;
  263.  
  264.     case WM_BUTTON1DOWN:
  265.         if (pcd->fSelect) {
  266.             if (pcd->hbm) {
  267.                 GpiDeleteBitmap(pcd->hbm);
  268.                 pcd->hbm = NULL;
  269.             }
  270.             WinSetRect(hab, &pcd->rcl, SHORT1FROMMP(mp1), SHORT2FROMMP(mp1),
  271.                     SHORT1FROMMP(mp1), SHORT2FROMMP(mp1));
  272.             WinMapWindowPoints(hwnd, (HWND)HWND_DESKTOP, (PPOINTL)&pcd->rcl, 2);
  273.             hps = WinGetScreenPS(HWND_DESKTOP);
  274.             DrawRgn(hps, &pcd->rcl);
  275.             WinReleasePS(hps);
  276.             pcd->fSelecting = TRUE;
  277.         }
  278.         break;
  279.  
  280.     case WM_MOUSEMOVE:
  281.         if (pcd->fSelect) {
  282.             WinSetPointer(HWND_DESKTOP, hptrSelBmap);
  283.         } else {
  284.             WinSetPointer(HWND_DESKTOP,
  285.                     WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE));
  286.         }
  287.         if (pcd->fSelecting) {
  288.             hps = WinGetScreenPS(HWND_DESKTOP);
  289.             DrawRgn(hps, &pcd->rcl);    /* erase old rect */
  290.             pt.x = SHORT1FROMMP(mp1);
  291.             pt.y = SHORT2FROMMP(mp1);
  292.             WinMapWindowPoints(hwnd, HWND_DESKTOP, &pt, 1);
  293.             pcd->rcl.xRight = pt.x;
  294.             pcd->rcl.yTop = pt.y;
  295.             DrawRgn(hps, &pcd->rcl);    /* draw new one */
  296.             WinReleasePS(hps);
  297.         }
  298.         break;
  299.  
  300.     case WM_BUTTON1UP:
  301.         if (pcd->fSelecting) {
  302.             WinSetCapture(HWND_DESKTOP, (HWND)NULL);
  303.             hps = WinGetScreenPS(HWND_DESKTOP);
  304.             DrawRgn(hps, &pcd->rcl);
  305.             pcd->hbm = SnapRegion(hps, &pcd->rcl);
  306.             WinReleasePS(hps);
  307.             pcd->fSelecting = FALSE;
  308.             pcd->fSelect = FALSE;
  309.             WinEnableWindow(WinWindowFromID(hwnd, IDC_SENDBITMAP),
  310.                     !WinIsRectEmpty(hab, &pcd->rcl));
  311.         }
  312.         break;
  313.  
  314.         
  315.     default:
  316.         return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  317.         break;
  318.     }
  319.     return(0);
  320. }
  321.  
  322.  
  323. /*
  324.  * returns fFailed
  325.  */
  326. MPARAM sndBmapInit(hwnd, pUserItem)
  327. HWND hwnd;
  328. NPUSERLIST pUserItem;
  329. {
  330.     NPCDATA pcd;
  331.     char szTitle[MAX_TITLESTR];
  332.     char szName[MAX_NAMESTR];
  333.     
  334.     if (!(pcd = (NPCDATA)WinAllocMem(hheap, sizeof(CDATA))))
  335.         return(1);
  336.     WinSetRectEmpty(hab, &pcd->rcl);
  337.     pcd->hbm = NULL;
  338.     pcd->fSelect = FALSE;
  339.     pcd->fSelecting = FALSE;
  340.     pcd->npUL = pUserItem;
  341.     if (pcd->npUL->hConvMsg == NULL) {
  342.         NotifyUser(SZCANTCONNECT);
  343.         return(1);
  344.     }
  345.     WinQueryWindowText(hwnd, MAX_TITLESTR, szTitle);
  346.     DdeGetHszString(pcd->npUL->hsz, szName, (LONG)MAX_NAMESTR);
  347.     lstrcat(szTitle, szTitle, szName);
  348.     WinSetWindowText(hwnd, (PSZ)szTitle);
  349.     WinSetWindowUShort(hwnd, QWS_USER, (USHORT)pcd);
  350.     return(0);
  351. }
  352.  
  353.  
  354. BOOL sndBmap(pcd)
  355. NPCDATA pcd;
  356. {
  357.     BITMAPINFOHEADER bih;
  358.     SHORT cbBuffer, cbBitmapInfo;
  359.     PBYTE pbBuffer;
  360.     PBITMAPINFO pbmi;
  361.     PSZ pszName;
  362.     SEL sel;
  363.     HPS hps;
  364.     HDC hdc;
  365.     SIZEL size;
  366.     
  367.     /*
  368.      * Compute the size of the image-data buffer and the bitmap information
  369.      * structure.
  370.      */
  371.     GpiQueryBitmapParameters(pcd->hbm, &bih);
  372.     cbBuffer = (((bih.cBitCount * bih.cx) + 31) / 32) * 4;
  373.     if (cbBuffer > 0xFFFF / bih.cy / bih.cPlanes) {
  374.         NotifyUser(SZTOOBIG);
  375.         return(FALSE);
  376.     }
  377.     cbBuffer *= bih.cy * bih.cPlanes;
  378.     cbBitmapInfo = sizeof(BITMAPINFO) +
  379.         (sizeof(RGB) * (1 << bih.cBitCount));
  380.     
  381.     /*
  382.      * Allocate memory for the image data-buffer and the bitmap information
  383.      * structure.
  384.      */
  385.     DosAllocSeg(cbBuffer + cbBitmapInfo + MAX_NAMESTR + 1, &sel, 0);
  386.     pszName = (PSZ)MAKEP(sel, 0);
  387.     lstrcpy(pszName, szEmailName);
  388.     pbmi = (PBITMAPINFO)(pszName + MAX_NAMESTR + 1);
  389.     pbBuffer = (PBYTE)&pbmi->argbColor[1 << bih.cBitCount];
  390.     *(PBITMAPINFOHEADER)pbmi = bih;
  391.  
  392.     size.cx = (LONG)bih.cx;
  393.     size.cy = (LONG)bih.cy;
  394.     hdc = CreateDC((PSZ)"MEMORY", (HDC)NULL);
  395.     hps = GpiCreatePS(hab, hdc, &size,
  396.             PU_ARBITRARY | GPIT_NORMAL | GPIA_ASSOC );
  397.     GpiSetBitmap(hps, pcd->hbm);
  398.     GpiQueryBitmapBits(hps, 0L, (LONG)bih.cy, (PBYTE)pbBuffer,
  399.             (PBITMAPINFO2)pbmi);
  400.     GpiAssociate(hps, NULL);
  401.     GpiDestroyPS(hps);
  402.     DevCloseDC(hdc);
  403.  
  404.     if (!DdeClientXfer(pszName,
  405.             (LONG)(cbBuffer + cbBitmapInfo + MAX_NAMESTR + 1),
  406.             pcd->npUL->hConvMsg,
  407.             msgTopicItemList[IIL_BMPXFER].hszItem,
  408.             fmtBmapPkt, XTYP_POKE, ulTimeout, 0L)) {
  409.         MyPostError(DdeGetLastError());
  410.     }
  411.  
  412.     DosFreeSeg(sel);
  413.     GpiDeleteBitmap(pcd->hbm);
  414.     pcd->hbm = NULL;
  415.     WinSetRectEmpty(hab, &pcd->rcl);
  416.     return(TRUE);
  417. }
  418.  
  419.  
  420. HBITMAP SnapRegion(hps, prcl)
  421. HPS hps;
  422. PRECTL prcl;
  423. {
  424.     HDC hdc;
  425.     HBITMAP hbm, hbmOld;
  426.     BITMAPINFOHEADER bih;
  427.     POINTL rgpt[3];
  428.     HPS hpsMem;
  429.     SIZEL size;
  430.  
  431.     SortRect(prcl, prcl);
  432.     WinInflateRect(hab, prcl, -1, -1);
  433.  
  434.     size.cx = (USHORT)(prcl->xRight - prcl->xLeft);
  435.     size.cy = (USHORT)(prcl->yTop - prcl->yBottom);
  436.  
  437.     /* Create a memory DC */
  438.     hdc = CreateDC((PSZ)"MEMORY", (HDC)NULL);
  439.  
  440.     /* create a memory PS */
  441.     hpsMem = GpiCreatePS(hab, hdc, &size,
  442.             PU_ARBITRARY | GPIT_NORMAL | GPIA_ASSOC );
  443.  
  444.     /* Create a bitmap */
  445.     bih.cbFix = sizeof(BITMAPINFOHEADER);
  446.     bih.cx = (SHORT)size.cx;
  447.     bih.cy = (SHORT)size.cy;
  448.     bih.cPlanes = 1;
  449.     bih.cBitCount = 8;
  450.     hbm = GpiCreateBitmap(hpsMem, (PBITMAPINFOHEADER2)&bih, 0L, 0, 0);
  451.     if (hbm == GPI_ERROR) 
  452.         return(0);
  453.     
  454.     /* put the bitmap into the memory PS */
  455.     hbmOld = GpiSetBitmap(hpsMem, hbm);
  456.  
  457.     /* copy the window to the memory PS */
  458.     rgpt[0].x = 0;
  459.     rgpt[0].y = 0;
  460.     rgpt[1].x = size.cx;
  461.     rgpt[1].y = size.cy;
  462.     rgpt[2].x = prcl->xLeft;
  463.     rgpt[2].y = prcl->yBottom;
  464.     GpiBitBlt(hpsMem, hps, 3L, (PPOINTL)&rgpt[0], ROP_SRCCOPY, 0L);
  465.  
  466.     /* free the bitmap */
  467.     GpiSetBitmap(hpsMem, hbmOld);
  468.  
  469.     /* destroy the memory DC */
  470.     GpiAssociate(hpsMem, NULL);
  471.     GpiDestroyPS(hpsMem);
  472.     DevCloseDC(hdc);
  473.     return(hbm);
  474. } /* end snapregion */
  475.  
  476.  
  477. HDC CreateDC(lpszDriver, hdcCompat)
  478. PSZ  lpszDriver;
  479. HDC hdcCompat;
  480. {
  481.     struct {
  482.         ULONG FAR *lpLogAddr;
  483.         PSZ  lpszDriver;
  484.     } opendc;
  485.  
  486.     opendc.lpLogAddr = NULL;
  487.     opendc.lpszDriver = lpszDriver;
  488.  
  489.     return((HDC)DevOpenDC(hab, OD_MEMORY, (PSZ)"*", 2L,
  490.             (PDEVOPENDATA)&opendc, hdcCompat));
  491. }
  492.  
  493.  
  494. void DrawRgn(hps, prcl)
  495. HPS hps;
  496. PRECTL prcl;
  497. {
  498.     RECTL rclSorted;
  499.     
  500.     SortRect(prcl, &rclSorted);
  501.     WinDrawBorder(hps, &rclSorted, 1, 1, SYSCLR_WINDOW, SYSCLR_WINDOW,
  502.             DB_DESTINVERT | DB_STANDARD);
  503. }
  504.  
  505. void SortRect(prcl, prclSorted)
  506. PRECTL prcl;
  507. PRECTL prclSorted;
  508. {
  509.     LONG l;
  510.  
  511.     WinCopyRect(hab, prclSorted, prcl);
  512.     if (prclSorted->yTop < prclSorted->yBottom) {
  513.         l = prclSorted->yBottom;
  514.         prclSorted->yBottom = prclSorted->yTop;
  515.         prclSorted->yTop = l;
  516.     }
  517.     
  518.     if (prclSorted->xRight < prclSorted-> xLeft) {
  519.         l = prclSorted->xRight;
  520.         prclSorted->xRight = prclSorted->xLeft;
  521.         prclSorted->xLeft = l;
  522.     }
  523. }
  524.  
  525.  
  526.  
  527.  
  528.