/////////////////////////////////////////////////////////////////////////////
// Name:        condeditpanel.cpp
// Purpose:     wxPackageConditionPanel
// Author:      Francesco Montorsi
// Modified by:
// Created:     Thu 10 Aug 2006 18:49:53 CEST
// RCS-ID:      $Id: condeditpanel.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 "guicmn/condeditpanel.h"
#include "guipm/smartmsgdlg.h"

#include "wx/miscutils.h"


// ----------------------------------------------------------------------------
// wxPackageConditionPanel
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageConditionPanel, wxPanel )
BEGIN_EVENT_TABLE( wxPackageConditionPanel, wxPanel )

END_EVENT_TABLE()

wxPackageConditionPanel::wxPackageConditionPanel( )
{
}

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

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

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

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

void wxPackageConditionPanel::CreateControls()
{
////@begin wxPackageConditionPanel content construction
    wxPackageConditionPanel* itemPanel1 = this;

    wxFlexGridSizer* itemFlexGridSizer2 = new wxFlexGridSizer(2, 2, 0, 0);
    itemFlexGridSizer2->AddGrowableRow(1);
    itemFlexGridSizer2->AddGrowableRow(3);
    itemFlexGridSizer2->AddGrowableCol(0);
    itemFlexGridSizer2->AddGrowableCol(1);
    itemPanel1->SetSizer(itemFlexGridSizer2);

    wxStaticText* itemStaticText3 = new wxStaticText( itemPanel1, wxID_STATIC, _("Build system(s):"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer2->Add(itemStaticText3, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxStaticText* itemStaticText4 = new wxStaticText( itemPanel1, wxID_STATIC, _("Platform(s):"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer2->Add(itemStaticText4, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxString* m_pBuildSystemsStrings = NULL;
    m_pBuildSystems = new wxCheckListBox( itemPanel1, ID_CONDEDIT_BUILDSYS, wxDefaultPosition, wxSize(-1, 100), 0, m_pBuildSystemsStrings, wxLB_SINGLE );
    itemFlexGridSizer2->Add(m_pBuildSystems, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL|wxFIXED_MINSIZE, 5);

    wxString* m_pPlatformsStrings = NULL;
    m_pPlatforms = new wxCheckListBox( itemPanel1, ID_CONDEDIT_PLATFORMS, wxDefaultPosition, wxSize(-1, 100), 0, m_pPlatformsStrings, wxLB_SINGLE );
    itemFlexGridSizer2->Add(m_pPlatforms, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL|wxFIXED_MINSIZE, 5);

    wxStaticText* itemStaticText7 = new wxStaticText( itemPanel1, wxID_STATIC, _("Format(s):"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer2->Add(itemStaticText7, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemPanel1, wxID_STATIC, _("Stage(s):"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer2->Add(itemStaticText8, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxString* m_pFormatsStrings = NULL;
    m_pFormats = new wxCheckListBox( itemPanel1, ID_CONDEDIT_FORMATS, wxDefaultPosition, wxSize(-1, 100), 0, m_pFormatsStrings, wxLB_SINGLE );
    itemFlexGridSizer2->Add(m_pFormats, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL|wxFIXED_MINSIZE, 5);

    wxString* m_pStagesStrings = NULL;
    m_pStages = new wxCheckListBox( itemPanel1, ID_CONDEDIT_STAGES, wxDefaultPosition, wxSize(-1, 100), 0, m_pStagesStrings, wxLB_SINGLE );
    itemFlexGridSizer2->Add(m_pStages, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL|wxFIXED_MINSIZE, 5);

////@end wxPackageConditionPanel content construction

    // init all wxCheckListBoxes
    m_pFormats->Append(wxArrayString(wxPCF_MAX, wxPackageCompilerFormatName));
    m_pBuildSystems->Append(wxArrayString(wxPBST_MAX, wxPackageBuildSystemTypeName));
    m_pStages->Append(wxArrayString(wxPBSS_MAX, wxPackageBuildSystemStageName));

    for (size_t i=0; i<wxPORT_MAX; i++)
        m_pPlatforms->Append(wxPlatformInfo::GetPortIdName((wxPortId)(1 << i), false));
}

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

void wxPackageConditionPanel::SetCondition(const wxPackageCondition &cond)
{
    wxFlags2CheckListBox(m_pFormats, cond.GetFormats(), wxPCF_MAX);
    wxFlags2CheckListBox(m_pBuildSystems, cond.GetBuildSystems(), wxPBST_MAX);
    wxFlags2CheckListBox(m_pPlatforms, cond.GetPlatforms(), wxPORT_MAX);
    wxFlags2CheckListBox(m_pStages, cond.GetStages(), wxPBSS_MAX);
}

wxPackageCondition wxPackageConditionPanel::GetCondition() const
{
    wxPackageCondition ret;
    ret.SetFormats(wxCheckListBox2Flags(m_pFormats));
    ret.SetBuildSystems(wxCheckListBox2Flags(m_pBuildSystems));
    ret.SetPlatforms(wxCheckListBox2Flags(m_pPlatforms));
    ret.SetStages(wxCheckListBox2Flags(m_pStages));
    return ret;
}

bool wxPackageConditionPanel::IsOk() const
{
    if (GetCondition().IsAlwaysFalse())
    {
        wxLogError(_("The condition for this object is always false. Please check at least one format, one build system, one platform and one stage."));
        return false;
    }

#define CHECKLISTBOX(p, msg)                            \
    if (wxCheckListBox2Flags(p) == 0)                   \
        { wxLogError(msg); return false; }

    CHECKLISTBOX(m_pFormats, _("This object does not apply to any format. Please check at least one format."));
    CHECKLISTBOX(m_pBuildSystems, _("This object does not apply to any build system. Please check at least one build system."));
    CHECKLISTBOX(m_pPlatforms, _("This object does not apply to any platform. Please check at least one platform."));
    CHECKLISTBOX(m_pStages, _("This object does not apply to any stage. Please check at least one stage."));

    return true;
}
