changeset 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 afc0d5f5d74f
children 36013ddd3f5b
files dw.def dw.h dww.def gtk/dw.c os2/dw.c win/dw.c
diffstat 6 files changed, 234 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dw.def	Tue Feb 18 21:45:32 2003 +0000
+++ b/dw.def	Wed Feb 19 06:00:54 2003 +0000
@@ -192,6 +192,7 @@
   dw_pixmap_new                          @341
   dw_pixmap_grab                         @342
   dw_pixmap_destroy                      @343
+  dw_pixmap_new_from_file                @344
 
   dw_dialog_new                          @350
   dw_dialog_dismiss                      @351
--- a/dw.h	Tue Feb 18 21:45:32 2003 +0000
+++ b/dw.h	Wed Feb 19 06:00:54 2003 +0000
@@ -764,6 +764,7 @@
 void API dw_flush(void);
 void API dw_pixmap_bitblt(HWND dest, HPIXMAP destp, int xdest, int ydest, int width, int height, HWND src, HPIXMAP srcp, int xsrc, int ysrc);
 HPIXMAP API dw_pixmap_new(HWND handle, unsigned long width, unsigned long height, int depth);
+HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename);
 HPIXMAP API dw_pixmap_grab(HWND handle, ULONG id);
 void API dw_pixmap_destroy(HPIXMAP pixmap);
 void API dw_beep(int freq, int dur);
--- a/dww.def	Tue Feb 18 21:45:32 2003 +0000
+++ b/dww.def	Wed Feb 19 06:00:54 2003 +0000
@@ -189,6 +189,7 @@
   dw_pixmap_new                          @341
   dw_pixmap_grab                         @342
   dw_pixmap_destroy                      @343
+  dw_pixmap_new_from_file                @344
                                          
   dw_dialog_new                          @350
   dw_dialog_dismiss                      @351
--- a/gtk/dw.c	Tue Feb 18 21:45:32 2003 +0000
+++ b/gtk/dw.c	Wed Feb 19 06:00:54 2003 +0000
@@ -5049,6 +5049,58 @@
 }
 
 /*
+ * Creates a pixmap from a file.
+ * Parameters:
+ *       handle: Window handle the pixmap is associated with.
+ *       filename: Name of the file, omit extention to have
+ *                 DW pick the appropriate file extension.
+ *                 (BMP on OS/2 or Windows, XPM on Unix)
+ * Returns:
+ *       A handle to a pixmap or NULL on failure.
+ */
+HPIXMAP dw_pixmap_new_from_file(HWND handle, char *filename)
+{
+	int _locked_by_me = FALSE;
+	HPIXMAP pixmap;
+	GdkBitmap *bitmap = NULL;
+#if GTK_MAJOR_VERSION > 1
+	GdkPixbuf *pixbuf;
+#endif
+	char *file = alloca(strlen(filename) + 5);
+
+	if (!file || !(pixmap = calloc(1,sizeof(struct _hpixmap))))
+		return NULL;
+
+	strcpy(file, filename);
+
+	/* check if we can read from this file (it exists and read permission) */
+	if(access(file, 04) != 0)
+	{
+		/* Try with .xpm extention */
+		strcat(file, ".xpm");
+		if(access(file, 04) != 0)
+		{
+			free(pixmap);
+			return NULL;
+		}
+	}
+
+	DW_MUTEX_LOCK;
+#if GTK_MAJOR_VERSION > 1
+	pixbuf = gdk_pixbuf_new_from_file(file);
+
+	pixmap->width = gdk_pixbuf_get_width(pixbuf);
+	pixmap->height = gdk_pixbuf_get_height(pixbuf);
+
+	gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap->pixmap, &bitmap, 1);
+	g_object_unref(pixbuf);
+#endif
+	pixmap->handle = handle;
+	DW_MUTEX_UNLOCK;
+	return pixmap;
+}
+
+/*
  * Creates a pixmap from internal resource graphic specified by id.
  * Parameters:
  *       handle: Window handle the pixmap is associated with.
--- a/os2/dw.c	Tue Feb 18 21:45:32 2003 +0000
+++ b/os2/dw.c	Wed Feb 19 06:00:54 2003 +0000
@@ -22,6 +22,7 @@
 #include <ctype.h>
 #include <process.h>
 #include <time.h>
+#include <io.h>
 #ifndef __EMX__
 #include <direct.h>
 #endif
@@ -6734,6 +6735,135 @@
 }
 
 /*
+ * Creates a pixmap from a file.
+ * Parameters:
+ *       handle: Window handle the pixmap is associated with.
+ *       filename: Name of the file, omit extention to have
+ *                 DW pick the appropriate file extension.
+ *                 (BMP on OS/2 or Windows, XPM on Unix)
+ * Returns:
+ *       A handle to a pixmap or NULL on failure.
+ */
+HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename)
+{
+	HFILE BitmapFileHandle = NULLHANDLE; /* handle for the file */
+	ULONG OpenAction = 0;
+	PBYTE BitmapFileBegin; /* pointer to the first byte of bitmap data  */
+	FILESTATUS BitmapStatus;
+	ULONG cbRead;
+	PBITMAPFILEHEADER2 pBitmapFileHeader;
+	PBITMAPINFOHEADER2 pBitmapInfoHeader;
+	ULONG ScanLines, ulFlags;
+	HPS hps;
+	HDC hdc;
+	SIZEL sizl = { 0, 0 };
+	HPIXMAP pixmap;
+	char *file = alloca(strlen(filename) + 5);
+
+	if(!file || !(pixmap = calloc(1,sizeof(struct _hpixmap))))
+		return NULL;
+
+	strcpy(file, filename);
+
+	/* check if we can read from this file (it exists and read permission) */
+	if(access(file, 04) != 0)
+	{
+		/* Try with .bmp extention */
+		strcat(file, ".bmp");
+		if(access(file, 04) != 0)
+		{
+			free(pixmap);
+			return NULL;
+		}
+	}
+
+	/* open bitmap file */
+	DosOpen(filename, &BitmapFileHandle, &OpenAction, 0L,
+			FILE_ARCHIVED | FILE_NORMAL | FILE_READONLY,
+			OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
+			OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY |
+			OPEN_FLAGS_NOINHERIT, 0L);
+	if(!BitmapFileHandle)
+	{
+		free(pixmap);
+		return NULL;
+	}
+
+	/* find out how big the file is  */
+	DosQueryFileInfo(BitmapFileHandle, 1, &BitmapStatus,
+					 sizeof(BitmapStatus));
+
+	/* allocate memory to load the bitmap */
+	DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)BitmapStatus.cbFile,
+				PAG_READ | PAG_WRITE | PAG_COMMIT);
+
+	/* read bitmap file into memory buffer */
+	DosRead(BitmapFileHandle, (PVOID)BitmapFileBegin,
+			BitmapStatus.cbFile, &cbRead);
+
+	/* access first bytes as bitmap header */
+	pBitmapFileHeader = (PBITMAPFILEHEADER2)BitmapFileBegin;
+
+	/* check if it's a valid bitmap data file */
+	if((pBitmapFileHeader->usType != BFT_BITMAPARRAY) &&
+	   (pBitmapFileHeader->usType != BFT_BMAP))
+	{
+		/* free memory of bitmap file buffer */
+		DosFreeMem(BitmapFileBegin);
+		/* close the bitmap file */
+		DosClose(BitmapFileHandle);
+		return NULL;
+	}
+
+	/* check if it's a file with multiple bitmaps */
+	if(pBitmapFileHeader->usType == BFT_BITMAPARRAY)
+	{
+		/* we'll just use the first bitmap and ignore the others */
+		pBitmapFileHeader = &(((PBITMAPARRAYFILEHEADER2)BitmapFileBegin)->bfh2);
+	}
+
+	/* set pointer to bitmap information block */
+	pBitmapInfoHeader = &pBitmapFileHeader->bmp2;
+
+	/* find out if it's the new 2.0 format or the old format */
+	/* and query number of lines */
+	if(pBitmapInfoHeader->cbFix == sizeof(BITMAPINFOHEADER))
+		ScanLines = (ULONG)((PBITMAPINFOHEADER)pBitmapInfoHeader)->cy;
+	else
+		ScanLines = pBitmapInfoHeader->cy;
+
+	/* now we need a presentation space, get it from static control */
+	hps = WinGetPS(handle);
+
+	hdc     = GpiQueryDevice(hps);
+	ulFlags = GpiQueryPS(hps, &sizl);
+
+	pixmap->handle = handle;
+	pixmap->hdc = DevOpenDC(dwhab, OD_MEMORY, "*", 0L, NULL, hdc);
+	pixmap->hps = GpiCreatePS (dwhab, pixmap->hdc, &sizl, ulFlags | GPIA_ASSOC);
+
+	pixmap->width = pBitmapInfoHeader->cx; pixmap->height = pBitmapInfoHeader->cy;
+
+	/* create bitmap now using the parameters from the info block */
+	pixmap->hbm = GpiCreateBitmap(pixmap->hps, pBitmapInfoHeader, 0L, NULL, NULL);
+
+	/* select the new bitmap into presentation space */
+	GpiSetBitmap(pixmap->hps, pixmap->hbm);
+
+	/* now copy the bitmap data into the bitmap */
+	GpiSetBitmapBits(pixmap->hps, 0L, ScanLines,
+					 BitmapFileBegin + pBitmapFileHeader->offBits,
+					 (PBITMAPINFO2)pBitmapInfoHeader);
+
+	/* free memory of bitmap file buffer */
+	DosFreeMem(BitmapFileBegin);
+	/* close the bitmap file */
+	DosClose(BitmapFileHandle);
+
+	return pixmap;
+}
+
+/*
  * Creates a pixmap from internal resource graphic specified by id.
  * Parameters:
  *       handle: Window handle the pixmap is associated with.
--- a/win/dw.c	Tue Feb 18 21:45:32 2003 +0000
+++ b/win/dw.c	Wed Feb 19 06:00:54 2003 +0000
@@ -6684,6 +6684,55 @@
 }
 
 /*
+ * Creates a pixmap from a file.
+ * Parameters:
+ *       handle: Window handle the pixmap is associated with.
+ *       filename: Name of the file, omit extention to have
+ *                 DW pick the appropriate file extension.
+ *                 (BMP on OS/2 or Windows, XPM on Unix)
+ * Returns:
+ *       A handle to a pixmap or NULL on failure.
+ */
+HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename)
+{
+	HPIXMAP pixmap;
+	BITMAP bm;
+	HDC hdc;
+	char *file = alloca(strlen(filename) + 5);
+
+	if (!file || !(pixmap = calloc(1,sizeof(struct _hpixmap))))
+		return NULL;
+
+	strcpy(file, filename);
+
+	/* check if we can read from this file (it exists and read permission) */
+	if(access(file, 04) != 0)
+	{
+		/* Try with .bmp extention */
+		strcat(file, ".bmp");
+		if(access(file, 04) != 0)
+		{
+			free(pixmap);
+			return NULL;
+		}
+	}
+
+	dc = GetDC(handle);
+
+	pixmap->width = width; pixmap->height = height;
+
+	pixmap->handle = handle;
+	pixmap->hbm = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
+	pixmap->hdc = CreateCompatibleDC(hdc);
+
+	SelectObject(pixmap->hdc, pixmap->hbm);
+
+	ReleaseDC(handle, hdc);
+
+	return pixmap;
+}
+
+/*
  * Creates a pixmap from internal resource graphic specified by id.
  * Parameters:
  *       handle: Window handle the pixmap is associated with.