//this file is part of eMule
//Copyright (C)2002-2007 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
#include "emule.h"
#include "PreferencesDlg.h"
#include "opcodes.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(CPreferencesDlg, PREF_PARENT_CLASS)

BEGIN_MESSAGE_MAP(CPreferencesDlg, PREF_PARENT_CLASS)
	ON_WM_DESTROY()
	ON_WM_HELPINFO()
	ON_MESSAGE(WM_SBN_SELCHANGED, OnSlideBarSelChanged) //>>> WiZaRd::SlideBar [eMule+]
END_MESSAGE_MAP()

CPreferencesDlg::CPreferencesDlg()
{
	m_psh.dwFlags &= ~PSH_HASHELP;
	m_wndGeneral.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndDisplay.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndConnection.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndServer.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndDirectories.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndFiles.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndStats.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndIRC.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndWebServer.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndTweaks.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndSecurity.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndScheduler.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndProxy.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndMessages.m_psp.dwFlags &= ~PSH_HASHELP;
	m_wndTweaksMod1.m_psp.dwFlags &= ~PSH_HASHELP; //>> pP: PrefsPage
	m_wndTweaksMod2.m_psp.dwFlags &= ~PSH_HASHELP; //>> pP: PrefsPage_2
	m_wndTweaksMod3.m_psp.dwFlags &= ~PSH_HASHELP; //>> pP: PrefsPage_3
#if defined(_DEBUG) || defined(USE_DEBUG_DEVICE)
	m_wndDebug.m_psp.dwFlags &= ~PSH_HASHELP;
#endif

//>>> WiZaRd::SlideBar [eMule+]	
	EnableStackedTabs(FALSE);
//<<< WiZaRd::SlideBar [eMule+]

	//basics:
	AddPage(&m_wndGeneral);
	AddPage(&m_wndDisplay);
	AddPage(&m_wndConnection);
	AddPage(&m_wndServer);
	AddPage(&m_wndDirectories);
	AddPage(&m_wndFiles);

	//advanced:
	AddPage(&m_wndProxy);
	AddPage(&m_wndNotify);
	AddPage(&m_wndStats);
	AddPage(&m_wndIRC);
	AddPage(&m_wndMessages);
	AddPage(&m_wndSecurity);
	AddPage(&m_wndScheduler);
	AddPage(&m_wndWebServer);
	AddPage(&m_wndTweaks);
#if defined(_DEBUG) || defined(USE_DEBUG_DEVICE)
	AddPage(&m_wndDebug);
#endif

	//mod:
	AddPage(&m_wndTweaksMod1); //>> pP: PrefsPage
	AddPage(&m_wndTweaksMod2); //>> pP: PrefsPage_2
	AddPage(&m_wndTweaksMod3); //>> pP: PrefsPage_3

	StartPageUpdates = 0;
//<<< shadow2004::Tabbed Prefs [TPT] reworked


//>>> shadow2004::Tabbed Prefs [TPT] reworked
	m_nActiveWnd = 0;
//<<< shadow2004::Tabbed Prefs [TPT] reworked
	m_bSaveIniFile = false;
}

CPreferencesDlg::~CPreferencesDlg()
{
	m_ImageList.DeleteImageList(); //>>> WiZaRd::SlideBar [eMule+]
}

void CPreferencesDlg::OnDestroy()
{
	PREF_PARENT_CLASS::OnDestroy();
	if (m_bSaveIniFile)
	{
		thePrefs.Save();
		m_bSaveIniFile = false;
	}
//>>> shadow2004::Tabbed Prefs [TPT] reworked
	m_nActiveWnd = GetActiveIndex();
//>>> shadow2004::Tabbed Prefs [TPT] reworked
}

BOOL CPreferencesDlg::OnInitDialog()
{
//>>> WiZaRd::SlideBar [eMule+]
	m_SlideBar.CreateEx(WS_EX_CLIENTEDGE, WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(0, 0, 0, 0), this, IDC_PREFS_SLIDEBAR);
	m_SlideBar.SetImageList(&m_ImageList);
	m_SlideBar.SetHAlignCaption(DT_CENTER);

//	Sets a bold font for the group buttons
	CFont* pGroupFont = m_SlideBar.GetGroupFont();
	ASSERT_VALID(pGroupFont);
	LOGFONT logFont;
	pGroupFont->GetLogFont(&logFont);
	logFont.lfWeight *= 2;
	if (logFont.lfWeight > FW_BLACK)
		logFont.lfWeight = FW_BLACK;
	pGroupFont->DeleteObject();
	pGroupFont->CreateFontIndirect(&logFont);
	ASSERT_VALID(pGroupFont);
//<<< WiZaRd::SlideBar [eMule+]

	ASSERT( !m_bSaveIniFile );
	BOOL bResult = PREF_PARENT_CLASS::OnInitDialog();
	InitWindowStyles(this);

//>>> shadow2004::Tabbed Prefs [TPT] reworked
	SetActivePage(m_nActiveWnd);
//<<< shadow2004::Tabbed Prefs [TPT] reworked
//>>> Spike2::Prefs Side Banner [TPT]
	CBitmap bmp;
	bmp.LoadBitmap(IDB_BANNER);
	m_banner.SetTexture((HBITMAP)bmp.Detach());	
	m_banner.SetFillFlag(KCSB_FILL_TEXTURE);
	m_banner.SetSize(74);
	m_banner.SetTitle(_T(""));
	m_banner.SetCaption(_T(""));
	m_banner.Attach(this, KCSB_ATTACH_RIGHT);	
//<<< Spike2::Prefs Side Banner [TPT]

	Localize();	

	m_SlideBar.SetFocus(); //>>> WiZaRd::SlideBar [eMule+]
	
	return bResult;
}

//>>> WiZaRd::SlideBar [eMule+]
LRESULT CPreferencesDlg::OnSlideBarSelChanged(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
	int iCurrentGlobalSel = m_SlideBar.GetGlobalSelectedItem();

	SetActivePage(iCurrentGlobalSel);

	CListBoxST* pListBox = m_SlideBar.GetGroupListBox(m_SlideBar.GetSelectedGroupIndex());
	ASSERT_VALID(pListBox);

	CString strCurrentItemText;
	pListBox->GetText(pListBox->GetCurSel(), strCurrentItemText);

	CString strCurrentGroupText = m_SlideBar.GetGroupName(m_SlideBar.GetSelectedGroupIndex());
	strCurrentGroupText.Remove('&');

	CString strTitle = RemoveAmbersand(GetResString(IDS_EM_PREFS));
	SetWindowText(strTitle + _T(" -> ") + strCurrentGroupText + _T(" -> ") + strCurrentItemText);

	pListBox->SetFocus();

	return TRUE;
}
//<<< WiZaRd::SlideBar [eMule+]

void CPreferencesDlg::Localize()
{
//>>> WiZaRd::SlideBar [eMule+]
	m_ImageList.DeleteImageList();
	m_ImageList.Create(16, 16, theApp.m_iDfltImageListColorFlags | ILC_MASK, 0, 1);
	//basics:
	m_ImageList.Add(CTempIconLoader(_T("PREFERENCES")));
	m_ImageList.Add(CTempIconLoader(_T("DISPLAY")));
	m_ImageList.Add(CTempIconLoader(_T("CONNECTION")));
	m_ImageList.Add(CTempIconLoader(_T("SERVER")));
	m_ImageList.Add(CTempIconLoader(_T("FOLDERS")));
	m_ImageList.Add(CTempIconLoader(_T("TRANSFER")));

	//advanced:
	m_ImageList.Add(CTempIconLoader(_T("PROXY")));
	m_ImageList.Add(CTempIconLoader(_T("NOTIFICATIONS")));
	m_ImageList.Add(CTempIconLoader(_T("STATISTICS")));
	m_ImageList.Add(CTempIconLoader(_T("IRC")));
	m_ImageList.Add(CTempIconLoader(_T("MESSAGE")));
	m_ImageList.Add(CTempIconLoader(_T("SECURITY")));
	m_ImageList.Add(CTempIconLoader(_T("SCHEDULER")));
	m_ImageList.Add(CTempIconLoader(_T("WEB")));
	int iTweak = m_ImageList.Add(CTempIconLoader(_T("TWEAK")));	
#if defined(_DEBUG) || defined(USE_DEBUG_DEVICE)
	m_ImageList.Add(CTempIconLoader(_T("PREFERENCES")));
#endif

	//mod:
	int iMod = m_ImageList.Add(CTempIconLoader(L"PREFPAGEMOD"));	//>> pP: PrefsGroup
	int iMod1 = m_ImageList.Add(CTempIconLoader(L"SOURCEPREFS"));	//>> pP: PrefsPage_1 - sourcedrops
	int iMod2 = m_ImageList.Add(CTempIconLoader(L"DISPLAYPREFS"));	//>> pP: PrefsPage_2 - Display tweaks
	int iMod3 = m_ImageList.Add(CTempIconLoader(L"OTHERPREFS"));	//>> pP: PrefsPage_3 - Other tweaks
//<<< WiZaRd::SlideBar [eMule+]

	SetTitle(RemoveAmbersand(GetResString(IDS_EM_PREFS))); 

	m_wndGeneral.Localize();
	m_wndDisplay.Localize();
	m_wndConnection.Localize();
	m_wndServer.Localize();
	m_wndDirectories.Localize();
	m_wndFiles.Localize();
	m_wndStats.Localize();
	m_wndNotify.Localize();
	m_wndIRC.Localize();
	m_wndSecurity.Localize();
	m_wndTweaks.Localize();
	m_wndWebServer.Localize();
	m_wndScheduler.Localize();
	m_wndProxy.Localize();
	m_wndMessages.Localize();
	//thePrefs.Localize_Init();
	m_wndTweaksMod1.Localize(); //>> pP: PrefsPage
	m_wndTweaksMod2.Localize(); //>> pP: PrefsPage_2
	m_wndTweaksMod3.Localize(); //>> pP: PrefsPage_3

	int c = 0;

//>>> WiZaRd::SlideBar [eMule+]
	m_SlideBar.ResetContent();

	//basics:
	int iGroup = m_SlideBar.AddGroup(GetResString(IDS_LD_BASICOPT), 0);
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_GENERAL), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_DISPLAY), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_CONNECTION), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_SERVER), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_DIR), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_FILES), iGroup, c++); 

	//advanced:
	iGroup = m_SlideBar.AddGroup(GetResString(IDS_LD_ADVANCEDOPT), iTweak);
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_PROXY), iGroup, c++); 	
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_EKDEV_OPTIONS), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_STATSSETUPINFO), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_IRC), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_MESSAGESCOMMENTS), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_SECURITY), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_SCHEDULER), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_WS), iGroup, c++); 
	m_SlideBar.AddGroupItem(GetResString(IDS_PW_TWEAK), iGroup, c++); 
#if defined(_DEBUG) || defined(USE_DEBUG_DEVICE)
	m_SlideBar.AddGroupItem(_T("Debug"), iGroup, c++); 
#endif

	//mod:
	//>> pP: PrefsPage
	iGroup = m_SlideBar.AddGroup(thePrefs.ResMisc(1), iMod); //>> pP: localize
	m_SlideBar.AddGroupItem(thePrefs.ResMisc(9), iGroup, iMod1);
	m_SlideBar.AddGroupItem(thePrefs.ResMisc(10), iGroup, iMod2);
	m_SlideBar.AddGroupItem(thePrefs.ResMisc(11), iGroup, iMod3);
	//>> pP: PrefsPage

	int width = m_SlideBar.GetGreaterStringWidth();
	width+=60+10;

	CRect rectOld;
	m_SlideBar.GetWindowRect(rectOld);

	int xoffset, yoffset;
	if(IsWindowVisible())
	{
		yoffset=0;
		xoffset=width-rectOld.Width();
	}
	else
	{
		xoffset=width-rectOld.Width()+10;
		GetActivePage()->GetWindowRect(rectOld);
		ScreenToClient (rectOld);
		yoffset = -rectOld.top;
	}
	GetWindowRect(rectOld);
	SetWindowPos(NULL,0,0,rectOld.Width()+xoffset,rectOld.Height()+yoffset,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
	CTabCtrl* tab = GetTabControl(); 
	tab->GetWindowRect (rectOld);
	ScreenToClient (rectOld);
	tab->SetWindowPos(NULL,rectOld.left+xoffset,rectOld.top+yoffset,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
	CPropertyPage* activepage = GetActivePage();
	activepage->GetWindowRect(rectOld);
	ScreenToClient (rectOld);
	activepage->SetWindowPos(NULL,rectOld.left+xoffset,rectOld.top+yoffset,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
	GetClientRect(rectOld);
	m_SlideBar.SetWindowPos(NULL, 6, 6, width, rectOld.Height() - 12, SWP_NOZORDER | SWP_NOACTIVATE);
	int _PropSheetButtons[] = {IDOK, IDCANCEL, ID_APPLY_NOW, IDHELP };
	CWnd* PropSheetButton;
	for (int i = 0; i < sizeof (_PropSheetButtons) / sizeof(_PropSheetButtons[0]); i++)
	{
		if ((PropSheetButton = GetDlgItem(_PropSheetButtons[i])) != NULL)
		{
			PropSheetButton->GetWindowRect (rectOld);
			ScreenToClient (rectOld);
			PropSheetButton->SetWindowPos (NULL, rectOld.left+xoffset,rectOld.top+yoffset,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
		}
	}
	tab->ShowWindow(SW_HIDE);

//>>> shadow2004::Tabbed Prefs [TPT] reworked
	m_SlideBar.SelectGlobalItem(m_nActiveWnd);
//<<< shadow2004::Tabbed Prefs [TPT] reworked
	OnSlideBarSelChanged(NULL, NULL); 
	m_banner.UpdateSize(); //>>> Spike2::Prefs Side Banner [TPT]
	CenterWindow(); 
//<<< WiZaRd::SlideBar [eMule+]	
}

void CPreferencesDlg::OnHelp()
{
	int iCurSel = GetActiveIndex();
	if (iCurSel >= 0)
	{
		CPropertyPage* pPage = GetPage(iCurSel);
		if (pPage)
		{
			HELPINFO hi = {0};
			hi.cbSize = sizeof hi;
			hi.iContextType = HELPINFO_WINDOW;
			hi.iCtrlId = 0;
			hi.hItemHandle = pPage->m_hWnd;
			hi.dwContextId = 0;
			pPage->SendMessage(WM_HELP, 0, (LPARAM)&hi);
			return;
		}
	}

	theApp.ShowHelp(0, HELP_CONTENTS);
}

BOOL CPreferencesDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
	if (wParam == ID_HELP)
	{
		OnHelp();
		return TRUE;
	}
	if (wParam == IDOK || wParam == ID_APPLY_NOW)
		m_bSaveIniFile = true;
	return __super::OnCommand(wParam, lParam);
}

BOOL CPreferencesDlg::OnHelpInfo(HELPINFO* /*pHelpInfo*/)
{
	OnHelp();
	return TRUE;
}

void CPreferencesDlg::SetStartPage(UINT uStartPageID)
{
//>>> shadow2004::Tabbed Prefs [TPT] reworked
	m_nActiveWnd = uStartPageID;
//<<< shadow2004::Tabbed Prefs [TPT] reworked
}
