/////////////////////////////////////////////////////////////////////////////
// Name:        dialog_eeschema_config.cpp
// Purpose:     
// Author:      jean-pierre Charras
// Modified by: 
// Created:     17/02/2006 21:14:46
// RCS-ID:      
// Copyright:   License GNU
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// Generated by DialogBlocks (unregistered), 17/02/2006 21:14:46

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "dialog_eeschema_config.h"
#endif

// 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

////@begin includes
////@end includes
#include "fctsys.h"

#include "common.h"
#include "program.h"
#include "libcmp.h"
#include "general.h"

#include "protos.h"
#include "netlist.h"

#include "id.h"

#include "dialog_eeschema_config.h"

////@begin XPM images
////@end XPM images

/******************************************************************/
void WinEDA_SchematicFrame::InstallConfigFrame(const wxPoint & pos)
/******************************************************************/
{
	KiConfigEeschemaFrame * CfgFrame = new KiConfigEeschemaFrame(this);
	CfgFrame->ShowModal(); CfgFrame->Destroy();
}


/*!
 * KiConfigEeschemaFrame type definition
 */

IMPLEMENT_DYNAMIC_CLASS( KiConfigEeschemaFrame, wxDialog )

/*!
 * KiConfigEeschemaFrame event table definition
 */

BEGIN_EVENT_TABLE( KiConfigEeschemaFrame, wxDialog )

////@begin KiConfigEeschemaFrame event table entries
    EVT_CLOSE( KiConfigEeschemaFrame::OnCloseWindow )

    EVT_BUTTON( SAVE_CFG, KiConfigEeschemaFrame::OnSaveCfgClick )

    EVT_RADIOBOX( FORMAT_NETLIST, KiConfigEeschemaFrame::OnFormatNetlistSelected )

    EVT_BUTTON( DEL_LIB, KiConfigEeschemaFrame::OnDelLibClick )

    EVT_BUTTON( ADD_LIB, KiConfigEeschemaFrame::OnAddLibClick )

    EVT_BUTTON( INSERT_LIB, KiConfigEeschemaFrame::OnInsertLibClick )

    EVT_BUTTON( ID_LIB_PATH_SEL, KiConfigEeschemaFrame::OnLibPathSelClick )

////@end KiConfigEeschemaFrame event table entries

END_EVENT_TABLE()

/*!
 * KiConfigEeschemaFrame constructors
 */

KiConfigEeschemaFrame::KiConfigEeschemaFrame( )
{
}

KiConfigEeschemaFrame::KiConfigEeschemaFrame( WinEDA_SchematicFrame* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
wxString msg;

	m_Parent = parent;
	m_LibListChanged = FALSE;

    Create(parent, id, caption, pos, size, style);
	
	msg = _("from ") + EDA_Appl->m_CurrentOptionFile;
	SetTitle(msg);
	SetFormatsNetListes();
	m_ListLibr->InsertItems(g_LibName_List, 0);
	m_LibDirCtrl->SetValue( g_UserLibDirBuffer );
}

/*!
 * KiConfigEeschemaFrame creator
 */

bool KiConfigEeschemaFrame::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin KiConfigEeschemaFrame member initialisation
    m_NetFormatBox = NULL;
    m_FileExtList = NULL;
    m_ListLibr = NULL;
    m_LibDirCtrl = NULL;
////@end KiConfigEeschemaFrame member initialisation

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

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

/*!
 * Control creation for KiConfigEeschemaFrame
 */

void KiConfigEeschemaFrame::CreateControls()
{    
	SetFont(*g_DialogFont);

////@begin KiConfigEeschemaFrame content construction
    // Generated by DialogBlocks, 28/02/2007 15:16:31 (unregistered)

    KiConfigEeschemaFrame* itemDialog1 = this;

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

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

    wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer3->Add(itemBoxSizer4, 0, wxGROW|wxALL, 5);

    wxButton* itemButton5 = new wxButton( itemDialog1, SAVE_CFG, _("Save Cfg"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton5->SetForegroundColour(wxColour(204, 0, 0));
    itemBoxSizer4->Add(itemButton5, 0, wxGROW|wxALL, 5);

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

    wxString m_NetFormatBoxStrings[] = {
        _("PcbNew"),
        _("&OrcadPcb2"),
        _("&CadStar"),
        _("&Spice"),
        _("Other")
    };
    m_NetFormatBox = new wxRadioBox( itemDialog1, FORMAT_NETLIST, _("NetList Formats:"), wxDefaultPosition, wxDefaultSize, 5, m_NetFormatBoxStrings, 1, wxRA_SPECIFY_COLS );
    m_NetFormatBox->SetSelection(0);
    itemBoxSizer4->Add(m_NetFormatBox, 0, wxGROW|wxALL, 5);

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

    wxStaticBox* itemStaticBoxSizer9Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Files ext:"));
    m_FileExtList = new wxStaticBoxSizer(itemStaticBoxSizer9Static, wxVERTICAL);
    itemBoxSizer4->Add(m_FileExtList, 0, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer10 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer3->Add(itemBoxSizer10, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);

    wxBoxSizer* itemBoxSizer11 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer10->Add(itemBoxSizer11, 0, wxALIGN_CENTER_HORIZONTAL|wxLEFT|wxRIGHT|wxBOTTOM, 5);

    wxBoxSizer* itemBoxSizer12 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer11->Add(itemBoxSizer12, 0, wxALIGN_CENTER_VERTICAL, 5);

    wxBoxSizer* itemBoxSizer13 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer12->Add(itemBoxSizer13, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxButton* itemButton14 = new wxButton( itemDialog1, DEL_LIB, _("Del"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton14->SetForegroundColour(wxColour(204, 0, 0));
    itemBoxSizer13->Add(itemButton14, 0, wxGROW|wxALL, 5);

    wxButton* itemButton15 = new wxButton( itemDialog1, ADD_LIB, _("Add"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton15->SetForegroundColour(wxColour(0, 128, 0));
    itemBoxSizer13->Add(itemButton15, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton16 = new wxButton( itemDialog1, INSERT_LIB, _("Ins"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton16->SetForegroundColour(wxColour(0, 0, 255));
    itemBoxSizer13->Add(itemButton16, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer17 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer12->Add(itemBoxSizer17, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText18 = new wxStaticText( itemDialog1, wxID_STATIC, _("Libraries"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticText18->SetForegroundColour(wxColour(204, 0, 0));
    itemBoxSizer17->Add(itemStaticText18, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);

    wxString* m_ListLibrStrings = NULL;
    m_ListLibr = new wxListBox( itemDialog1, ID_LIST_LIBS, wxDefaultPosition, wxSize(-1, 300), 0, m_ListLibrStrings, wxLB_SINGLE );
    itemBoxSizer17->Add(m_ListLibr, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);

    wxStaticBox* itemStaticBoxSizer20Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Library files path:"));
    wxStaticBoxSizer* itemStaticBoxSizer20 = new wxStaticBoxSizer(itemStaticBoxSizer20Static, wxHORIZONTAL);
    itemStaticBoxSizer20Static->SetForegroundColour(wxColour(206, 0, 0));
    itemBoxSizer2->Add(itemStaticBoxSizer20, 0, wxGROW|wxALL, 5);

    m_LibDirCtrl = new wxTextCtrl( itemDialog1, ID_TEXTCTRL, _T(""), wxDefaultPosition, wxSize(350, -1), 0 );
    itemStaticBoxSizer20->Add(m_LibDirCtrl, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);

    wxButton* itemButton22 = new wxButton( itemDialog1, ID_LIB_PATH_SEL, _("Browse"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticBoxSizer20->Add(itemButton22, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end KiConfigEeschemaFrame content construction

wxString msg = _("Cmp file Ext: ") + g_NetCmpExtBuffer;
    wxStaticText * text = new wxStaticText( itemDialog1, -1, msg, wxDefaultPosition, wxDefaultSize, 0 );
    m_FileExtList->Add(text, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);

	msg = _("Net file Ext: ") + g_NetExtBuffer;
    text = new wxStaticText( itemDialog1, -1, msg, wxDefaultPosition, wxDefaultSize, 0 );
    m_FileExtList->Add(text, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);

	msg = _("Library file Ext: ") + g_LibExtBuffer;
    text = new wxStaticText( itemDialog1, -1, msg, wxDefaultPosition, wxDefaultSize, 0 );
    m_FileExtList->Add(text, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);

	msg = _("Symbol file Ext: ") + g_SymbolExtBuffer;
    text = new wxStaticText( itemDialog1, -1, msg, wxDefaultPosition, wxDefaultSize, 0 );
    m_FileExtList->Add(text, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);

	msg = _("Schematic file Ext: ") + g_SchExtBuffer;
    text = new wxStaticText( itemDialog1, -1, msg, wxDefaultPosition, wxDefaultSize, 0 );
    m_FileExtList->Add(text, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxADJUST_MINSIZE, 5);
}

/*!
 * Should we show tooltips?
 */

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

/*!
 * Get bitmap resources
 */

wxBitmap KiConfigEeschemaFrame::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin KiConfigEeschemaFrame bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end KiConfigEeschemaFrame bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon KiConfigEeschemaFrame::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin KiConfigEeschemaFrame icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end KiConfigEeschemaFrame icon retrieval
}



/**************************************************************/
void KiConfigEeschemaFrame::OnCloseWindow(wxCloseEvent & event)
/**************************************************************/
{
	ChangeSetup();
	if ( m_LibListChanged )
	{
		LoadLibraries(m_Parent);
		if ( m_Parent->m_Parent->ViewlibFrame )
			m_Parent->m_Parent->ViewlibFrame->ReCreateListLib();
	}
	EndModal(0);
}


/*********************************************/
void KiConfigEeschemaFrame::ChangeSetup(void)
/*********************************************/
{
	g_UserLibDirBuffer = m_LibDirCtrl->GetValue();
	SetRealLibraryPath( wxT("library") );
}



/********************************************************/
void KiConfigEeschemaFrame::LibDelFct(wxCommandEvent& event)
/********************************************************/
{
int ii;

	ii = m_ListLibr->GetSelection();
	if ( ii < 0 ) return;

	g_LibName_List.RemoveAt(ii);
	m_ListLibr->Clear();
	m_ListLibr->InsertItems(g_LibName_List, 0);
	m_LibListChanged = TRUE;
}

/****************************************************************/
void KiConfigEeschemaFrame::AddOrInsertLibrary(wxCommandEvent& event)
/****************************************************************/
/* Insert or add a library to the existing library list:
	New library is put in list before (insert) or after (add)
	the selection
*/
{
int ii;
wxString FullLibName,ShortLibName, Mask;

	ii = m_ListLibr->GetSelection();
	if ( ii < 0 ) ii = 0;
	ChangeSetup();
	if( event.GetId() == ADD_LIB)
	{
		if( g_LibName_List.GetCount() != 0 ) ii ++;	/* Add after selection */
	}

	Mask = wxT("*") + g_LibExtBuffer;
	FullLibName = EDA_FileSelector( _("Library files:"),
				g_RealLibDirBuffer,		/* Chemin par defaut */
				wxEmptyString,					/* nom fichier par defaut */
				g_LibExtBuffer,		/* extension par defaut */
				Mask,				/* Masque d'affichage */
				this,
				wxFD_OPEN,
				TRUE
				);

	if ( FullLibName.IsEmpty() ) return;

	ShortLibName = MakeReducedFileName(FullLibName,g_RealLibDirBuffer,g_LibExtBuffer);

	//Add or insert new library name
	if (FindLibrary(ShortLibName) == NULL)
	{
		m_LibListChanged = TRUE;
		g_LibName_List.Insert(ShortLibName, ii);
		m_ListLibr->Clear();
		m_ListLibr->InsertItems(g_LibName_List, 0);
	}

	else DisplayError(this, _("Library already in use"));

}



/****************************************************/
void KiConfigEeschemaFrame::SetFormatsNetListes(void)
/****************************************************/
/* Adjust the m_NetFormatBox current selection, according to the current netlist format*/

{
	switch( g_NetFormat )
		{
		case NET_TYPE_NOT_INIT:
		case NET_TYPE_PCBNEW:
			m_NetFormatBox->SetSelection(0);
			break;

		case NET_TYPE_ORCADPCB2:
			m_NetFormatBox->SetSelection(1);
			break;

		case NET_TYPE_CADSTAR:
			m_NetFormatBox->SetSelection(2);
			break;

		case NET_TYPE_SPICE:
			m_NetFormatBox->SetSelection(3);
			break;

		default:
			m_NetFormatBox->SetSelection(4);
			break;
		}
}

/*!
 * wxEVT_COMMAND_RADIOBOX_SELECTED event handler for FORMAT_NETLIST
 */

void KiConfigEeschemaFrame::OnFormatNetlistSelected( wxCommandEvent& event )
{
int ii;

	ii = m_NetFormatBox->GetSelection();
	if ( ii == 0 ) g_NetFormat = NET_TYPE_PCBNEW;
	if ( ii == 1 ) g_NetFormat = NET_TYPE_ORCADPCB2;
	else if ( ii == 2 ) g_NetFormat = NET_TYPE_CADSTAR;
	else if ( ii == 3 ) g_NetFormat = NET_TYPE_SPICE;
	else if ( g_NetFormat < NET_TYPE_CUSTOM1 ) g_NetFormat = NET_TYPE_CUSTOM1;
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for DEL_LIB
 */

void KiConfigEeschemaFrame::OnDelLibClick( wxCommandEvent& event )
{
	LibDelFct(event);
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ADD_LIB
 */

void KiConfigEeschemaFrame::OnAddLibClick( wxCommandEvent& event )
{
	AddOrInsertLibrary(event);
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for INSERT_LIB
 */

void KiConfigEeschemaFrame::OnInsertLibClick( wxCommandEvent& event )
{
	AddOrInsertLibrary(event);
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for SAVE_CFG
 */

void KiConfigEeschemaFrame::OnSaveCfgClick( wxCommandEvent& event )
{
	ChangeSetup();
	m_Parent->Save_Config(this);
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_LIB_PATH_SEL
 */

void KiConfigEeschemaFrame::OnLibPathSelClick( wxCommandEvent& event )
{
wxString path = g_RealLibDirBuffer;
	
bool select = EDA_DirectorySelector(_(" Default Path for libraries"),		/* Titre de la fenetre */
					path,			/* Chemin par defaut */
					wxDD_DEFAULT_STYLE,
					this,	/* parent frame */
					wxDefaultPosition);

	if ( !select ) return;
	
	m_LibDirCtrl->SetValue(path);

}


