// VcdAPITestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VcdAPITest.h"
#include "VcdAPITestDlg.h"
#include "DlgSetDrives.h"
#include "DlgAskVCD.h"

// Virtual CD API
#include "vcdapidefs.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVcdAPITestDlg dialog

CVcdAPITestDlg::CVcdAPITestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVcdAPITestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVcdAPITestDlg)
	m_bVCDDebug = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVcdAPITestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVcdAPITestDlg)
	DDX_Check(pDX, IDC_VCDDEBUG, m_bVCDDebug);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVcdAPITestDlg, CDialog)
	//{{AFX_MSG_MAP(CVcdAPITestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_GETVCDDRIVELETTERS, OnGetvcddriveletters)
	ON_BN_CLICKED(IDC_VCDDEBUG, OnVcddebug)
	ON_BN_CLICKED(IDC_GETVCDFILEFROMDRIVE, OnGetvcdfilefromdrive)
	ON_BN_CLICKED(IDC_VCDEJECT, OnVcdeject)
	ON_BN_CLICKED(IDC_GETVCDLASTERROR, OnGetvcdlasterror)
	ON_BN_CLICKED(IDC_SETVCDDRIVELETTERS, OnSetvcddriveletters)
	ON_BN_CLICKED(IDC_VCDINSERT, OnVcdinsert)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVcdAPITestDlg message handlers

BOOL CVcdAPITestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVcdAPITestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVcdAPITestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVcdAPITestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::OnGetvcddriveletters() 
//
// Show the existing virtual drive letters
//
{
	char cVcdDrives[27] = "\0";

	DWORD dwRC = VCDApiGetVCDDriveLetters(cVcdDrives, 27);
	
	if(dwRC == VCD_ERROR_NONE)
	{
		CString strMsg;
		strMsg.Format("Virtual drive letters found: '%s'", cVcdDrives);
		AfxMessageBox(cVcdDrives, MB_OK | MB_ICONINFORMATION);
	}
	else
		ShowVCDError(dwRC);
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::OnGetvcdfilefromdrive() 
//
// Shows all virtual drives and the inserted image file names
//
{
	char cVcdDrives[27] = "\0";

	DWORD dwRC = VCDApiGetVCDDriveLetters(cVcdDrives, 27);
	
	if(dwRC == VCD_ERROR_NONE)
	{
		CString strMsg;
		CString strLine;
		char cVCDFilename[MAX_PATH];
		int iLen = strlen(cVcdDrives);

		for(int i = 0; i < iLen && dwRC == VCD_ERROR_NONE; i++)
		{
			dwRC = VCDApiGetMountedFileFromDrive(cVcdDrives[i], cVCDFilename, MAX_PATH);

			if(dwRC == VCD_ERROR_NONE)
			{
				if(! strMsg.IsEmpty())
					strMsg += "\n";

				strLine.Format("%C:\t= %s", cVcdDrives[i], (strlen(cVCDFilename) > 0 ? cVCDFilename : "<Empty drive>"));
				strMsg += strLine;
			}
		}
	
		if(dwRC == VCD_ERROR_NONE)
		{
			if(strMsg.IsEmpty())
				AfxMessageBox("No virtual drives found!", MB_OK | MB_ICONINFORMATION);
			else
				AfxMessageBox(strMsg, MB_OK | MB_ICONINFORMATION);
		}
	}

	if(dwRC != VCD_ERROR_NONE)
		ShowVCDError(dwRC);
}

/////////////////////////////////////////////////////////////////////////////
void CVcdAPITestDlg::OnVcdinsert() 
//
// Insert a virtual CD into the first available virtual drive
//
{
	CDlgAskVCD dlg;

	if(dlg.DoModal() == IDOK)
	{
		char cVcdDrives[27] = "\0";

		DWORD dwRC = VCDApiGetVCDDriveLetters(cVcdDrives, 27);
		
		if(dwRC == VCD_ERROR_NONE)
		{
			dwRC = VCDApiInsert(cVcdDrives[0], dlg.m_strVCDFilename, "");

			if(dwRC == VCD_ERROR_NONE)
			{
				CString strMsg;
				strMsg.Format("'%s' inserted into virtual drive %C:", dlg.m_strVCDFilename, cVcdDrives[0]);
				AfxMessageBox(strMsg, MB_OK | MB_ICONINFORMATION);
			}
		}

		if(dwRC != VCD_ERROR_NONE)
			ShowVCDError(dwRC);
	}	
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::OnVcdeject() 
//
// Eject all virtual CDs
//
{
	AfxMessageBox("Eject all virtual CDs", MB_OK | MB_ICONINFORMATION);
	
	char cVcdDrives[27] = "\0";

	DWORD dwRC = VCDApiGetVCDDriveLetters(cVcdDrives, 27);
	
	if(dwRC == VCD_ERROR_NONE)
	{
		char cVCDFilename[MAX_PATH];
		char cDrv[2];
		int iLen = strlen(cVcdDrives);

		cDrv[1] = '\0';

		for(int i = 0; i < iLen && dwRC == VCD_ERROR_NONE; i++)
		{
			dwRC = VCDApiGetMountedFileFromDrive(cVcdDrives[i], cVCDFilename, MAX_PATH);

			if(strlen(cVCDFilename) > 0 && dwRC == VCD_ERROR_NONE)
			{
				cDrv[0] = cVcdDrives[i];
				dwRC = VCDApiEject(cDrv);
			}
		}
	}

	if(dwRC != VCD_ERROR_NONE)
		ShowVCDError(dwRC);
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::OnSetvcddriveletters() 
//
// set virtual drive letters
//
{
	char cVcdDrives[27] = "\0";

	DWORD dwRC = VCDApiGetVCDDriveLetters(cVcdDrives, 27);
	
	if(dwRC == VCD_ERROR_NONE)
	{
		CDlgSetDrives dlg;
		dlg.m_strDrives = cVcdDrives;

		if(dlg.DoModal() == IDOK)
		{
			if(dlg.m_strDrives.IsEmpty())
				AfxMessageBox("There must be at least 1 virtual drive!\nOperation stopped.", MB_OK | MB_ICONSTOP);
			else
			{
				BeginWaitCursor();
				dwRC = VCDApiSetDriveLetters(dlg.m_strDrives);
				EndWaitCursor();
			
				if(dwRC == VCD_ERROR_NONE)
				{
					CString strMsg;
					strMsg.Format("Virtual drive letters changed : '%s'", dlg.m_strDrives);
					AfxMessageBox(strMsg, MB_OK | MB_ICONINFORMATION);
				}
			}
		}
	}

	if(dwRC != VCD_ERROR_NONE)
		ShowVCDError(dwRC);
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::OnGetvcdlasterror() 
//
// Show the last error from the Virtual CD API
// 
{
	ShowVCDError(VCDApiGetLastError());	
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::OnVcddebug() 
//
// if active and an error happens in the API, dialogs pop up and show
// additional debug information
//
{
	UpdateData(TRUE);
	VCDApiSetDebug(m_bVCDDebug);
}

/////////////////////////////////////////////////////////////////////////////

void CVcdAPITestDlg::ShowVCDError(DWORD dwRC)
//
// Display Virtual CD API error
//
{
	CString strMsg;
	strMsg.Format("Virtual CD API error - EC %d!", dwRC);
	AfxMessageBox(strMsg, MB_OK | MB_ICONSTOP);
}

/////////////////////////////////////////////////////////////////////////////
