home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvwclip.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  10KB  |  337 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvwclip.c */
  19. /* Clipboard module for Windows GSview */
  20. #include "gvwin.h"
  21. #include "gvceps.h"
  22.  
  23. long hugewrite(HFILE hf, const void _huge *hpvBuffer, long cbBuffer);  /* in gvweps.c */
  24. void paste_to_file(void);
  25. void clip_convert(void);
  26. void clip_add_palette(void);
  27. void clip_add_ddb(void);
  28. void clip_add_dib(void);
  29.  
  30. /* copy a DIB from the clipboard to a file */
  31. void
  32. paste_to_file(void)
  33. {
  34. HGLOBAL hglobal;
  35. LPBITMAPINFOHEADER pbmih;
  36. BITMAPFILEHEADER bmfh;
  37. UINT palcolors;
  38. UINT palsize;
  39. DWORD bitmap_size;
  40. BYTE _huge *lpBits;
  41. static char output[MAXSTR];
  42. HFILE hfile;
  43.     if (!OpenClipboard(hwndimg)) {
  44.         play_sound(SOUND_ERROR);
  45.         return;
  46.     }
  47.     if (!IsClipboardFormatAvailable(CF_DIB)) {
  48.         CloseClipboard();
  49.         play_sound(SOUND_ERROR);
  50.         return;
  51.     }
  52.     hglobal = (HGLOBAL)GetClipboardData(CF_DIB);
  53.     pbmih = (LPBITMAPINFOHEADER)GlobalLock(hglobal);
  54.     palcolors = dib_pal_colors((LPBITMAP2)pbmih);
  55.     if (pbmih->biSize == sizeof(BITMAPCOREHEADER))
  56.         palsize = palcolors * sizeof(RGBTRIPLE); 
  57.     else
  58.         palsize = palcolors * sizeof(RGBQUAD);
  59.     bitmap_size = (DWORD)pbmih->biHeight *  dib_bytewidth((LPBITMAP2)pbmih);
  60.  
  61.     bmfh.bfType = ('M'<<8) | 'B';
  62.     bmfh.bfReserved1 = 0;
  63.     bmfh.bfReserved2 = 0;
  64.     bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + pbmih->biSize + palsize;
  65.     bmfh.bfSize = bmfh.bfOffBits + bitmap_size;
  66.  
  67.     if ( get_filename(output, TRUE, FILTER_BMP, NULL, IDS_TOPICEDIT)
  68.         && ((hfile = _lcreat(output, 0)) != HFILE_ERROR) ) {
  69.         hugewrite(hfile, &bmfh, sizeof(BITMAPFILEHEADER));
  70.         hugewrite(hfile, pbmih, pbmih->biSize + palsize);
  71.         lpBits =  ((BYTE _huge *)pbmih) + pbmih->biSize + palsize;
  72.         hugewrite(hfile, lpBits, bitmap_size);
  73.         _lclose(hfile);
  74.     }
  75.     GlobalUnlock(hglobal);
  76.     CloseClipboard();
  77. }
  78.  
  79. /* convert bitmap (DIB or DDB) in clipboard to */
  80. /* CF_DIB, CF_BITMAP and CF_PALETTE */
  81. void
  82. clip_convert(void)
  83. {
  84.     if (!OpenClipboard(hwndimg)) {
  85.         play_sound(SOUND_ERROR);
  86.         return;
  87.     }
  88.     if (IsClipboardFormatAvailable(CF_DIB)) {
  89.         if (!IsClipboardFormatAvailable(CF_PALETTE))
  90.         clip_add_palette();
  91.         if (!IsClipboardFormatAvailable(CF_BITMAP))
  92.             clip_add_ddb();
  93.     }
  94.     else {
  95.         if (IsClipboardFormatAvailable(CF_BITMAP)) {
  96.         clip_add_dib();
  97.             if (!IsClipboardFormatAvailable(CF_PALETTE))
  98.             clip_add_palette();
  99.         }
  100.         else 
  101.         play_sound(SOUND_ERROR);
  102.     }
  103.     CloseClipboard();
  104. }
  105.  
  106. /* Read DIB from the clipboard, create PALETTE and add to clipboard */
  107. void
  108. clip_add_palette(void)
  109. {
  110. HGLOBAL hglobal;
  111. LPBITMAPINFOHEADER pbmih;
  112. UINT palcolors;
  113. UINT palsize;
  114. int i;
  115. LPLOGPALETTE logpalette;
  116. HPALETTE hpalette;
  117. RGBQUAD FAR *prgbquad;
  118. RGBTRIPLE FAR *prgbtriple;
  119.  
  120.     if (!IsClipboardFormatAvailable(CF_DIB)) {
  121.         play_sound(SOUND_ERROR);
  122.         return;
  123.     }
  124.     hglobal = (HGLOBAL)GetClipboardData(CF_DIB);
  125.     pbmih = (LPBITMAPINFOHEADER)GlobalLock(hglobal);
  126.     palcolors = dib_pal_colors((LPBITMAP2)pbmih);
  127.     if (pbmih->biSize == sizeof(BITMAPCOREHEADER))
  128.         palsize = palcolors * sizeof(RGBTRIPLE); 
  129.     else
  130.         palsize = palcolors * sizeof(RGBQUAD);
  131.     hpalette = (HPALETTE)NULL;
  132.     if (palsize) {
  133.         /* create palette to match DIB */
  134.         logpalette = (LPLOGPALETTE) malloc( sizeof(LOGPALETTE) + 
  135.         palcolors * sizeof(PALETTEENTRY) );
  136.         if (logpalette == (LPLOGPALETTE)NULL) {
  137.         GlobalUnlock(hglobal);
  138.         play_sound(SOUND_ERROR);
  139.         return;
  140.         }
  141.         logpalette->palVersion = 0x300;
  142.         logpalette->palNumEntries = (WORD)palcolors;
  143.         prgbquad = (RGBQUAD FAR *)(((BYTE _huge *)pbmih) + pbmih->biSize);
  144.         if (pbmih->biSize == sizeof(BITMAPCOREHEADER)) {
  145.         /* OS2 format */
  146.             prgbtriple = (RGBTRIPLE FAR *)prgbquad;
  147.             for (i=0; i<palcolors; i++) {
  148.                 logpalette->palPalEntry[i].peFlags = 0;
  149.             logpalette->palPalEntry[i].peRed   = prgbtriple[i].rgbtRed;
  150.             logpalette->palPalEntry[i].peGreen = prgbtriple[i].rgbtGreen;
  151.             logpalette->palPalEntry[i].peBlue  = prgbtriple[i].rgbtBlue;
  152.             }
  153.         }
  154.         else {
  155.         /* Windows Format */
  156.             for (i=0; i<palcolors; i++) {
  157.                 logpalette->palPalEntry[i].peFlags = 0;
  158.             logpalette->palPalEntry[i].peRed   = prgbquad[i].rgbRed;
  159.             logpalette->palPalEntry[i].peGreen = prgbquad[i].rgbGreen;
  160.             logpalette->palPalEntry[i].peBlue  = prgbquad[i].rgbBlue;
  161.             }
  162.         }
  163.         hpalette = CreatePalette(logpalette);
  164.         free((void *)logpalette);
  165.         SetClipboardData(CF_PALETTE, hpalette);
  166.     }
  167.     GlobalUnlock(hglobal);
  168. }
  169.  
  170.  
  171. /* Read DIB from the clipboard, convert to DDB and add to clipboard */
  172. void
  173. clip_add_ddb(void)
  174. {
  175. HGLOBAL hglobal;
  176. LPBITMAPINFOHEADER pbmih;
  177. UINT palcolors;
  178. UINT palsize;
  179. HPALETTE hpalette;
  180. HDC hdc;
  181. HBITMAP hbitmap;
  182.  
  183.     hglobal = (HGLOBAL)GetClipboardData(CF_DIB);
  184.     pbmih = (LPBITMAPINFOHEADER)GlobalLock(hglobal);
  185.     palcolors = dib_pal_colors((LPBITMAP2)pbmih);
  186.     if (pbmih->biSize == sizeof(BITMAPCOREHEADER))
  187.         palsize = palcolors * sizeof(RGBTRIPLE); 
  188.     else
  189.         palsize = palcolors * sizeof(RGBQUAD);
  190.  
  191.     hdc = GetDC(hwndimg);
  192.     hpalette = GetClipboardData(CF_PALETTE);
  193.     if (hpalette) {
  194.         SelectPalette(hdc,hpalette,NULL);
  195.         RealizePalette(hdc);
  196.     }
  197.     hbitmap = CreateDIBitmap(hdc, pbmih, CBM_INIT,
  198.         ((BYTE _huge *)pbmih) + pbmih->biSize + palsize,
  199.         (LPBITMAPINFO)pbmih, DIB_RGB_COLORS);
  200.     ReleaseDC(hwndimg, hdc);
  201.     GlobalUnlock(hglobal);
  202.     SetClipboardData(CF_BITMAP, hbitmap);
  203. }
  204.  
  205. /* make a DIB from a BITMAP in the clipboard */
  206. /* GetDIBits won't work for 4 plane or 4 bit/pixel bitmaps */
  207. /* clipboard must be open */
  208. HGLOBAL 
  209. make_dib(void)
  210. {
  211. LPBITMAPINFOHEADER pbmih;
  212. LPBITMAPINFO pbmi;
  213. BYTE FAR *lpBits;
  214. HBITMAP hbitmap;
  215. UINT palcolors;
  216. UINT palsize;
  217. UINT byte_width;
  218. DWORD bitmap_size;
  219. HGLOBAL hglobal;
  220. HDC hdc;
  221. HDC hdc_bit;
  222. BITMAP bm;
  223. PALETTEENTRY *pe;
  224. int i;
  225.     hbitmap = GetClipboardData(CF_BITMAP);
  226.     hdc = GetDC((HWND)NULL);
  227.     hdc_bit = CreateCompatibleDC(hdc);
  228.     ReleaseDC((HWND)NULL,hdc);
  229.     GetObject(hbitmap, sizeof(BITMAP), &bm);
  230.     if (bm.bmPlanes == 4) {
  231.         HBITMAP hbitmap_new, hbitmap_old;
  232.         HDC hdc_new;
  233.         /* convert format to 1 plane, 1 bit/pixel */ 
  234.         bm.bmPlanes = 1;
  235.         bm.bmBitsPixel = 1;
  236.         hdc_new = CreateCompatibleDC(hdc_bit);
  237.         hbitmap_new = CreateBitmap(bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel, NULL);
  238.         SelectBitmap(hdc_new, hbitmap_new);
  239.         hbitmap_old = SelectBitmap(hdc_bit, hbitmap);
  240.         BitBlt(hdc_new, 0, 0, bm.bmWidth, bm.bmHeight, hdc_bit, 0, 0, SRCCOPY);
  241.         SelectBitmap(hdc_bit, hbitmap_old);
  242.         DeleteDC(hdc_new);
  243.         hbitmap = hbitmap_new;
  244.     }
  245.      byte_width = (((bm.bmWidth * bm.bmBitsPixel + 31) & ~31) >> 3);
  246.     bitmap_size = (DWORD)bm.bmHeight *  byte_width;
  247.     palcolors = 1<<(bm.bmBitsPixel * bm.bmPlanes);
  248.     palsize = palcolors * sizeof(RGBQUAD);
  249.     hglobal = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) + palsize + bitmap_size);
  250.     if (hglobal != (HGLOBAL)NULL) {
  251.         lpBits = GlobalLock(hglobal);
  252.         pbmi = (LPBITMAPINFO)lpBits;
  253.         pbmih = (LPBITMAPINFOHEADER)lpBits;
  254.         lpBits += sizeof(BITMAPINFOHEADER) + palsize;
  255.         pbmih->biSize = sizeof(BITMAPINFOHEADER);
  256.         pbmih->biWidth = bm.bmWidth;
  257.         pbmih->biHeight = bm.bmHeight;
  258.         pbmih->biPlanes = 1;
  259.         pbmih->biBitCount = (WORD)(bm.bmBitsPixel * bm.bmPlanes);
  260.         pbmih->biCompression = BI_RGB;
  261.         pbmih->biSizeImage = bitmap_size;
  262.         pbmih->biXPelsPerMeter = (int)(option.xdpi / 25.4 * 1000);
  263.         pbmih->biYPelsPerMeter = (int)(option.ydpi / 25.4 * 1000);
  264.         pbmih->biClrUsed = palcolors;
  265.         pbmih->biClrImportant = palcolors;
  266.         /* create colour table from system palette */
  267.         pe = malloc(palcolors * sizeof(PALETTEENTRY));
  268.         if (IsClipboardFormatAvailable(CF_PALETTE)) {
  269.         HPALETTE hpalette = GetClipboardData(CF_PALETTE);
  270.         i = GetObject(hpalette, sizeof(int), pe);
  271.         GetPaletteEntries(hpalette, 0, i, pe);
  272.         }
  273.         else
  274.             GetSystemPaletteEntries(hdc_bit, 0, palcolors, pe);
  275.         for (i=0; i<palcolors; i++) {
  276.         pbmi->bmiColors[i].rgbRed = pe[i].peRed;
  277.         pbmi->bmiColors[i].rgbGreen = pe[i].peGreen;
  278.         pbmi->bmiColors[i].rgbBlue = pe[i].peBlue;
  279.         pbmi->bmiColors[i].rgbReserved = 0;
  280.         }
  281.         free((void *)pe);
  282.         i = GetDIBits(hdc_bit, hbitmap, 0, (UINT)pbmih->biHeight,
  283.         lpBits, pbmi, DIB_RGB_COLORS);
  284.         GlobalUnlock(hglobal);
  285.         if (i == 0) {
  286.         GlobalFree(hglobal);
  287.         hglobal = NULL;
  288.         }
  289.     }
  290.     DeleteDC(hdc_bit);
  291.     if (hbitmap != GetClipboardData(CF_BITMAP))
  292.         DeleteBitmap(hbitmap);
  293.     return hglobal;
  294. }
  295.  
  296. /* Read DDB from the clipboard, convert to DIB and add to clipboard */
  297. void
  298. clip_add_dib(void)
  299. {
  300. HGLOBAL hglobal;
  301.     hglobal = make_dib();
  302.     if (hglobal != (HGLOBAL)NULL)
  303.             SetClipboardData(CF_DIB, hglobal);
  304. }
  305.  
  306. #ifdef NOT_USED
  307. /* not used */
  308. /* Read bitmap (CF_DIB or CF_BITMAP) from the clipboard */
  309. /* convert to Metafile Picture and add to clipboard */
  310. void
  311. clip_add_metafile(void)
  312. {
  313. LPMETAFILEPICT lpmfp;
  314. HGLOBAL hglobal;
  315. HMETAFILE hmf;
  316.     if ( (hmf = make_metafile()) == (HMETAFILE)NULL ) {
  317.         return;
  318.     }
  319.  
  320.     hglobal = GlobalAlloc(GHND | GMEM_SHARE, sizeof(METAFILEPICT)); 
  321.     lpmfp = GlobalLock(hglobal);
  322.     lpmfp->mm = MM_ANISOTROPIC;
  323.     if (bitmap_width)
  324.         lpmfp->xExt = (int)(bitmap_width / xdpi * 2540);
  325.     else 
  326.         lpmfp->xExt = 21000;    /* A4 */
  327.     if (bitmap_height)
  328.         lpmfp->yExt = (int)(bitmap_height / ydpi * 2540);
  329.     else
  330.         lpmfp->yExt = 29700;    /* A4 */
  331.     lpmfp->hMF = hmf;
  332.     GlobalUnlock(hglobal);
  333.     SetClipboardData(CF_METAFILEPICT, hglobal);
  334.     return;
  335. }
  336. #endif
  337.