/////////////////////////////////////////////////////////////////////////////
// Name:        wxbuildpropdlg.cpp
// Purpose:     wxPackageWXBuildPropDlg
// Author:      Francesco Montorsi
// Modified by:
// Created:     25/07/2006 16:35:04
// RCS-ID:      $Id: wxbuildpropdlg.cpp,v 1.3 2007/01/17 21:51:46 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/opteditctrl.h"
#include "guipm/wxbuildpropdlg.h"
#include "wxp/wxp.h"

#include "wx/filepicker.h"
#include "wx/apptrait.h"


// the list of wx ports which can be used on the platform we're running on
#if defined( __WXMSW__ )
    long g_supportedPorts = wxPORT_MSW;
#elif defined( __WXGTK__ ) || defined( __WXX11__ ) || defined( __WXMOTIF__ )
    long g_supportedPorts = wxPORT_GTK|wxPORT_X11|wxPORT_MOTIF;
#elif defined( __WXMAC__ )
    long g_supportedPorts = wxPORT_MAC;
#endif



// ----------------------------------------------------------------------------
// wxPackageWXBuildPropDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageWXBuildPropDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageWXBuildPropDlg, wxDialog )
    EVT_BUTTON(wxID_OK, wxPackageWXBuildPropDlg::OnOK)
    EVT_BUTTON(ID_WXBUILDPROP_AUTODETECT, wxPackageWXBuildPropDlg::OnAutodetect)
END_EVENT_TABLE()

wxPackageWXBuildPropDlg::wxPackageWXBuildPropDlg( )
{
}

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

bool wxPackageWXBuildPropDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageWXBuildPropDlg member initialisation
    m_pPortChoice = NULL;
    m_pVersionChoice = NULL;
    m_pCompilerChoice = NULL;
    m_pUnicodeRadioBox = NULL;
    m_pDebugRadioBox = NULL;
    m_pStaticRadioBox = NULL;
////@end wxPackageWXBuildPropDlg member initialisation

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

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

void wxPackageWXBuildPropDlg::CreateControls()
{
    m_pLocation = new wxDirPickerCtrl(this, ID_WXBUILDPROP_LOCATION);

////@begin wxPackageWXBuildPropDlg content construction
    wxPackageWXBuildPropDlg* itemDialog1 = this;

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

    wxStaticText* itemStaticText3 = new wxStaticText( itemDialog1, wxID_STATIC, _("Location of this wxWidgets build:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText3, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxWindow* itemWindow4 = (wxWindow*) FindWindow(ID_WXBUILDPROP_LOCATION);
    wxASSERT( itemWindow4 != NULL );
    itemBoxSizer2->Add(itemWindow4, 0, wxGROW|wxALL, 5);

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

    wxStaticText* itemStaticText6 = new wxStaticText( itemDialog1, wxID_STATIC, _("Port:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer5->Add(itemStaticText6, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pPortChoiceStrings = NULL;
    m_pPortChoice = new wxChoice( itemDialog1, ID_WXBUILDPROP_PORT, wxDefaultPosition, wxDefaultSize, 0, m_pPortChoiceStrings, 0 );
    itemBoxSizer5->Add(m_pPortChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Version:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer5->Add(itemStaticText8, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pVersionChoiceStrings = NULL;
    m_pVersionChoice = new wxChoice( itemDialog1, ID_WXBUILDPROP_VERSION, wxDefaultPosition, wxDefaultSize, 0, m_pVersionChoiceStrings, 0 );
    itemBoxSizer5->Add(m_pVersionChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

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

    wxStaticText* itemStaticText11 = new wxStaticText( itemDialog1, wxID_STATIC, _("Compiler used to build it:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer10->Add(itemStaticText11, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pCompilerChoiceStrings = NULL;
    m_pCompilerChoice = new wxChoice( itemDialog1, ID_WXBUILDPROP_COMPILER, wxDefaultPosition, wxDefaultSize, 0, m_pCompilerChoiceStrings, 0 );
    itemBoxSizer10->Add(m_pCompilerChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText13 = new wxStaticText( itemDialog1, wxID_STATIC, _("Main build options:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText13, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxBoxSizer* itemBoxSizer14 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer14, 0, wxALIGN_CENTER_HORIZONTAL, 5);

    wxString m_pUnicodeRadioBoxStrings[] = {
        _("Unicode"),
        _("ANSI")
    };
    m_pUnicodeRadioBox = new wxRadioBox( itemDialog1, ID_WXBUILDPROP_UNICODE, _T(""), wxDefaultPosition, wxDefaultSize, 2, m_pUnicodeRadioBoxStrings, 1, wxRA_SPECIFY_ROWS );
    m_pUnicodeRadioBox->SetSelection(0);
    itemBoxSizer14->Add(m_pUnicodeRadioBox, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxString m_pDebugRadioBoxStrings[] = {
        _("Debug"),
        _("Release")
    };
    m_pDebugRadioBox = new wxRadioBox( itemDialog1, ID_WXBUILDPROP_DEBUG, _T(""), wxDefaultPosition, wxDefaultSize, 2, m_pDebugRadioBoxStrings, 1, wxRA_SPECIFY_ROWS );
    m_pDebugRadioBox->SetSelection(0);
    itemBoxSizer14->Add(m_pDebugRadioBox, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxString m_pStaticRadioBoxStrings[] = {
        _("&Static"),
        _("&DLL")
    };
    m_pStaticRadioBox = new wxRadioBox( itemDialog1, ID_WXBUILDPROP_STATIC, _T(""), wxDefaultPosition, wxDefaultSize, 2, m_pStaticRadioBoxStrings, 1, wxRA_SPECIFY_ROWS );
    m_pStaticRadioBox->SetSelection(0);
    itemBoxSizer14->Add(m_pStaticRadioBox, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

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

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

    wxButton* itemButton20 = new wxButton( itemDialog1, ID_WXBUILDPROP_AUTODETECT, _("Autodetect..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer19->Add(itemButton20, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    itemBoxSizer19->Add(5, 5, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton22 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer19->Add(itemButton22, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton23 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer19->Add(itemButton23, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end wxPackageWXBuildPropDlg content construction


    // setup controls

    wxString versions[] = {
        _("2.6.0"),
        _("2.6.1"),
        _("2.6.2"),
        _("2.6.3"),
        _("2.6.4"),
        _("2.7.0"),
        _("2.7.1"),
        _("2.7.2"),
        _("2.8.0"),
        _("2.8.1"),
        _("2.8.2"),
        _("2.8.3"),
        _("2.8.4"),
        _("2.9.0"),
        _("2.9.1")
    };

    m_pVersionChoice->Append(wxArrayString(WXSIZEOF(versions), versions));
    m_pCompilerChoice->Append(wxArrayString(wxPCF_MAX, wxPackageCompilerFormatName));

    wxPlatformInfo pi;
    wxPortId thisport = pi.GetPortId();
    for (size_t i=0; i < wxPORT_MAX; i++)
    {
        wxPortId current = (wxPortId)(1 << i);

        // is this port usable on the running platform ?
        // if not it does not make sense to add it here as it's not possible
        // that the user built that wxport on this platform...
        if (g_supportedPorts & current)
        {
            int last = m_pPortChoice->Append(wxPlatformInfo::GetPortIdName(current, false));
            if (current == thisport)
                m_pPortChoice->SetSelection(last);
        }
    }

    wxASSERT(m_pPortChoice->GetSelection() != wxNOT_FOUND);

    m_pLocation->SetPath(wxGetCwd());

    // select the first available items
    m_pCompilerChoice->SetSelection(0);
    m_pVersionChoice->SetSelection(0);
}

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

void wxPackageWXBuildPropDlg::SetBuild(const wxWidgetsBuild &build)
{
    m_pLocation->SetPath(build.GetLocation());

    // update selections
    m_pPortChoice->SetStringSelection(wxPlatformInfo::GetPortIdName(build.GetPortId(), false));
    m_pVersionChoice->SetStringSelection(build.GetVersion());
    m_pCompilerChoice->SetStringSelection(wxPackageCompilerFormat2String(build.GetCompilerUsed()));

    // get defaults for the radio boxes from 'wx' option preset
    m_pUnicodeRadioBox->SetSelection(build.IsUnicodeBuild() ? 0 : 1);
    m_pDebugRadioBox->SetSelection(build.IsDebugBuild() ? 0 : 1);
    m_pStaticRadioBox->SetSelection(build.IsStaticBuild() ? 0 : 1);
}

wxWidgetsBuild wxPackageWXBuildPropDlg::GetBuild() const
{
    wxWidgetsBuild ret;

    ret.SetCompilerUsed(
        wxIdx2PackageCompilerFormat(m_pCompilerChoice->GetSelection()));
    ret.SetPortId(wxPlatformInfo::GetPortId(m_pPortChoice->GetStringSelection()));

    ret.SetLocation(m_pLocation->GetPath());
    ret.SetVersion(m_pVersionChoice->GetStringSelection());

    ret.SetBuildProp(m_pStaticRadioBox->GetSelection() == 0,
                     m_pDebugRadioBox->GetSelection() == 0,
                     m_pUnicodeRadioBox->GetSelection() == 0);

    return ret;
}


// ----------------------------------------------------------------------------
// wxPackageWXBuildPropDlg - event handlers
// ----------------------------------------------------------------------------

void wxPackageWXBuildPropDlg::OnOK(wxCommandEvent &ev)
{
    // let default handler process this
    ev.Skip();
}

void wxPackageWXBuildPropDlg::OnAutodetect(wxCommandEvent &ev)
{
    wxMessageBox(wxT("Not implemented yet! Sorry"));
}

