www.pudn.com > uoth_src.zip > FilterDlg.cpp


//----------------------------------------------------------------------------- 
//  
// @doc 
// 
// @module	FilterDlg.cpp - Filter configuration dialog | 
// 
// This module contains the definition of the filter configuration dialog. 
// 
// Copyright (c) 2002 - Descartes Systems Sciences, Inc. 
// 
// All rights reserved. 
// 
// Redistribution and use in source and binary forms, with or without  
// modification, are permitted provided that the following conditions are  
// met: 
//  
// 1. Redistributions of source code must retain the above copyright notice,  
//    this list of conditions and the following disclaimer.  
// 2. Neither the name of Descartes Systems Sciences, Inc nor the names of  
//    its contributors may be used to endorse or promote products derived  
//    from this software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR  
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING  
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS  
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
// 
// @end 
// 
// $History: FilterDlg.cpp $ 
//       
//----------------------------------------------------------------------------- 
 
#include "stdafx.h" 
#include "resource.h" 
#include "FilterDlg.h" 
#include "Filter.h" 
#include "Level.h" 
#include "uoth.h" 
 
// 
// Externals 
// 
 
extern TCHAR g_szAppName []; 
 
// 
// Local structure 
// 
 
#pragma pack(push, 1) 
struct DLGTEMPLATEEX 
{ 
	WORD dlgVer; 
	WORD signature; 
	DWORD helpID; 
	DWORD exStyle; 
	DWORD style; 
	WORD cDlgItems; 
	short x; 
	short y; 
	short cx; 
	short cy; 
 
	// Everything else in this structure is variable length, 
	// and therefore must be determined dynamically 
 
	// sz_Or_Ord menu;			// name or ordinal of a menu resource 
	// sz_Or_Ord windowClass;	// name or ordinal of a window class 
	// WCHAR title[titleLen];	// title string of the dialog box 
	// short pointsize;			// only if DS_SETFONT is set 
	// short weight;			// only if DS_SETFONT is set 
	// short bItalic;			// only if DS_SETFONT is set 
	// WCHAR font[fontLen];		// typeface name, if DS_SETFONT is set 
}; 
#pragma pack(pop) 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  constructor. 
// 
// @parm CFilter * | pFilter | Filter being edited 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CFilterDlg::CFilterDlg (CFilter *pFilter) 
{ 
	m_pFilter = pFilter; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  destructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CFilterDlg::~CFilterDlg() 
{ 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Initialize the dialog 
// 
// @parm UINT | uMsg | Message 
// 
// @parm WPARAM | wParam | Message w-parameter 
// 
// @parm LPARAM | lParam | Message l-parameter 
// 
// @parm BOOL & | bHandled | If handled, set to true 
// 
// @rdesc Routine results 
// 
//----------------------------------------------------------------------------- 
 
LRESULT CFilterDlg::OnInitDialog (UINT uMsg,  
	WPARAM wParam, LPARAM lParam, BOOL &bHandled)  
{ 
 
	// 
	// Subclass the controls 
	// 
 
	m_lbShow = GetDlgItem (IDC_FILTER_SHOW); 
	m_lbDontShow = GetDlgItem (IDC_FILTER_DONT_SHOW); 
	m_btnAdd = GetDlgItem (IDC_FILTER_ADD); 
	m_btnRemove = GetDlgItem (IDC_FILTER_REMOVE); 
	m_btnEnableLevel = GetDlgItem (IDC_FILTER_LEVEL_ENABLE); 
	m_btnEnableTack = GetDlgItem (IDC_FILTER_TACK); 
	m_btnEnableMine = GetDlgItem (IDC_FILTER_MINE); 
 
	// 
	// Center the window 
	// 
 
	CenterWindow (); 
 
	// 
	// Loop through all the levels 
	// 
	// FYI: This code also resets the rune list if there are 
	// invalid entries. 
	// 
 
	for (int il = 0; il < CLevel::gm_vLevels .GetCount (); ++il) 
	{ 
		CLevel *pLevel = &CLevel::gm_vLevels [il]; 
		if (!pLevel ->IsValid ()) 
			continue; 
		bool fFound = m_pFilter ->m_afLevels [pLevel ->m_nIndex - 1]; 
		CListBox *plb = fFound ? &m_lbShow : &m_lbDontShow; 
		CString str; 
		str .Format (_T ( "%d - %s"), pLevel ->m_nIndex,  
			(LPCSTR) pLevel ->m_strName); 
		plb ->AddString (str); 
	} 
 
	// 
	// Set the listbox positions to the start 
	// 
 
	m_lbShow .SetCurSel (0); 
	m_lbDontShow .SetCurSel (0); 
 
	// 
	// Update the bottons 
	// 
 
	m_btnEnableLevel .SetCheck (m_pFilter ->m_fEnableLevelFilter ? 1 : 0); 
	m_btnEnableTack .SetCheck (m_pFilter ->m_fEnableTackFilter ? 1 : 0); 
	m_btnEnableMine .SetCheck (m_pFilter ->m_fEnableMineFilter ? 1 : 0); 
 
	// 
	// Update the windows 
	// 
 
	UpdateWindow (); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Handle the user press Ok 
// 
// @parm WORD | wNotifyCode | Command notification code 
//  
// @parm WORD | wID | ID of the control 
// 
// @parm HWND | hWndCtl | Handle of the control 
// 
// @parm BOOL & | bHandled | If handled, set to true 
// 
// @rdesc Routine results 
// 
//----------------------------------------------------------------------------- 
 
LRESULT CFilterDlg::OnOk (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
	 
	// 
	// Get the current settings 
	// 
 
	GetSettings (); 
	*m_pFilter = m_sFilter; 
 
	// 
	// End the dialog 
	// 
 
	EndDialog (IDOK); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Handle the user press Ok 
// 
// @parm WORD | wNotifyCode | Command notification code 
//  
// @parm WORD | wID | ID of the control 
// 
// @parm HWND | hWndCtl | Handle of the control 
// 
// @parm BOOL & | bHandled | If handled, set to true 
// 
// @rdesc Routine results 
// 
//----------------------------------------------------------------------------- 
 
LRESULT CFilterDlg::OnCancel (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
	 
	// 
	// End the dialog 
	// 
 
	EndDialog (IDCANCEL); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Move a level from the don't show to the show side 
// 
// @parm WORD | wNotifyCode | Command notification code 
//  
// @parm WORD | wID | ID of the control 
// 
// @parm HWND | hWndCtl | Handle of the control 
// 
// @parm BOOL & | bHandled | If handled, set to true 
// 
// @rdesc Routine results 
// 
//----------------------------------------------------------------------------- 
 
LRESULT CFilterDlg::OnAdd (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
 
	// 
	// Get the selection 
	// 
 
	int nSel = m_lbDontShow .GetCurSel (); 
	if (nSel < 0 ) 
		return TRUE; 
 
	// 
	// Get the text of the listbox and add to other side 
	// 
 
	CString str; 
	m_lbDontShow .GetText (nSel, str); 
	AddLevel (m_lbShow, str); 
 
	// 
	// Remove the rune from me 
	// 
 
	if (nSel + 1 == m_lbDontShow .GetCount ()) 
	{ 
		if (m_lbDontShow .GetCount () > 1) 
			m_lbDontShow .SetCurSel (nSel - 1); 
	} 
	else 
		m_lbDontShow .SetCurSel (nSel + 1); 
	m_lbDontShow .DeleteString (nSel); 
 
	// 
	// Update the window 
	// 
 
	UpdateWindow (); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Move a level from the show to the don't show size 
// 
// @parm WORD | wNotifyCode | Command notification code 
//  
// @parm WORD | wID | ID of the control 
// 
// @parm HWND | hWndCtl | Handle of the control 
// 
// @parm BOOL & | bHandled | If handled, set to true 
// 
// @rdesc Routine results 
// 
//----------------------------------------------------------------------------- 
 
LRESULT CFilterDlg::OnRemove (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
 
	// 
	// Get the selection 
	// 
 
	int nSel = m_lbShow .GetCurSel (); 
	if (nSel < 0) 
		return TRUE; 
 
	// 
	// Get the text of the listbox and add to other side 
	// 
 
	CString str; 
	m_lbShow .GetText (nSel, str); 
	AddLevel (m_lbDontShow, str); 
 
	// 
	// Remove the rune from me 
	// 
 
	if (nSel + 1 == m_lbShow .GetCount ()) 
	{ 
		if (m_lbShow .GetCount () > 1) 
			m_lbShow .SetCurSel (nSel - 1); 
	} 
	else 
		m_lbShow .SetCurSel (nSel + 1); 
	m_lbShow .DeleteString (nSel); 
 
	// 
	// Update the window 
	// 
 
	UpdateWindow (); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Update the window controls 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CFilterDlg::UpdateWindow () 
{ 
 
	// 
	// Update the settings 
	// 
 
	GetSettings (); 
 
	// 
	// If we aren't enabled for level filter 
	// 
 
	if (m_btnEnableLevel .GetCheck () == 0) 
	{ 
		 
		// 
		// Disable them all 
		// 
 
		m_btnAdd .EnableWindow (FALSE); 
		m_btnRemove .EnableWindow (FALSE); 
		m_lbShow .EnableWindow (FALSE); 
		m_lbDontShow .EnableWindow (FALSE); 
	} 
 
	// 
	// Otherwise 
	// 
	 
	else 
	{ 
 
		// 
		// Enable the list boxes 
		// 
 
		m_lbShow .EnableWindow (TRUE); 
		m_lbDontShow .EnableWindow (TRUE); 
 
		// 
		// If we have an avialable selected 
		// 
 
		if (m_lbDontShow .GetCurSel () >= 0) 
			m_btnAdd .EnableWindow (TRUE); 
		else 
			m_btnAdd .EnableWindow (FALSE); 
 
		// 
		// If we have an in selected 
		// 
 
		if (m_lbShow .GetCurSel () >= 0) 
			m_btnRemove .EnableWindow (TRUE); 
		else 
			m_btnRemove .EnableWindow (FALSE); 
	} 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Get the current settings 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CFilterDlg::GetSettings ()  
{ 
	 
	// 
	// Get the level list 
	// 
 
	for (int i = 0; i < Max_Level; i++) 
		m_sFilter .m_afLevels [i] = false; 
	for (int nSel = 0; nSel < m_lbShow .GetCount () &&  
		nSel < Max_Level; ++nSel) 
	{ 
		CString str; 
		m_lbShow .GetText (nSel, str); 
		int nLevel = atol (str); 
		m_sFilter .m_afLevels [nLevel - 1] = true; 
	} 
 
	// 
	// Get the buttons 
	// 
 
	m_sFilter .m_fEnableLevelFilter = m_btnEnableLevel .GetCheck () != 0; 
	m_sFilter .m_fEnableTackFilter = m_btnEnableTack .GetCheck () != 0; 
	m_sFilter .m_fEnableMineFilter = m_btnEnableMine .GetCheck () != 0; 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Add a level to the given listbox 
// 
// @parm CListBox & | lb | Listbox in question 
// 
// @parm LPCTSTR | pszLevel | Level to be added 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CFilterDlg::AddLevel (CListBox &lb, LPCTSTR pszLevel) 
{ 
 
	// 
	// Get the level number 
	// 
 
	int nLevel = atol (pszLevel); 
 
	// 
	// Ok, a binary search would be really cool here, but screw it 
	// we are talking UI here and not high speed processing 
	// 
 
	int nSel; 
	for (nSel = 0; nSel < lb .GetCount (); nSel++) 
	{ 
		CString str; 
		lb .GetText (nSel, str); 
		int nLevelTest = atol (str); 
		if (nLevelTest > nLevel) 
			break; 
	} 
 
	// 
	// Insert here 
	// 
 
	lb .InsertString (nSel, pszLevel); 
	lb .SetCurSel (nSel); 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Create a modeless dialog 
// 
// @parm HWND | hWndParent | Parent window 
// 
// @parm LPARAM | dwInitParam | Initialization param 
// 
// @rdesc Window handle 
// 
//----------------------------------------------------------------------------- 
 
HWND CFilterDlg::Create (HWND hWndParent, LPARAM dwInitParam) 
{ 
 
	// 
	// Find the region 
	// 
 
	HRSRC hRsrc = FindResource (_Module .GetResourceInstance (), 
        MAKEINTRESOURCE (IDD), RT_DIALOG); 
	if (hRsrc == NULL) 
		return NULL; 
 
	// 
	// Get the size of the resource 
	// 
 
	DWORD dwSize = ::SizeofResource (_Module .GetResourceInstance (), hRsrc); 
 
	// 
	// Allocate the global memory to contain the regions 
	// 
 
	HGLOBAL hTemplate = ::GlobalAlloc (GPTR, dwSize); 
	if (hTemplate == NULL) 
		return NULL; 
	DLGTEMPLATE *pTemplate = (DLGTEMPLATE *) ::GlobalLock (hTemplate); 
	DLGTEMPLATEEX *pTemplateEx = (DLGTEMPLATEEX *) pTemplate; 
 
	// 
	// Load and lock the resource 
	// 
 
	HGLOBAL hSource = ::LoadResource (_Module .GetResourceInstance (), hRsrc); 
	LPVOID pSource = ::LockResource (hSource); 
	memcpy (pTemplate, pSource, dwSize); 
	UnlockResource (hSource); 
	::FreeResource (hSource); 
 
	// 
	// Adjust the flags  
	// 
 
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_DLGFRAME | 
		DS_3DLOOK | DS_FIXEDSYS | DS_SETFONT | DS_CONTROL; 
	DWORD dwExStyle = 0; 
	if (pTemplateEx ->signature == 0xFFFF) 
	{ 
		pTemplateEx ->exStyle = dwExStyle; 
		pTemplateEx ->style = dwStyle; 
	} 
	else 
	{ 
		pTemplate ->dwExtendedStyle = dwExStyle; 
		pTemplate ->style = dwStyle; 
	} 
 
	// 
	// Create the window 
	// 
 
	ATLASSERT (m_hWnd == NULL); 
	_AtlWinModule .AddCreateWndData (&m_thunk.cd,  
		(CDialogImplBaseT  *) this); 
#ifdef _DEBUG 
	m_bModal = false; 
#endif //_DEBUG 
	HWND hWnd = ::CreateDialogIndirectParam ( 
		_AtlBaseModule.GetResourceInstance(),  
		pTemplate, hWndParent, StartDialogProc, 
		dwInitParam); 
	ATLASSERT (m_hWnd == hWnd); 
 
	// 
	// If we created the window, delete OK and CANCEL :) 
	// 
 
	if (m_hWnd) 
	{ 
		::DestroyWindow (GetDlgItem (IDOK)); 
		::DestroyWindow (GetDlgItem (IDCANCEL)); 
	} 
 
	// 
	// Unlock the globals 
	// 
 
	::GlobalUnlock (hTemplate); 
	::GlobalFree (hTemplate); 
	return hWnd; 
}