// PrPgeSpellOptions_Common.cpp : implementation file
//
#include "stdafx.h"
#include "FPSSpellChecker.h"
#include "PrPgeSpellOptions_Common.h"
#include "FPSSpellCheckEngine.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int CALLBACK SortList(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
/////////////////////////////////////////////////////////////////////////////
// CPrPgeSpellOptions_Common property page
IMPLEMENT_DYNCREATE(CPrPgeSpellOptions_Common, CPropertyPage)
CPrPgeSpellOptions_Common::CPrPgeSpellOptions_Common() : CPropertyPage(CPrPgeSpellOptions_Common::IDD)
{
m_bDisplayed = FALSE;
//{{AFX_DATA_INIT(CPrPgeSpellOptions_Common)
m_strCorrect = _T("");
m_strInCorrect = _T("");
//}}AFX_DATA_INIT
m_pEngine = NULL;
m_bChangeEdit = TRUE;
}
CPrPgeSpellOptions_Common::~CPrPgeSpellOptions_Common()
{
CleanupWordList();
}
void CPrPgeSpellOptions_Common::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPrPgeSpellOptions_Common)
DDX_Control(pDX, IDC_LIST, m_lstWords);
DDX_Text(pDX, IDC_CORRECT, m_strCorrect);
DDX_Text(pDX, IDC_INCORRECT, m_strInCorrect);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPrPgeSpellOptions_Common, CPropertyPage)
//{{AFX_MSG_MAP(CPrPgeSpellOptions_Common)
ON_EN_CHANGE(IDC_CORRECT, OnChangeCorrect)
ON_EN_CHANGE(IDC_INCORRECT, OnChangeIncorrect)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
ON_NOTIFY(LVN_KEYDOWN, IDC_LIST, OnKeydownList)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnItemchangedList)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPrPgeSpellOptions_Common message handlers
BOOL CPrPgeSpellOptions_Common::OnInitDialog()
{
CPropertyPage::OnInitDialog();
CRect ListRect;
m_lstWords.GetWindowRect(ListRect);
ScreenToClient(ListRect);
m_lstWords.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_lstWords.InsertColumn(0, "Incorrect", LVCFMT_LEFT, ListRect.Width() / 2 - 10);
m_lstWords.InsertColumn(0, "Correct", LVCFMT_LEFT, ListRect.Width() / 2 - 10);
ASSERT(m_pEngine);
BuildList();
m_bDisplayed = TRUE;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPrPgeSpellOptions_Common::BuildList()
{
CStdioFile fp;
CString strLine;
CString strFile = m_pEngine->GetOptions().GetCommonDic();
try
{
if (fp.Open(strFile, CFile::modeRead | CFile::shareExclusive))
{
while (fp.ReadString(strLine))
{
strLine.TrimLeft(); strLine.TrimRight();
int iPos = strLine.Find("=");
if (iPos > -1)
{
CString strIncorrect = strLine.Left(iPos); strIncorrect.TrimLeft(); strIncorrect.TrimRight();
CString strCorrect = strLine.Mid(iPos+1); strCorrect.TrimLeft(); strCorrect.TrimRight();
AddEntry(strIncorrect, strCorrect);
}
}
m_lstWords.SortItems(SortList, (DWORD)&m_lstWords);
AddEntry("(New)","(New)");
fp.Close();
}
}
catch(CException* pError)
{
pError->Delete();
ASSERT(FALSE);
}
catch(...)
{
ASSERT(FALSE);
}
}
void CPrPgeSpellOptions_Common::AddEntry(LPCSTR lpszIncorrect, LPCSTR lpszCorrect)
{
ASSERT(::IsWindow(m_lstWords.GetSafeHwnd()));
CDlgSpellChecker_CHANGEALL* pEntry = new CDlgSpellChecker_CHANGEALL;
ASSERT(pEntry);
pEntry->strBad = lpszIncorrect;
pEntry->strGood = lpszCorrect;
int iItem = m_lstWords.GetItemCount();
iItem = m_lstWords.InsertItem(iItem, lpszIncorrect, 0);
m_lstWords.SetItemText(iItem, 1, lpszCorrect);
m_lstWords.SetItemData(iItem, (DWORD)pEntry);
}
void CPrPgeSpellOptions_Common::OnChangeCorrect()
{
UpdateListControl();
}
void CPrPgeSpellOptions_Common::OnChangeIncorrect()
{
UpdateListControl();
}
void CPrPgeSpellOptions_Common::OnKeydownList(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDown = (LV_KEYDOWN*)pNMHDR;
ASSERT(pLVKeyDown);
if (pLVKeyDown->wVKey == VK_DELETE)
{
POSITION Pos = m_lstWords.GetFirstSelectedItemPosition();
if (Pos)
{
int iSel = m_lstWords.GetNextSelectedItem(Pos);
if (iSel < m_lstWords.GetItemCount()-1)
{
m_lstWords.DeleteItem(iSel);
m_lstWords.SetItemState(iSel, LVIS_SELECTED, LVIS_SELECTED);
m_lstWords.EnsureVisible(iSel, FALSE);
}
}
}
*pResult = 0;
}
void CPrPgeSpellOptions_Common::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
{
UpdateEditControls();
*pResult = 0;
}
void CPrPgeSpellOptions_Common::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult)
{
UpdateEditControls();
*pResult = 0;
}
void CPrPgeSpellOptions_Common::UpdateEditControls()
{
if (!m_bChangeEdit)
return;
POSITION Pos = m_lstWords.GetFirstSelectedItemPosition();
if (Pos)
{
int iSel = m_lstWords.GetNextSelectedItem(Pos);
m_strInCorrect = m_lstWords.GetItemText(iSel, 0);
m_strCorrect = m_lstWords.GetItemText(iSel, 1);
}
UpdateData(FALSE);
}
void CPrPgeSpellOptions_Common::UpdateListControl()
{
m_bChangeEdit = FALSE;
UpdateData();
CString strBad = m_strInCorrect;
CString strGood = m_strCorrect;
int iSel = m_lstWords.GetItemCount()-1;
strBad.TrimLeft(); strBad.TrimRight();
strGood.TrimLeft(); strGood.TrimRight();
if (strBad == "(New)") strBad = "";
if (strGood == "(New)") strGood = "";
POSITION Pos = m_lstWords.GetFirstSelectedItemPosition();
if (Pos)
{
iSel = m_lstWords.GetNextSelectedItem(Pos);
}
else
{
m_lstWords.SetItemState(iSel, LVIS_SELECTED , LVIS_SELECTED );
m_lstWords.SetSelectionMark(iSel);
m_lstWords.EnsureVisible(iSel, FALSE);
}
m_lstWords.SetItemText(iSel, 0, strBad);
m_lstWords.SetItemText(iSel, 1, strGood);
if (iSel == m_lstWords.GetItemCount() - 1)
{
iSel = m_lstWords.InsertItem(iSel+1, "(New)", 0);
m_lstWords.SetItemText(iSel, 1, "(New)");
}
m_bChangeEdit = TRUE;
}
void CPrPgeSpellOptions_Common::OnDestroy()
{
CPropertyPage::OnDestroy();
CleanupWordList();
int iCount = m_lstWords.GetItemCount();
for (int iPos = 0; iPos < iCount; iPos++)
{
CString strBad = m_lstWords.GetItemText(iPos, 0);
CString strGood = m_lstWords.GetItemText(iPos, 1);
CDlgSpellChecker_CHANGEALL* pEntry = new CDlgSpellChecker_CHANGEALL;
ASSERT(pEntry);
m_Words.AddTail(pEntry);
pEntry->strBad = strBad;
pEntry->strGood = strGood;
}
}
void CPrPgeSpellOptions_Common::Save(CFPSSpellCheckEngine *pEngine)
{
if (!m_bDisplayed)
return;
ASSERT(pEngine);
CString strFile = pEngine->GetOptions().GetCommonDic();
CStdioFile fp;
try
{
if (fp.Open(strFile, CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive))
{
POSITION Pos = m_Words.GetHeadPosition();
while (Pos)
{
CDlgSpellChecker_CHANGEALL* pEntry = m_Words.GetNext(Pos);
CString strBad = pEntry->strBad;
CString strGood = pEntry->strGood;
CString strLine = strBad;
strLine += "=";
strLine += strGood;
if (strBad != "(New)")
{
fp.WriteString(strLine);
fp.WriteString("\n");
}
}
fp.Close();
}
}
catch(CException* pError)
{
pError->Delete();
ASSERT(FALSE);
}
catch(...)
{
ASSERT(FALSE);
}
}
int CALLBACK SortList(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
int iReturn = 0;
CDlgSpellChecker_CHANGEALL* pEntry1 = (CDlgSpellChecker_CHANGEALL*)lParam1;
CDlgSpellChecker_CHANGEALL* pEntry2 = (CDlgSpellChecker_CHANGEALL*)lParam2;
ASSERT(pEntry1);
ASSERT(pEntry2);
if (pEntry1->strBad < pEntry2->strBad)
return -1;
if (pEntry1->strBad > pEntry2->strBad)
return 1;
return 0;
}
void CPrPgeSpellOptions_Common::CleanupWordList()
{
CDlgSpellChecker_CHANGEALL* pEntry;
POSITION Pos;
Pos = m_Words.GetHeadPosition();
while (Pos)
{
pEntry = m_Words.GetNext(Pos);
delete pEntry;
}
m_Words.RemoveAll();
}