Click here to Skip to main content
15,891,033 members
Articles / Web Development / HTML

SurfHelper: A popup window killer and history cleaner

Rate me:
Please Sign up or sign in to vote.
4.77/5 (13 votes)
15 Nov 2001 190.3K   3.7K   46  
A free tool to remove popup windows, clear history, control window properties of IE, and more.
// PopupTitleDlg.h : interface of the CPopupTitleDlg class
//
/////////////////////////////////////////////////////////////////////////////

#ifndef __PopupTitleDlg_h__
#define __PopupTitleDlg_h__

class CPopupTitleDlg : public CDialogImpl<CPopupTitleDlg>
{
public:
	enum { IDD = IDD_POPUP_TITLE };

	CDlgItemResizer m_Resizer;
	CListViewCtrl	m_listBlackList;
	CListViewCtrl	m_listExclusion;

	BEGIN_MSG_MAP(CPopupTitleDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_CONTEXTMENU, OnContextMenu)
		NOTIFY_HANDLER(IDC_BLACKLIST, LVN_ITEMCHANGED, OnBlacklistChanged)
		NOTIFY_HANDLER(IDC_EXCLUSION, LVN_ITEMCHANGED, OnExclusionChanged)
		COMMAND_ID_HANDLER(ID_BLACKLIST_ADD, OnBlacklistAdd)
		COMMAND_ID_HANDLER(ID_BLACKLIST_EDIT, OnBlacklistEdit)
		COMMAND_ID_HANDLER(ID_BLACKLIST_REMOVE, OnBlacklistRemove)
		COMMAND_ID_HANDLER(ID_BLACKLIST_EXCLUDE, OnBlacklistExclude)
		COMMAND_ID_HANDLER(ID_EXCLUSION_ADD, OnExclusionAdd)
		COMMAND_ID_HANDLER(ID_EXCLUSION_EDIT, OnExclusionEdit)
		COMMAND_ID_HANDLER(ID_EXCLUSION_REMOVE, OnExclusionRemove)
		COMMAND_ID_HANDLER(ID_EXCLUSION_INCLUDE, OnExclusionInclude)
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		//add controls to resize manager
		m_Resizer.Add(m_hWnd, IDC_BLACKLIST,
			RESIZE_LOCKLEFT|RESIZE_LOCKRIGHT|RESIZE_LOCKTOP|RESIZE_LOCKBOTTOM);
		m_Resizer.Add(m_hWnd, IDC_EXCLUSION, 
			RESIZE_LOCKLEFT|RESIZE_LOCKRIGHT|RESIZE_LOCKBOTTOM);
		m_Resizer.Add(m_hWnd, IDC_LABEL_EXCLUSION, RESIZE_LOCKBOTTOM);
		
		//set grid style for list control
		m_listBlackList.Attach(GetDlgItem(IDC_BLACKLIST));
		m_listExclusion.Attach(GetDlgItem(IDC_EXCLUSION));

		DWORD dwStyle;
		
		dwStyle = m_listBlackList.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
		dwStyle |= LVS_EX_CHECKBOXES | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP;
		m_listBlackList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);

		dwStyle = m_listExclusion.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
		dwStyle |= LVS_EX_CHECKBOXES | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP;
		m_listExclusion.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);

		// set column header
		CString sHeaderTitle;
		LV_COLUMN lvc;
		lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

		sHeaderTitle.LoadString(IDS_POPUP_TITLE);
		lvc.iSubItem = 0;
		lvc.pszText = sHeaderTitle.LockBuffer();
		lvc.cx = 80;
		lvc.fmt = LVCFMT_LEFT;
		m_listBlackList.InsertColumn(0, &lvc);
		m_listExclusion.InsertColumn(0, &lvc);
		sHeaderTitle.UnlockBuffer();

		FillFilterList();

		return TRUE;
	}

	void FillFilterList()
	{
		MAP_FILTER::iterator theFilter;
		LV_ITEM	lvi;
		lvi.iItem = 0;
		lvi.iSubItem = 0;
		lvi.mask = LVIF_TEXT;

		m_listBlackList.DeleteAllItems();
		for (theFilter=g_Global.m_mapTitleBlacklist.begin(); 
			theFilter!=g_Global.m_mapTitleBlacklist.end(); theFilter++)
		{
			lvi.pszText = (LPTSTR)(LPCTSTR)theFilter->first;
			int nIndex = m_listBlackList.InsertItem(&lvi);
			ListView_SetItemState(m_listBlackList.m_hWnd, nIndex, 
				INDEXTOSTATEIMAGEMASK((int(theFilter->second) + 1)), 
				LVIS_STATEIMAGEMASK);
		}

		m_listExclusion.DeleteAllItems();
		for (theFilter=g_Global.m_mapTitleExclusion.begin(); 
			theFilter!=g_Global.m_mapTitleExclusion.end(); theFilter++)
		{
			lvi.pszText = (LPTSTR)(LPCTSTR)theFilter->first;
			int nIndex = m_listExclusion.InsertItem(&lvi);
			ListView_SetItemState(m_listExclusion.m_hWnd, nIndex, 
				INDEXTOSTATEIMAGEMASK((int(theFilter->second) + 1)), 
				LVIS_STATEIMAGEMASK);
		}
	}

	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		m_Resizer.Resize(m_hWnd);

		CRect rect;
		int nNewWidth;

		m_listBlackList.GetWindowRect(&rect);
		nNewWidth = rect.Width() - g_Global.m_nVScroolWidth - 
			g_Global.m_nBorderWidth*2;
		m_listBlackList.SetColumnWidth(0, nNewWidth);
		m_listExclusion.SetColumnWidth(0, nNewWidth);
		
		return 0;
	}

	LRESULT OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CMenu menu;
		CListViewCtrl* pListView;
		CPoint point;
		CRect rectBlacklist;
		CRect rectExclusion;

		point.x = GET_X_LPARAM(lParam);
		point.y = GET_Y_LPARAM(lParam);

		m_listBlackList.GetWindowRect(&rectBlacklist);
		m_listExclusion.GetWindowRect(&rectExclusion);

		if (rectBlacklist.PtInRect(point))
		{
			menu.LoadMenu(IDR_POPUP_BLACKLIST);
			pListView = &m_listBlackList;
		}
		else if (rectExclusion.PtInRect(point))
		{
			menu.LoadMenu(IDR_POPUP_EXCLUSION);
			pListView = &m_listExclusion;
		}
		else
		{
			bHandled = FALSE;
			return 0;
		}

		int nIndex = pListView->GetNextItem(-1,LVNI_SELECTED);
		CMenuHandle menuPopup = menu.GetSubMenu(0);

		if (nIndex < 0)
		{
			::EnableMenuItem(menuPopup, 1, MF_BYPOSITION | MF_GRAYED);
			::EnableMenuItem(menuPopup, 2, MF_BYPOSITION | MF_GRAYED);
			::EnableMenuItem(menuPopup, 3, MF_BYPOSITION | MF_GRAYED);
		}

		::TrackPopupMenu(menuPopup,
			TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_VERTICAL,
			point.x, point.y, 0, m_hWnd, NULL);
		return 0;
	}

	LRESULT OnBlacklistChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
	{
		NMLISTVIEW* pnmv = (LPNMLISTVIEW)pnmh;

		if (pnmv->uOldState == 0 && pnmv->uNewState == 0)
			return 0;	// No change

		// Old check box state
		BOOL bPrevState = (BOOL)(((pnmv->uOldState & LVIS_STATEIMAGEMASK)>>12)-1);
		if (bPrevState < 0)	// On startup there's no previous state 
			bPrevState = 0; // so assign as false (unchecked)

		// New check box state
		BOOL bChecked = (BOOL)(((pnmv->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);   
		if (bChecked < 0) // On non-checkbox notifications assume false
			bChecked = 0;

		if (bPrevState == bChecked) // No change in check box
			return 0;

		// Get item text
		CString sFilter;
		LPTSTR pFilter;
		pFilter = sFilter.GetBuffer(MAX_PATH);
		m_listBlackList.GetItemText(pnmv->iItem, 0, pFilter, MAX_PATH);
		sFilter.ReleaseBuffer();

		// Change filter state
		g_Global.ChangeFilter(&g_Global.m_mapTitleBlacklist,
			&g_Global.m_lockTitleBlacklist, sFilter, bChecked);
		return 0;
	}

	LRESULT OnExclusionChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
	{
		NMLISTVIEW* pnmv = (LPNMLISTVIEW)pnmh;

		if (pnmv->uOldState == 0 && pnmv->uNewState == 0)
			return 0;	// No change

		// Old check box state
		BOOL bPrevState = (BOOL)(((pnmv->uOldState & LVIS_STATEIMAGEMASK)>>12)-1);
		if (bPrevState < 0)	// On startup there's no previous state 
			bPrevState = 0; // so assign as false (unchecked)

		// New check box state
		BOOL bChecked = (BOOL)(((pnmv->uNewState & LVIS_STATEIMAGEMASK)>>12)-1);   
		if (bChecked < 0) // On non-checkbox notifications assume false
			bChecked = 0;

		if (bPrevState == bChecked) // No change in check box
			return 0;

		// Get item text
		CString sFilter;
		LPTSTR pFilter;
		pFilter = sFilter.GetBuffer(MAX_PATH);
		m_listExclusion.GetItemText(pnmv->iItem, 0, pFilter, MAX_PATH);
		sFilter.ReleaseBuffer();

		// Change filter state
		g_Global.ChangeFilter(&g_Global.m_mapTitleExclusion,
			&g_Global.m_lockTitleExclusion, sFilter, bChecked);
		return 0;
	}

	LRESULT OnBlacklistAdd(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CCommonInputDlg dlg;
		dlg.m_sTitle.LoadString(IDS_BLACKLIST_ADD);
		dlg.m_sLabel.LoadString(IDS_LABEL_TITLE);
		if (dlg.DoModal() == IDOK)
		{
			BlacklistAdd(dlg.m_sText);
		}
		return 0;
	}

	LRESULT OnBlacklistEdit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CCommonInputDlg dlg;
		CString sFilter;
		LPTSTR pFilter;
		int nIndex = m_listBlackList.GetNextItem(-1,LVNI_SELECTED);
		pFilter = sFilter.GetBuffer(MAX_PATH);
		m_listBlackList.GetItemText(nIndex, 0, pFilter, MAX_PATH);
		sFilter.ReleaseBuffer();

		dlg.m_sTitle.LoadString(IDS_BLACKLIST_EDIT);
		dlg.m_sLabel.LoadString(IDS_LABEL_TITLE);
		dlg.m_sText = sFilter;
		if (dlg.DoModal() == IDOK)
		{
			BOOL bEnable = ListView_GetCheckState(m_listBlackList.m_hWnd, nIndex);
			if (! g_Global.AddFilter(&g_Global.m_mapTitleBlacklist,
				&g_Global.m_lockTitleBlacklist, dlg.m_sText, bEnable))
			{
				CString sErrorMsg;
				sErrorMsg.LoadString(IDS_FILTER_EXIST);
				MessageBox(sErrorMsg, NULL, MB_OK | MB_ICONERROR);
			}
			else
			{
				g_Global.RemoveFilter(&g_Global.m_mapTitleBlacklist,
					&g_Global.m_lockTitleBlacklist, sFilter);
				m_listBlackList.SetItemText(nIndex, 0, dlg.m_sText);
			}
		}
		return 0;
	}

	LRESULT OnBlacklistRemove(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CString sMsg;
		sMsg.LoadString(IDS_CONFIRM_DELETE_FILTER);
		if (MessageBox(sMsg, APP_NAME, MB_YESNO | MB_ICONQUESTION) == IDYES)
		{
			CString sFilter;
			LPTSTR pFilter;
			int nIndex = m_listBlackList.GetNextItem(-1,LVNI_SELECTED);
			pFilter = sFilter.GetBuffer(MAX_PATH);
			m_listBlackList.GetItemText(nIndex, 0, pFilter, MAX_PATH);
			sFilter.ReleaseBuffer();

			g_Global.RemoveFilter(&g_Global.m_mapTitleBlacklist,
				&g_Global.m_lockTitleBlacklist, sFilter);
			m_listBlackList.DeleteItem(nIndex);
		}
		return 0;
	}

	LRESULT OnBlacklistExclude(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CString sFilter;
		LPTSTR pFilter;
		int nIndex = m_listBlackList.GetNextItem(-1,LVNI_SELECTED);
		pFilter = sFilter.GetBuffer(MAX_PATH);
		m_listBlackList.GetItemText(nIndex, 0, pFilter, MAX_PATH);
		sFilter.ReleaseBuffer();

		Exclude(sFilter);
		return 0;
	}

	LRESULT OnExclusionAdd(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CCommonInputDlg dlg;
		dlg.m_sTitle.LoadString(IDS_EXCLUSION_ADD);
		dlg.m_sLabel.LoadString(IDS_LABEL_TITLE);
		if (dlg.DoModal() == IDOK)
		{
			ExclusionAdd(dlg.m_sText);
		}
		return 0;
	}

	LRESULT OnExclusionEdit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CCommonInputDlg dlg;
		CString sFilter;
		LPTSTR pFilter;
		int nIndex = m_listExclusion.GetNextItem(-1,LVNI_SELECTED);
		pFilter = sFilter.GetBuffer(MAX_PATH);
		m_listExclusion.GetItemText(nIndex, 0, pFilter, MAX_PATH);
		sFilter.ReleaseBuffer();

		dlg.m_sTitle.LoadString(IDS_EXCLUSION_EDIT);
		dlg.m_sLabel.LoadString(IDS_LABEL_TITLE);
		dlg.m_sText = sFilter;
		if (dlg.DoModal() == IDOK)
		{
			BOOL bEnable = ListView_GetCheckState(m_listExclusion.m_hWnd, nIndex);
			if (! g_Global.AddFilter(&g_Global.m_mapTitleExclusion,
				&g_Global.m_lockTitleExclusion, dlg.m_sText, bEnable))
			{
				CString sErrorMsg;
				sErrorMsg.LoadString(IDS_FILTER_EXIST);
				MessageBox(sErrorMsg, NULL, MB_OK | MB_ICONERROR);
			}
			else
			{
				g_Global.RemoveFilter(&g_Global.m_mapTitleExclusion,
					&g_Global.m_lockTitleExclusion, sFilter);
				m_listExclusion.SetItemText(nIndex, 0, dlg.m_sText);
			}
		}
		return 0;
	}

	LRESULT OnExclusionRemove(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CString sMsg;
		sMsg.LoadString(IDS_CONFIRM_DELETE_FILTER);
		if (MessageBox(sMsg, APP_NAME, MB_YESNO | MB_ICONQUESTION) == IDYES)
		{
			CString sFilter;
			LPTSTR pFilter;
			int nIndex = m_listExclusion.GetNextItem(-1,LVNI_SELECTED);
			pFilter = sFilter.GetBuffer(MAX_PATH);
			m_listExclusion.GetItemText(nIndex, 0, pFilter, MAX_PATH);
			sFilter.ReleaseBuffer();

			g_Global.RemoveFilter(&g_Global.m_mapTitleExclusion,
				&g_Global.m_lockTitleExclusion, sFilter);
			m_listExclusion.DeleteItem(nIndex);
		}
		return 0;
	}

	LRESULT OnExclusionInclude(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CString sFilter;
		LPTSTR pFilter;
		int nIndex = m_listExclusion.GetNextItem(-1,LVNI_SELECTED);
		pFilter = sFilter.GetBuffer(MAX_PATH);
		m_listExclusion.GetItemText(nIndex, 0, pFilter, MAX_PATH);
		sFilter.ReleaseBuffer();

		Include(sFilter);
		return 0;
	}

	void BlacklistAdd(CString& sFilter, BOOL bCheck = TRUE)
	{
		if (! g_Global.AddFilter(&g_Global.m_mapTitleBlacklist,
			&g_Global.m_lockTitleBlacklist, sFilter, TRUE))
		{
			CString sErrorMsg;
			sErrorMsg.LoadString(IDS_FILTER_EXIST);
			MessageBox(sErrorMsg, NULL, MB_OK | MB_ICONERROR);
		}
		else
		{
			LV_ITEM	lvi;
			lvi.iItem = 0;
			lvi.iSubItem = 0;
			lvi.mask = LVIF_TEXT;
			lvi.pszText = sFilter.LockBuffer();
			int nIndex = m_listBlackList.InsertItem(&lvi);
			sFilter.UnlockBuffer();

			ListView_SetItemState(m_listBlackList.m_hWnd, nIndex, 
				INDEXTOSTATEIMAGEMASK((int(bCheck) + 1)), LVIS_STATEIMAGEMASK);
		}
	}

	void ExclusionAdd(CString& sFilter, BOOL bCheck = TRUE)
	{
		if (! g_Global.AddFilter(&g_Global.m_mapTitleExclusion,
			&g_Global.m_lockTitleExclusion, sFilter, TRUE))
		{
			CString sErrorMsg;
			sErrorMsg.LoadString(IDS_FILTER_EXIST);
			MessageBox(sErrorMsg, NULL, MB_OK | MB_ICONERROR);
		}
		else
		{
			LV_ITEM	lvi;
			lvi.iItem = 0;
			lvi.iSubItem = 0;
			lvi.mask = LVIF_TEXT;
			lvi.pszText = sFilter.LockBuffer();
			int nIndex = m_listExclusion.InsertItem(&lvi);
			sFilter.UnlockBuffer();

			ListView_SetItemState(m_listExclusion.m_hWnd, nIndex, 
				INDEXTOSTATEIMAGEMASK((int(bCheck) + 1)), LVIS_STATEIMAGEMASK);
		}
	}

	void Exclude(CString& sFilter)
	{
		LVFINDINFO lvfi;
		lvfi.flags = LVFI_STRING;
		lvfi.psz = sFilter.LockBuffer();
		int nIndex = m_listBlackList.FindItem(&lvfi, -1);

		g_Global.RemoveFilter(&g_Global.m_mapTitleBlacklist,
			&g_Global.m_lockTitleBlacklist, sFilter);
		m_listBlackList.DeleteItem(nIndex);

		if (g_Global.AddFilter(&g_Global.m_mapTitleExclusion,
			&g_Global.m_lockTitleExclusion, sFilter, TRUE))
		{
			LV_ITEM	lvi;
			lvi.iItem = 0;
			lvi.iSubItem = 0;
			lvi.mask = LVIF_TEXT;
			lvi.pszText = sFilter.LockBuffer();
			nIndex = m_listExclusion.InsertItem(&lvi);
			sFilter.UnlockBuffer();

			ListView_SetItemState(m_listExclusion.m_hWnd, nIndex, 
				INDEXTOSTATEIMAGEMASK(2), LVIS_STATEIMAGEMASK);
		}
	}

	void Include(CString& sFilter)
	{
		LVFINDINFO lvfi;
		lvfi.flags = LVFI_STRING;
		lvfi.psz = sFilter.LockBuffer();
		int nIndex = m_listExclusion.FindItem(&lvfi, -1);

		g_Global.RemoveFilter(&g_Global.m_mapTitleExclusion,
			&g_Global.m_lockTitleExclusion, sFilter);
		m_listExclusion.DeleteItem(nIndex);

		if (g_Global.AddFilter(&g_Global.m_mapTitleBlacklist,
			&g_Global.m_lockTitleBlacklist, sFilter, TRUE))
		{
			LV_ITEM	lvi;
			lvi.iItem = 0;
			lvi.iSubItem = 0;
			lvi.mask = LVIF_TEXT;
			lvi.pszText = sFilter.LockBuffer();
			nIndex = m_listBlackList.InsertItem(&lvi);
			sFilter.UnlockBuffer();

			ListView_SetItemState(m_listBlackList.m_hWnd, nIndex, 
				INDEXTOSTATEIMAGEMASK(2), LVIS_STATEIMAGEMASK);
		}
	}
};


#endif //__PopupTitleDlg_h__

By viewing downloads associated with this article you agree to the Terms of Service 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions