/////////////////////////////////////////////////////////////////////////////
// Name:        settingsdlg.cpp
// Purpose:     wxPackageManagerSettingsDlg
// Author:      Francesco Montorsi
// Modified by: 
// Created:     25/07/2006 17:27:47
// RCS-ID:      $Id: settingsdlg.cpp,v 1.6 2007/02/01 20:28:15 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 "wx/config.h"
#include "wx/bookctrl.h"
#include "wx/filepicker.h"
#include "wx/stdpaths.h"

#include "guipm/settingsdlg.h"
#include "guipm/presetspanel.h"
#include "guipm/viewerpanel.h"
#include "guipm/browsepanel.h"
#include "guipm/compilerpanel.h"
#include "guipm/wxbuildpropdlg.h"
#include "guipm/smartmsgdlg.h"

#include "wxp/wxp.h"
#include "wxp/package.h"


// ----------------------------------------------------------------------------
// wxPackageManagerSettingsDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageManagerSettingsDlg, wxPropertySheetDialog )
BEGIN_EVENT_TABLE( wxPackageManagerSettingsDlg, wxPropertySheetDialog )

    // wxwidgets build panel events
    EVT_BUTTON( ID_SETTINGS_WX_ADDNEW, 
                wxPackageManagerSettingsDlg::OnWxBuildAddNew )
    EVT_BUTTON( ID_SETTINGS_WX_EDIT, 
                wxPackageManagerSettingsDlg::OnWxBuildEdit )
    EVT_BUTTON( ID_SETTINGS_WX_DELETE, 
                wxPackageManagerSettingsDlg::OnWxBuildDelete )
    EVT_BUTTON( ID_SETTINGS_WX_AUTODETECT,
                wxPackageManagerSettingsDlg::OnWxBuildAutodetect ) 
    EVT_LISTBOX_DCLICK( ID_SETTINGS_LIST,
                wxPackageManagerSettingsDlg::OnWxBuildEdit ) 

    // local repository panel events
    EVT_BUTTON( ID_SETTINGS_LOCALREPO_ADDPATH, 
                wxPackageManagerSettingsDlg::OnLocalRepoAddPath )
    EVT_BUTTON( ID_SETTINGS_LOCALREPO_EDITPATH, 
                wxPackageManagerSettingsDlg::OnLocalRepoEditPath )
    EVT_BUTTON( ID_SETTINGS_LOCALREPO_OPENPATH,
                wxPackageManagerSettingsDlg::OnLocalRepoOpenPath )
    EVT_BUTTON( ID_SETTINGS_LOCALREPO_DELETEPATH, 
                wxPackageManagerSettingsDlg::OnLocalRepoDeletePath )
    EVT_LISTBOX_DCLICK( ID_SETTINGS_LOCALREPO_PATHS,
                wxPackageManagerSettingsDlg::OnLocalRepoEditPath ) 

    // misc
    EVT_BUTTON( wxID_OK, wxPackageManagerSettingsDlg::OnOK )
    EVT_UPDATE_UI( wxID_ANY, wxPackageManagerSettingsDlg::OnUpdateUI )

END_EVENT_TABLE()

wxPackageManagerSettingsDlg::wxPackageManagerSettingsDlg( )
{
}

wxPackageManagerSettingsDlg::wxPackageManagerSettingsDlg( wxWindow* parent, wxWindowID id, wxConfigBase *cfg, const wxString &path, wxPackageViewerPanel *vp, wxPackageBrowserPanel *bp )
{
    Create(parent, id, cfg, path, vp, bp);
}

bool wxPackageManagerSettingsDlg::Create( wxWindow* parent, wxWindowID id, wxConfigBase *cfg, const wxString &path, wxPackageViewerPanel *vp, wxPackageBrowserPanel *bp )
{
////@begin wxPackageManagerSettingsDlg member initialisation
    m_pViewMode = NULL;
    m_pLoadWxDocs = NULL;
    m_pAutoLoadWxPackages = NULL;
    m_pDownloadPicker = NULL;
    m_pDecompressionPicker = NULL;
    m_pInstallationPicker = NULL;
    m_pQuestionList = NULL;
    m_pPathList = NULL;
    m_pWxBuildList = NULL;
////@end wxPackageManagerSettingsDlg member initialisation

    // init objects whose settings will be updated through this dialog
    m_pCfg = cfg;
    m_strPath = path;
    m_pViewerPanel = vp;
    m_pBrowserPanel = bp;

    SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
    wxPropertySheetDialog::Create( parent, id, wxT("wxWidgets Package Manager Settings"),
                                    wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
    CreateButtons(wxOK|wxCANCEL|wxHELP);
    CreateControls();
    LayoutDialog();
    Centre();
    return true;
}

void wxPackageManagerSettingsDlg::CreateControls()
{    
////@begin wxPackageManagerSettingsDlg content construction

    wxPanel* itemPanel2 = new wxPanel( GetBookCtrl(), wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
    itemPanel2->SetSizer(itemBoxSizer3);

    wxStaticBox* itemStaticBoxSizer4Static = new wxStaticBox(itemPanel2, wxID_ANY, _("Miscellaneous"));
    wxStaticBoxSizer* itemStaticBoxSizer4 = new wxStaticBoxSizer(itemStaticBoxSizer4Static, wxVERTICAL);
    itemBoxSizer3->Add(itemStaticBoxSizer4, 0, wxGROW|wxALL, 5);
    wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    itemStaticBoxSizer4->Add(itemBoxSizer5, 0, wxGROW, 5);
    wxStaticText* itemStaticText6 = new wxStaticText( itemPanel2, wxID_STATIC, _("View mode for package summary and details:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer5->Add(itemStaticText6, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxString* m_pViewModeStrings = NULL;
    m_pViewMode = new wxChoice( itemPanel2, ID_SETTINGS_VIEWMODE, wxDefaultPosition, wxDefaultSize, 0, m_pViewModeStrings, 0 );
    itemBoxSizer5->Add(m_pViewMode, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_pLoadWxDocs = new wxCheckBox( itemPanel2, ID_SETTINGS_LOADWXDOC, _("Load wxWidgets manual in the 'Documentation' tab"), wxDefaultPosition, wxDefaultSize, 0 );
    m_pLoadWxDocs->SetValue(false);
    itemStaticBoxSizer4->Add(m_pLoadWxDocs, 0, wxGROW|wxALL, 5);

    m_pAutoLoadWxPackages = new wxCheckBox( itemPanel2, ID_SETTINGS_AUTOLOAD_WXPACKAGES, _("Load local packages at startup"), wxDefaultPosition, wxDefaultSize, 0 );
    m_pAutoLoadWxPackages->SetValue(true);
    itemStaticBoxSizer4->Add(m_pAutoLoadWxPackages, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);

    wxStaticBox* itemStaticBoxSizer10Static = new wxStaticBox(itemPanel2, wxID_ANY, _("Directories"));
    wxStaticBoxSizer* itemStaticBoxSizer10 = new wxStaticBoxSizer(itemStaticBoxSizer10Static, wxVERTICAL);
    itemBoxSizer3->Add(itemStaticBoxSizer10, 0, wxGROW|wxALL, 5);
    wxFlexGridSizer* itemFlexGridSizer11 = new wxFlexGridSizer(3, 2, 0, 0);
    itemFlexGridSizer11->AddGrowableRow(0);
    itemFlexGridSizer11->AddGrowableRow(1);
    itemFlexGridSizer11->AddGrowableRow(2);
    itemFlexGridSizer11->AddGrowableCol(1);
    itemStaticBoxSizer10->Add(itemFlexGridSizer11, 0, wxGROW, 5);
    wxStaticText* itemStaticText12 = new wxStaticText( itemPanel2, wxID_STATIC, _("Default download folder:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(itemStaticText12, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pDownloadPicker = new wxDirPickerCtrl( itemPanel2, ID_SETTINGS_DOWNLOAD_PICKER );
    itemFlexGridSizer11->Add(m_pDownloadPicker, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText14 = new wxStaticText( itemPanel2, wxID_STATIC, _("Default decompression folder:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(itemStaticText14, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pDecompressionPicker = new wxDirPickerCtrl( itemPanel2, ID_SETTINGS_DOWNLOAD_PICKER );
    itemFlexGridSizer11->Add(m_pDecompressionPicker, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText16 = new wxStaticText( itemPanel2, wxID_STATIC, _("Default installation folder:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(itemStaticText16, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pInstallationPicker = new wxDirPickerCtrl( itemPanel2, ID_SETTINGS_DOWNLOAD_PICKER );
    itemFlexGridSizer11->Add(m_pInstallationPicker, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticBox* itemStaticBoxSizer18Static = new wxStaticBox(itemPanel2, wxID_ANY, _("Questions"));
    wxStaticBoxSizer* itemStaticBoxSizer18 = new wxStaticBoxSizer(itemStaticBoxSizer18Static, wxVERTICAL);
    itemBoxSizer3->Add(itemStaticBoxSizer18, 1, wxGROW|wxALL, 5);
    wxString* m_pQuestionListStrings = NULL;
    m_pQuestionList = new wxCheckListBox( itemPanel2, ID_SETTINGS_QUESTIONS_LIST, wxDefaultPosition, wxDefaultSize, 0, m_pQuestionListStrings, wxLB_SINGLE|wxLB_NEEDED_SB );
    itemStaticBoxSizer18->Add(m_pQuestionList, 1, wxGROW|wxALL, 5);

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

    GetBookCtrl()->AddPage(itemPanel2, _("General"));

    wxPanel* itemPanel21 = new wxPanel( GetBookCtrl(), wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer22 = new wxBoxSizer(wxVERTICAL);
    itemPanel21->SetSizer(itemBoxSizer22);

    wxStaticText* itemStaticText23 = new wxStaticText( itemPanel21, wxID_STATIC, _("The following directories are scanned automatically for WXZ \npackages and the results are listed as 'local packages':"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer22->Add(itemStaticText23, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 5);

    wxBoxSizer* itemBoxSizer24 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer22->Add(itemBoxSizer24, 1, wxGROW, 5);
    wxString* m_pPathListStrings = NULL;
    m_pPathList = new wxListBox( itemPanel21, ID_SETTINGS_LOCALREPO_PATHS, wxDefaultPosition, wxDefaultSize, 0, m_pPathListStrings, wxLB_SINGLE|wxLB_HSCROLL );
    itemBoxSizer24->Add(m_pPathList, 1, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer26 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer24->Add(itemBoxSizer26, 0, wxALIGN_BOTTOM, 5);
    wxButton* itemButton27 = new wxButton( itemPanel21, ID_SETTINGS_LOCALREPO_ADDPATH, _("Add new..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer26->Add(itemButton27, 0, wxGROW|wxALL, 5);

    wxButton* itemButton28 = new wxButton( itemPanel21, ID_SETTINGS_LOCALREPO_EDITPATH, _("Edit..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer26->Add(itemButton28, 0, wxGROW|wxALL, 5);

    wxButton* itemButton29 = new wxButton( itemPanel21, ID_SETTINGS_LOCALREPO_OPENPATH, _("Open..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer26->Add(itemButton29, 0, wxGROW|wxALL, 5);

    wxButton* itemButton30 = new wxButton( itemPanel21, ID_SETTINGS_LOCALREPO_DELETEPATH, _("Delete"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer26->Add(itemButton30, 0, wxGROW|wxALL, 5);

    GetBookCtrl()->AddPage(itemPanel21, _("Local repositories"));

    wxPanel* itemPanel31 = new wxPanel( GetBookCtrl(), wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
    wxBoxSizer* itemBoxSizer32 = new wxBoxSizer(wxVERTICAL);
    itemPanel31->SetSizer(itemBoxSizer32);

    wxStaticText* itemStaticText33 = new wxStaticText( itemPanel31, wxID_STATIC, _("Available wxWidgets builds:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer32->Add(itemStaticText33, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxBoxSizer* itemBoxSizer34 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer32->Add(itemBoxSizer34, 1, wxGROW, 5);
    wxString* m_pWxBuildListStrings = NULL;
    m_pWxBuildList = new wxListBox( itemPanel31, ID_SETTINGS_LIST, wxDefaultPosition, wxDefaultSize, 0, m_pWxBuildListStrings, wxLB_SINGLE|wxLB_HSCROLL );
    itemBoxSizer34->Add(m_pWxBuildList, 1, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer36 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer34->Add(itemBoxSizer36, 0, wxALIGN_BOTTOM, 5);
    wxButton* itemButton37 = new wxButton( itemPanel31, ID_SETTINGS_WX_ADDNEW, _("Add new..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer36->Add(itemButton37, 0, wxGROW|wxALL, 5);

    wxButton* itemButton38 = new wxButton( itemPanel31, ID_SETTINGS_WX_EDIT, _("Edit..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer36->Add(itemButton38, 0, wxGROW|wxALL, 5);

    wxButton* itemButton39 = new wxButton( itemPanel31, ID_SETTINGS_WX_DELETE, _("Delete"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer36->Add(itemButton39, 0, wxGROW|wxALL, 5);

    wxButton* itemButton40 = new wxButton( itemPanel31, ID_SETTINGS_WX_AUTODETECT, _("Autodetect..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer36->Add(itemButton40, 0, wxGROW|wxALL, 5);

    GetBookCtrl()->AddPage(itemPanel31, _("wxWidgets"));

////@end wxPackageManagerSettingsDlg content construction

    // init view mode list
    const wxPackageHTMLOutputFormatHashMap &h = wxPackageInfo::s_hashmapHTMLFormats;
    for (wxPackageHTMLOutputFormatHashMap::const_iterator it = h.begin();
         it != h.end(); it++)
    {
        const wxPackageHTMLOutputFormat &fmt = it->second;

        // show to the user only those viewformats coinceived to the used with wxHtmlWindow!
        if (fmt.GetContext() == wxPSIC_WXHTML)
            m_pViewMode->Append(fmt.GetName());
    }

    // init wxWidgets list property panel
    m_arrBuilds = wxWidgetsBuildArray::s_arrWxBuilds;
    for (size_t i=0; i < m_arrBuilds.GetCount(); i++)
        m_pWxBuildList->Append(m_arrBuilds[i].GetUserFriendlyName());

    // add a static text above the preset panel
    wxPanel *panel = new wxPanel(GetBookCtrl(), wxID_ANY);
    wxSizer *sz = new wxBoxSizer(wxVERTICAL);
    sz->Add(new wxStaticText(panel, wxID_ANY, 
                             wxT("Here you can edit the default values of the set of standard options.\nNote that are shown all options for all compilers and platforms.")), 0, wxGROW|wxALL, 5);
    m_pPresetsPanel = new wxPackagePresetsPanel(panel, wxID_ANY);
    sz->Add(m_pPresetsPanel, 1, wxGROW|wxALL, 5);
    panel->SetSizer(sz);
    sz->SetSizeHints(panel);

    // build subpanels
    m_pCompilerOptPanel = new wxCompilerSettingsPanel(GetBookCtrl(), wxID_ANY);
    m_pCompilerOptPanel->InitAsGlobal();

    GetBookCtrl()->AddPage(panel, _("Options"));
    GetBookCtrl()->AddPage(m_pCompilerOptPanel, _("Compiler"));
}

bool wxPackageManagerSettingsDlg::LoadSettings()
{
    if (m_pViewerPanel)
        m_pLoadWxDocs->SetValue(
                m_pViewerPanel->GetDocs().Index(wxT("wx.htb")) != wxNOT_FOUND);
    if (m_pViewerPanel)
    {
        wxString name = m_pViewerPanel->GetViewModeName();

        // see how m_pViewMode listbox is filled above...
        wxASSERT_MSG(m_pViewMode->FindString(name) != wxNOT_FOUND,
                     wxT("maybe the viewformat in use is not compatible with wxHtmlWindow??"));

        m_pViewMode->SetStringSelection(name);
    }

    m_pDownloadPicker->SetPath(m_pBrowserPanel->GetDefaultDownloadPath());
    m_pDecompressionPicker->SetPath(m_pBrowserPanel->GetDefaultDecompressionPath());
    m_pInstallationPicker->SetPath(m_pBrowserPanel->GetDefaultInstallationPath());
    m_pAutoLoadWxPackages->SetValue(m_pBrowserPanel->IsAutoLoadLocalPackagesEnabled());

    m_pPathList->Clear();
    m_pPathList->Append(wxPackage::s_arrLocalRepo);

    // load questions and their "do not ask again" status
    wxStringHashMap::iterator it;
    for( it = wxSmartMessageDialog::s_hashDesc.begin(); 
         it != wxSmartMessageDialog::s_hashDesc.end(); ++it )
    {
        wxString key = it->first, desc = it->second;

        // NB: we can't use client data with a wxCheckListBox so we rather
        //     use an internal array...
        m_questionListKeynames.Add(key);

        int last = m_pQuestionList->Append(desc);
        m_pQuestionList->Check(last, wxSmartMessageDialog::GetValueFor(key));
    }

    return true;
}

void wxPackageManagerSettingsDlg::ApplySettings()
{
    m_pPresetsPanel->ApplySettings();

    // apply settings to the linked windows

    m_pViewerPanel->SetViewMode(m_pViewMode->GetStringSelection().MakeLower());

    bool wxdocs = m_pViewerPanel->GetDocs().Index(wxT("wx.htb")) != wxNOT_FOUND;
    if (m_pLoadWxDocs->GetValue())
        if (wxdocs)
            m_pViewerPanel->GetDocs().Remove(wxT("wx.htb"));
    else
        if (!wxdocs)
            m_pViewerPanel->GetDocs().Add(wxT("wx.htb"));

    m_pBrowserPanel->SetDefaultDownloadPath(m_pDownloadPicker->GetPath());
    m_pBrowserPanel->SetDefaultDecompressionPath(m_pDecompressionPicker->GetPath());
    m_pBrowserPanel->SetDefaultInstallationPath(m_pInstallationPicker->GetPath());
    m_pBrowserPanel->SetAutoLoadLocalPackages(m_pAutoLoadWxPackages->GetValue());

    wxPackage::s_arrLocalRepo = m_pPathList->GetStrings();

    // compiler paths could have changed - update all build-system options
    // in all loaded packages
    m_pBrowserPanel->GetLocalPackages().InvalidateCachedSubstHashMap();
    m_pBrowserPanel->GetNewPackages().InvalidateCachedSubstHashMap();

    // update also global list of wxwidgets builds
    wxWidgetsBuildArray::s_arrWxBuilds = m_arrBuilds;

    // save "do not ask again" status of questions
    for (size_t i=0; i<m_pQuestionList->GetCount(); i++)
        wxSmartMessageDialog::SetValueFor(m_questionListKeynames[i],
                                          m_pQuestionList->IsChecked(i));

    // update global selected compiler
    wxCompilerSettings::SetDefaultFormat(m_pCompilerOptPanel->GetSelCompilerFormat());
}




//----------------------------------------------------------------------------
// wxPackageManagerSettingsDlg - event handlers
//----------------------------------------------------------------------------

void wxPackageManagerSettingsDlg::OnLocalRepoAddPath(wxCommandEvent &)
{
    wxDirDialog dlg(this);
    if (dlg.ShowModal() == wxID_OK)
        m_pPathList->SetSelection(m_pPathList->Append(dlg.GetPath()));
}

void wxPackageManagerSettingsDlg::OnLocalRepoDeletePath(wxCommandEvent &)
{
    m_pPathList->Delete(m_pPathList->GetSelection());
}

void wxPackageManagerSettingsDlg::OnLocalRepoEditPath(wxCommandEvent &)
{
    wxDirDialog dlg(this);

    dlg.SetPath(m_pPathList->GetStringSelection());
    if (dlg.ShowModal() == wxID_OK)
        m_pPathList->SetString(m_pPathList->GetSelection(), dlg.GetPath());
}

void wxPackageManagerSettingsDlg::OnLocalRepoOpenPath(wxCommandEvent &)
{
#ifdef __WXGTK__
    wxFileName fn = wxFileName::DirName(m_pPathList->GetStringSelection());
    wxLaunchDefaultBrowser(wxFileSystem::FileNameToURL(fn));
#elif defined(__WXMSW__)
    wxExecute(wxT("explorer ") + m_pPathList->GetStringSelection());
#endif
}

void wxPackageManagerSettingsDlg::OnWxBuildAddNew(wxCommandEvent &)
{
    wxPackageWXBuildPropDlg dlg(this);
    if (dlg.ShowModal() == wxID_OK)
    {
        wxWidgetsBuild b(dlg.GetBuild());

        m_pWxBuildList->SetSelection(
            m_pWxBuildList->Append(b.GetUserFriendlyName()));
        m_arrBuilds.Add(b);
    }
}

void wxPackageManagerSettingsDlg::OnWxBuildEdit(wxCommandEvent &)
{
    size_t n = m_pWxBuildList->GetSelection();

    wxPackageWXBuildPropDlg dlg(this);
    dlg.SetBuild(m_arrBuilds[n]);
    if (dlg.ShowModal() == wxID_OK)
    {
        wxWidgetsBuild b(dlg.GetBuild());
        m_pWxBuildList->SetString(n, b.GetUserFriendlyName());
        m_arrBuilds[n] = b;
    }
}

void wxPackageManagerSettingsDlg::OnWxBuildDelete(wxCommandEvent &)
{
    size_t n = m_pWxBuildList->GetSelection();

    m_arrBuilds.RemoveAt(n);
    m_pWxBuildList->Delete(n);
}

void wxPackageManagerSettingsDlg::OnWxBuildAutodetect(wxCommandEvent &)
{
    // TODO
}

void wxPackageManagerSettingsDlg::OnOK(wxCommandEvent &event)
{
    wxString downloadpath = m_pDownloadPicker->GetPath();

    // should this event be 'vetoed' ?
    if (m_pPathList->FindString(downloadpath) == wxNOT_FOUND)
    {
        int reply = wxMessageBox(wxString::Format(
            _("The directory currently chosen as destination for package downloads is not listed among the directories scanned for packages (see the 'Local repositories' tab). This means that when packages are downloaded, they won't show in the 'Local packages' tab panel.\n\nDo you want to add '%s' in the directories used as local repositories ?"),
            downloadpath.c_str()), wxT("Warning"), 
            wxICON_EXCLAMATION|wxYES_NO|wxCANCEL);

        switch (reply)
        {
        case wxYES:
            m_pPathList->Append(downloadpath);
            event.Skip();
            break;

        case wxNO:
            event.Skip();   // close this window and leave it as is
            break;

        case wxCANCEL:

            // do not close this window
            return;
        }
    }

    // do default processing
    event.Skip();
}

void wxPackageManagerSettingsDlg::OnUpdateUI(wxUpdateUIEvent &)
{
    bool toenable;
    
    toenable = m_pPathList->GetSelection() != wxNOT_FOUND;
    FindWindowById(ID_SETTINGS_LOCALREPO_EDITPATH, this)->Enable(toenable);
    FindWindowById(ID_SETTINGS_LOCALREPO_OPENPATH, this)->Enable(toenable);
    FindWindowById(ID_SETTINGS_LOCALREPO_DELETEPATH, this)->Enable(toenable);

    toenable = m_pWxBuildList->GetSelection() != wxNOT_FOUND;
    FindWindowById(ID_SETTINGS_WX_EDIT, this)->Enable(toenable);
    FindWindowById(ID_SETTINGS_WX_DELETE, this)->Enable(toenable);
}

