Mercurial > dwindows
comparison os2/dw.c @ 241:00d2b1bcf036
Added dw_pixmap_new_from_file().
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Wed, 19 Feb 2003 06:00:54 +0000 |
parents | 5592cdeb29cf |
children | 9ea4ac9a097f |
comparison
equal
deleted
inserted
replaced
240:afc0d5f5d74f | 241:00d2b1bcf036 |
---|---|
20 #include <stdarg.h> | 20 #include <stdarg.h> |
21 #include <stddef.h> | 21 #include <stddef.h> |
22 #include <ctype.h> | 22 #include <ctype.h> |
23 #include <process.h> | 23 #include <process.h> |
24 #include <time.h> | 24 #include <time.h> |
25 #include <io.h> | |
25 #ifndef __EMX__ | 26 #ifndef __EMX__ |
26 #include <direct.h> | 27 #include <direct.h> |
27 #endif | 28 #endif |
28 #include "dw.h" | 29 #include "dw.h" |
29 | 30 |
6732 | 6733 |
6733 return pixmap; | 6734 return pixmap; |
6734 } | 6735 } |
6735 | 6736 |
6736 /* | 6737 /* |
6738 * Creates a pixmap from a file. | |
6739 * Parameters: | |
6740 * handle: Window handle the pixmap is associated with. | |
6741 * filename: Name of the file, omit extention to have | |
6742 * DW pick the appropriate file extension. | |
6743 * (BMP on OS/2 or Windows, XPM on Unix) | |
6744 * Returns: | |
6745 * A handle to a pixmap or NULL on failure. | |
6746 */ | |
6747 HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename) | |
6748 { | |
6749 HFILE BitmapFileHandle = NULLHANDLE; /* handle for the file */ | |
6750 ULONG OpenAction = 0; | |
6751 PBYTE BitmapFileBegin; /* pointer to the first byte of bitmap data */ | |
6752 FILESTATUS BitmapStatus; | |
6753 ULONG cbRead; | |
6754 PBITMAPFILEHEADER2 pBitmapFileHeader; | |
6755 PBITMAPINFOHEADER2 pBitmapInfoHeader; | |
6756 ULONG ScanLines, ulFlags; | |
6757 HPS hps; | |
6758 HDC hdc; | |
6759 SIZEL sizl = { 0, 0 }; | |
6760 HPIXMAP pixmap; | |
6761 char *file = alloca(strlen(filename) + 5); | |
6762 | |
6763 if(!file || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) | |
6764 return NULL; | |
6765 | |
6766 strcpy(file, filename); | |
6767 | |
6768 /* check if we can read from this file (it exists and read permission) */ | |
6769 if(access(file, 04) != 0) | |
6770 { | |
6771 /* Try with .bmp extention */ | |
6772 strcat(file, ".bmp"); | |
6773 if(access(file, 04) != 0) | |
6774 { | |
6775 free(pixmap); | |
6776 return NULL; | |
6777 } | |
6778 } | |
6779 | |
6780 /* open bitmap file */ | |
6781 DosOpen(filename, &BitmapFileHandle, &OpenAction, 0L, | |
6782 FILE_ARCHIVED | FILE_NORMAL | FILE_READONLY, | |
6783 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS, | |
6784 OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY | | |
6785 OPEN_FLAGS_NOINHERIT, 0L); | |
6786 if(!BitmapFileHandle) | |
6787 { | |
6788 free(pixmap); | |
6789 return NULL; | |
6790 } | |
6791 | |
6792 /* find out how big the file is */ | |
6793 DosQueryFileInfo(BitmapFileHandle, 1, &BitmapStatus, | |
6794 sizeof(BitmapStatus)); | |
6795 | |
6796 /* allocate memory to load the bitmap */ | |
6797 DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)BitmapStatus.cbFile, | |
6798 PAG_READ | PAG_WRITE | PAG_COMMIT); | |
6799 | |
6800 /* read bitmap file into memory buffer */ | |
6801 DosRead(BitmapFileHandle, (PVOID)BitmapFileBegin, | |
6802 BitmapStatus.cbFile, &cbRead); | |
6803 | |
6804 /* access first bytes as bitmap header */ | |
6805 pBitmapFileHeader = (PBITMAPFILEHEADER2)BitmapFileBegin; | |
6806 | |
6807 /* check if it's a valid bitmap data file */ | |
6808 if((pBitmapFileHeader->usType != BFT_BITMAPARRAY) && | |
6809 (pBitmapFileHeader->usType != BFT_BMAP)) | |
6810 { | |
6811 /* free memory of bitmap file buffer */ | |
6812 DosFreeMem(BitmapFileBegin); | |
6813 /* close the bitmap file */ | |
6814 DosClose(BitmapFileHandle); | |
6815 return NULL; | |
6816 } | |
6817 | |
6818 /* check if it's a file with multiple bitmaps */ | |
6819 if(pBitmapFileHeader->usType == BFT_BITMAPARRAY) | |
6820 { | |
6821 /* we'll just use the first bitmap and ignore the others */ | |
6822 pBitmapFileHeader = &(((PBITMAPARRAYFILEHEADER2)BitmapFileBegin)->bfh2); | |
6823 } | |
6824 | |
6825 /* set pointer to bitmap information block */ | |
6826 pBitmapInfoHeader = &pBitmapFileHeader->bmp2; | |
6827 | |
6828 /* find out if it's the new 2.0 format or the old format */ | |
6829 /* and query number of lines */ | |
6830 if(pBitmapInfoHeader->cbFix == sizeof(BITMAPINFOHEADER)) | |
6831 ScanLines = (ULONG)((PBITMAPINFOHEADER)pBitmapInfoHeader)->cy; | |
6832 else | |
6833 ScanLines = pBitmapInfoHeader->cy; | |
6834 | |
6835 /* now we need a presentation space, get it from static control */ | |
6836 hps = WinGetPS(handle); | |
6837 | |
6838 hdc = GpiQueryDevice(hps); | |
6839 ulFlags = GpiQueryPS(hps, &sizl); | |
6840 | |
6841 pixmap->handle = handle; | |
6842 pixmap->hdc = DevOpenDC(dwhab, OD_MEMORY, "*", 0L, NULL, hdc); | |
6843 pixmap->hps = GpiCreatePS (dwhab, pixmap->hdc, &sizl, ulFlags | GPIA_ASSOC); | |
6844 | |
6845 pixmap->width = pBitmapInfoHeader->cx; pixmap->height = pBitmapInfoHeader->cy; | |
6846 | |
6847 /* create bitmap now using the parameters from the info block */ | |
6848 pixmap->hbm = GpiCreateBitmap(pixmap->hps, pBitmapInfoHeader, 0L, NULL, NULL); | |
6849 | |
6850 /* select the new bitmap into presentation space */ | |
6851 GpiSetBitmap(pixmap->hps, pixmap->hbm); | |
6852 | |
6853 /* now copy the bitmap data into the bitmap */ | |
6854 GpiSetBitmapBits(pixmap->hps, 0L, ScanLines, | |
6855 BitmapFileBegin + pBitmapFileHeader->offBits, | |
6856 (PBITMAPINFO2)pBitmapInfoHeader); | |
6857 | |
6858 /* free memory of bitmap file buffer */ | |
6859 DosFreeMem(BitmapFileBegin); | |
6860 /* close the bitmap file */ | |
6861 DosClose(BitmapFileHandle); | |
6862 | |
6863 return pixmap; | |
6864 } | |
6865 | |
6866 /* | |
6737 * Creates a pixmap from internal resource graphic specified by id. | 6867 * Creates a pixmap from internal resource graphic specified by id. |
6738 * Parameters: | 6868 * Parameters: |
6739 * handle: Window handle the pixmap is associated with. | 6869 * handle: Window handle the pixmap is associated with. |
6740 * id: Resource ID associated with requested pixmap. | 6870 * id: Resource ID associated with requested pixmap. |
6741 * Returns: | 6871 * Returns: |