/* ----------------------------------------------------------------------

	CNFTEST sample for Microsoft ActiveX Conferencing

	Copyright (c) 1996, Microsoft Corporation
	All rights reserved.

	MODULE: init.c

	PURPOSE: Performs application and instance specific initialization.

	COMMENTS:
		This file initializes the application. It loads MSCONF.DLL
		into memory and assign pointers to the APIs. It creates the 
		Main window with menus and toolbars.  Once the application 
		is initialized, it displays the window with the message 
		"Application Initialized."  From this point the window is 
		ready to take input from the user.  Selecting the menu items 
		executes the API.

	FUNCTIONS:
		VOID ReadPref(void)- Reads the current preferences
		VOID WritePref(void)- Writes the current preference
		VOID FCreateMsgWindow(void)- Creates the main message window
		BOOL FInitInstance(int nCmdShow)- Initializes the window data 
				and register the window class 
		BOOL FInitApp(LPSTR lpszCmd)-Initialize the application after 
				checking command line
---------------------------------------------------------------------- */

#include "main.h" //includes the necessary header files

static char _szAppTitle[] = "Microsoft Conferencing API Test Application";

// string constants (no change for international)
static char _szAppClassName[] = "MSCONFTEST";
static char _szAppName[] = "CnfTest";
static char _szIniName[] = "CnfTest.ini";

// Section Names
static char _szConfirm[]   = "Confirm";
static char _szLogFlags[]  = "Log Flags";
static char _szMainWnd[]   = "MainWnd";
static char _szSbar[]      = "Show Status Bar";
static char _szMsgFont[]   = "Message Font";
static char _szDefName[]   = "Default Name";
static char _szConferenceName[]   = "Conference Name";
static char _szAddrType[]   = "Address Type";
static char _szMediaType[]  = "Media Type";
static char _szGuid[]       = "GUID";
static char _szAppGuid[]    = "AppName";
static char _szCmdGuid[]    = "CmdLine";
static char _szDirGuid[]    = "CurrDir";
static char _szGuidRemote[] = "GUID Remote";
static char _szFileName[]   = "File Name";


/* R E A D  P R E F */
/*-------------------------------------------------------------------------
	%%Function: ReadPref

	Reads the current preferences from the CnfTest.ini file.
	Calls GetIniBool(), GetIniInt(), GetIniStr(), and GetIniHex(). 
-------------------------------------------------------------------------*/
VOID ReadPref(void)
{
	LPSTR lpsz;

	ClearStruct(&gPref);

	gPref.fConfirm    = GetIniBool(_szConfirm, fTrue);
	gPref.fSbar       = GetIniBool(_szSbar, fTrue);
	gPref.grfLog      = GetIniInt(_szLogFlags, LOG_ALWAYS);
	gPref.iAddrType   = GetIniInt(_szAddrType, 0);
	gPref.dwMediaType = GetIniInt(_szMediaType, CONF_MT_DATA);

	lpsz = GetIniStr(_szDefName, "user1");
	lstrcpy(gPref.szDefName, lpsz);
	FreePlpv(&lpsz);

	lpsz = GetIniStr(_szConferenceName, "Default");
	lstrcpy(gPref.szConferenceName, lpsz);
	FreePlpv(&lpsz);

	lpsz = GetIniStr(_szFileName, "c:\\autoexec.bat");
	lstrcpy(gPref.szFileName, lpsz);
	FreePlpv(&lpsz);

	GetIniHex(_szMainWnd, &gPref.wpMain, sizeof(WINDOWPLACEMENT));
	GetIniHex(_szMsgFont, &gPref.lf, sizeof(LOGFONT));

	lpsz = GetIniStr(_szGuid, "1234567890123456789012345679012");
	SzToGuid(lpsz, &gPref.guid);
	FreePlpv(&lpsz);

	lpsz = GetIniStr(_szGuidRemote, "1234567890123456789012345679012");
	SzToGuid(lpsz, &gPref.guidRemote);
	FreePlpv(&lpsz);

	lpsz = GetIniStr(_szAppGuid, "appname.exe");
	lstrcpy(gPref.szAppName, lpsz);
	FreePlpv(&lpsz);

	lpsz = GetIniStr(_szCmdGuid, "parameter");
	lstrcpy(gPref.szCmdLine, lpsz);
	FreePlpv(&lpsz);

	lpsz = GetIniStr(_szDirGuid, "c:\\program files\\Meeting");
	lstrcpy(gPref.szCurrDir, lpsz);
	FreePlpv(&lpsz);

	lstrcpy(gPref.szData, "data");
}


/*  W R I T E  P R E F */
/*-------------------------------------------------------------------------
	%%Function: WritePref

	Write the current preferences
-------------------------------------------------------------------------*/
VOID WritePref(void)
{
	WriteIniBool(_szConfirm, gPref.fConfirm);
	WriteIniBool(_szSbar, gPref.fSbar);
	WriteIniInt(_szLogFlags, gPref.grfLog);
	WriteIniStr(_szDefName, gPref.szDefName);
	WriteIniStr(_szConferenceName, gPref.szConferenceName);
	WriteIniInt(_szAddrType, gPref.iAddrType);
	WriteIniInt(_szMediaType, gPref.dwMediaType);

	if (ghwndMain != NULL) 
	{
		gPref.wpMain.length = sizeof(WINDOWPLACEMENT);
		GetWindowPlacement(ghwndMain, &gPref.wpMain);
	}
	WriteIniHex(_szMainWnd, &gPref.wpMain, sizeof(WINDOWPLACEMENT));

	WriteIniHex(_szMsgFont, &gPref.lf, sizeof(LOGFONT));
	WriteIniStr(_szAppGuid, gPref.szAppName);
	WriteIniStr(_szCmdGuid, gPref.szCmdLine);
	WriteIniStr(_szDirGuid, gPref.szCurrDir);
	WriteIniStr(_szFileName,gPref.szFileName);


	{
		CHAR sz[MAX_PATH];
		GuidToSz(&gPref.guid, sz);
		WriteIniStr(_szGuid, sz);
		GuidToSz(&gPref.guidRemote, sz);
		WriteIniStr(_szGuidRemote, sz);
	}
}


/* F  C R E A T E  M S G  W I N D O W */
/*----------------------------------------------------------------------------
	%%Function: FCreateMsgWindow

	Create the main message window

----------------------------------------------------------------------------*/
BOOL FCreateMsgWindow(void)
{
	ghwndMsg = CreateWindow("LISTBOX", NULL,
		WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER |
		LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS ,
		0, 0, 0, 0,
		ghwndMain, (HMENU) IDW_MSG, ghInst, NULL);
	if (ghwndMsg == NULL)
		return fFalse;

	// set the font for the window
	ghfontEntry = CreateFontIndirect(&gPref.lf);
	if (ghfontEntry == hfontNil)
		return fTrue;

	SendMessage(ghwndMsg, WM_SETFONT, (WPARAM) ghfontEntry, MAKELPARAM(fTrue, 0));

	return fTrue;
}


/*  F  I N I T  I N S T A N C E */
/*-------------------------------------------------------------------------
	%%Function: FInitInstance

	Initialize the window data and register the window class
-------------------------------------------------------------------------*/
BOOL FInitInstance(int nCmdShow)
{
	char  szTitle[MAX_PATH];
	WNDCLASS  wc;

	Assert(ghInst != NULL);

	ghAccelTable = LoadAccelerators(ghInst, MAKEINTRESOURCE(ID_APPACCEL));

	LoadString(ghInst, IDS_APP_TITLE, szTitle, sizeof(szTitle));

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = ghInst;
	wc.hIcon         = LoadIcon(ghInst, MAKEINTRESOURCE(ICO_APP));
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDM_APP);
	wc.lpszClassName = _szAppClassName;

	// Register the window class and return FALSE if unsuccesful.
	if (!RegisterClass(&wc))
		return fFalse;

	// MAIN WINDOW
	// Create a main window for this application instance.
	ghwndMain = CreateWindow(_szAppClassName, szTitle, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
		NULL, NULL, ghInst, NULL);
	if (ghwndMain == NULL)
		return fFalse;

	ghMenu = GetMenu(ghwndMain);

	if (!FCreateSbar() ||
		!FCreateMsgWindow())
		return fFalse;

	ShowHwnd(ghwndSbar, gPref.fSbar, IDM_VIEW_SBAR);

	if (gPref.wpMain.length != 0)
		SetWindowPlacement(ghwndMain, &gPref.wpMain);

	ShowWindow(ghwndMain, nCmdShow);
	UpdateWindow(ghwndMain);

	Log(LOG_ALWAYS, "Application Initialized");  // Successful and ready for input
	return fTrue;
}


/*  F  I N I T  A P P */
/*-------------------------------------------------------------------------
	%%Function: FInitApp

	Initialize the application after checking command line, etc.
-------------------------------------------------------------------------*/
BOOL FInitApp(LPSTR lpszCmd)
{
	InitCommonControls();

	// Initialize stuff
	ReadPref();

	ClearBytes( &grhConfNotify, sizeof(grhConfNotify));

	return fTrue;
}


