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

	CNFTEST sample for Microsoft ActiveX Conferencing API

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

	dlg.c

	Dialog box procedures

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

#include "main.h"

#include "resource.h"
#define IDR_ADDR_FIRST     IDR_ADDR_UNKNOWN
#define IDR_ADDR_LAST      IDR_ADDR_ISDN

// Local dialog procs
LRESULT CALLBACK ConferenceDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK GuidDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK RemoteDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK DataDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK RemoveNotifyDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK SendFileDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam);

/*  D L G  C O N F E R E N C E  C O N N E C T */
/*----------------------------------------------------------------------------
	%%Function: DlgConferenceConnect

----------------------------------------------------------------------------*/
BOOL DlgConferenceConnect(VOID)
{
	ASSERT((IDR_ADDR_UNKNOWN - IDR_ADDR_FIRST) == CONF_ADDR_UNKNOWN);
	ASSERT((IDR_ADDR_IP - IDR_ADDR_FIRST) == CONF_ADDR_IP);
	ASSERT((IDR_ADDR_MACHINE - IDR_ADDR_FIRST) == CONF_ADDR_MACHINENAME);
	ASSERT((IDR_ADDR_PSTN - IDR_ADDR_FIRST) == CONF_ADDR_PSTN);

	ASSERT((IDC_MT_DATA  - IDC_MT_DATA) == 0);
	ASSERT((IDC_MT_AUDIO - IDC_MT_DATA) == 1);
	ASSERT((IDC_MT_VIDEO - IDC_MT_DATA) == 2);

	return (0 == DialogBox(ghInst, MAKEINTRESOURCE(IDD_CONFERENCE),
		ghwndMain, (DLGPROC)ConferenceDlgProc));
}

/*  D L G  G U I D */
/*----------------------------------------------------------------------------
	%%Function: DlgGuid

----------------------------------------------------------------------------*/
BOOL DlgGuid(VOID)
{
	return (0 == DialogBox(ghInst, MAKEINTRESOURCE(IDD_SETGUID),
		ghwndMain, (DLGPROC)GuidDlgProc));
}

/*  D L G  S E N D  D A T A */
/*----------------------------------------------------------------------------
	%%Function: DlgSendData

----------------------------------------------------------------------------*/
BOOL DlgSendData(VOID)
{
	return (0 == DialogBox(ghInst, MAKEINTRESOURCE(IDD_SENDDATA),
		ghwndMain, (DLGPROC)DataDlgProc));
}

/*  D L G  L A U N C H  R E M O T E */
/*----------------------------------------------------------------------------
	%%Function: DlgLaunchRemote

----------------------------------------------------------------------------*/
BOOL DlgLaunchRemote(VOID)
{
	return (0 == DialogBox(ghInst, MAKEINTRESOURCE(IDD_LAUNCHREMOTE),
		ghwndMain, (DLGPROC)RemoteDlgProc));
}

/*  D L G  R E M O V E  N O T I F Y */
/*----------------------------------------------------------------------------
	%%Function: DlgRemoveNotify

----------------------------------------------------------------------------*/
BOOL DlgRemoveNotify(VOID)
{
	return (0 == DialogBox(ghInst, MAKEINTRESOURCE(IDD_REMOVENOTIFY),
		ghwndMain, (DLGPROC)RemoveNotifyDlgProc));
}

/*  D L G  S E N D  F I L E */
/*----------------------------------------------------------------------------
	%%Function: DlgSendFile

----------------------------------------------------------------------------*/
BOOL DlgSendFile(VOID)
{
	return (0 == DialogBox(ghInst, MAKEINTRESOURCE(IDD_SENDFILE),
		ghwndMain, (DLGPROC)SendFileDlgProc));
}




/*  C O N F E R E N C E  D L G  P R O C */
/*----------------------------------------------------------------------------
	%%Function: ConferenceDlgProc

----------------------------------------------------------------------------*/
LRESULT CALLBACK ConferenceDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
		CenterWindow(hdlg, ghwndMain);

		SetDlgItemText(hdlg, IDE_CONFNAME, gPref.szConferenceName);
		SetDlgItemText(hdlg, IDE_ADDR, gPref.szDefName);
		SetDlgItemInt(hdlg, IDE_HCONF, (UINT) gPref.hConf, fFalse);
		CheckRadioButton(hdlg, IDR_ADDR_FIRST, IDR_ADDR_LAST,
				IDR_ADDR_FIRST + gPref.iAddrType);

		{ // check media types
			int id;
			DWORD dwFlag = 0x01;
			for (id = IDC_MT_DATA; id <= IDC_MT_VIDEO; id++)
			{
				CheckDlgButton(hdlg, id, 0 != (gPref.dwMediaType & dwFlag));
				dwFlag <<= 1;
			}
		}

		return fTrue;

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wparam, lparam))
		{
		case IDOK:
			gPref.iAddrType = GetRadioButton(hdlg, IDR_ADDR_FIRST, IDR_ADDR_LAST)
				- IDR_ADDR_FIRST;

			GetDlgItemText(hdlg, IDE_CONFNAME,
				gPref.szConferenceName, sizeof(gPref.szConferenceName));
			GetDlgItemText(hdlg, IDE_ADDR,
				gPref.szDefName, sizeof(gPref.szDefName));
			gPref.hConf = (HCONF) GetDlgItemInt(hdlg, IDE_HCONF, NULL, fFalse);

			{ // check media types
				int id;
				DWORD dwFlag = 0x01;
				gPref.dwMediaType = 0;
				for (id = IDC_MT_DATA; id <= IDC_MT_VIDEO; id++)
				{
					if (IsDlgButtonChecked(hdlg, id))
						gPref.dwMediaType |= dwFlag;
					dwFlag <<= 1;
				}
			}

			EndDialog(hdlg, 0);
			return fTrue;

		case IDCANCEL:
			EndDialog(hdlg, 1);
			return fTrue;

		default:
			return fTrue;
		} /* switch (cmd) */

	default:
		break;
	} /* switch (uMsg) */

	return fFalse;
}


/*  G U I D  D L G  P R O C */
/*----------------------------------------------------------------------------
	%%Function: GuidDlgProc

----------------------------------------------------------------------------*/
LRESULT CALLBACK GuidDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		CHAR sz[MAX_PATH];

		CenterWindow(hdlg, ghwndMain);

		GuidToSz(&gPref.guid, sz);
		SetDlgItemText(hdlg, IDE_GUID, sz);
		SetDlgItemText(hdlg, IDE_APPLICATION, gPref.szAppName);
		SetDlgItemText(hdlg, IDE_COMMANDLINE, gPref.szCmdLine);
		SetDlgItemText(hdlg, IDE_DIRECTORY,   gPref.szCurrDir);

		return fTrue;
	}

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wparam, lparam))
		{
		case IDOK:
		{
			CHAR sz[MAX_PATH];

			GetDlgItemText(hdlg, IDE_APPLICATION,
				gPref.szAppName, sizeof(gPref.szAppName));
			GetDlgItemText(hdlg, IDE_COMMANDLINE,
				gPref.szCmdLine, sizeof(gPref.szCmdLine));
			GetDlgItemText(hdlg, IDE_DIRECTORY,
				gPref.szCurrDir, sizeof(gPref.szCurrDir));
			GetDlgItemText(hdlg, IDE_GUID, sz, sizeof(sz));
			SzToGuid(sz, &gPref.guid);

			EndDialog(hdlg, 0);
			return fTrue;
		}

		case IDCANCEL:
			EndDialog(hdlg, 1);
			return fTrue;

		default:
			return fTrue;
		} /* switch (cmd) */

	default:
		break;
	} /* switch (uMsg) */

	return fFalse;
}



/*  R E M O T E  D L G  P R O C */
/*----------------------------------------------------------------------------
	%%Function: RemoteDlgProc

----------------------------------------------------------------------------*/
LRESULT CALLBACK RemoteDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		CHAR sz[MAX_PATH];
		CONFUSERINFO confUserInfo;
		HWND hwnd;
		CONFERR dwResult;

		CenterWindow(hdlg, ghwndMain);

		// Set remote guid
		GuidToSz(&gPref.guidRemote, sz);
		SetDlgItemText(hdlg, IDE_GUID, sz);

		// Set remote user
		InitStruct(&confUserInfo);
		hwnd = GetDlgItem(hdlg, IDC_USERNAME);

		// Add a string for sending to all users
		SendMessage(hwnd, CB_ADDSTRING, 0,(LPARAM)"<everyone>");

		for( ; ; )
		{
			dwResult = ConferenceGetInfo(gPref.hConf, CONF_ENUM_USER, &confUserInfo);
			if(dwResult != CONFERR_SUCCESS)
			{
				if(dwResult != CONFERR_ENUM_COMPLETE)
				{
					Log1(LOG_ERROR, "ConferenceGetInfo returns %08x", dwResult);
					break;
				}
				break;
			}
			SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM) confUserInfo.szUserName);
		}
		
		// Set the default user to everyone
		SetDlgItemText(hdlg, IDC_USERNAME, "<everyone>");

		return fTrue;
	}

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wparam, lparam))
		{
		case IDOK:
		{
			CHAR sz[MAX_PATH];

			GetDlgItemText(hdlg, IDC_USERNAME, gPref.szUserName, sizeof(gPref.szUserName));
			GetDlgItemText(hdlg, IDE_GUID, sz, sizeof(sz));
			SzToGuid(sz, &gPref.guidRemote);

			EndDialog(hdlg, 0);
			return fTrue;
		}

		case IDCANCEL:
			EndDialog(hdlg, 1);
			return fTrue;

		default:
			return fTrue;
		} /* switch (cmd) */

	default:
		break;
	} /* switch (uMsg) */

	return fFalse;
}



/*  D A T A  D L G  P R O C */
/*----------------------------------------------------------------------------
	%%Function: DataDlgProc

----------------------------------------------------------------------------*/
LRESULT CALLBACK DataDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		CHAR sz[MAX_PATH];
		CONFDEST confDest;
		HWND hwnd;
		CONFERR dwResult;

		CenterWindow(hdlg, ghwndMain);

		GuidToSz(&gPref.guidRemote, sz);
		SetDlgItemText(hdlg, IDE_GUID, sz);
		SetDlgItemText(hdlg, IDE_DATA, gPref.szData);

		InitStruct(&confDest);
		hwnd = GetDlgItem(hdlg, IDC_USERID);

		// Add a string for sending to all users
		SendMessage(hwnd, CB_ADDSTRING, 0,(LPARAM)"0");

		for( ; ; )
		{
			dwResult = ConferenceGetInfo(gPref.hConf, CONF_ENUM_PEER, &confDest);
			if( dwResult != CONFERR_SUCCESS)
			{
				if( dwResult != CONFERR_ENUM_COMPLETE)
				{
					Log1(LOG_ERROR, "ConferenceGetInfo returns %08x", dwResult);
					break;
				}
				break;	
			}
			wsprintf(sz, "%d", confDest.dwUserId);
			SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM) sz);
		}
		
		// Set the default user to everyone
		SetDlgItemText(hdlg, IDC_USERID, "0");

		return fTrue;
	}

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wparam, lparam))
		{
		case IDOK:
		{
			CHAR sz[MAX_PATH];

			GetDlgItemText(hdlg, IDE_DATA, gPref.szData, sizeof(gPref.szData));
			gPref.dwRemoteId = GetDlgItemInt(hdlg, IDC_USERID, NULL, fFalse);

			GetDlgItemText(hdlg, IDE_GUID, sz, sizeof(sz));
			SzToGuid(sz, &gPref.guidRemote);

			EndDialog(hdlg, 0);
			return fTrue;
		}

		case IDCANCEL:
			EndDialog(hdlg, 1);
			return fTrue;

		default:
			return fTrue;
		} /* switch (cmd) */

	default:
		break;
	} /* switch (uMsg) */

	return fFalse;
}







/*  R E M O V E  N O T I F Y D L G  P R O C */
/*----------------------------------------------------------------------------
	%%Function: RemoveNotifyDlgProc

----------------------------------------------------------------------------*/
LRESULT CALLBACK RemoveNotifyDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		HWND hwnd;
		int i;
		
		// Fill in the drop down list here.
		CHAR sz[MAX_PATH];

		CenterWindow(hdlg, ghwndMain);
		
		hwnd = GetDlgItem(hdlg, IDC_HCONFNOTIFY);

		for( i = 0; i < giCount; i++)
		{
			wsprintf(sz, "%d", grhConfNotify[i]);
			SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM) sz);
		}

		if( giCount != 0)  // there are callbacks registered
			SetDlgItemInt(hdlg, IDC_HCONFNOTIFY, (UINT)grhConfNotify[0], fFalse);

		
		return fTrue;
	}

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wparam, lparam))
		{
		case IDOK:
		{
			// return the selected handle
			gPref.hConfNotify = (HCONFNOTIFY)GetDlgItemInt(hdlg, IDC_HCONFNOTIFY, NULL, fFalse);
			EndDialog(hdlg, 0);
			return fTrue;
		}

		case IDCANCEL:
			EndDialog(hdlg, 1);
			return fTrue;

		default:
			return fTrue;
		} /* switch (cmd) */

	default:
		break;
	} /* switch (uMsg) */

	return fFalse;
}


/*  S E N D F I L E  D L G  P R O C */
/*----------------------------------------------------------------------------
	%%Function: SendFileDlgProc

----------------------------------------------------------------------------*/
LRESULT CALLBACK SendFileDlgProc(HWND hdlg, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		HWND hwnd;
		CONFERR dwResult;
		CONFUSERINFO confUserInfo;
	
		CenterWindow(hdlg, ghwndMain);

		InitStruct(&confUserInfo);
		hwnd = GetDlgItem(hdlg, IDC_USERNAME);

		// Add a string for sending to all users
		SendMessage(hwnd, CB_ADDSTRING, 0,(LPARAM)"<everyone>");

		for( ; ; )
		{
			dwResult = ConferenceGetInfo(gPref.hConf, CONF_ENUM_USER, &confUserInfo);
			if( dwResult != CONFERR_SUCCESS)
			{
				if( dwResult != CONFERR_ENUM_COMPLETE)
				{
					Log1(LOG_ERROR, "ConferenceGetInfo returns %08x", dwResult);
					break;
				}
				break;
			}
			SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM) confUserInfo.szUserName);
		}
				
		// Set the default user to everyone
		SetDlgItemText(hdlg, IDC_USERNAME, "<everyone>");
		
		// Set the default filename
		SetDlgItemText(hdlg, IDE_FILENAME, gPref.szFileName);
		
		return fTrue;
	}

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wparam, lparam))
		{
		case IDOK:
		{
			GetDlgItemText(hdlg, IDE_FILENAME, gPref.szFileName, sizeof(gPref.szFileName));
			GetDlgItemText(hdlg, IDC_USERNAME, gPref.szUserName, sizeof(gPref.szUserName));
			EndDialog(hdlg, 0);
			return fTrue;
		}

		case IDCANCEL:
			EndDialog(hdlg, 1);
			return fTrue;
		
		case IDC_BROWSE:
		{
			char szFileName[MAX_PATH];

			// Get the file name
			if (!FGetFileName(szFileName))
				return fFalse;
			
			SetDlgItemText(hdlg, IDE_FILENAME, szFileName);

			return fTrue;
		}


		default:
			return fTrue;
		} /* switch (cmd) */

	default:
		break;
	} /* switch (uMsg) */

	return fFalse;
}
