home *** CD-ROM | disk | FTP | other *** search
- /*
- User code function that changes the Authorware palette at runtime.
-
- Copyright 1987-1992 Macromedia, Inc.
-
- Revision History:
- 7/25/91 - Initial version
- 11/29/92 - Add loading palettes from DIB files.
-
- This file defines two user code functions:
-
- 1. error := SetPalette(WindowHandle, <palettefile>)
-
- Sets the Authorware palette to the one in <palettefile>. The file
- must be a RIFF palette file (.PAL) as created by the Microsoft
- PalEdit application, or a DIB or BMP file. In the case of DIB or
- BMP files, the palette is extracted and the image information
- is ignored.
-
- The first argument is the handle of the Authorware presentation
- window contained in the WindowHandle system variable.
-
- Returns:
- 0 if the palette was loaded
- 1 if the window handle was invalid
- 2 if insufficient memory to load the palette
- 3 if the file was not found
- 4 if the file was an unsupported type or an I/O error occurred
-
- 2. error := ResetPalette(WindowHandle)
-
- Resets the Authorware palette to the default (as loaded in File Setup).
-
- The argument is the handle of the Authorware presentation
- window contained in the WindowHandle system variable.
-
- Returns:
- 0 if the palette was reset
- 1 if the window handle was invalid
-
-
- Notes:
- Setting and Reseting the palette is done by sending custom messages
- to the Authorware presentation window. These messages are:
-
- APWC_SETPAL - set the palette
- APWC_RESETPAL - reset the palette
- */
-
- #define WINVER 0x0300
- #include <windows.h>
- #include <mmsystem.h>
- #include "apwapi.h"
-
-
- // Error codes returned by these functions.
-
- #define PAL_OK 0 // palette loaded/reset ok
- #define PAL_BADWIND 1 // bad window handle
- #define PAL_NOMEM 2 // not enough memory
- #define PAL_NOFILE 3 // specified file not found
- #define PAL_BADTYPE 4 // unsupported file type, bad file, or I/O error
-
-
- typedef struct { // RIFF PAL file header
- FOURCC riffcc;
- DWORD riffsize;
- FOURCC palform;
- FOURCC datacc;
- DWORD datasize;
- } RIFFHEADER;
-
- #define FOURCC_PAL mmioFOURCC('P', 'A', 'L', ' ')
- #define FOURCC_DATA mmioFOURCC('d', 'a', 't', 'a')
-
- typedef struct { // LOGPALETTE with 256 entries
- LOGPALETTE lp; // includes one entry
- PALETTEENTRY extra[255]; // space for 255 more
- } LOGPAL256;
-
-
-
- static int LoadRiffPalette( HANDLE fh, HPALETTE FAR *lphPal );
- static int LoadDibPalette( HANDLE fh, HPALETTE FAR *lphPal );
- static unsigned DibNumColors( LPBITMAPINFOHEADER lpbi );
-
-
-
- short FAR PASCAL LibMain( HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine )
- /*
- DLL startup routine. Nothing to do.
- */
- {
- return 1;
- }
-
-
-
- short FAR PASCAL WEP( short bSystemExit )
- /*
- DLL cleanup routine. Nothing to do.
- */
- {
- return 1;
- }
-
-
-
- short FAR PASCAL SetPalette( HWND hWnd, LPSTR szFileName )
- /*
- Changes the Authorware palette to the one found in the specified file.
- Returns an error code as described above.
-
- Parameters:
- hWnd Authorware presentation window handle
- szFileName Name of a RIFF palette, DIB or BMP file.
- */
- {
- short rv;
- HPALETTE hPal;
- OFSTRUCT of;
- HANDLE fh;
-
- if (hWnd == NULL || !IsWindow(hWnd))
- rv = PAL_BADWIND;
-
- // Open the file
- else if ((fh = OpenFile(szFileName, &of, OF_READ)) == -1)
- rv = PAL_NOFILE;
-
- else
- {
- // First try to load as a RIFF palette.
- rv = LoadRiffPalette(fh, &hPal);
-
- // If it's not a RIFF palette, see if it's a DIB
- if (rv == PAL_BADTYPE)
- rv = LoadDibPalette(fh, &hPal);
-
- // Give palette to APW
- if (rv == PAL_OK)
- if (!APWSetPalette(hWnd, hPal, SP_DEFAULT))
- rv = PAL_NOMEM;
-
- _lclose(fh);
- }
-
- return rv;
- }
-
-
-
- BOOL FAR PASCAL ResetPalette( HWND hWnd )
- /*
- Restores the default course palette as loaded in File Setup.
- Returns an error code as described above.
- */
- {
- if (hWnd == NULL || !IsWindow(hWnd))
- return PAL_BADWIND;
-
- APWResetPalette(hWnd);
- return PAL_OK;
- }
-
-
-
- static int LoadRiffPalette( HANDLE fh, HPALETTE FAR *lphPal )
- /*
- This function extracts the palette from a RIFF PAL file.
-
- Returns:
- PAL_OK - palette handle in *lphPal
- PAL_NOMEM - insufficient memory
- PAL_BADTYPE - not a RIFF PAL
-
- In RIFF notation, a RIFF palette file is defined as:
-
- RIFF ( 'PAL' data(<palette:LOGPALETTE>) )
- */
- {
- LOGPAL256 logPal;
- RIFFHEADER riff;
-
- *lphPal = NULL;
-
- // read header
- if (_lread(fh, (LPSTR)&riff, sizeof(riff)) != sizeof(riff))
- return PAL_BADTYPE;
- if (riff.riffcc != FOURCC_RIFF || riff.palform != FOURCC_PAL
- || riff.datacc != FOURCC_DATA)
- return PAL_BADTYPE;
-
-
- // read as much data as is present, but at most 256 entries
- _lread(fh, (LPSTR)&logPal, sizeof(LOGPALETTE) + 255*sizeof(PALETTEENTRY));
-
- // create the palette and we're done
- *lphPal = CreatePalette(&logPal.lp);
- if (*lphPal == NULL)
- return PAL_NOMEM;
- else
- return PAL_OK;
- }
-
-
-
- static int LoadDibPalette( HANDLE fh, HPALETTE FAR *lphPal )
- /*
- This function extracts the palette from a DIB file.
-
- Returns:
- PAL_OK - palette handle in *lphPal
- PAL_NOMEM - insufficient memory
- PAL_BADTYPE - not a dib file or no palette
- */
- {
- BITMAPFILEHEADER fhead;
- BITMAPINFOHEADER bmi;
- LOGPAL256 logPal;
- unsigned palEntries;
- unsigned palSize;
- unsigned i;
- BYTE blue;
-
- *lphPal = NULL;
-
- // read the file header & check file type
- if (_llseek(fh, 0, 0) == -1)
- return PAL_BADTYPE;
- if (_lread(fh, (LPSTR)&fhead, sizeof(fhead)) < sizeof(fhead))
- return PAL_BADTYPE;
- if (fhead.bfType != 0x4d42)
- return PAL_BADTYPE;
-
- // read the bitmap header & make sure it's a Windows DIB (not OS/2)
- if (_lread(fh, (LPSTR)&bmi, sizeof(bmi)) < sizeof(bmi))
- return PAL_BADTYPE;
- if (bmi.biSize != sizeof(bmi))
- return PAL_BADTYPE;
-
- // read the palette
- palEntries = DibNumColors(&bmi);
- if (palEntries == 0)
- return PAL_BADTYPE;
- palSize = palEntries * sizeof(RGBQUAD);
- if (_lread(fh, (LPSTR)(logPal.lp.palPalEntry), palSize) < palSize)
- return PAL_BADTYPE;
-
- logPal.lp.palVersion = 0x300;
- logPal.lp.palNumEntries = palEntries;
-
- // we just wrote the DIB palette, consisting of RGBQUADs into a
- // log palette, consisting of PALETTEENTRYs so convert from quads
- // to pal entries by exchanging red & blue.
-
- for (i = 0; i < palEntries; i++)
- {
- blue = logPal.lp.palPalEntry[i].peRed;
- logPal.lp.palPalEntry[i].peRed = logPal.lp.palPalEntry[i].peBlue;
- logPal.lp.palPalEntry[i].peBlue = blue;
- }
-
- // create the palette and we're done
- *lphPal = CreatePalette(&logPal.lp);
- if (*lphPal == NULL)
- return PAL_NOMEM;
- else
- return PAL_OK;
- }
-
-
-
- static unsigned DibNumColors( LPBITMAPINFOHEADER lpbi )
- /*
- Returns the # of entries in the DIB's color table.
- */
- {
- if (lpbi->biClrUsed != 0)
- return (int)(lpbi->biClrUsed);
-
- else
- {
- switch (lpbi->biBitCount)
- {
- case 1:
- return 2;
- case 4:
- return 16;
- case 8:
- return 256;
- default:
- /* A 24 bit DIB has no color table */
- return 0;
- }
- }
- }
-