/////////////////////////////////////////////////////////////////////////////
// Name:        presetspanel.cpp
// Purpose:     wxPackagePresetsPanel
// Author:      Francesco Montorsi
// Modified by:
// Created:     10/07/2006 19:02:39
// RCS-ID:      $Id: presetspanel.cpp,v 1.3 2007/02/01 22:00:34 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/presetspanel.h"
#include "guipm/opteditctrl.h"
#include "wxp/package.h"
#include "wxp/wxp.h"



// ----------------------------------------------------------------------------
// wxPackagePresetsPanel
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackagePresetsPanel, wxPanel )
BEGIN_EVENT_TABLE( wxPackagePresetsPanel, wxPanel )
    EVT_BUTTON( ID_PRESETS_RESET,
                wxPackagePresetsPanel::OnResetToDefaults)
    EVT_CHOICE( ID_PRESETS_PRESETCHOICE,
                wxPackagePresetsPanel::OnPresetChanged)
    EVT_OPTIONS_CHANGED( ID_PRESETS_OPTIONS,
                         wxPackagePresetsPanel::OnOptionChanged)
END_EVENT_TABLE()

wxPackagePresetsPanel::wxPackagePresetsPanel( )
{
}

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

bool wxPackagePresetsPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackagePresetsPanel member initialisation
    m_pPresetChoice = NULL;
////@end wxPackagePresetsPanel member initialisation

////@begin wxPackagePresetsPanel creation
    SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
    wxPanel::Create( parent, id, pos, size, style );

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

void wxPackagePresetsPanel::CreateControls()
{
    m_pOptionsList = new wxOptionEditorCtrl(this, ID_PRESETS_OPTIONS);

////@begin wxPackagePresetsPanel content construction
    wxPackagePresetsPanel* itemPanel1 = this;

    wxStaticBox* itemStaticBoxSizer2Static = new wxStaticBox(itemPanel1, wxID_ANY, _("Default option values"));
    wxStaticBoxSizer* itemStaticBoxSizer2 = new wxStaticBoxSizer(itemStaticBoxSizer2Static, wxVERTICAL);
    itemPanel1->SetSizer(itemStaticBoxSizer2);

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

    wxStaticText* itemStaticText4 = new wxStaticText( itemPanel1, wxID_STATIC, _("Shows option set:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer3->Add(itemStaticText4, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pPresetChoiceStrings = NULL;
    m_pPresetChoice = new wxChoice( itemPanel1, ID_PRESETS_PRESETCHOICE, wxDefaultPosition, wxDefaultSize, 0, m_pPresetChoiceStrings, 0 );
    itemBoxSizer3->Add(m_pPresetChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton6 = new wxButton( itemPanel1, ID_PRESETS_RESET, _("Reset to defaults"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer3->Add(itemButton6, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxWindow* itemWindow7 = (wxWindow*) FindWindow(ID_PRESETS_OPTIONS);
    wxASSERT( itemWindow7 != NULL );
    itemStaticBoxSizer2->Add(itemWindow7, 1, wxGROW|wxALL, 5);

////@end wxPackagePresetsPanel content construction

    // init the presets list
    m_optHashmap = wxPresetXMLDescriptor::s_hashmapOptions;
    wxPackageCommandOptionArrayHashMap::iterator it;
    for( it = m_optHashmap.begin(); it != m_optHashmap.end(); ++it )
    {
        // compiler is a special preset whose values are automatically updated
        // by wxCompilerSettings when the selected bakefile format changes
        //if (it->first != wxT("compiler"))
        m_pPresetChoice->Append(it->first, new wxStringClientData(it->first));
    }

    // select the first preset
    if (m_pPresetChoice->GetCount() > 0)
        m_pPresetChoice->SetSelection(0);

    UpdatePresetChoiceStrings();

    wxCommandEvent fake;
    OnPresetChanged(fake);

    m_pOptionsList->TransferDataToWindow();
}

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

bool wxPackagePresetsPanel::TransferDataFromWindow()
{
    // save currently selected option presets
    if (!m_strCurPresetSelection.empty())
        m_optHashmap[m_strCurPresetSelection] = m_pOptionsList->GetOptions();

    return true;
}

wxString wxPackagePresetsPanel::GetPresetName(unsigned int n) const
{
    // the client data always contains the real name of the preset
    return ((wxStringClientData*)m_pPresetChoice->GetClientObject(n))->GetData();
}

void wxPackagePresetsPanel::UpdatePresetChoiceStrings()
{
    // save selection to restore it later
    int n = m_pPresetChoice->GetSelection();

    for (size_t i=0; i<m_pPresetChoice->GetCount(); i++)
    {
        wxString name = GetPresetName(i);
        if (m_optHashmap[name].IsDirty())
            name += wxT(" *");

        m_pPresetChoice->SetString(i, name);
    }

    m_pPresetChoice->SetSelection(n);
}

void wxPackagePresetsPanel::ApplySettings()
{
    TransferDataFromWindow();
    wxPresetXMLDescriptor::s_hashmapOptions = m_optHashmap;
}



// ----------------------------------------------------------------------------
// wxPackagePresetsPanel - event handlers
// ----------------------------------------------------------------------------

void wxPackagePresetsPanel::OnPresetChanged(wxCommandEvent &WXUNUSED(ev))
{
    // save current edits
    TransferDataFromWindow();

    // update m_strCurPresetSelection
    m_strCurPresetSelection = GetPresetName(m_pPresetChoice->GetSelection());
    const wxPackageCommandOptionArray &arr = m_optHashmap[m_strCurPresetSelection];

    // now, init the property grid with the currently-selected preset options
    m_pOptionsList->SetOptions(arr);
    m_pOptionsList->TransferDataToWindow();
}

void wxPackagePresetsPanel::OnResetToDefaults(wxCommandEvent &ev)
{
    int reply = wxMessageBox(
        wxString::Format(_T("Are you sure you want to reset to the default values all the options for the '%s' preset ?"),
                         m_strCurPresetSelection.c_str()), _T("Warning"), wxYES_NO|wxICON_QUESTION);

    if (reply == wxYES)
    {
        m_optHashmap[m_strCurPresetSelection].ResetToDefaults();
        m_optHashmap[m_strCurPresetSelection].SetDirty();
        UpdatePresetChoiceStrings();

        // to avoid that OnPresetChanged saves the option values
        // currently in the option control in the preset that we've just
        // resetted to defaults, we empty m_strCurPresetSelection
        m_strCurPresetSelection.Empty();
        OnPresetChanged(ev);
    }
}

void wxPackagePresetsPanel::OnOptionChanged(wxCommandEvent &WXUNUSED(ev))
{
    // set this preset as "modified" and thus to save
    m_optHashmap[m_strCurPresetSelection].SetDirty();
    UpdatePresetChoiceStrings();
}

