mgwin.c

#include <windows.h>
#include "mgwin.h"

/***********************************************************************
** MGRegisterClass - A simplified, standard windows-class register function
** Parameters: hInst - Instance handle for class
**             szClassName - Class name to register as (probably sxAppName)
**             WndProc - The window procedure for the class
**             MenuID - Integer ID for menu, or 0 if none
**             hIcon - Icon handle, or 0 for default
** Returns: Either MF_CHECKED or MF_UNCHECKED, the previous state of
**          the check status, or 0xFFFFFFFF if not a valid ID or menu.
** Author:  MG 2/5/98
************************************************************************/
int MGRegisterClass( HINSTANCE hInst, char* szClassName, WNDPROC WndProc, UINT MenuID, HICON hIcon )
{
	WNDCLASS WndClass;
	ATOM Ret;

	// Define Window Class
	WndClass.hInstance = hInst;
	WndClass.lpszClassName = szClassName;
	WndClass.lpfnWndProc = WndProc;
	WndClass.style = 0;	// No special styles

	// Set resources for the program: Icon, Cursor, Menu (we have none), and background
	if( !hIcon )
		WndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	else
		WndClass.hIcon = hIcon;
	WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
	// Specify the menu, by it's resource ID:
	if( !MenuID )
		WndClass.lpszMenuName = NULL;
	else
		WndClass.lpszMenuName = MAKEINTRESOURCE(MenuID);

	//WndClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
	WndClass.hbrBackground = CreateSolidBrush( GetSysColor(COLOR_WINDOW));


	WndClass.cbClsExtra = 0; // 'Extra' information about this window,
	WndClass.cbWndExtra = 0; // our program has none.

	// Register the class (this includes the 'WndProc')
	if( !(Ret=RegisterClass( &WndClass )) )
		MessageBox( NULL, "Error registering class", NULL, MB_ICONEXCLAMATION|MB_OK );
	return( Ret );
}

/***********************************************************************
** MGShowAndLoop - Shows a window, and implements a message loop
** Parameters: hWnd - Window to Show
**             nCmdShow - Show value (probably the WinMain parameter)
** Returns: Code that reminated loop
** Author:  MG 2/5/98
************************************************************************/
int MGShowAndLoop( HWND hWnd, UINT nCmdShow )
{
	MSG Msg;
	ShowWindow( hWnd, nCmdShow );
	UpdateWindow( hWnd );

	while( GetMessage( &Msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &Msg );
		DispatchMessage( &Msg );
	}

	return( Msg.wParam );
}
/***********************************************************************
** MGToggleMenuCheck - Toggles a menu item check
** Parameters: Owner - Parent window of menu
**             ID - The ID of the menu item
** Returns: Either MF_CHECKED or MF_UNCHECKED, the previous state of
**          the check status, or 0xFFFFFFFF if not a valid ID or menu.
** Author:  MG 2/5/98
************************************************************************/
UINT MGToggleMenuCheck( HWND Owner, UINT ID )
{
	UINT Ret;
	HMENU hMenu = GetMenu( Owner );
	if( !hMenu )
		return( 0xFFFFFFFF );
	Ret = CheckMenuItem( hMenu, ID, MF_UNCHECKED );
	if( Ret == MF_UNCHECKED )
		CheckMenuItem( hMenu, ID, MF_CHECKED );
	return( Ret );
}

/***********************************************************************
** MyFontEnumCount - Enumeration callback function, to get font count
** Parameters: Standard windows EnumFont parameters
** Returns: 1 Always
** Comments: Used internally by GetFontCount
** Author:  MG 2/5/98
************************************************************************/
static int CALLBACK MyFontEnumCount(  LOGFONT * lplf,  TEXTMETRIC* lptm,  DWORD dwType,  LPARAM lpData  )
{
	(*(int*)lpData)++;
	return(1);
}

/***********************************************************************
** MGGetFontCount - Returns number of fonts for a DC
** Parameters: DC - The DC to get the font count for
**             Family - The desired family name, or NULL for all families
** Returns: The number of fonts for the DC
** Author:  MG 2/5/98
************************************************************************/
int MGGetFontCount( HDC DC, char *Family )
{
	int Ret=0;
	EnumFonts( DC, Family, (FONTENUMPROC)MyFontEnumCount, (DWORD)&Ret );
	return( Ret );
}


/***********************************************************************
** MyFontEnumName - Enumeration callback function, to get font name
** Parameters: Standard windows EnumFont parameters
** Returns: 0 if current font is specified font, 1 otherwise
** Comments: Used internally by GetFontName
** Author:  MG 2/5/98
************************************************************************/
static int _FontNameCount, _FontNameSize;
static int CALLBACK MyFontEnumName(  LOGFONT * lplf,  TEXTMETRIC* lptm,  DWORD dwType,  LPARAM lpData  )
{
	if( _FontNameCount==0 )
	{
		strncpy( (char*)lpData, lplf->lfFaceName, _FontNameSize );
		if( _FontNameSize != -1 )
			((char*)lpData)[_FontNameSize-1]='\0';
		return(0);
	}
	else
		_FontNameCount--;
	return(1);
}

/***********************************************************************
** MGGetFontName - Retrieves a font name by index (not sorted)
** Parameters: DC - The DC to get the fonts for
**             Family - The font family to get, of NULL for all
**             Dest - Where to store the name of the font
**             BufferSize - The amount of space available at 'Dest', or -1
**             Index - Which font to get
** Returns: Nothing
** Author:  MG 2/5/98
************************************************************************/
void MGGetFontName( HDC DC, char* Family, char* Dest, int BufferSize, int Index )
{
	_FontNameCount=Index;
	_FontNameSize=BufferSize;
	EnumFonts( DC, Family, (FONTENUMPROC)MyFontEnumName, (DWORD)Dest );
}

/***********************************************************************
** MGCreateFont - A simplified CreateFont function
** Parameters: Height - Represents font height, in device units
**             Bold - TRUE for boldface, FALSE fo rnormal
**             Italic - TRUE for italics, FALSE fo rnormal
**             Underline - TRUE for underline, FALSE fo rnormal
**             Strikeout - TRUE for strikeout, FALSE fo rnormal
**             Family - Family type, such as FF_ROMAN or FF_DECORATIVE
**             FaceName - Actualy font name, may be NULL
** Returns: Handle to new font
** Author:  MG 2/5/98
************************************************************************/
HFONT MGCreateFont( int Height, BOOL Bold, BOOL Italic, BOOL Underline, BOOL StrikeOut, DWORD Family, char* FaceName )
{
	return( CreateFont( Height, 0, 0, 0, Bold ? FW_BOLD : FW_NORMAL,
		Italic, Underline, StrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | Family, FaceName ) );
}