/////////////////////////////////////////////////////////////////////////////
// Name:        builddlg.cpp
// Purpose:     wxPackageBuildDlg
// Author:      Francesco Montorsi
// Modified by:
// Created:     Fri 23 Jun 2006 14:45:45 CEST
// RCS-ID:      $Id: builddlg.cpp,v 1.4 2007/02/01 20:11:27 frm Exp $
// Copyright:   (c) 2006 Francesco Montorsi
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "guipm/builddlg.h"
#include "guipm/extcmdoptionspanel.h"
#include "guipm/compilerpanel.h"


// ----------------------------------------------------------------------------
// wxPackageBuildDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageBuildDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageBuildDlg, wxDialog )
    EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxPackageBuildDlg::OnPageChanged)
    EVT_BUTTON(wxID_OK, wxPackageBuildDlg::OnOK)
END_EVENT_TABLE()

wxPackageBuildDlg::wxPackageBuildDlg( )
{
}

wxPackageBuildDlg::wxPackageBuildDlg( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Create(parent, id, caption, pos, size, style);
}

bool wxPackageBuildDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    m_pBuildOptPanel = NULL;
    m_pCompilerPanel = NULL;

    SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    if (GetSizer())
    {

        GetSizer()->SetSizeHints(this);
    }
    CentreOnScreen();

    return true;
}

void wxPackageBuildDlg::CreateControls()
{
////@begin wxPackageBuildDlg content construction
    wxPackageBuildDlg* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    m_pNotebook = new wxNotebook( itemDialog1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_TOP );

    itemBoxSizer2->Add(m_pNotebook, 1, wxGROW|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer4 = new wxStdDialogButtonSizer;

    itemBoxSizer2->Add(itemStdDialogButtonSizer4, 0, wxALIGN_RIGHT|wxALL, 5);
    wxButton* itemButton5 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer4->AddButton(itemButton5);

    wxButton* itemButton6 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer4->AddButton(itemButton6);

    itemStdDialogButtonSizer4->Realize();

////@end wxPackageBuildDlg content construction

    // build notebook tabs
    m_pBuildOptPanel = new wxExtCmdOptionsPanel( m_pNotebook, wxID_ANY, 
        wxDefaultPosition, wxDefaultSize, SYMBOL_WXEXTCMDOPTIONSPANEL_STYLE, GetTitle() );
    m_pNotebook->AddPage(m_pBuildOptPanel, _("Package options"));

    // add a static text above the usual compiler settings
    // (the text will be updated in SetPackage())
    wxPanel *panel = new wxPanel(m_pNotebook, wxID_ANY);
    wxSizer *sz = new wxBoxSizer(wxVERTICAL);
    m_pDescLabel = new wxStaticText(panel, wxID_ANY, wxT("DESCRIPTION HERE\nDESCRIPTION HERE\nDESCRIPTION HERE"));
    m_pCompilerPanel = new wxCompilerSettingsPanel(panel, wxID_ANY);
    sz->Add(m_pDescLabel, 0, wxGROW|wxALL, 5);
    sz->Add(m_pCompilerPanel, 1, wxGROW);
    panel->SetSizer(sz);
    sz->SetSizeHints(panel);

    m_pNotebook->AddPage(panel, _("Global compiler options"));
}

void wxPackageBuildDlg::SetPackage(const wxPackage &p)
{ 
    m_pBuildOptPanel->SetPackage(p, wxPBSS_BUILD);
    m_pCompilerPanel->SetPackage(p);

    // remember the current format (see OnPageChanged)
    m_last = p.GetCompilerSettings().GetSelFormat();

    wxString str;
    str.Printf(_("This package uses %s as build system.\nOnly the compilers it supports will be listed here.\nAlso note that changes to the compiler paths have global scope."),
#ifdef __WXGTK20__
               wxString(wxT("<b>") + p.GetBuildSystemTypeStr() + wxT("</b>")).c_str()
#else
               p.GetBuildSystemTypeStr().c_str()
#endif
               );         
    m_pDescLabel->SetLabel(str);
}


// ----------------------------------------------------------------------------
// wxPackageBuildDlg - event handlers
// ----------------------------------------------------------------------------

void wxPackageBuildDlg::OnPageChanged(wxNotebookEvent &WXUNUSED(ev))
{
    if (!m_pCompilerPanel)
        return;     // still building the dialog!

    if (IsSelectedBuildOptPage())       // compiler panel => build options panel
    {
        m_pCompilerPanel->TransferDataFromWindow();

        wxPackage &pkg = m_pCompilerPanel->GetPackage();
        wxCompilerSettings &sett = pkg.GetCompilerSettings();

        // did the user update the currently selected compiler format
        // using the wxCompilerSettingsPanel panel ?
        if (m_last != sett.GetSelFormat())
        {
            wxStringHashMap h = sett.GetOptionsWithDefaults(pkg.GetBuildSystemType());
            wxString list;
            for( wxStringHashMap::iterator it = h.begin(); it != h.end(); ++it )
                list += it->first + wxT(",");
            list.RemoveLast();

            wxLogMessage(_("You have changed the compiler to use. The options %s in the 'Package options' panel will be updated discarding any previous change..."),
                         list.c_str());

            // reset the package's options which belong to the "compiler"
            // group to their default value (so that the build option panel
            // will be forced to re-do substitutions and thus use the updated
            // selected compiler format):
            wxPackageCommandOptionArray &arr = pkg.GetBuildSysOptions();
            for (size_t i=0; i<arr.GetCount(); i++)
                if (arr[i].GetGroup() == wxT("Compiler"))
                    arr[i].ResetToDefault();

            // very important:
            pkg.InvalidateCachedSubstHashMap();

            // update the 'compiler' presets of the options panel
            m_pBuildOptPanel->SetPackage(pkg, wxPBSS_BUILD);
            m_pBuildOptPanel->RefreshOptions();

            // also updates command (some keyword substitutions have changed)
            m_pBuildOptPanel->UpdateCommands();

            // update last selection
            m_last = sett.GetSelFormat();
        }
    }
    else            // build options panel => compiler panel
    {
        // move the updated package data from the build panel to the compiler panel:

        m_pBuildOptPanel->TransferDataFromWindow();
        m_pCompilerPanel->SetPackage(m_pBuildOptPanel->GetPackage());
    }
}

void wxPackageBuildDlg::OnOK(wxCommandEvent &ev)
{
    m_pCompilerPanel->TransferDataFromWindow();
    m_pBuildOptPanel->TransferDataFromWindow();

    // let default event handler close this dialog
    ev.Skip();
}

