/**
    File        : License.cpp
    Description : Displays license agreement
    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 "Licene.h"

#ifndef _DEBUG
    const wxString g_FileName = _("COPYING.txt");
#else
    const wxString g_FileName = _("..\\COPYING.txt");
#endif

const int g_LinesPerPage = 45;


CLicenseDlg::CLicenseDlg(wxWindow* pParent)
: wxDialog(pParent, wxID_ANY, _("License Agreement"), wxDefaultPosition, wxSize(400, 500), wxCAPTION),
  m_pParent(pParent)
{
    // text message
    wxString msg = _("Ace Personal Finance Assistant is distributed under the terms of the ");
    msg += _("GNU General Public License.  Please read the following agreement.  You must ");
    msg += _("accept the terms of this agreement before continuing with the application.");
    wxStaticText* pTitleText = new wxStaticText(this, wxID_ANY, msg);

    // the license text
    m_pLicenseText = new wxTextCtrl(this,
                                    wxID_ANY,
                                    wxEmptyString,
                                    wxDefaultPosition,
                                    wxDefaultSize,
                                    wxTE_MULTILINE | wxTE_READONLY);
    if (!m_pLicenseText->LoadFile(g_FileName) )
        wxFAIL_MSG(_("Could not find License File!") );

    // selection box
    m_pChoiceBox = new wxCheckBox(this, idChoiceBox, _("I Accept") );
    m_pChoiceBox->SetValue(false);

    // hyperlinks
    wxHyperlinkCtrl* pGNUHyperlink = new wxHyperlinkCtrl(this,
                                                         wxID_ANY,
                                                         _("Click here for more information about the GNU project"),
                                                         _("http://www.gnu.org/") );

    // buttons
    wxButton* m_pPrintBtn = new wxButton(this, idPrintBtn, _("print") );
    wxButton* m_pContinueBtn = new wxButton(this, wxID_OK, _("Continue") );
    wxButton* m_pCancelBtn = new wxButton(this, wxID_CANCEL, _("Cancel") );
    wxStdDialogButtonSizer* pBtnSizer = new wxStdDialogButtonSizer;
    pBtnSizer->AddButton(m_pContinueBtn);
    pBtnSizer->AddButton(m_pCancelBtn);
    pBtnSizer->Realize();

    // sizers
    wxBoxSizer* pSubSizer = new wxBoxSizer(wxHORIZONTAL);
    pSubSizer->Add(m_pChoiceBox, 1, wxALL | wxALIGN_LEFT, 3);
    pSubSizer->AddStretchSpacer(5);
    pSubSizer->Add(m_pPrintBtn, 1, wxALL | wxALIGN_RIGHT, 3);

    wxBoxSizer* pMainSizer = new wxBoxSizer(wxVERTICAL);
    pMainSizer->Add(pTitleText, 1, wxALL | wxEXPAND, 5);
    pMainSizer->Add(m_pLicenseText, 5, wxALL | wxEXPAND, 5);
    pMainSizer->Add(pGNUHyperlink, 1, wxALL | wxALIGN_LEFT, 3);
    pMainSizer->Add(pSubSizer, 1, wxALL | wxEXPAND, 3);
    pMainSizer->Add(new wxStaticLine(this), 0, wxALL | wxEXPAND, 3);
    pMainSizer->Add(pBtnSizer, 1, wxALL | wxEXPAND, 5);

    SetSizer(pMainSizer);

    Connect(wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(CLicenseDlg::OnContinue) );
    Connect(wxID_CANCEL, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(CLicenseDlg::OnCancel) );
    Connect(idPrintBtn, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(CLicenseDlg::OnPrint) );
}

CLicenseDlg::~CLicenseDlg()
{

}

void CLicenseDlg::OnContinue(wxCommandEvent &event)
{
    if (!m_pChoiceBox->IsChecked() )
        aceMSGBOX(this, ace::MB_INFO, _("You must accept the terms\nbefore you can continue"), NULL);
    else
        EndModal(wxID_OK);
}

void CLicenseDlg::OnCancel(wxCommandEvent &event)
{
    EndModal(wxID_CANCEL);
}

void CLicenseDlg::OnPrint(wxCommandEvent &event)
{
    wxPrintData PrintData;
    wxPrintDialogData PrintDialogData(PrintData);

    wxPrinter Printer(&PrintDialogData);
    CPrintout Printout(this, wxT("GNU License Agreement") );
    if (!Printer.Print(this, &Printout, true) )
        aceMSGBOX(this, ace::MB_ERROR, _("Could not print document!"), NULL);
}


///** class CPrintout *******************************************************///


CPrintout::CPrintout(wxDialog* pParent, const wxString& Title)
: wxPrintout(Title),
  m_pParent(pParent)
{

}

CPrintout::~CPrintout()
{

}

bool CPrintout::OnPrintPage(int page)
{
    if (!m_LicenseText.Open(g_FileName) )
    {
        aceMSGBOX(NULL, ace::MB_ERROR, _("Could not find License File!"), NULL);
        return false;
    }

    wxDC* DC = GetDC();
    if (!DC)
    {
        aceMSGBOX(NULL, ace::MB_ERROR, _("Failed to retrive DC!"), NULL);
        return false;
    }

    wxCoord w, h, d, e;
    DC->GetTextExtent(_("Yy"), &w, &h, &d, &e);
    const int LineHeight = (int)(h + d + e + 6);
    const size_t NumLines = m_LicenseText.GetLineCount();

    for (size_t x = 0; x != NumLines; x++)
    {
        DC->DrawText(m_LicenseText.GetLine(x), 0, (x * LineHeight) );
    }

    m_LicenseText.Close();
    return true;
}

