Click here to Skip to main content
15,885,435 members
Articles / Desktop Programming / MFC

Easy Installer

Rate me:
Please Sign up or sign in to vote.
4.81/5 (57 votes)
14 Sep 2006CPOL11 min read 276.8K   3.7K   155  
The Easy Installer program
// SetupPage.cpp : implementation file
//

#include "stdafx.h"
#include "DestinationPage.h"
#include "NewWizDialog.h"
#include "unwrap.h"

#include <io.h>
#include "..\shared\path.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDestinationPage dialog


CDestinationPage::CDestinationPage(CWnd* pParent /*=NULL*/)
	: CNewWizPage(CDestinationPage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDestinationPage)
	//}}AFX_DATA_INIT
}


void CDestinationPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDestinationPage)
	DDX_Control(pDX, ST_CAPTION, m_CaptionCtrl);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, ST_CAPTION1, m_Caption1Ctrl);
	DDX_Control(pDX, ST_CAPTION2, m_Caption2Ctrl);
	DDX_Control(pDX, IDC_EDIT1, m_edtPath);
}


BEGIN_MESSAGE_MAP(CDestinationPage, CNewWizPage)
	//{{AFX_MSG_MAP(CDestinationPage)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDestinationPage message handlers

BOOL CDestinationPage::OnInitDialog() 
{
	CNewWizPage::OnInitDialog();

	m_CaptionCtrl.SetFont(&m_LargeFont, TRUE);

	CString str;

	str = _T("c:\\Program Files\\") + unwrap.GetPrjName();
	m_edtPath.SetWindowText(str);

	str = "Where should " + unwrap.GetPrjName() + " be installed?";
	m_Caption1Ctrl.SetWindowText(str);

	str = "Setup will install " + unwrap.GetPrjName() + " into the following folder.";
	m_Caption2Ctrl.SetWindowText(str);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDestinationPage::OnSetActive()
{
}

BOOL CDestinationPage::OnKillActive()
{
	return TRUE;
}


CString CDestinationPage::BrowseForFolder()
{
	// We're going to use the shell to display a 
	// "Choose Directory" dialog box for the user.

	CString strResult = "";

	LPMALLOC lpMalloc;  // pointer to IMalloc

	if (::SHGetMalloc(&lpMalloc) != NOERROR)
		return strResult; // failed to get allocator  char szDisplayName[_MAX_PATH];

	char szBuffer[_MAX_PATH];
	BROWSEINFO browseInfo;

	browseInfo.hwndOwner = AfxGetMainWnd()->GetSafeHwnd();
	// set root at Desktop
	browseInfo.pidlRoot = NULL; 
	browseInfo.pszDisplayName = szBuffer;
	browseInfo.lpszTitle = "Please choose a folder for extraction";   // passed in
	browseInfo.ulFlags = BIF_RETURNONLYFSDIRS ;   // also passed in
	browseInfo.lpfn = NULL;      // not used
	browseInfo.lParam = 0;      // not used

	LPITEMIDLIST lpItemIDList;

	if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
	{
		// Get the path of the selected folder from the
		// item ID list.
		if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
		{
			// At this point, szBuffer contains the path 
			// the user chose.
			if (szBuffer[0] == '\0')
			{
				// SHGetPathFromIDList failed, or
				// SHBrowseForFolder failed.
				AfxMessageBox("Cannot install to this path.");
				return strResult;
			}

			// We have a path in szBuffer!
			// Return it.
			strResult = szBuffer;
		}
		else
		{
			// The thing referred to by lpItemIDList 
			// might not have been a file system object.
			// For whatever reason, SHGetPathFromIDList
			// didn't work!
			AfxMessageBox("Cannot install to this path.");
//			return strResult; // strResult is empty 
		}
		lpMalloc->Free(lpItemIDList);
		lpMalloc->Release();      
	}
	// If we made it this far, SHBrowseForFolder failed.
	return strResult;
}

void CDestinationPage::OnBnClickedButton1()
{
	CString str = CDestinationPage::BrowseForFolder();
	m_edtPath.SetWindowText(str);
}

BOOL CDestinationPage::OnWizardFinish()
{
	CString str;
	m_edtPath.GetWindowText(str);

	// Ensure we have \ at the end
	if(!str.IsEmpty() && str[str.GetLength() - 1] != '\\')
	{
		str += '\\';
		UpdateData(FALSE);
	}
	if(_access(str, 2) != 0)
	{
		if(!MakeSurePathExists(str))
		{
			CString err;
			err.Format(_T("Could not create directory %s."), str);
			AfxMessageBox(err);
			return TRUE;
		}
	}

	unwrap.SetPath(str);
	return TRUE;
}

LRESULT CDestinationPage::OnWizardBack()
{
	CString license = unwrap.GetAgreement();
	CString info = unwrap.GetInfo();
	if(info.IsEmpty())
		return IDW_INTRO;
	else
	{
		if(license.IsEmpty())
			return IDW_INFO;
		else
			return IDW_AGREEMENT;
	}
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Israel Israel
lichen farmer, semi-lingual, never saw a Sylvester Stallone movie

Comments and Discussions