/////////////////////////////////////////////////////////////////////////////
// Name:        extcmddlg.cpp
// Purpose:     wxExtCmdWizard
// Author:      Julian Smart, Francesco Montorsi
// Modified by:
// Created:     15/06/2006 21:23:11
// RCS-ID:      $Id: extcmddlg.cpp,v 1.2 2007/01/01 20:06:21 frm Exp $
// Copyright:   (c) 2004 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/extcmddlg.h"


// ----------------------------------------------------------------------------
// wxExtCmdWizard
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxExtCmdWizard, wxWizard )
BEGIN_EVENT_TABLE( wxExtCmdWizard, wxWizard )
    EVT_BUTTON(wxID_FORWARD, wxExtCmdWizard::OnNext)

    EVT_WIZARD_PAGE_CHANGED(wxID_ANY, wxExtCmdWizard::OnPageChanged)
    EVT_PROGRESS_END(wxID_ANY, wxExtCmdWizard::OnProgressPanelEnd)

END_EVENT_TABLE()


wxExtCmdWizard::wxExtCmdWizard( )
{
}

wxExtCmdWizard::wxExtCmdWizard( wxWindow* parent, wxWindowID id, const wxString &title, const wxPoint& pos )
{
    Create(parent, id, title, pos);
}

bool wxExtCmdWizard::Create( wxWindow* parent, wxWindowID id, const wxString &title, const wxPoint& pos )
{
    SetExtraStyle(GetExtraStyle()|wxWIZARD_EX_HELPBUTTON);
    wxBitmap wizardBitmap(wxNullBitmap);
    wxWizard::Create( parent, id, title, wizardBitmap, pos, wxDEFAULT_DIALOG_STYLE|wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX );

    CreateControls();

    return true;
}

void wxExtCmdWizard::CreateControls()
{
    m_pPage1 = new wxExtCmdWizardPage1( this, GetTitle() );
    GetPageAreaSizer()->Add(m_pPage1);

    m_pPage2 = new wxExtCmdWizardPage2( this, GetTitle() );
    GetPageAreaSizer()->Add(m_pPage2);

    wxWizardPageSimple::Chain(m_pPage1, m_pPage2);

    // make this wizard a little bigger
    //wxSize size = GetPageAreaSizer()->GetMinSize();
    //GetPageAreaSizer()->SetMinSize(wxSize(size.GetWidth()*2, size.GetHeight()));
}

bool wxExtCmdWizard::Run()
{
    wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
    while (node)
    {
        wxWizardPage* startPage = wxDynamicCast(node->GetData(), wxWizardPage);
        if (startPage) return RunWizard(startPage);
        node = node->GetNext();
    }
    return false;
}

void wxExtCmdWizard::Set(const wxPackage &p, wxPackageBuildSystemStage stage)
{
    // by now set the commands and their options only in the first
    // page as that page allows the user to modify them.
    // We'll pass them to page #2 once the user clicks 'next'
    m_pPage1->GetPanel()->SetPackage(p, stage);
}

void wxExtCmdWizard::Start()
{
    wxButton *btn = wxStaticCast(FindWindowById(wxID_FORWARD, this), wxButton);
    wxButton *cancel = wxStaticCast(FindWindowById(wxID_CANCEL, this), wxButton);
    wxButton *back = wxStaticCast(FindWindowById(wxID_BACKWARD, this), wxButton);

    m_pPage2->GetPanel()->Start();

    btn->SetLabel(ID_EXTCMD_STOPLABEL);
    cancel->Enable(false);
    back->Enable(false);
}

void wxExtCmdWizard::Stop()
{
    wxButton *btn = wxStaticCast(FindWindowById(wxID_FORWARD, this), wxButton);
    wxButton *cancel = wxStaticCast(FindWindowById(wxID_CANCEL, this), wxButton);
    wxButton *back = wxStaticCast(FindWindowById(wxID_BACKWARD, this), wxButton);

    m_pPage2->GetPanel()->Stop();

    btn->SetLabel(ID_EXTCMD_STARTLABEL);
    cancel->Enable(true);
    back->Enable(true);
}

void wxExtCmdWizard::OnNext(wxCommandEvent &ev)
{
    if (GetCurrentPage() == m_pPage1)
    {
        wxExtCmdOptionsPanel *opt = m_pPage1->GetPanel();
        wxASSERT(opt);

        // pass the user-edited options from page1 to page2
        m_pPage2->GetPanel()->SetStage(opt->GetPackageBuildSystemStage());
        m_pPage2->GetPanel()->SetPackage(opt->GetPackage());
        ev.Skip();  // let wxWizard handle this event !
        return;
    }

    wxASSERT(GetCurrentPage() == m_pPage2);
    wxButton *btn = wxStaticCast(FindWindowById(wxID_FORWARD, this), wxButton);
    if (btn->GetLabel() == ID_EXTCMD_STARTLABEL)
        Start();
    else
        Stop();
}

void wxExtCmdWizard::OnPageChanged(wxWizardEvent &)
{
    if (GetCurrentPage() == m_pPage2)
    {
        // change the label to something best-suited for us
        wxButton *btn = wxStaticCast(FindWindowById(wxID_FORWARD, this), wxButton);
        btn->SetLabel(ID_EXTCMD_STARTLABEL);
    }
}

void wxExtCmdWizard::OnProgressPanelEnd(wxCommandEvent &)
{
    Stop();
}




// ----------------------------------------------------------------------------
// wxExtCmdWizardPage1
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxExtCmdWizardPage1, wxWizardPageSimple )
BEGIN_EVENT_TABLE( wxExtCmdWizardPage1, wxWizardPageSimple )
END_EVENT_TABLE()


wxExtCmdWizardPage1::wxExtCmdWizardPage1( )
{
}

wxExtCmdWizardPage1::wxExtCmdWizardPage1( wxWizard* parent, const wxString &name )
{
    Create( parent, name );
}

bool wxExtCmdWizardPage1::Create( wxWizard* parent, const wxString &name )
{
    wxBitmap wizardBitmap(wxNullBitmap);
    wxWizardPageSimple::Create( parent, NULL, NULL, wizardBitmap );
    SetName(name);

    CreateControls();

    return true;
}

void wxExtCmdWizardPage1::CreateControls()
{
    wxBoxSizer *sz = new wxBoxSizer(wxVERTICAL);

    // build the panel with our same name
    m_pPanel = new wxExtCmdOptionsPanel(this, wxID_ANY, wxDefaultPosition,
                                         wxDefaultSize, SYMBOL_WXEXTCMDOPTIONSPANEL_STYLE,
                                         GetName());
    sz->Add(m_pPanel, 1, wxGROW);
    SetSizer(sz);
    GetSizer()->SetSizeHints(this);
}




// ----------------------------------------------------------------------------
// wxExtCmdWizardPage2
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxExtCmdWizardPage2, wxWizardPageSimple )
BEGIN_EVENT_TABLE( wxExtCmdWizardPage2, wxWizardPageSimple )
END_EVENT_TABLE()

wxExtCmdWizardPage2::wxExtCmdWizardPage2( )
{
}

wxExtCmdWizardPage2::wxExtCmdWizardPage2( wxWizard* parent, const wxString &name )
{
    Create( parent, name );
}

bool wxExtCmdWizardPage2::Create( wxWizard* parent, const wxString &name )
{
    wxBitmap wizardBitmap(wxNullBitmap);
    wxWizardPageSimple::Create( parent, NULL, NULL, wizardBitmap );
    SetName(name);

    CreateControls();

    return true;
}

void wxExtCmdWizardPage2::CreateControls()
{
    wxBoxSizer *sz = new wxBoxSizer(wxVERTICAL);

    // build the panel with our same name
    m_pPanel = new wxExtCmdProgressPanel(this, wxID_ANY, wxDefaultPosition,
                                         wxDefaultSize, SYMBOL_WXEXTCMDPROGRESSPANEL_STYLE,
                                         GetName());
    sz->Add(m_pPanel, 1, wxGROW);
    SetSizer(sz);
    GetSizer()->SetSizeHints(this);
}
