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


//----------------------------------------------------------------------------- 
//  
// @doc 
// 
// @module	RuneBookDlg.cpp - Rune book configuration dialog | 
// 
// This module contains the definition of the rune book 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: RuneBookDlg.cpp $ 
//       
//----------------------------------------------------------------------------- 
 
#include "stdafx.h" 
#include "resource.h" 
#include "RuneBookDlg.h" 
#include "RuneBook.h" 
#include "RuneLibrary.h" 
#include "uoth.h" 
#include "Treasure.h" 
 
// 
// Externals 
// 
 
extern TCHAR g_szAppName []; 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  constructor. 
// 
// @parm CRuneLibrary * | pRuneLibrary | Rune library being edited. 
// 
// @parm CRuneBook * | pRuneBook | Rune book being edited 
//		or a NULL if a new rune book is being added. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CRuneBookDlg::CRuneBookDlg (CRuneLibrary *pRuneLibrary, CRuneBook *pRuneBook) 
{ 
	m_pRuneLibrary = pRuneLibrary; 
	m_pRuneBook = pRuneBook; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  destructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CRuneBookDlg::~CRuneBookDlg() 
{ 
} 
 
//----------------------------------------------------------------------------- 
// 
// @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 CRuneBookDlg::OnInitDialog (UINT uMsg,  
	WPARAM wParam, LPARAM lParam, BOOL &bHandled)  
{ 
 
	// 
	// Subclass the controls 
	// 
 
	m_lbAvailable = GetDlgItem (IDC_RUNE_BOOK_TAVAILABLE); 
	m_lbIn = GetDlgItem (IDC_RUNE_BOOK_TIN); 
	m_btnAdd = GetDlgItem (IDC_RUNE_BOOK_TADD); 
	m_btnRemove = GetDlgItem (IDC_RUNE_BOOK_TREMOVE); 
	m_editName = GetDlgItem (IDC_RUNE_BOOK_NAME); 
	m_editName .LimitText (32); 
 
	// 
	// Center the window 
	// 
 
	CenterWindow (); 
 
	// 
	// Populate the name 
	// 
 
	m_editName .SetWindowText (m_pRuneBook ? m_pRuneBook ->m_strName  
		: CString (MAKEINTRESOURCE (IDS_NEW_RUNE_BOOK_NAME))); 
 
	// 
	// Loop through all the treasures 
	// 
 
	for (int it = 0; it < CTreasure::gm_vTreasures .GetCount (); ++it) 
	{ 
		CTreasure *pTreasure = &CTreasure::gm_vTreasures [it]; 
		if (!pTreasure ->IsValid ()) 
			continue; 
		bool fFound = false; 
		if (m_pRuneBook) 
		{ 
            for (int i = 0; i < m_pRuneBook ->m_nRunes; i++) 
			{ 
				if (m_pRuneBook ->m_anRunes [i] == pTreasure ->m_nIndex) 
				{ 
					fFound = true; 
					break; 
				} 
			} 
		} 
		CListBox *plb = fFound ? &m_lbIn : &m_lbAvailable; 
		CString str; 
		str .Format (_T ( "%d - %s"), pTreasure ->m_nIndex,  
			(LPCSTR) pTreasure ->m_strName); 
		plb ->AddString (str); 
	} 
 
	// 
	// Set the listbox positions to the start 
	// 
 
	m_lbAvailable .SetCurSel (0); 
	m_lbIn .SetCurSel (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 CRuneBookDlg::OnOk (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
	 
	// 
	// Get the rune book name  
	// 
 
	CString strRuneBook; 
	m_editName .GetWindowText (strRuneBook); 
	 
	// 
	// Check for valid rune book name 
	// 
 
	strRuneBook .TrimRight (); 
	strRuneBook .TrimLeft (); 
	if (!IsValidName (strRuneBook)) 
	{ 
		CString str (MAKEINTRESOURCE (IDS_ERR_INVALID_RUNEBOOK_NAME)); 
		::MessageBox (m_hWnd, str, g_szAppName, MB_OK); 
		return TRUE; 
	} 
 
	// 
	// Check for duplicate 
	// 
 
	CRuneBook *pRuneBook = m_pRuneLibrary ->FindBook (strRuneBook); 
	if (pRuneBook != NULL && pRuneBook != m_pRuneBook) 
	{ 
		CString str (MAKEINTRESOURCE (IDS_ERR_DUPLICATE_RUNEBOOK_NAME)); 
		::MessageBox (m_hWnd, str, g_szAppName, MB_OK); 
		return TRUE; 
	} 
 
	// 
	// Add/Modify the rune book 
	// 
 
	if (m_pRuneBook) 
		m_pRuneBook ->m_strName = strRuneBook; 
	else 
		m_pRuneBook = m_pRuneLibrary ->AddBook (strRuneBook); 
 
	// 
	// Get the rune list 
	// 
 
	m_pRuneBook ->m_nRunes = 0; 
	for (int nSel = 0; nSel < m_lbIn .GetCount () &&  
		m_pRuneBook ->m_nRunes < CRuneBook::Max_Runes; ++nSel) 
	{ 
		CString str; 
		m_lbIn .GetText (nSel, str); 
		int nRune = atol (str); 
		m_pRuneBook ->m_anRunes [m_pRuneBook ->m_nRunes++] = nRune; 
	} 
 
	// 
	// 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 CRuneBookDlg::OnCancel (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
	 
	// 
	// End the dialog 
	// 
 
	EndDialog (IDCANCEL); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Create a new rune book 
// 
// @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 CRuneBookDlg::OnAdd (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
 
	// 
	// Get the selection and verify the is room 
	// 
 
	int nSel = m_lbAvailable .GetCurSel (); 
	if (nSel < 0 || m_lbIn .GetCount () >= CRuneBook::Max_Runes) 
		return TRUE; 
 
	// 
	// Get the text of the listbox and add to other side 
	// 
 
	CString str; 
	m_lbAvailable .GetText (nSel, str); 
	AddRune (m_lbIn, str); 
 
	// 
	// Remove the rune from me 
	// 
 
	if (nSel + 1 == m_lbAvailable .GetCount ()) 
	{ 
		if (m_lbAvailable .GetCount () > 1) 
			m_lbAvailable .SetCurSel (nSel - 1); 
	} 
	else 
		m_lbAvailable .SetCurSel (nSel + 1); 
	m_lbAvailable .DeleteString (nSel); 
 
	// 
	// Update the window 
	// 
 
	UpdateWindow (); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Create a new rune book 
// 
// @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 CRuneBookDlg::OnRemove (WORD wNotifyCode,  
	WORD wID, HWND hWndCtl, BOOL &bHandled)  
{ 
 
	// 
	// Get the selection 
	// 
 
	int nSel = m_lbIn .GetCurSel (); 
	if (nSel < 0) 
		return TRUE; 
 
	// 
	// Get the text of the listbox and add to other side 
	// 
 
	CString str; 
	m_lbIn .GetText (nSel, str); 
	AddRune (m_lbAvailable, str); 
 
	// 
	// Remove the rune from me 
	// 
 
	if (nSel + 1 == m_lbIn .GetCount ()) 
	{ 
		if (m_lbIn .GetCount () > 1) 
			m_lbIn .SetCurSel (nSel - 1); 
	} 
	else 
		m_lbIn .SetCurSel (nSel + 1); 
	m_lbIn .DeleteString (nSel); 
 
	// 
	// Update the window 
	// 
 
	UpdateWindow (); 
	return TRUE; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Update the window controls 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBookDlg::UpdateWindow () 
{ 
 
	// 
	// If we have an avialable selected 
	// 
 
	if (m_lbAvailable .GetCurSel () >= 0 && 
		m_lbIn .GetCount () < CRuneBook::Max_Runes) 
		m_btnAdd .EnableWindow (TRUE); 
	else 
		m_btnAdd .EnableWindow (FALSE); 
 
	// 
	// If we have an in selected 
	// 
 
	if (m_lbIn .GetCurSel () >= 0) 
		m_btnRemove .EnableWindow (TRUE); 
	else 
		m_btnRemove .EnableWindow (FALSE); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Add a rune to the given listbox 
// 
// @parm CListBox & | lb | Listbox in question 
// 
// @parm LPCTSTR | pszRune | Rune to be added 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneBookDlg::AddRune (CListBox &lb, LPCTSTR pszRune) 
{ 
 
	// 
	// Get the rune number 
	// 
 
	int nRune = atol (pszRune); 
 
	// 
	// 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 nRuneTest = atol (str); 
		if (nRuneTest > nRune) 
			break; 
	} 
 
	// 
	// Insert here 
	// 
 
	lb .InsertString (nSel, pszRune); 
	lb .SetCurSel (nSel); 
	return; 
}