/////////////////////////////////////////////////////////////////////////////
// Name:        cmdeditdlg.cpp
// Purpose:     wxPackageCommandEditDlg
// Author:      Francesco Montorsi
// Modified by:
// Created:     Sat 12 Aug 2006 16:34:58 CEST
// RCS-ID:      $Id: cmdeditdlg.cpp,v 1.2 2007/01/01 20:06:20 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 "guipkg/cmdeditdlg.h"


// ----------------------------------------------------------------------------
// wxPackageCommandEditDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageCommandEditDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageCommandEditDlg, wxDialog )
    EVT_BUTTON( wxID_OK, wxPackageCommandEditDlg::OnOK )
END_EVENT_TABLE()

wxPackageCommandEditDlg::wxPackageCommandEditDlg( )
{
}

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

bool wxPackageCommandEditDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageCommandEditDlg member initialisation
    m_pNotebook = NULL;
    m_pLabel = NULL;
    m_pCmd = NULL;
    m_pWorkDir = NULL;
////@end wxPackageCommandEditDlg member initialisation

////@begin wxPackageCommandEditDlg creation
    SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end wxPackageCommandEditDlg creation
return true;
}

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

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

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

    wxPanel* itemPanel4 = new wxPanel( m_pNotebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxVERTICAL);
    itemPanel4->SetSizer(itemBoxSizer5);

    itemBoxSizer5->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    m_pLabel = new wxStaticText( itemPanel4, wxID_STATIC, _("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT\nTEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT\nTEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT\n** SET BY wxPackageCmdEditDlg AUTOMATICALLY **"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE|wxST_NO_AUTORESIZE );
    itemBoxSizer5->Add(m_pLabel, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 5);

    itemBoxSizer5->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxStaticText* itemStaticText9 = new wxStaticText( itemPanel4, wxID_STATIC, _("Note that, unlike local references, the 'working directory'\ncan be specified without using file:/// prefix.\nIf empty, the command will be executed from the package root."), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE );
    itemBoxSizer5->Add(itemStaticText9, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 5);

    itemBoxSizer5->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer11 = new wxFlexGridSizer(2, 2, 0, 0);
    itemFlexGridSizer11->AddGrowableCol(1);
    itemBoxSizer5->Add(itemFlexGridSizer11, 0, wxGROW|wxALL, 5);
    wxStaticText* itemStaticText12 = new wxStaticText( itemPanel4, wxID_STATIC, _("Command:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(itemStaticText12, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pCmd = new wxTextCtrl( itemPanel4, ID_CMDEDIT_CMD, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(m_pCmd, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText14 = new wxStaticText( itemPanel4, wxID_STATIC, _("Working directory:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(itemStaticText14, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pWorkDir = new wxTextCtrl( itemPanel4, ID_CMDEDIT_WORKDIR, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(m_pWorkDir, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    itemBoxSizer5->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    m_pNotebook->AddPage(itemPanel4, _("Command"));

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

    wxStdDialogButtonSizer* itemStdDialogButtonSizer17 = new wxStdDialogButtonSizer;

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

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

    itemStdDialogButtonSizer17->Realize();

////@end wxPackageCommandEditDlg content construction

    // add a static text above the condition panel
    wxPanel *panel = new wxPanel(m_pNotebook, wxID_ANY);
    wxSizer *sz = new wxBoxSizer(wxVERTICAL);
    m_pConditionPanel = new wxPackageConditionPanel(panel, wxID_ANY);
    sz->Add(new wxStaticText(panel, wxID_ANY,
            _("Check the build systems, platforms, formats and stages to which this command applies.")), 0, wxGROW|wxALL, 5);
    sz->AddSpacer(10);
    sz->Add(m_pConditionPanel, 1, wxGROW);
    sz->AddSpacer(10);
    panel->SetSizer(sz);
    sz->SetSizeHints(panel);
    m_pNotebook->AddPage(panel, _("Condition"));

    // init the condition panel checking everything
    wxPackageCondition cond;
    cond.SetAlwaysTrue();
    m_pConditionPanel->SetCondition(cond);

    // a command is always inserted in a stage which is fixed;
    // don't allow the user to edit that useless part of a wxPackageCondition, then
    m_pConditionPanel->EnableStageList(false);
}

bool wxPackageCommandEditDlg::ShowToolTips()
{
    return true;
}


// ----------------------------------------------------------------------------
// wxPackageCommandEditDlg - event handlers
// ----------------------------------------------------------------------------

void wxPackageCommandEditDlg::OnOK(wxCommandEvent &WXUNUSED(ev))
{
    wxString str(m_pCmd->GetValue());
    if (str.IsEmpty())
    {
        wxLogError(wxT("Please provide the command to execute."));
        return;
    }

    // wxPackageConditionPanel already warns the user
    if (!m_pConditionPanel->IsOk())
        return;

    EndModal(wxID_OK);
}
