/********************************************************************
created: 2001/11/05
created: 5:11:2001 16:24
filename: d:\My Projects\Own\Add-on\ToDoCached\ShtKeywords.cpp
file path: d:\My Projects\Own\Add-on\ToDoCached
file base: ShtKeywords
file ext: cpp
author: Alex Kucherenko
purpose:
*********************************************************************/
#include "stdafx.h"
#include "ShtKeywords.h"
#include "todohlp.h"
#ifdef _DEBUG
# define new DEBUG_NEW
# undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//
IMPLEMENT_DYNCREATE(CShtKeywords, CHHPropPage)
//////////////////////////////////////////////////////////////////////////
// main constructor of dialog with help support
CShtKeywords::CShtKeywords( CRegistryEx *pReg /*= NULL*/ )
: CHHPropPage(CShtKeywords::IDD, true, IDH_PAGE_KEYWORDS )
, CInitDialogImpl( "keywords_optdlg", pReg )
, m_bCommModified( FALSE )
, m_bKeyModified( FALSE )
{
//{{AFX_DATA_INIT(CShtKeywords)
m_strExtension = _T("");
m_strComment = _T("");
m_strKeyword = _T("");
m_strKeywordList = _T("");
//}}AFX_DATA_INIT
}
//////////////////////////////////////////////////////////////////////////
// destructor
CShtKeywords::~CShtKeywords()
{
}
//////////////////////////////////////////////////////////////////////////
// data exchange function of MFC dialog
void CShtKeywords::DoDataExchange(CDataExchange* pDX)
{
CHHPropPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShtKeywords)
DDX_Control(pDX, IDC_KEYLIST, m_cKeys);
DDX_Text(pDX, IDC_EXTENSIONS, m_strExtension);
DDV_MaxChars(pDX, m_strExtension, 255);
DDX_Text(pDX, IDC_COMMENT, m_strComment);
DDV_MaxChars(pDX, m_strComment, 255);
DDX_Text(pDX, IDC_KEYWORD, m_strKeyword);
DDV_MaxChars(pDX, m_strKeyword, 255);
DDX_LBString(pDX, IDC_KEYLIST, m_strKeywordList);
//}}AFX_DATA_MAP
}
//////////////////////////////////////////////////////////////////////////
// message mapping
BEGIN_MESSAGE_MAP(CShtKeywords, CHHPropPage)
//{{AFX_MSG_MAP(CShtKeywords)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_LBN_SELCHANGE(IDC_KEYLIST, OnSelchangeKeylist)
ON_EN_CHANGE(IDC_COMMENT, OnChangeComment)
ON_EN_CHANGE(IDC_KEYWORD, OnChangeKeyword)
ON_BN_CLICKED(IDC_APPLY, OnApplyChanges)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Reover dialog settings from registry
void CShtKeywords::GetInitSettings()
{
m_arrKeywords = m_pRegistry->GetArrayByTemplate( "search\\search%d" );
m_arrComments = m_pRegistry->GetArrayByTemplate( "search\\text%d" );
m_strExtension = GetDlgItemValue( "extensions" ).c_str();
};
//////////////////////////////////////////////////////////////////////////
// save settings to registry
void CShtKeywords::SetInitSettings()
{
m_pRegistry->DeleteByTemplate( "search\\search%d" );
m_pRegistry->SetValueByTemplate( "search\\search%d", m_arrKeywords );
m_pRegistry->DeleteByTemplate( "search\\text%d" );
m_pRegistry->SetValueByTemplate( "search\\text%d", m_arrComments );
SetDlgItemParam( "extensions", m_strExtension );
};
//////////////////////////////////////////////////////////////////////////
// Init function of dialog
BOOL CShtKeywords::OnInitDialog()
{
CHHPropPage::OnInitDialog();
GetInitSettings();
TStrArrayIter i;
m_cKeys.LockWindowUpdate();
for( i = m_arrKeywords.begin(); i != m_arrKeywords.end(); i++ )
m_cKeys.AddString( i->c_str() );
m_cKeys.UnlockWindowUpdate();
if( !m_arrKeywords.empty() )
{
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_DELETE ), TRUE );
m_strKeyword = m_arrKeywords[0].c_str();
}
if( !m_arrComments.empty() )
m_strComment = m_arrComments[0].c_str();
m_cKeys.SetCurSel( 0 );
UpdateData( FALSE );
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
// save settings to registry on dialog destroy
void CShtKeywords::OnDestroy()
{
UpdateData();
SetInitSettings();
CHHPropPage::OnDestroy();
}
//////////////////////////////////////////////////////////////////////////
// delete one keyword from list
void CShtKeywords::OnDelete()
{
UpdateData();
int iSel = m_cKeys.GetCurSel();
if( !m_arrComments.empty() )
{
TStrArrayIter i = m_arrKeywords.begin();
TStrArrayIter k = m_arrComments.begin();
std::advance( i, iSel );
std::advance( k, iSel );
m_arrKeywords.erase( i );
m_arrComments.erase( k );
UpdateListControl();
if( m_arrKeywords.empty() )
{
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_DELETE ), FALSE );
m_strKeyword = "";
m_strComment = "";
UpdateData( FALSE );
}
}
else
{
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_DELETE ), FALSE );
}
}
//////////////////////////////////////////////////////////////////////////
// Sort items of list control
void CShtKeywords::SortListItems(void)
{
// Sort and unique values in list control
TKeyMap mapTemp;
for( UINT i = 0 ; i < m_arrKeywords.size(); i++ )
mapTemp.insert( TKeyMap::value_type( m_arrKeywords[i], m_arrComments[i] ) );
m_arrKeywords.clear();
m_arrComments.clear();
TKeyMap::iterator j;
for( j = mapTemp.begin(); j != mapTemp.end(); j++ )
{
m_arrKeywords.push_back( j->first );
m_arrComments.push_back( j->second );
}
mapTemp.clear();
}
//////////////////////////////////////////////////////////////////////////
// add new keyword to list
void CShtKeywords::OnAdd()
{
UpdateData();
if( m_strKeyword.IsEmpty() ) return;
string temp = m_strKeyword;
m_arrKeywords.push_back( temp );
temp = m_strComment;
m_arrComments.push_back( temp );
SortListItems();
UpdateListControl();
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_APPLY ), FALSE );
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_DELETE ), TRUE );
UpdateData( FALSE );
}
//////////////////////////////////////////////////////////////////////////
// on selection change in list change data in edit boxes
void CShtKeywords::OnSelchangeKeylist()
{
UpdateData();
int iSel = m_cKeys.GetCurSel();
if( !m_arrKeywords.empty() )
m_strKeyword = m_arrKeywords[ iSel ].c_str();
if( !m_arrComments.empty() )
m_strComment = m_arrComments[ iSel ].c_str();
UpdateData( FALSE );
}
//////////////////////////////////////////////////////////////////////////
// user change some comments
void CShtKeywords::OnChangeComment()
{
if( !m_arrKeywords.empty() )
{
m_bCommModified = TRUE;
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_APPLY ), TRUE );
}
}
//////////////////////////////////////////////////////////////////////////
// user change keyword
void CShtKeywords::OnChangeKeyword()
{
if( !m_arrKeywords.empty() )
{
m_bKeyModified = TRUE;
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_APPLY ), TRUE );
}
}
//////////////////////////////////////////////////////////////////////////
// Store changes into current keyowrd item
void CShtKeywords::OnApplyChanges()
{
UpdateData();
int iSel = m_cKeys.GetCurSel();
string temp = m_strKeyword;
TStrArrayIter k = std::find( m_arrKeywords.begin(), m_arrKeywords.end(), temp );
if( k != m_arrKeywords.end() && m_bKeyModified == TRUE )
{
::MessageBox( NULL, "Keyword must be unique. Please change keyword", "Error", MB_OK );
return;
}
if( !m_arrKeywords.empty() )
{
m_arrKeywords[ iSel ] = m_strKeyword;
m_arrComments[ iSel ] = m_strComment;
}
SortListItems();
UpdateListControl();
::EnableWindow( ::GetDlgItem( m_hWnd, IDC_APPLY ), FALSE );
m_bCommModified = FALSE;
m_bKeyModified = FALSE;
UpdateData( FALSE );
}
//////////////////////////////////////////////////////////////////////////
// Create keyword list from the vector list
void CShtKeywords::UpdateListControl( int iSel /*= 0*/ )
{
TStrArrayIter i;
m_cKeys.LockWindowUpdate();
m_cKeys.ResetContent();
for( i = m_arrKeywords.begin(); i != m_arrKeywords.end(); i++ )
m_cKeys.AddString( i->c_str() );
m_cKeys.SetCurSel( iSel );
m_cKeys.UnlockWindowUpdate();
};
//:> end of file