/////////////////////////////////////////////////////////////////////////////
// Name:        cmdpresetchooserdlg.cpp
// Purpose:     wxPackageCommandPresetChooserDialog
// Author:      Francesco Montorsi
// Modified by:
// Created:     18/09/2006 23:11:59
// RCS-ID:      $Id: cmdpresetchooserdlg.cpp,v 1.3 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/cmdpresetchooserdlg.h"
#include "guicmn/commandlistbox.h"



// ----------------------------------------------------------------------------
// wxPackageCommandPresetChooserDialog
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageCommandPresetChooserDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageCommandPresetChooserDlg, wxDialog )
    EVT_CHOICE( ID_CMDPRESETCHOOSER_PRESETS, 
                wxPackageCommandPresetChooserDlg::OnPreset )
END_EVENT_TABLE()

wxPackageCommandPresetChooserDlg::wxPackageCommandPresetChooserDlg( )
{
}

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

bool wxPackageCommandPresetChooserDlg::Create( wxWindow* parent, wxWindowID id, wxPackageBuildSystemType type, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageCommandPresetChooserDlg member initialisation
    m_pPresets = NULL;
    m_pDescription = NULL;
    m_pVariables = NULL;
    m_pPreview = NULL;
////@end wxPackageCommandPresetChooserDlg member initialisation

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

    CreateControls(type);
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();

    return true;
}

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

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

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer3, 0, wxGROW, 5);

    wxStaticText* itemStaticText4 = new wxStaticText( itemDialog1, wxID_STATIC, _("Available command presets:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer3->Add(itemStaticText4, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pPresetsStrings = NULL;
    m_pPresets = new wxChoice( itemDialog1, ID_CMDPRESETCHOOSER_PRESETS, wxDefaultPosition, wxDefaultSize, 0, m_pPresetsStrings, 0 );
    itemBoxSizer3->Add(m_pPresets, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer6, 0, wxGROW, 5);

    wxStaticText* itemStaticText7 = new wxStaticText( itemDialog1, wxID_STATIC, _("Preset description:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer6->Add(itemStaticText7, 2, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Variables:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer6->Add(itemStaticText8, 1, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxBoxSizer* itemBoxSizer9 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer9, 2, wxGROW, 5);

    m_pDescription = new wxTextCtrl( itemDialog1, ID_CMDPRESETCHOOSER_DESC, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY );
    itemBoxSizer9->Add(m_pDescription, 2, wxGROW|wxALL, 5);

    wxString* m_pVariablesStrings = NULL;
    m_pVariables = new wxListBox( itemDialog1, ID_CMDPRESETCHOOSER_VARIABLES, wxDefaultPosition, wxDefaultSize, 0, m_pVariablesStrings, wxLB_SINGLE );
    itemBoxSizer9->Add(m_pVariables, 1, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText12 = new wxStaticText( itemDialog1, wxID_STATIC, _("Preset preview:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText12, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    m_pPreview = new wxCommandListBox( itemDialog1, ID_CMDPRESETCHOOSER_PREVIEW, wxDefaultPosition, wxSize(400, 100), 0 );
    itemBoxSizer2->Add(m_pPreview, 3, wxGROW|wxALL|wxFIXED_MINSIZE, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer14 = new wxStdDialogButtonSizer;

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

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

    itemStdDialogButtonSizer14->Realize();

////@end wxPackageCommandPresetChooserDlg content construction

    // init controls with presets suitable for the given build system type
    m_pPresets->Append(wxPresetXMLDescriptor::GetCommandPresetNamesFor(type));

    wxASSERT(m_pPresets->GetCount() > 0);
    m_pPresets->Select(0);

    wxCommandEvent fake;
    OnPreset(fake);
}

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


// ----------------------------------------------------------------------------
// wxPackageCommandPresetChooserDialog - event handlers
// ----------------------------------------------------------------------------

void wxPackageCommandPresetChooserDlg::OnPreset(wxCommandEvent &WXUNUSED(ev))
{
    const wxPackageCommandSet &arr = GetSelectedPreset();

    // update description
    m_pDescription->SetValue(arr.GetDescription());

    // update preview
    m_pPreview->SetCommands(arr);

    // update variables
    m_pVariables->Clear();
    m_pVariables->Append(arr.GetVars());
}
