/************************************************************************
*
* Resource ID Organiser Core Library
*
* (c) Copyright 2000-2004 by Anna-Jayne Metcalfe (resorg@annasplace.me.uk)
* All rights reserved.
*
************************************************************************
*
* Filename : SymbolPropertiesGeneralPage.cpp
*
* Description : CSymbolPropertiesGeneralPage - property page class
*
* Compiler : Microsoft Visual C++ 6.0, Service Pack 3 or later
* Microsoft Visual C++ .NET 2002
*
* Target
* Environment : Windows 98/NT/2000/XP
*
* NOTE:
*
* This software is provided "as is" free for personal use. All
* title and copyrights in and to the software, including but not
* limited to any images, text, etc. incorporated into it, are
* owned by Anna-Jayne Metcalfe, except where acknowledged otherwise.
*
* Your may freely to use this code in your own products, PROVIDED
* this notice is not removed or modified.
*
*
* Visit http://www.annasplace.me.uk/resorg for latest updates
*
************************************************************************
*
* MODIFICATION HISTORY:
*
* This is a controlled document. See project configuration
* control tool for latest version and full version history.
*
* $Archive: /Projects/AddIns/ResOrg/ResOrgCore/SymbolPropertiesGeneralPage.cpp $
* $Revision: 18 $
* $Date: 23/08/04 21:37 $
* $Author: Anna $
*
* $History: SymbolPropertiesGeneralPage.cpp $
*
* ***************** Version 18 *****************
* User: Anna Date: 23/08/04 Time: 21:37
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* The value of a symbol is now automatically recalculated when its type
* is changed
*
* ***************** Version 17 *****************
* User: Anna Date: 11/07/04 Time: 16:13
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Added support for detection of out of range symbols
*
* ***************** Version 16 *****************
* User: Anna Date: 25/02/03 Time: 21:25
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Symbol conflict checking now handles inter-file conflicts better
*
* ***************** Version 15 *****************
* User: Anna Date: 2/01/03 Time: 0:10
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Added a "File Name" control
*
* ***************** Version 14 *****************
* User: Anna Date: 3/12/02 Time: 20:17
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Replaced the rich edit control with an HTML static control
*
* ***************** Version 13 *****************
* User: Anna Date: 25/11/02 Time: 15:19
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Changed website address in banner
*
* ***************** Version 12 *****************
* User: Anna Date: 22/10/02 Time: 13:24
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Changed name/mail address (at last!)
*
* ***************** Version 11 *****************
* User: Andy Date: 7/06/02 Time: 17:04
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Renamed the ResOrgUtils module to ResOrgCore. Updated file banners
* accordingly
*
* ***************** Version 10 *****************
* User: Andy Date: 27/05/02 Time: 13:37
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* Knock on changes from CResourceSymbolBuffer (now
* CResourceSymbolManager)
*
* ***************** Version 9 *****************
* User: Andy Date: 12/27/01 Time: 10:39p
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* Added an option to allow editing of the value but not the name
*
* ***************** Version 8 *****************
* User: Andy Date: 23/04/01 Time: 21:15
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* 1. Added "Fix Conflict" button
* 2. A warning message is now displayed when a symbol is made read-only
*
* ***************** Version 7 *****************
* User: Andy Date: 2/04/01 Time: 17:26
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* 1. Added a rich edit control (and associated icon) to warn of symbol
* name/value conflicts
* 2. Improved validation routines and messages
*
* ***************** Version 6 *****************
* User: Andy Date: 27/03/01 Time: 15:22
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* 1. Added validation of symbol names
* 2. When the symbol value changes, the parent property sheet is
* notified to update the "Conflicts" page
*
* ***************** Version 5 *****************
* User: Andy Date: 9/03/01 Time: 7:06
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* Make the warning prompt when a symbol name is changed optional, so it
* can be disabled when the dialog is used to edit the properties of a
* symbol being added
*
* ***************** Version 4 *****************
* User: Andy Date: 17/02/01 Time: 7:05
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* 1. Converted to proeprty page
* 2. Added "type" icon
* 3. Added support for read only symbols
* 4. The "type" control is now set automatically when the symbol name is
* changed
*
* ***************** Version 3 *****************
* User: Andy Date: 29/11/00 Time: 18:38
* Updated in $/Projects/AddIns/ResOrg/ResOrgUtils
* Added file banners
*
* $Nokeywords: $
*
************************************************************************/
#include "StdAfx.h"
#include <Shlwapi.h>
#include "ResOrgCore_Priv.h"
#include "SymbolPropertiesGeneralPage.h"
#pragma comment(lib, "Shlwapi.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Registered message definitions
const UINT WM_SYM_CHANGING = ::RegisterWindowMessage( _T("SymbolChanging") );
/////////////////////////////////////////////////////////////////////////////
// CSymbolPropertiesGeneralPage property page
CSymbolPropertiesGeneralPage::CSymbolPropertiesGeneralPage(void)
: CSymbolPropertiesGeneralPage_BASE(CSymbolPropertiesGeneralPage::IDD)
{
m_pSymbols = NULL;
m_pSymbol = NULL;
m_sName = _T("");
m_sType = _T("");
m_uIconID = 0;
m_uValue = 0;
m_sFileName = _T("");
m_sSavedName = _T("");
m_bReadOnly = false;
m_bEditValueOnly = false;
m_bWarnOnSymbolChanges = Options.WarnOnSymbolChanges();
}
CSymbolPropertiesGeneralPage::~CSymbolPropertiesGeneralPage(void)
{
}
void CSymbolPropertiesGeneralPage::DoDataExchange(CDataExchange* pDX)
{
CSymbolPropertiesGeneralPage_BASE::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SYM_ICON, m_ctrlIcon);
DDX_Control(pDX, IDC_SYM_WARNING, m_ctrlWarning);
DDX_Control(pDX, IDC_SYM_WARNING_ICON, m_ctrlWarningIcon);
DDX_Control(pDX, IDC_SYM_READONLY_CAUTION, m_ctrlReadOnlyWarning);
DDX_SymbolName(pDX, IDC_SYM_NAME, m_sName);
DDV_SymbolName(pDX, m_sName);
DDX_Text(pDX, IDC_SYM_TYPE, m_sType);
DDX_Text(pDX, IDC_SYM_VALUE, m_uValue);
DDV_MinMaxUInt(pDX, m_uValue, 1, 65535);
DDX_Check(pDX, IDC_SYM_READONLY, m_bReadOnly);
if (!pDX->m_bSaveAndValidate)
{
::PathSetDlgItemPath( m_hWnd,
IDC_SYM_FILENAME,
m_sFileName);
}
}
BEGIN_MESSAGE_MAP(CSymbolPropertiesGeneralPage, CSymbolPropertiesGeneralPage_BASE)
ON_COMMAND( IDC_SYM_NAME, OnCmdSymbolName)
ON_COMMAND( IDC_SYM_VALUE, OnCmdSymbolValue)
ON_COMMAND( IDC_SYM_READONLY, OnCmdReadOnly)
ON_BN_CLICKED( IDC_SYM_FIX_VALUE, OnClickedFixValue)
END_MESSAGE_MAP()
BOOL CSymbolPropertiesGeneralPage::OnInitDialog(void)
{
CSymbolPropertiesGeneralPage_BASE::OnInitDialog();
m_sSavedName = m_sName;
OnCmdSymbolName(); // Set the type and icon, and
// check for name conflicts
OnCmdSymbolValue(); // Check for value conflicts
OnCmdReadOnly(); // Set available controls
return true;
}
BOOL CSymbolPropertiesGeneralPage::OnKillActive(void)
{
// Force the "Conflicts" page to update before losing focus
GetParent()->SendMessage( WM_SYM_CHANGING,
m_uValue,
(LPARAM)(LPCTSTR)m_sName);
return CSymbolPropertiesGeneralPage_BASE::OnKillActive();
}
void CSymbolPropertiesGeneralPage::OnOK(void)
{
// If the option is enabled, show a warning
// if the symbol name has been changed
if (m_bWarnOnSymbolChanges && (m_sSavedName != m_sName) )
{
CNGMessageBox dlg;
dlg.SetMsgEx( MAKEINTRESOURCE(IDP_SYM_RENAME_WARNING), _T("RTF") );
dlg.SetRtf();
dlg.SetStandardIcon(IDI_EXCLAMATION);
dlg.AddButton(IDYES, false, false, _T("Yes") );
dlg.AddButton(IDNO, true, true, _T("No") );
if (IDYES != dlg.DoModal())
{
m_sName = m_sSavedName;
UpdateData(false);
return;
}
}
CSymbolPropertiesGeneralPage_BASE::OnOK();
}
/////////////////////////////////////////////////////////////////////////////
// CSymbolPropertiesGeneralPage operations
bool CSymbolPropertiesGeneralPage::Initialise( CResourceSymbolManager* pSymbols,
CResourceSymbol* pSymbol,
bool bEditValueOnly /*= false*/)
{
m_pSymbols = pSymbols;
ASSERT(NULL != m_pSymbols);
m_pSymbol = pSymbol;
ASSERT(NULL != m_pSymbol);
m_bEditValueOnly = bEditValueOnly;
if (NULL != m_pSymbol)
{
m_sName = m_pSymbol->GetName();
m_uValue = m_pSymbol->GetValue();
m_sFileName = m_pSymbol->GetFileName();
m_bReadOnly = m_pSymbol->IsReadOnly();
}
return (m_pSymbols != NULL);
}
void CSymbolPropertiesGeneralPage::DisableSymbolChangeWarnings(void)
{
m_bWarnOnSymbolChanges = false;
}
/////////////////////////////////////////////////////////////////////////////
// CSymbolPropertiesGeneralPage implementation
bool CSymbolPropertiesGeneralPage::SetTypeIcon(UINT uIconID)
{
if ( (uIconID != 0) && ::IsWindow(m_ctrlIcon.m_hWnd) )
{
m_uIconID = uIconID;
HICON hIcon = AfxGetApp()->LoadIcon(m_uIconID);
ASSERT(hIcon != NULL);
m_ctrlIcon.SetIcon(hIcon);
return true;
}
return false;
}
void CSymbolPropertiesGeneralPage::DDX_SymbolName(CDataExchange* pDX,
UINT uIDC,
CString& rsSymbolName)
{
if (pDX->m_bSaveAndValidate)
{
CString sNewSymbolName;
::DDX_Text(pDX, uIDC, sNewSymbolName);
// Remove whitespace before validating
sNewSymbolName.TrimLeft();
sNewSymbolName.TrimRight();
// Now work out whether it's valid
if (CResourceSymbol::IsValidName(sNewSymbolName) )
{
rsSymbolName = sNewSymbolName;
}
else
{
// Nope - alert the user
CNGMessageBox dlg;
dlg.FormatMsgEx(MAKEINTRESOURCE(IDP_SYM_NAME_INVALID),
_T("RTF"),
sNewSymbolName);
dlg.SetRtf();
dlg.SetStandardIcon(IDI_ERROR);
dlg.AddButton(IDOK, true, true, _T("OK") );
dlg.DoModal();
pDX->Fail();
}
}
else
{
::DDX_Text(pDX, uIDC, rsSymbolName);
}
}
void CSymbolPropertiesGeneralPage::DDV_SymbolName(CDataExchange* pDX,
CString& rsSymbolName)
{
if (pDX->m_bSaveAndValidate)
{
if ( (m_pSymbol != NULL) && !IsNameUnique(m_pSymbol, rsSymbolName, true) )
{
CNGMessageBox dlg;
dlg.SetMsgEx( MAKEINTRESOURCE(IDP_SYM_NAME_WARNING), _T("RTF") );
dlg.SetRtf();
dlg.SetStandardIcon(IDI_ERROR);
dlg.AddButton(IDOK, true, true, _T("OK") );
dlg.DoModal();
pDX->Fail();
}
}
}
bool CSymbolPropertiesGeneralPage::IsNameUnique(CResourceSymbol* pSymbol,
const CString& sName,
bool bCheckSameFileOnly) const
{
bool bUnique = true;
if ( (m_pSymbols != NULL) && (pSymbol != NULL) )
{
CResourceSymbolList listSymbols;
m_pSymbols->GetSymbols(sName, listSymbols);
POSITION pos = listSymbols.GetHeadPosition();
while (pos != NULL)
{
CResourceSymbol* pNextSymbol = listSymbols.GetNext(pos);
if (pNextSymbol != NULL)
{
if (pNextSymbol != pSymbol)
{
if (bCheckSameFileOnly)
{
if (AreSymbolsInSameFile(pNextSymbol, pSymbol) )
{
bUnique = false;
}
}
else
{
bUnique = false;
break;
}
}
}
}
}
return bUnique;
}
bool CSymbolPropertiesGeneralPage::IsValueUnique(CResourceSymbol* pSymbol,
UINT uValue,
bool bCheckSameFileOnly) const
{
bool bUnique = true;
if ( (NULL != m_pSymbols) && (NULL != pSymbol) )
{
CResourceSymbolList listSymbols;
m_pSymbols->GetSymbols(uValue, listSymbols);
POSITION pos = listSymbols.GetHeadPosition();
while (pos != NULL)
{
CResourceSymbol* pNextSymbol = listSymbols.GetNext(pos);
if (pNextSymbol != NULL)
{
if (pNextSymbol != pSymbol)
{
if (bCheckSameFileOnly)
{
if (AreSymbolsInSameFile(pNextSymbol, pSymbol) )
{
bUnique = false;
}
}
else
{
bUnique = false;
break;
}
}
}
}
}
return bUnique;
}
bool CSymbolPropertiesGeneralPage::SetWarningText( const CString& sWarning,
LPCTSTR pszIconID /*= NULL*/)
{
if (!sWarning.IsEmpty() )
{
// Set the icon
m_ctrlWarningIcon.SetIcon(AfxGetApp()->LoadStandardIcon(pszIconID));
// Set the text
m_ctrlWarning.SetWindowText(sWarning);
m_ctrlWarningIcon.ShowWindow(true);
m_ctrlWarning.ShowWindow(true);
return true;
}
m_ctrlWarningIcon.ShowWindow(false);
m_ctrlWarning.ShowWindow(false);
return false;
}
bool CSymbolPropertiesGeneralPage::AreSymbolsInSameFile(CResourceSymbol* pSymbol1, CResourceSymbol* pSymbol2) const
{
return (pSymbol1->GetUserData() == pSymbol2->GetUserData() );
}
void CSymbolPropertiesGeneralPage::CheckNameAndValue(void)
{
CString sWarning;
if (!IsNameUnique(m_pSymbol, m_sName, true) )
{
sWarning.LoadString(IDS_SYM_NAME_CONFLICT_CAPTION);
SetWarningText( sWarning,
IDI_ERROR); // Check same files
ShowDlgControl(IDC_SYM_FIX_VALUE, false);
}
else if (!IsNameUnique(m_pSymbol, m_sName, false) ) // Check other files
{
sWarning.LoadString(IDS_SYM_NAME_CONFLICT_CAPTION_EXTERNAL);
SetWarningText( sWarning,
IDI_WARNING);
ShowDlgControl(IDC_SYM_FIX_VALUE, false);
}
else if (!IsValueUnique(m_pSymbol, m_uValue, true) ) // Check same file
{
sWarning.LoadString(IDS_SYM_VALUE_CONFLICT_CAPTION);
SetWarningText( sWarning,
IDI_WARNING);
ShowDlgControl(IDC_SYM_FIX_VALUE, true);
}
else if (!IsValueUnique(m_pSymbol, m_uValue, false) ) // Check other files
{
sWarning.LoadString(IDS_SYM_VALUE_CONFLICT_CAPTION_EXTERNAL);
SetWarningText( sWarning,
IDI_WARNING);
ShowDlgControl(IDC_SYM_FIX_VALUE, true);
}
else if (m_pSymbols->IsOutOfRange(SymbolTypes.GetType(m_sName), m_uValue) ) // Check if out of range
{
sWarning.LoadString(IDS_SYM_VALUE_OUT_OF_RANGE_CAPTION);
SetWarningText( sWarning,
IDI_WARNING);
ShowDlgControl(IDC_SYM_FIX_VALUE, true);
}
else
{
SetWarningText( _T("") );
ShowDlgControl(IDC_SYM_FIX_VALUE, false);
}
// Force a repaint so the warning paints correctly
Invalidate();
// Inform the parent that the symbol is changing
// This is used to update the "Conflicts" page
GetParent()->SendMessage( WM_SYM_CHANGING,
m_uValue,
(LPARAM)(LPCTSTR)m_sName);
}
/////////////////////////////////////////////////////////////////////////////
// CSymbolPropertiesGeneralPage message handlers
void CSymbolPropertiesGeneralPage::OnCmdSymbolName(void)
{
// Update type control and icon
int eType = SymbolTypes.GetType(m_sName);
// Set the icon...
SetTypeIcon( SymbolTypes.GetIconID(eType) );
// ...and the displayed description
CString sNewType = SymbolTypes.GetTypeName(eType);
bool bTypeChanged = (!m_sType.IsEmpty() && (sNewType != m_sType) );
m_sType = sNewType;
ASSERT(!m_sType.IsEmpty());
if (bTypeChanged)
{
// Pick a value for the symbol which is within range for its new type
OnClickedFixValue();
}
UpdateData(false);
// Check for name conflicts...
CheckNameAndValue();
}
void CSymbolPropertiesGeneralPage::OnCmdSymbolValue(void)
{
CheckNameAndValue();
}
void CSymbolPropertiesGeneralPage::OnCmdReadOnly(void)
{
ShowDlgControl( IDC_SYM_READONLY_CAUTION, m_bReadOnly);
SetReadOnly( IDC_SYM_NAME, m_bReadOnly || m_bEditValueOnly);
SetReadOnly( IDC_SYM_VALUE, m_bReadOnly);
EnableDlgControl( IDC_SYM_FIX_VALUE, !m_bReadOnly);
if (m_bEditValueOnly)
{
// If only the value is editable, set the focus to it
GetDlgItem(IDC_SYM_VALUE)->SetFocus();
}
}
void CSymbolPropertiesGeneralPage::OnClickedFixValue(void)
{
BOOL bFailed = false;
UINT uNewValue = m_pSymbols->GetBaseSymbolValue( SymbolTypes.GetType(m_sName) );
while (!IsValueUnique(m_pSymbol, uNewValue, false) )
{
uNewValue++;
if (uNewValue >= IDC_STATIC)
{
bFailed = true;
ASSERT(false);
break; // Can't fix this one
}
}
if (!bFailed)
{
m_uValue = uNewValue;
UpdateData(false);
CheckNameAndValue();
}
else
{
::AfxMessageBox(IDP_SYM_FIX_VALUE_FAILED,
MB_OK | MB_ICONEXCLAMATION);
}
}