// InsertElementDialog.cpp : implementation file
//
#include "stdafx.h"
#include "Exile.h"
#include "InsertElementDialog.h"
#include "ChangeIconDialog.h"
#include "PasswordGeneratorDialog.h"
#include "Security.h"
#include "predefined.h"
#include "messagebox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CInsertElementDialog dialog
CInsertElementDialog::CInsertElementDialog(CWnd* pParent /*=NULL*/)
: CDialog(CInsertElementDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CInsertElementDialog)
m_strAddress = _T("");
m_strElementName = _T("");
m_strLogin = _T("");
m_strPassword = _T("");
m_strNotes = _T("");
//}}AFX_DATA_INIT
}
CInsertElementDialog::~CInsertElementDialog()
{
CleanString(m_strAddress);
CleanString(m_strElementName);
CleanString(m_strLogin);
CleanString(m_strPassword);
CleanString(m_strNotes);
}
void CInsertElementDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CInsertElementDialog)
DDX_Control(pDX, IDC_RATING, m_ctrlRating);
DDX_Control(pDX, IDC_HOTKEY, m_ctrlHotKey);
DDX_Control(pDX, IDC_ELEMENTICON, m_ctrlElementIcon);
DDX_Text(pDX, IDC_ADDRESS, m_strAddress);
DDX_CBString(pDX, IDC_ELEMENTNAME, m_strElementName);
DDX_Text(pDX, IDC_LOGIN, m_strLogin);
DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
DDX_Text(pDX, IDC_NOTES, m_strNotes);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CInsertElementDialog, CDialog)
//{{AFX_MSG_MAP(CInsertElementDialog)
ON_CBN_SELCHANGE(IDC_ELEMENTNAME, OnSelchangeElementname)
ON_BN_CLICKED(IDC_CHANGEICON, OnChangeicon)
ON_BN_CLICKED(IDC_PASSWORD_TITLE, OnPasswordTitle)
ON_COMMAND(ID_PASSWORDCONTEXT_GENERATEPASSWORD, OnPasswordcontextGeneratepassword)
ON_COMMAND(ID_PASSWORDCONTEXT_HIDEPASSWORD, OnPasswordcontextHidepassword)
ON_COMMAND(ID_PASSWORDCONTEXT_SHOWPASSWORD, OnPasswordcontextShowpassword)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInsertElementDialog message handlers
BOOL CInsertElementDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_ELEMENTNAME);
for(int n = 0; n < sizeof(g_aElements) / sizeof(g_aElements[0]); ++n)
{
pCombo->AddString(g_aElements[n].szElementName);
} // for
// Ratings
for(n = 0; n < sizeof(g_aRatings) / sizeof(g_aRatings[0]); ++n)
{
m_ctrlRating.InsertString(n, g_aRatings[n].szName);
m_ctrlRating.SetItemData(n, (DWORD)g_aRatings[n].cRating);
} // for
m_ctrlElementIcon.SetIcon(m_pimgList->ExtractIcon(9));
m_ctrlRating.SetCurSel(5);
m_lIconID = 0;
m_bPassword = TRUE;
((CEdit *)GetDlgItem(IDC_PASSWORD))->SetPasswordChar(m_bPassword ? _T(0) : _T('*'));
m_ctrlPasswordLink.SubclassDlgItem(IDC_PASSWORD_TITLE, this);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CInsertElementDialog::OnSelchangeElementname()
{
CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_ELEMENTNAME);
if(CB_ERR != pCombo->GetCurSel())
{
TCHAR szName[nNameLength];
pCombo->GetLBText(pCombo->GetCurSel(), szName);
// Look in predefined categories
for(int n = 0; n < sizeof(g_aElements) / sizeof(g_aElements[0]); ++n)
{
#ifdef _UNICODE
if(0 == wcscmp(szName, g_aElements[n].szElementName))
{
m_strAddress = g_aElements[n].szAddress;
m_strNotes = g_aElements[n].szNotes;
m_lIconID = g_aElements[n].lIconID;
m_ctrlElementIcon.SetIcon(m_pimgList->ExtractIcon(m_lIconID));
UpdateData(FALSE);
break;
} // if
#else
if(0 == strcmp(szName, g_aElements[n].szElementName))
{
m_strAddress = g_aElements[n].szAddress;
m_strNotes = g_aElements[n].szNotes;
m_lIconID = g_aElements[n].lIconID;
m_ctrlElementIcon.SetIcon(m_pimgList->ExtractIcon(m_lIconID));
UpdateData(FALSE);
break;
} // if
#endif // _UNICODE
} // for
} // if
}
long CInsertElementDialog::GetIconID()
{
return m_lIconID;
}
void CInsertElementDialog::OnChangeicon()
{
CChangeIconDialog dlgChange;
dlgChange.SetImageList(m_pimgList);
if(IDOK == dlgChange.DoModal())
{
m_lIconID = dlgChange.GetIconID();
m_ctrlElementIcon.SetIcon(m_pimgList->ExtractIcon(m_lIconID));
} // if
}
void CInsertElementDialog::OnPasswordTitle()
{
CRect rc;
GetDlgItem(IDC_PASSWORD_TITLE)->GetWindowRect(rc);
CMenu mCtx;
mCtx.LoadMenu(IDR_PASSWORD_CONTEXT);
CMenu *pmCtx = mCtx.GetSubMenu(0);
pmCtx->EnableMenuItem(ID_PASSWORDCONTEXT_HIDEPASSWORD, MF_BYCOMMAND |
(m_bPassword ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
pmCtx->EnableMenuItem(ID_PASSWORDCONTEXT_SHOWPASSWORD, MF_BYCOMMAND |
(!m_bPassword ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
pmCtx->TrackPopupMenu(TPM_LEFTALIGN, rc.left, rc.bottom, this);
}
void CInsertElementDialog::OnPasswordcontextGeneratepassword()
{
CPasswordGeneratorDialog dlgPassword;
if(IDOK == dlgPassword.DoModal())
{
UpdateData();
m_strPassword = dlgPassword.m_strPassword;
UpdateData(FALSE);
CleanString(dlgPassword.m_strPassword);
} // if
}
void CInsertElementDialog::OnPasswordcontextHidepassword()
{
m_bPassword = FALSE;
((CEdit *)GetDlgItem(IDC_PASSWORD))->SetPasswordChar(_T('*'));
GetDlgItem(IDC_PASSWORD)->Invalidate();
}
void CInsertElementDialog::OnPasswordcontextShowpassword()
{
m_bPassword = TRUE;
((CEdit *)GetDlgItem(IDC_PASSWORD))->SetPasswordChar(_T(0));
GetDlgItem(IDC_PASSWORD)->Invalidate();
}
void CInsertElementDialog::OnOK()
{
UpdateData();
if(m_strElementName.IsEmpty())
{
MessageBoxEx(*this, IDS_ENTERELEMENTNAME, IDS_TITLE);
return;
} // if
if(CB_ERR == m_ctrlRating.GetCurSel())
{
MessageBoxEx(*this, IDS_SELECTRATING, IDS_TITLE);
return;
} // if
m_cRating = (BYTE)m_ctrlRating.GetItemData(m_ctrlRating.GetCurSel());
m_ctrlHotKey.GetHotKey(m_wVKCode, m_wModifiers);
CDialog::OnOK();
}