/**
    File        : CSettings.cpp
    Description : Select settings
    Author      : Raymond Marsh
    Created     : 08/01/2009
    Copyright 2009, 2010 Raymond Marsh

    This program is distributed under the terms of the GNU General Public License.
    To view a copy of the license see the file 'copying.txt'

    This file is part of the Ace Personal Finance Assistant Suite.

    Ace Personal Finance Assistant 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 3 of the License, or (at your option) any later version.

    Ace Personal Finance Assistant 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 Ace Personal Finance Assistant.
    If not, see <http://www.gnu.org/licenses/>.
**/

#include "SettingsDlg.h"

//(*InternalHeaders(CSettings)
#include <wx/xrc/xmlres.h>
//*)

//(*IdInit(CSettings)
//*)

BEGIN_EVENT_TABLE(CSettings,wxDialog)
	//(*EventTable(CSettings)
	//*)
END_EVENT_TABLE()

CSettings::CSettings(wxWindow* parent)
: m_pParent(parent), m_IsChanged(false), m_TempOptions(*ace::g_pOptions)
{
    SetOwnBackgroundColour(ace::g_pOptions->COLORS.DialogBGColor);

	//(*Initialize(CSettings)
	wxXmlResource::Get()->LoadObject(this,parent,_T("CSettings"),_T("wxDialog"));
	CheckBox1 = (wxCheckBox*)FindWindow(XRCID("ID_CHECKBOX1"));
	Panel2 = (wxPanel*)FindWindow(XRCID("ID_PANEL2"));
	StaticText1 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT1"));
	ColourPickerCtrl1 = (wxColourPickerCtrl*)FindWindow(XRCID("ID_COLOURPICKERCTRL1"));
	StaticText2 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT2"));
	ColourPickerCtrl2 = (wxColourPickerCtrl*)FindWindow(XRCID("ID_COLOURPICKERCTRL2"));
	Panel1 = (wxPanel*)FindWindow(XRCID("ID_PANEL1"));
	CheckBox2 = (wxCheckBox*)FindWindow(XRCID("ID_CHECKBOX2"));
	Panel3 = (wxPanel*)FindWindow(XRCID("ID_PANEL3"));
	Notebook1 = (wxNotebook*)FindWindow(XRCID("ID_NOTEBOOK1"));

	Connect(XRCID("ID_CHECKBOX1"),wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&CSettings::OnCheckBox1Click);
	Connect(XRCID("ID_COLOURPICKERCTRL1"),wxEVT_COMMAND_COLOURPICKER_CHANGED,(wxObjectEventFunction)&CSettings::OnColourPickerCtrl1ColourChanged);
	Connect(XRCID("ID_COLOURPICKERCTRL2"),wxEVT_COMMAND_COLOURPICKER_CHANGED,(wxObjectEventFunction)&CSettings::OnColourPickerCtrl2ColourChanged);
	//*)

    // disable apply button at start
    wxButton* button = (wxButton*)(FindWindowById(wxID_APPLY) );
    wxASSERT_MSG(button != NULL, _("Could not locate button ID") );
    button->Disable();

    // set starting options
    CheckBox1->SetValue(ace::g_pOptions->GLOBAL.HideReconciledTransactionsOnStart);
    ColourPickerCtrl1->SetColour(ace::g_pOptions->COLORS.MainBGColor);
    ColourPickerCtrl2->SetColour(ace::g_pOptions->COLORS.DialogBGColor);
}

CSettings::~CSettings()
{
	//(*Destroy(CSettings)
	//*)
}


void CSettings::OnOK(wxCommandEvent &event)
{
    if (m_IsChanged)
    {
        m_IsChanged = false;
        *ace::g_pOptions = m_TempOptions;
        ace::g_pOptions->SaveOptions();
    }
    EndModal(wxID_OK);
}

void CSettings::OnApply(wxCommandEvent &event)
{
    if (m_IsChanged)
    {
        m_IsChanged = false;
        *ace::g_pOptions = m_TempOptions;
        ace::g_pOptions->SaveOptions();

        // apply settings
        m_pParent->SetOwnBackgroundColour(ace::g_pOptions->COLORS.MainBGColor);
        this->SetOwnBackgroundColour(ace::g_pOptions->COLORS.DialogBGColor);

        ace::g_pOptions->SaveOptions();
        m_pParent->Refresh();
        this->Refresh();

        ( (wxButton*)(FindWindowById(wxID_APPLY) ) )->Disable();
    }
}

void CSettings::OnColourPickerCtrl1ColourChanged(wxColourPickerEvent& event)
{
    m_TempOptions.COLORS.MainBGColor = ColourPickerCtrl1->GetColour();
    m_IsChanged = true;
    ( (wxButton*)(FindWindowById(wxID_APPLY) ) )->Enable();
}

void CSettings::OnColourPickerCtrl2ColourChanged(wxColourPickerEvent& event)
{
    m_TempOptions.COLORS.DialogBGColor = ColourPickerCtrl2->GetColour();
    m_IsChanged = true;
    ( (wxButton*)(FindWindowById(wxID_APPLY) ) )->Enable();
}

void CSettings::OnCheckBox1Click(wxCommandEvent& event)
{
    bool flag = m_TempOptions.GLOBAL.HideReconciledTransactionsOnStart;
    m_TempOptions.GLOBAL.HideReconciledTransactionsOnStart = !flag;
    m_IsChanged = true;
    ( (wxButton*)(FindWindowById(wxID_APPLY) ) )->Enable();
}
