/****************************** Module Header ******************************\
* Module Name: HELLO.C
*
* Example of a simple OS/2 Presentation Manager application
*
* Created by Microsoft Corp., 1988
*
\***************************************************************************/

#define INCL_PM
#define INCL_DOS
#include <os2.h>
#include <stdio.h>
#include "hello.h"

HAB     hAB;
HMQ     hmqHello;
HWND    hwndHello;
HWND    hwndHelloFrame;

HPS	hMemoryPS;

CHAR    szClassName[] = "Hello";
CHAR    szMessage[]   = "Hello World";


/***************************** Private Function ****************************\
*
* Hello World Main procedure
*
* Effects: Set globals   - hAB        Handle to Application Anchor Block
*			 - hmqHello   Handle to application window msg queue
*
* Warnings: None
*
\***************************************************************************/

int cdecl main()
{
    QMSG    qmsg;
    ULONG   ctldata;
    SIZEL   sizl;
    HPS     hWindowPS;
    HDC     hWindowDC;
    HDC     hMemoryDC;
    HBITMAP hSampleBitmap;

    hAB = WinInitialize(NULL);

    hmqHello = WinCreateMsgQueue(hAB, 0);

    if (!WinRegisterClass(hAB,
			  (PCH)szClassName,
			  (PFNWP)HelloWndProc,
			  CS_SYNCPAINT | CS_SIZEREDRAW,
			  0))
	return(0);

    ctldata = FCF_STANDARD & ~FCF_MENU & ~FCF_ACCELTABLE;

    hwndHelloFrame = WinCreateStdWindow(HWND_DESKTOP,
					0L,
					&ctldata,
					(PCH)szClassName,
					NULL,
					0L,
					(HMODULE)NULL,
					HELLOICON,
					(HWND FAR *)&hwndHello);

    /* Go build a memory DC and selected bitmap. */
    hWindowPS = WinGetPS(hwndHello);
    hWindowDC = GpiQueryDevice(hWindowPS);

    sizl.cx = 0L;
    sizl.cy = 0L;
    hMemoryDC = DevOpenDC(hAB, OD_MEMORY, "*", 0L, NULL, hWindowDC);
    hMemoryPS = GpiCreatePS(hAB, hMemoryDC, &sizl,
			    PU_PELS | GPIT_MICRO | GPIA_ASSOC);
    hSampleBitmap = GpiLoadBitmap(hMemoryPS, NULL,
				  ID_SAMPLE, SAMPLE_WIDTH, SAMPLE_HEIGHT);
    GpiSetBitmap(hMemoryPS, hSampleBitmap);

    WinReleasePS(hWindowPS);


    WinShowWindow(hwndHelloFrame, TRUE);

    /* Poll messages from event queue */

    while(WinGetMsg(hAB, (PQMSG)&qmsg, (HWND)NULL, 0, 0))
    {
	WinDispatchMsg(hAB, (PQMSG)&qmsg);
    }

    GpiSetBitmap(hMemoryPS, NULL);
    GpiDeleteBitmap(hSampleBitmap);
    /* We don't/can't disassociate the DC and PS since it's a micro PS. */
    GpiDestroyPS(hMemoryPS);
    DevCloseDC(hMemoryDC);

    WinDestroyWindow(hwndHelloFrame);
    WinDestroyMsgQueue(hmqHello);
    WinTerminate(hAB);
}



/***************************** Private Function ****************************\
*
* Procedure which processes window messages
*
* Effects: None
*
* Warnings: None
*
\***************************************************************************/

MRESULT EXPENTRY _loadds HelloWndProc(hWnd, msg, mp1, mp2)
HWND   hWnd;
USHORT msg;
MPARAM mp1;
MPARAM mp2;
{
    HPS 	hPS;
    POINTL	pt;
    POINTL	pts[4];
    static char Buf[128];
    FONTMETRICS fm;
    LONG	LogBackColor;
    LONG	LogForColor;
    LONG	ImgBackColor;
    LONG	ImgForColor;
    IMAGEBUNDLE ib;
    BOOL	ScreenBMapInverted;

    switch (msg)
    {
    case WM_CLOSE:
	WinPostMsg(hWnd, WM_QUIT, 0L, 0L);
        break;

    case WM_PAINT:

	hPS = WinBeginPaint(hWnd, (HPS)NULL, (PWRECT)NULL);

	/* Find out about screen inversion. */
	/* We need to call this before we tweek the PS at all. */
	ScreenBMapInverted = SenseBMapInversion(hPS);

#if	0
	ib.lColor     = 3;
	ib.lBackColor = 5;
	GpiSetAttrs(hPS, PRIM_IMAGE, IBB_COLOR | IBB_BACK_COLOR,
		    0L, (PBUNDLE)&ib);
#endif

	/* Put out the bitmap. */
	pts[0].x = 0L;
	pts[0].y = 0L;
	pts[1].x = pts[0].x + SAMPLE_WIDTH;
	pts[1].y = pts[0].y + SAMPLE_HEIGHT;
	pts[2].x = 0L;
	pts[2].y = 0L;
	GpiBitBlt(hPS, hMemoryPS,
		  3L, pts,
		  (ScreenBMapInverted ? ROP_NOTSRCCOPY : ROP_SRCCOPY),
		  0L);


	/* Tell what happened. */
	GpiQueryFontMetrics(hPS, (LONG)sizeof(fm), &fm);

	pt.x = 0L;
	pt.y = SAMPLE_HEIGHT + (fm.lExternalLeading + fm.lMaxBaselineExt);
	GpiCharStringAt(hPS, (PPOINTL)&pt,
			(LONG)sprintf(Buf,
				      "The device is %sinverted",
				      (ScreenBMapInverted ? "" : "not ")),
			Buf);
	pt.y += (fm.lExternalLeading + fm.lMaxBaselineExt);

	/* Output the various foreground and background colors. */

	if (GpiQueryLogColorTable(hPS, 0L, CLR_BACKGROUND, 1L, &LogBackColor)
						    != 1L) DosBeep(400, 400);
	if (GpiQueryLogColorTable(hPS, 0L, CLR_NEUTRAL	 , 1L, &LogForColor)
						    != 1L) DosBeep(800, 400);
	GpiCharStringAt(hPS, (PPOINTL)&pt,
			(LONG)sprintf(Buf,
				      "CLR_* back & for colors = %08lX %08lX",
				      LogBackColor, LogForColor),
			Buf);
	pt.y += (fm.lExternalLeading + fm.lMaxBaselineExt);

	GpiQueryAttrs(hPS, PRIM_IMAGE,
		      IBB_COLOR | IBB_BACK_COLOR, (PBUNDLE)&ib);
	GpiCharStringAt(hPS, (PPOINTL)&pt,
			(LONG)sprintf(Buf,
				      "Image back & for indeces = %08lX %08lX",
				      ib.lBackColor, ib.lColor),
			Buf);
	pt.y += (fm.lExternalLeading + fm.lMaxBaselineExt);

	if (GpiQueryLogColorTable(hPS, 0L, ib.lBackColor, 1L, &ImgBackColor)
						    != 1L) DosBeep(400, 400);
	if (GpiQueryLogColorTable(hPS, 0L, ib.lColor	, 1L, &ImgForColor)
						    != 1L) DosBeep(800, 400);
	GpiCharStringAt(hPS, (PPOINTL)&pt,
			(LONG)sprintf(Buf,
				      "Image back & for cols = %08lX %08lX",
				      ImgBackColor, ImgForColor),
			Buf);
	pt.y += (fm.lExternalLeading + fm.lMaxBaselineExt);

	/* The default colors are black on white. Check it out. */
	GpiCharStringAt(hPS, (PPOINTL)&pt,
			(LONG)sprintf(Buf,
				      "The screen colors are %s",
				      (((LogBackColor == 0xFFFFFF) &&
					(LogForColor  == 0x000000)) ?
							       "default" :
							       "Altered")),
			Buf);

	WinEndPaint(hPS);
        break;

    case WM_ERASEBACKGROUND:
	return(TRUE);
        break;

    default:
	return(WinDefWindowProc(hWnd, msg, mp1, mp2));
        break;
    }
    return(0L);
}


BOOL FAR PASCAL SenseBMapInversion(hSensePS)
HPS	    hSensePS;
{
    SIZEL		sizl;
    HDC 		hSenseDC;
    HDC 		hLoadDC;
    HPS 		hLoadPS;
    HBITMAP		hTestBitmap;
    HDC 		hDestDC;
    HPS 		hDestPS;
    HBITMAP		hDestBitmap;
    LONG		Info[2];
    BITMAPINFOHEADER	bmpFormat;
    POINTL		pt;
    POINTL		pts[4];
    IMAGEBUNDLE 	ib;
    LONG		BackPel;
    LONG		ForPel;
    BOOL		Inverted;

    /* Create two memory DCs and PSs compatible with the passed PS. */

    hSenseDC = GpiQueryDevice(hSensePS);

    sizl.cx = 0L;
    sizl.cy = 0L;

    hLoadDC = DevOpenDC(hAB, OD_MEMORY, "*", 0L, NULL, hSenseDC);
    hLoadPS = GpiCreatePS(hAB, hLoadDC, &sizl,
			  PU_PELS | GPIT_MICRO | GPIA_ASSOC);
    hTestBitmap = GpiLoadBitmap(hLoadPS, NULL,
				ID_TESTINVT, TESTINVT_WIDTH, TESTINVT_HEIGHT);
    GpiSetBitmap(hLoadPS, hTestBitmap);

    hDestDC = DevOpenDC(hAB, OD_MEMORY, "*", 0L, NULL, hSenseDC);
    hDestPS = GpiCreatePS(hAB, hDestDC, &sizl,
			  PU_PELS | GPIT_MICRO | GPIA_ASSOC);
    GpiQueryDeviceBitmapFormats(hSensePS, 2L, Info);
    bmpFormat.cbFix = sizeof(BITMAPINFOHEADER);
    bmpFormat.cx = TESTINVT_WIDTH;
    bmpFormat.cy = TESTINVT_HEIGHT;
    bmpFormat.cPlanes = (USHORT)Info[0];
    bmpFormat.cBitCount = (USHORT)Info[1];
    hDestBitmap = GpiCreateBitmap(hSensePS, &bmpFormat, 0L, NULL, NULL);
    GpiSetBitmap(hDestPS, hDestBitmap);

    /* Now we come to the good stuff. We blit from the test bitmap
       to another bitmap compatible with the sense PS/DC and look to see
       if it got inverted. */

    pts[0].x = 0L;
    pts[0].y = 0L;
    pts[1].x = pts[0].x + TESTINVT_WIDTH;
    pts[1].y = pts[0].y + TESTINVT_HEIGHT;
    pts[2].x = 0L;
    pts[2].y = 0L;
    GpiBitBlt(hDestPS, hLoadPS, 3L, pts, ROP_SRCCOPY, 0L);

    /* Check how the blit came out. */
    pt.x = pt.y = 0L;
    ForPel = GpiQueryPel(hDestPS, &pt);
    pt.x = 8L;
    BackPel = GpiQueryPel(hDestPS, &pt);

    /* Query the image bundle forground and background colors. */
    GpiQueryAttrs(hSensePS,
		  PRIM_IMAGE, IBB_COLOR | IBB_BACK_COLOR, (PBUNDLE)&ib);

    /* Check for an inversion. */
    if ((ib.lBackColor == BackPel) && (ib.lColor == ForPel)) {
	Inverted = FALSE;
	}
    else if ((ib.lColor == BackPel) && (ib.lBackColor == ForPel)) {
	Inverted = TRUE;
	}
    else {
	WinMessageBox(HWND_DESKTOP, NULL,
		      "Failure to detect inversion",
		      "SenseBMapInversion", NULL, MB_OK);
	Inverted = FALSE;
	}

    /* Clean up all our stuff. */
    GpiSetBitmap(hLoadPS, NULL);
    GpiDeleteBitmap(hTestBitmap);
    /* We don't/can't disassociate the DC and PS since it's a micro PS. */
    GpiDestroyPS(hLoadPS);
    DevCloseDC(hLoadDC);

    GpiSetBitmap(hDestPS, NULL);
    GpiDeleteBitmap(hDestBitmap);
    /* We don't/can't disassociate the DC and PS since it's a micro PS. */
    GpiDestroyPS(hDestPS);
    DevCloseDC(hDestDC);

    return(Inverted);
}
