Click here to Skip to main content
Click here to Skip to main content

Internet Explorer AdBlock

By , 17 Jan 2008
 
ieadblock_src.zip
IE AdBlock
AdBlockBand.rgs
AdBlockBHO.rgs
bin
XListCtrlSSDU.lib
XListCtrlSSRU.lib
IE AdBlock.def
IE AdBlock.rgs
res
bmpToolbar.bmp
checkboxes.bmp
icoSettings.ico
XListCtrl
checkboxes.bmp
IE AdBlockPS
IE AdBlockps.def
//*********************************************************************
// INCLUDES
//*********************************************************************

#include "stdafx.h"
#include "SettingsList.h"
#include "ListItemDlg.h"

//*********************************************************************
// MESSAGE MAP
//*********************************************************************

BEGIN_MESSAGE_MAP(CSettingsList, CSettingsPage)
	ON_NOTIFY(NM_DBLCLK, lstPatterns, OnListPatternsDoubleClick)
	ON_NOTIFY(NM_DBLCLK, lstRegularExpressions, OnListRegularExpressionsDoubleClick)
	ON_COMMAND(btnAddPattern, OnBtnAddPattern)
	ON_COMMAND(btnAddRegularExpression, OnBtnAddRegularExpression)
	ON_COMMAND(btnModifyPattern, OnBtnModifyPattern)
	ON_COMMAND(btnModifyRegularExpression, OnBtnModifyRegularExpression)
	ON_COMMAND(btnRemovePattern, OnBtnRemovePattern)
	ON_COMMAND(btnRemoveRegularExpression, OnBtnRemoveRegularExpression)
END_MESSAGE_MAP()

//*********************************************************************
// CONSTRUCTOR & DESTRUCTOR
//*********************************************************************

IMPLEMENT_DYNAMIC(CSettingsList, CSettingsPage)

//=====================================================================

CSettingsList::CSettingsList(CAdList * pAdList)
	: CSettingsPage(CSettingsList::IDD)
{
	// Store list pointer
	m_pOriginalList = pAdList;
	m_pList = new CAdList();

	// Copy temporary list
	CopyList(m_pOriginalList, m_pList);
}

//=====================================================================

CSettingsList::~CSettingsList()
{
	// Remove temporary list
	delete m_pList;
	m_pList = NULL;
}

//*********************************************************************
// PUBLIC FUNCTIONS
//*********************************************************************

BOOL CSettingsList::OnInitDialog()
{
	// Declare variables
	CSettings * pSettings = CSettings::Instance();

	// Call original function
	CSettingsPage::OnInitDialog();

	// Load strings
	m_lblPatterns.SetWindowText(m_pLanguage->GetString(IDS_LIST_PATTERNS));
	m_lblRegularExpressions.SetWindowText(m_pLanguage->GetString(IDS_LIST_REGULAREXPRESSIONS));
	m_btnAddPattern.SetWindowText(m_pLanguage->GetString(IDS_LIST_ADD));
	m_btnAddRegularExpression.SetWindowText(m_pLanguage->GetString(IDS_LIST_ADD));
	m_btnModifyPattern.SetWindowText(m_pLanguage->GetString(IDS_LIST_MODIFY));
	m_btnModifyRegularExpression.SetWindowText(m_pLanguage->GetString(IDS_LIST_MODIFY));
	m_btnRemovePattern.SetWindowText(m_pLanguage->GetString(IDS_LIST_REMOVE));
	m_btnRemoveRegularExpression.SetWindowText(m_pLanguage->GetString(IDS_LIST_REMOVE));

	// Automatically sort list if requested
	m_pList->SetKeepSorted(m_pOriginalList->GetKeepSorted());

	// Add new column to the list
	m_lstPatterns.InsertColumn(0, _T(""), LVCFMT_LEFT, 309);
	m_lstRegularExpressions.InsertColumn(0, _T(""), LVCFMT_LEFT, 309);

	// Set style
	m_lstPatterns.ModifyStyle(0, LVS_REPORT | LVS_SHOWSELALWAYS);
	m_lstPatterns.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_lstRegularExpressions.ModifyStyle(0, LVS_REPORT | LVS_SHOWSELALWAYS);
	m_lstRegularExpressions.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	// Update list data
	UpdateListPatterns();
	UpdateListRegularExpressions();

	// Return TRUE unless you want to set focus to control
	return TRUE;
}

//=====================================================================

BOOL CSettingsList::OnSetActive()
{
	// Is this dirty?
	if (!IsDirty())
	{
		// Copy list
		if (m_pList)
		{
			// Delete current list
			delete m_pList;
		}
		m_pList = new CAdList();

		// Copy temporary list
		CopyList(m_pOriginalList, m_pList);

		// Update list data
		UpdateListPatterns();
		UpdateListRegularExpressions();
	}

	// Call original function
	return CSettingsPage::OnSetActive();
}

//=====================================================================

BOOL CSettingsList::OnApply()
{
	// Is list dirty?
	if (IsDirty())
	{
		// Copy temporary list
		CopyList(m_pList, m_pOriginalList);

		// Save list to file
		CListData::Instance()->StoreListData();
	}

	// Call original function
	return CSettingsPage::OnApply();
}

//=====================================================================

void CSettingsList::OnListPatternsDoubleClick(NMHDR* pNMHDR, LRESULT* pResult)
{
	// User wants to modify item
	OnBtnModifyPattern();
}

//=====================================================================

void CSettingsList::OnListRegularExpressionsDoubleClick(NMHDR* pNMHDR, LRESULT* pResult)
{
	// User wants to modify item
	OnBtnModifyRegularExpression();
}

//=====================================================================

void CSettingsList::OnBtnAddPattern()
{
	// Create dialog
	CListItemDlg dlgAdd;
	
	// Show dialog
	if (dlgAdd.DoModal() == IDOK)
	{
		// Add item
		m_pList->AddPattern(dlgAdd.GetItemValue());

		// Update list data
		UpdateListPatterns();

		// Data is modified
		SetModified();
	}
}

//=====================================================================

void CSettingsList::OnBtnAddRegularExpression()
{
	// Create dialog
	CListItemDlg dlgAdd;
	
	// Show dialog
	if (dlgAdd.DoModal() == IDOK)
	{
		// Add item
		m_pList->AddRegularExpression(dlgAdd.GetItemValue());

		// Update list data
		UpdateListRegularExpressions();

		// Data is modified
		SetModified();
	}
}

//=====================================================================

void CSettingsList::OnBtnModifyPattern()
{
	// Get current selection
	int iSel = m_lstPatterns.GetSelectionMark();

	// Did we select something useful?
	if (iSel == -1)
	{
		// No, exit function
		return;
	}

	// Create dialog
	CListItemDlg dlgModify(m_pList->GetPattern(iSel));
	
	// Show dialog
	if (dlgModify.DoModal() == IDOK)
	{
		// Change data
		m_pList->ModifyPattern(iSel, dlgModify.GetItemValue());
		
		// Update list data
		UpdateListPatterns();

		// Data is modified
		SetModified();
	}
}

//=====================================================================

void CSettingsList::OnBtnModifyRegularExpression()
{
	// Get current selection
	int iSel = m_lstRegularExpressions.GetSelectionMark();

	// Did we select something useful?
	if (iSel == -1)
	{
		// No, exit function
		return;
	}

	// Create dialog
	CListItemDlg dlgModify(m_pList->GetRegularExpression(iSel));
	
	// Show dialog
	if (dlgModify.DoModal() == IDOK)
	{
		// Change data
		m_pList->ModifyRegularExpression(iSel, dlgModify.GetItemValue());
		
		// Update list data
		UpdateListRegularExpressions();

		// Data is modified
		SetModified();
	}
}

//=====================================================================

void CSettingsList::OnBtnRemovePattern()
{
	// Get current selection
	int iSel = m_lstPatterns.GetSelectionMark();

	// Ask user
	if (MessageBox(m_pLanguage->GetString(IDS_LIST_REMOVEQUESTION), m_pLanguage->GetString(IDS_PROJNAME),
		MB_ICONQUESTION | MB_YESNO) == IDYES)
	{
		// Remove item from data list
		m_pList->RemovePattern(iSel);

		// Remove item from UI list
		m_lstPatterns.DeleteItem(iSel);

		// Was this last item?
		if (iSel == m_lstPatterns.GetItemCount())
		{
			// Select previous
			iSel--;
		}

		// Select the item
		m_lstPatterns.SetItemState(iSel, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
		m_lstPatterns.EnsureVisible(iSel, FALSE);
		m_lstPatterns.SetFocus();

		// Data is modified
		SetModified();
	}
}

//=====================================================================

void CSettingsList::OnBtnRemoveRegularExpression()
{
	// Get current selection
	int iSel = m_lstRegularExpressions.GetSelectionMark();

	// Ask user
	if (MessageBox(m_pLanguage->GetString(IDS_LIST_REMOVEQUESTION), m_pLanguage->GetString(IDS_PROJNAME),
		MB_ICONQUESTION | MB_YESNO) == IDYES)
	{
		// Remove item from data list
		m_pList->RemoveRegularExpression(iSel);

		// Remove item from UI list
		m_lstRegularExpressions.DeleteItem(iSel);

		// Was this last item?
		if (iSel == m_lstRegularExpressions.GetItemCount())
		{
			// Select previous
			iSel--;
		}

		// Select the item
		m_lstRegularExpressions.SetItemState(iSel, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
		m_lstRegularExpressions.EnsureVisible(iSel, FALSE);
		m_lstRegularExpressions.SetFocus();

		// Data is modified
		SetModified();
	}
}

//*********************************************************************
// PRIVATE FUNCTIONS
//*********************************************************************

void CSettingsList::DoDataExchange(CDataExchange* pDX)
{
	CSettingsPage::DoDataExchange(pDX);
	DDX_Control(pDX, lstPatterns, m_lstPatterns);
	DDX_Control(pDX, lstRegularExpressions, m_lstRegularExpressions);
	DDX_Control(pDX, lblPatterns, m_lblPatterns);
	DDX_Control(pDX, lblRegularExpressions, m_lblRegularExpressions);
	DDX_Control(pDX, btnAddPattern, m_btnAddPattern);
	DDX_Control(pDX, btnAddRegularExpression, m_btnAddRegularExpression);
	DDX_Control(pDX, btnModifyPattern, m_btnModifyPattern);
	DDX_Control(pDX, btnModifyRegularExpression, m_btnModifyRegularExpression);
	DDX_Control(pDX, btnRemovePattern, m_btnRemovePattern);
	DDX_Control(pDX, btnRemoveRegularExpression, m_btnRemoveRegularExpression);
}

//=====================================================================

void CSettingsList::CopyList(CAdList * pSource, CAdList * pDestination)
{
	// Check if we have valid items
	if ((pSource == NULL) || (pDestination == NULL))
	{
		// Exit function
		return;
	}

	// Clear destination
	pDestination->DeleteAllItems();
		
	// Add patterns
	for (int i = 0; i < pSource->GetPatternCount(); i++)
	{
		pDestination->AddPattern(pSource->GetPattern(i));
	}

	// Add regular expressions
	for (int i = 0; i < pSource->GetRegularExpressionCount(); i++)
	{
		pDestination->AddRegularExpression(pSource->GetRegularExpression(i));
	}
}

//=====================================================================

void CSettingsList::UpdateListPatterns()
{
	// Remove all items
	m_lstPatterns.DeleteAllItems();

	// Now add all items
	for (int i = 0; i < m_pList->GetPatternCount(); i++)
	{
		m_lstPatterns.InsertItem(i, m_pList->GetPattern(i));
	}
}

//=====================================================================

void CSettingsList::UpdateListRegularExpressions()
{
	// Remove all items
	m_lstRegularExpressions.DeleteAllItems();

	// Now add all items
	for (int i = 0; i < m_pList->GetRegularExpressionCount(); i++)
	{
		m_lstRegularExpressions.InsertItem(i, m_pList->GetRegularExpression(i));
	}
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
Member
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.
 
I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source MVVM framework for WPF, Silverlight and Windows Phone 7.
 
I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.
 
To download (or buy) applications I have written, visit my website: http://www.catenalogic.com

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 17 Jan 2008
Article Copyright 2008 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid