/*****************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/


// CConfigWizPage.cpp : implementation file
//

#include "stdafx.h"
#include "ConfigWizPage.h"
#include "ConfigWizSheet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CConfigWizPage property page

IMPLEMENT_DYNCREATE(CConfigWizPage, CPropertyPage)

void CConfigWizPage::InitData(void)
{
	//{{AFX_DATA_INIT(CConfigWizPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

   m_pOwner = NULL;
   m_nState = -1;

   m_strTitle = TEXT("");

   m_fUseDefaultBmp = TRUE;
   m_ptPageBmpPlacement.x = 0;
   m_ptPageBmpPlacement.y = 0;
   m_pPageBmp = NULL;
}

CConfigWizPage::CConfigWizPage() : CPropertyPage()
{
   InitData();
}

CConfigWizPage::CConfigWizPage(UINT nIDTemplate, UINT nIDCaption /* = 0 */, CConfigObject const * const pOwner /* = NULL */, int const nState /* = -1 */) : CPropertyPage()
{
   InitData();
   Construct(nIDTemplate, nIDCaption, pOwner, nState);
}

void CConfigWizPage::Construct(UINT nIDTemplate, UINT nIDCaption /* = 0 */, CConfigObject const * const pOwner /* = NULL */, int const nState /* = -1 */)
{
   CPropertyPage::Construct(nIDTemplate, nIDCaption);

   m_pOwner = (CConfigObject *)pOwner;
   m_nState = nState;
}

CConfigWizPage::~CConfigWizPage()
{
}

void CConfigWizPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConfigWizPage)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConfigWizPage, CPropertyPage)
	//{{AFX_MSG_MAP(CConfigWizPage)
	ON_WM_ERASEBKGND()
	ON_WM_PALETTECHANGED()
	ON_WM_QUERYNEWPALETTE()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConfigWizPage message handlers

// virtual override for when the back button is pressed
// calls its owner sheet's method
LRESULT CConfigWizPage::OnWizardBack() 
{
	GetOwnerSheet()->WizReverse();
	return -1;
}

// virtual override for when the Next button is pressed
// calls its owner sheet's method
LRESULT CConfigWizPage::OnWizardNext() 
{
	// only if updated correctly do we forward
   if (UpdateData())
   {
	   // Let our owner know!
	   BOOL bWizAdvancePage = TRUE;
	   if (m_pOwner != NULL) 
		   bWizAdvancePage = m_pOwner->NextWizardPage(m_nState);
	   if (bWizAdvancePage)
	   GetOwnerSheet()->WizForward();
   }
	return -1;
}

BOOL CConfigWizPage::OnWizardFinish() 
{
   UpdateData();

   // Let our owner know!
   if (m_pOwner != NULL) m_pOwner->NextWizardPage(m_nState);

   // do default
	return CPropertyPage::OnWizardFinish();
}

BOOL CConfigWizPage::InWizardMode() const
{
   BOOL fInWizardMode = FALSE;

   try
   {
      CPropertySheet * pSheet = (CPropertySheet *)GetParent();
      AffirmThrow(pSheet != NULL);

      fInWizardMode = (pSheet->m_psh.dwFlags & PSH_WIZARD);
   }
   catch (CAffirmException ae)
   {
   }

   return fInWizardMode;
}

// virtual override called when the page becomes active.
// we use a common function to decide when the Back and Next/Finish buttons are
// displayed.
BOOL CConfigWizPage::OnSetActive() 
{
   BOOL fActive = CPropertyPage::OnSetActive();

   try
   {
      // its possible that CConfigWizPage item will be added to a regular PropertySheet
      if (InWizardMode())
      {
		   GetOwnerSheet()->CheckButtonState();
		   CString strTitle = GetOwnerSheet()->GetTitleBase() + m_strTitle;
   	   GetOwnerSheet()->SetTitle(strTitle);
      }
   }
   catch (CAffirmException ae)
   {
   }

	return fActive;
}

BOOL CConfigWizPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();

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

// return a pointer to the parent or underlying sheet object
CConfigWizSheet* CConfigWizPage::GetOwnerSheet() const
{
	CConfigWizSheet* psh = (CConfigWizSheet*)GetParent();
	ASSERT(psh);
	ASSERT_KINDOF(CConfigWizSheet, psh);
	return (psh);
}

// methods to add/insert/remove another page of same class to the underlying property sheet
// This may seem somewhat disjoint, but we provide this so that one page can
// add or insert a sibling to the underlying sheet. 
// These function may eventually be removed
BOOL CConfigWizPage::AddPage(CConfigWizPage* pPage, BOOL fSkipOnBack /* = FALSE */, BOOL fPermanent /* = FALSE */)
{
	ASSERT_KINDOF(CConfigWizPage, pPage);
	return (GetOwnerSheet()->AddPage(pPage,fSkipOnBack, fPermanent));
}

BOOL CConfigWizPage::InsertPage(CConfigWizPage* pPage, BOOL fSkipOnBack /* = FALSE */, BOOL fPermanent /* = FALSE */)
{
	ASSERT_KINDOF(CConfigWizPage, pPage);
	return (GetOwnerSheet()->InsertPage(pPage, fSkipOnBack, fPermanent));
}

BOOL CConfigWizPage::RemovePage(CConfigWizPage* pPage)
{
	ASSERT_KINDOF(CConfigWizPage, pPage);
	return (GetOwnerSheet()->RemovePage(pPage));
}

void CConfigWizPage::SetPageBitmap(CDib const * const pBmp, CPoint const & ptPlacement, BOOL const fUseDefault /* = FALSE */)
{
   try
   {
      // NOTE: pBmp MAY be NULL, if fUseDefault == FALSE
      // AffirmThrow(pBmp != NULL);

      m_pPageBmp = (CDib *) pBmp;
      m_ptPageBmpPlacement = ptPlacement;

      m_fUseDefaultBmp = fUseDefault;
   }
   catch (CAffirmException ae)
   {
   }
}

BOOL CConfigWizPage::OnEraseBkgnd(CDC* pDC) 
{
	CPropertyPage::OnEraseBkgnd(pDC);

   try
   {
      if ((m_pPageBmp == NULL) && m_fUseDefaultBmp && InWizardMode())
      {
         m_ptPageBmpPlacement = GetOwnerSheet()->m_ptDfltPageBmpPlacement;
         m_pPageBmp           = GetOwnerSheet()->m_pDfltPageBmp;
      }

      if (m_pPageBmp != NULL)
      {
         AffirmThrow(pDC != NULL);
         CRect rc(m_ptPageBmpPlacement, m_pPageBmp->GetSize());
         m_pPageBmp->Draw(*pDC, &rc);
      }
   }
   catch (CAffirmException)
   {
   }
	
	return TRUE;      // Erasing is all done
}

int CConfigWizPage::DoRealizePalette(BOOL const fForeground)
{
   TRACE("CConfigWizPage::DoRealizePalette()\n");
   int nColorsChanged = 0;

   try
   {
      if (m_pPageBmp != NULL)
      {
         // Selet palette into client DC and realize it
         CClientDC dc(this);

         CPalette * pPal = m_pPageBmp->GetPalette();
         AffirmThrow(pPal != NULL);

         CPalette * pOldPal = dc.SelectPalette(pPal, !fForeground);

         nColorsChanged = dc.RealizePalette();

         if (nColorsChanged > 0)
            Invalidate(FALSE);	// tell Windows to repaint

         if (pOldPal)
            dc.SelectPalette(pOldPal, TRUE);
      }
   }
   catch (CAffirmException ae)
   {
   }

   return nColorsChanged;
}

void CConfigWizPage::OnPaletteChanged(CWnd* pFocusWnd) 
{
	// CPropertyPage::OnPaletteChanged(pFocusWnd);
   TRACE("CConfigWizPage::OnPaletteChanged()\n");
   if (pFocusWnd != this)
      DoRealizePalette(FALSE);
}

BOOL CConfigWizPage::OnQueryNewPalette() 
{
	// return CPropertyPage::OnQueryNewPalette();
   TRACE("CConfigWizPage::OnQueryNewPalette()\n");
   return DoRealizePalette(TRUE);
}

void CConfigWizPage::OnSetFocus(CWnd* pOldWnd) 
{
   TRACE("CConfigWizPage::OnSetFocus()\n");
	CPropertyPage::OnSetFocus(pOldWnd);
   DoRealizePalette(TRUE);
}
