/////////////////////////////////////////////////////////////////////////////
// Name:        opteditdlg.cpp
// Purpose:     wxPackageOptionEditDlg
// Author:      Francesco Montorsi
// Modified by:
// Created:     Thu 10 Aug 2006 19:08:43 CEST
// RCS-ID:      $Id: opteditdlg.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/opteditdlg.h"


// ----------------------------------------------------------------------------
// wxPackageOptionEditDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageOptionEditDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageOptionEditDlg, wxDialog )
    EVT_BUTTON( wxID_OK, wxPackageOptionEditDlg::OnOK )
    EVT_CHOICE( ID_OPTEDIT_CATEGORY, wxPackageOptionEditDlg::OnCategory )
END_EVENT_TABLE()

wxPackageOptionEditDlg::wxPackageOptionEditDlg( )
{
}

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

bool wxPackageOptionEditDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageOptionEditDlg member initialisation
    m_pNotebook = NULL;
    m_pName = NULL;
    m_pCategory = NULL;
    m_pAllowedValues = NULL;
    m_pDefValue = NULL;
    m_pGroup = NULL;
    m_pDescription = NULL;
////@end wxPackageOptionEditDlg member initialisation

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

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

void wxPackageOptionEditDlg::CreateControls()
{
////@begin wxPackageOptionEditDlg content construction
    wxPackageOptionEditDlg* 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 );
    wxFlexGridSizer* itemFlexGridSizer5 = new wxFlexGridSizer(2, 2, 0, 0);
    itemFlexGridSizer5->AddGrowableRow(5);
    itemFlexGridSizer5->AddGrowableCol(1);
    itemPanel4->SetSizer(itemFlexGridSizer5);

    wxStaticText* itemStaticText6 = new wxStaticText( itemPanel4, wxID_STATIC, _("Name:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer5->Add(itemStaticText6, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pName = new wxTextCtrl( itemPanel4, ID_OPTEDIT_NAME, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    if (ShowToolTips())
        m_pName->SetToolTip(_("Be sure to use only characters in range A-Z and a-z for the option name!"));
    itemFlexGridSizer5->Add(m_pName, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemPanel4, wxID_STATIC, _("Category:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer5->Add(itemStaticText8, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pCategoryStrings = NULL;
    m_pCategory = new wxChoice( itemPanel4, ID_OPTEDIT_CATEGORY, wxDefaultPosition, wxDefaultSize, 0, m_pCategoryStrings, 0 );
    itemFlexGridSizer5->Add(m_pCategory, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText10 = new wxStaticText( itemPanel4, wxID_STATIC, _("Allowed values:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer5->Add(itemStaticText10, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pAllowedValues = new wxTextCtrl( itemPanel4, ID_OPTEDIT_VALUES, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    if (ShowToolTips())
        m_pAllowedValues->SetToolTip(_("Type a comma-separed list of the values allowed for this option"));
    itemFlexGridSizer5->Add(m_pAllowedValues, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText12 = new wxStaticText( itemPanel4, wxID_STATIC, _("Default value:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer5->Add(itemStaticText12, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pDefValue = new wxTextCtrl( itemPanel4, ID_OPTEDIT_DEFVALUE, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer5->Add(m_pDefValue, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

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

    wxString* m_pGroupStrings = NULL;
    m_pGroup = new wxComboBox( itemPanel4, ID_OPTEDIT_GROUP, _T(""), wxDefaultPosition, wxDefaultSize, 0, m_pGroupStrings, wxCB_DROPDOWN );
    itemFlexGridSizer5->Add(m_pGroup, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText16 = new wxStaticText( itemPanel4, wxID_STATIC, _("Description:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer5->Add(itemStaticText16, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pDescription = new wxTextCtrl( itemPanel4, ID_OPTEDIT_DESCRIPTION, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
    itemFlexGridSizer5->Add(m_pDescription, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

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

    itemBoxSizer2->Add(m_pNotebook, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer18 = new wxStdDialogButtonSizer;

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

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

    itemStdDialogButtonSizer18->Realize();

////@end wxPackageOptionEditDlg 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 option 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 category wxChoice
    m_pCategory->Append(wxArrayString(wxPCOC_MAX, wxPackageCommandOptionCategoryName));
    m_pCategory->Select(0);

    // init the group combobox
    m_pGroup->Append(wxT("wxWidgets"));
    m_pGroup->Append(wxT("Compiler"));

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

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


// ----------------------------------------------------------------------------
// wxPackageOptionEditDlg - event handlers
// ----------------------------------------------------------------------------

void wxPackageOptionEditDlg::OnOK(wxCommandEvent &WXUNUSED(ev))
{
    wxString name(m_pName->GetValue());
    if (name.IsEmpty())
    {
        wxLogError(wxT("Please provide the option name."));
        return;
    }
    else if (!wxIsPlainWord(name))
    {
        wxLogError(wxT("Invalid name '%s'.\nYou can use only the following characters:\n%s"), 
                   name.c_str(), wxGetPlainWordAllowedCharacters().c_str());
        return;
    }

    wxString defvalue(m_pDefValue->GetValue());
    if (!wxIsPlainWord(defvalue))
    {
        wxLogError(wxT("Invalid default value '%s'.\nYou can use only the following characters:\n%s"), 
                   defvalue.c_str(), wxGetPlainWordAllowedCharacters().c_str());
        return;
    }

    wxString values(m_pAllowedValues->GetValue());
    if (m_pCategory->GetSelection() == wxPCOC_LISTED_VALUES)
    {
        if (values.IsEmpty())
        {
            wxLogError(wxT("Please provide the allowed values."));
            return;
        }
        else if (!values.Contains(defvalue))
        {
            wxLogError(wxT("The default value '%s' is not one of the allowed values '%s' ?"),
                       defvalue.c_str(), values.c_str());
            return;
        }
    }

    if (m_pGroup->GetValue().IsEmpty())
    {
        wxLogError(wxT("Please provide the name of the group of the option.\nNote that you can create new groups just typing the name of a group which does not exist yet."));
        return;
    }

    EndModal(wxID_OK);
}

void wxPackageOptionEditDlg::OnCategory(wxCommandEvent &WXUNUSED(ev))
{
    bool enable = m_pCategory->GetSelection() == wxPCOC_LISTED_VALUES;
    m_pAllowedValues->Enable(enable);
}
