/////////////////////////////////////////////////////////////////////////////////
//
// This file is part of SACAMOS, cable models for EMI simulations in SPICE.
// It was developed by the University of Nottingham and the Netherlands Aerospace
// Centre (NLR) for ESA under contract number 4000112765/14/NL/HK.
//
// Copyright (C) 2015 - 2017 University of Nottingham
//
// SACAMOS is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// SACAMOS is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// A copy of the GNU General Public License version 3 can be found in the
// file GNU_GPL_v3 in the root or at <http://www.gnu.org/licenses/>.
//
// wxWidgets is currently licenced under the "wxWindows Library Licence".
// A copy of the wxWindows Library Licence, Version 3.1 can be found in the file
// wxWindows_Library_Licence_v3-1 in the root or at:
// <https://www.wxwidgets.org/about/licence/>
//
// The University of Nottingham can be contacted at: ggiemr@nottingham.ac.uk
//
// File Contents:
//
// NAME
//     FLEXCABLE.cpp
//
// DESCRIPTION
//     GUI for the construction of cable_spec file for cable type: Flexcable
//
// AUTHOR(S)
//     Steve Greedy
//
/////////////////////////////////////////////////////////////////////////////////
#include "FLEXCABLE.h"

//(*InternalHeaders(FLEXCABLE)
#include <wx/settings.h>
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(FLEXCABLE)
const long FLEXCABLE::ID_STATICTEXT1 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL1 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT16 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL13 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT2 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL2 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT3 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL3 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT4 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL4 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT5 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL5 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT6 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL6 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT17 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL14 = wxNewId();
const long FLEXCABLE::ID_PANEL2 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT12 = wxNewId();
const long FLEXCABLE::ID_TEXTCTRL12 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT13 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT14 = wxNewId();
const long FLEXCABLE::ID_STATICTEXT15 = wxNewId();
const long FLEXCABLE::ID_CHECKBOX1 = wxNewId();
const long FLEXCABLE::ID_PANEL3 = wxNewId();
const long FLEXCABLE::ID_NOTEBOOK1 = wxNewId();
const long FLEXCABLE::ID_BUTTON1 = wxNewId();
const long FLEXCABLE::ID_BUTTON2 = wxNewId();
const long FLEXCABLE::ID_BUTTON3 = wxNewId();
const long FLEXCABLE::ID_PANEL1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(FLEXCABLE,wxDialog)
	//(*EventTable(FLEXCABLE)
	//*)
END_EVENT_TABLE()

FLEXCABLE::FLEXCABLE(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(FLEXCABLE)
	wxBoxSizer* BoxSizer4;
	wxStaticBoxSizer* StaticBoxSizer2;
	wxFlexGridSizer* FlexGridSizer3;
	wxFlexGridSizer* FlexGridSizer5;
	wxFlexGridSizer* FlexGridSizer2;
	wxBoxSizer* BoxSizer2;
	wxBoxSizer* BoxSizer1;
	wxStaticBoxSizer* StaticBoxSizer1;
	wxFlexGridSizer* FlexGridSizer1;
	wxBoxSizer* BoxSizer3;

	Create(parent, wxID_ANY, _("SACAMOS: FLAXCABLE MODEL"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
	FlexGridSizer1 = new wxFlexGridSizer(0, 2, 0, 0);
	StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, this, _("Flex Cable Parameters"));
	FlexGridSizer2 = new wxFlexGridSizer(0, 1, 0, 0);
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Cabe Name"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
	BoxSizer1->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer1->Add(TextCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(BoxSizer1, 1, wxALL|wxEXPAND, 5);
	BoxSizer3 = new wxBoxSizer(wxVERTICAL);
	Notebook1 = new wxNotebook(this, ID_NOTEBOOK1, wxDefaultPosition, wxDefaultSize, 0, _T("ID_NOTEBOOK1"));
	Panel2 = new wxPanel(Notebook1, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL2"));
	FlexGridSizer3 = new wxFlexGridSizer(0, 2, 0, 0);
	StaticText16 = new wxStaticText(Panel2, ID_STATICTEXT16, _("Number of Conductors"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT16"));
	FlexGridSizer3->Add(StaticText16, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl13 = new wxTextCtrl(Panel2, ID_TEXTCTRL13, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL13"));
	FlexGridSizer3->Add(TextCtrl13, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText2 = new wxStaticText(Panel2, ID_STATICTEXT2, _("Conductor Width in x (m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
	FlexGridSizer3->Add(StaticText2, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl2 = new wxTextCtrl(Panel2, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
	FlexGridSizer3->Add(TextCtrl2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText3 = new wxStaticText(Panel2, ID_STATICTEXT3, _("Conductor Height in y (m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
	FlexGridSizer3->Add(StaticText3, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl3 = new wxTextCtrl(Panel2, ID_TEXTCTRL3, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL3"));
	FlexGridSizer3->Add(TextCtrl3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText4 = new wxStaticText(Panel2, ID_STATICTEXT4, _("Conductor Separation (m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
	FlexGridSizer3->Add(StaticText4, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl4 = new wxTextCtrl(Panel2, ID_TEXTCTRL4, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL4"));
	FlexGridSizer3->Add(TextCtrl4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText5 = new wxStaticText(Panel2, ID_STATICTEXT5, _("Dielectric Offset in x (m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT5"));
	FlexGridSizer3->Add(StaticText5, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl5 = new wxTextCtrl(Panel2, ID_TEXTCTRL5, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL5"));
	FlexGridSizer3->Add(TextCtrl5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText6 = new wxStaticText(Panel2, ID_STATICTEXT6, _("Dielectric Offset in y (m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT6"));
	FlexGridSizer3->Add(StaticText6, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl6 = new wxTextCtrl(Panel2, ID_TEXTCTRL6, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL6"));
	FlexGridSizer3->Add(TextCtrl6, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText17 = new wxStaticText(Panel2, ID_STATICTEXT17, _("Conductor Conductivity (S/m)"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT17"));
	FlexGridSizer3->Add(StaticText17, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl14 = new wxTextCtrl(Panel2, ID_TEXTCTRL14, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL14"));
	FlexGridSizer3->Add(TextCtrl14, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel2->SetSizer(FlexGridSizer3);
	FlexGridSizer3->Fit(Panel2);
	FlexGridSizer3->SetSizeHints(Panel2);
	Panel3 = new wxPanel(Notebook1, ID_PANEL3, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL3"));
	BoxSizer4 = new wxBoxSizer(wxVERTICAL);
	FlexGridSizer5 = new wxFlexGridSizer(0, 2, 0, 0);
	StaticText12 = new wxStaticText(Panel3, ID_STATICTEXT12, _("Dielectric Permittivity"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT12"));
	FlexGridSizer5->Add(StaticText12, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	TextCtrl12 = new wxTextCtrl(Panel3, ID_TEXTCTRL12, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL12"));
	FlexGridSizer5->Add(TextCtrl12, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
	StaticText13 = new wxStaticText(Panel3, ID_STATICTEXT13, _("Or"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT13"));
	FlexGridSizer5->Add(StaticText13, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText14 = new wxStaticText(Panel3, ID_STATICTEXT14, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT14"));
	FlexGridSizer5->Add(StaticText14, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText15 = new wxStaticText(Panel3, ID_STATICTEXT15, _("Frequency Dependent Model"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT15"));
	FlexGridSizer5->Add(StaticText15, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	CheckBox1 = new wxCheckBox(Panel3, ID_CHECKBOX1, _("Yes"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
	CheckBox1->SetValue(false);
	FlexGridSizer5->Add(CheckBox1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer4->Add(FlexGridSizer5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel3->SetSizer(BoxSizer4);
	BoxSizer4->Fit(Panel3);
	BoxSizer4->SetSizeHints(Panel3);
	Notebook1->AddPage(Panel2, _("General"), false);
	Notebook1->AddPage(Panel3, _("Material"), false);
	BoxSizer3->Add(Notebook1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(BoxSizer3, 0, wxALL|wxEXPAND, 5);
	StaticBoxSizer1->Add(FlexGridSizer2, 10, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
	Button1 = new wxButton(this, ID_BUTTON1, _("SAVE"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	BoxSizer2->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Button2 = new wxButton(this, ID_BUTTON2, _("BUILD"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
	BoxSizer2->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Button3 = new wxButton(this, ID_BUTTON3, _("CLOSE"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
	BoxSizer2->Add(Button3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBoxSizer1->Add(BoxSizer2, 1, wxALL|wxEXPAND, 5);
	FlexGridSizer1->Add(StaticBoxSizer1, 1, wxALL|wxEXPAND, 5);
	StaticBoxSizer2 = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Cross Section"));
	Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxSize(500,500), wxTAB_TRAVERSAL, _T("ID_PANEL1"));
	Panel1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
	Panel1->SetToolTip(_("Double Click to Reset Schematic"));
	StaticBoxSizer2->Add(Panel1, 1, wxALL|wxALIGN_TOP, 5);
	FlexGridSizer1->Add(StaticBoxSizer2, 1, wxALL|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
	SetSizer(FlexGridSizer1);
	FlexGridSizer1->Fit(this);
	FlexGridSizer1->SetSizeHints(this);

	Connect(ID_TEXTCTRL13,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl13Text);
	Connect(ID_TEXTCTRL2,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl2Text);
	Connect(ID_TEXTCTRL3,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl3Text);
	Connect(ID_TEXTCTRL4,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl4Text);
	Connect(ID_TEXTCTRL5,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl5Text);
	Connect(ID_TEXTCTRL6,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl6Text);
	Connect(ID_TEXTCTRL12,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&FLEXCABLE::OnTextCtrl12Text);
	Connect(ID_CHECKBOX1,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&FLEXCABLE::OnCheckBox1Click);
	Connect(ID_NOTEBOOK1,wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,(wxObjectEventFunction)&FLEXCABLE::OnNotebook1PageChanging);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&FLEXCABLE::OnButton1Click);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&FLEXCABLE::OnButton2Click);
	Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&FLEXCABLE::OnButton3Click);
	Panel1->Connect(wxEVT_PAINT,(wxObjectEventFunction)&FLEXCABLE::OnPanel1Paint,0,this);
	Panel1->Connect(wxEVT_LEFT_DCLICK,(wxObjectEventFunction)&FLEXCABLE::OnPanel1LeftDClick,0,this);
	//*)

    TextCtrl2->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    TextCtrl3->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    TextCtrl4->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    TextCtrl5->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    TextCtrl6->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    TextCtrl12->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    TextCtrl14->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
    CheckBox1->Bind(wxEVT_SET_FOCUS,(wxObjectEventFunction)&FLEXCABLE::OnHighlightSchematic, this);
}


FLEXCABLE::~FLEXCABLE()
{
	//(*Destroy(FLEXCABLE)
	//*)
}

void FLEXCABLE::SetPathToMOD(wxString Path, wxString Name)
{

    pathToMOD << Path;
    MODname << Name;

}

void FLEXCABLE::OnButton1Click(wxCommandEvent& event)
{
    int IsError = 0;

    wxString fileName = TextCtrl1->GetValue();

    TextCtrl1->SetBackgroundColour(wxColor(255,255,255));

    if (fileName == "")
    {
        wxMessageBox("Please Specify a Cable Name" , _("Missing Cable Name"));
        return;
    }

    check_is_empty(TextCtrl2, &IsError);
    check_is_empty(TextCtrl3, &IsError);
    check_is_empty(TextCtrl4, &IsError);
    check_is_empty(TextCtrl5, &IsError);
    check_is_empty(TextCtrl6, &IsError);
    check_is_empty(TextCtrl13, &IsError);
    check_is_empty(TextCtrl14, &IsError);

    if (CheckBox1->GetValue())
    {
//        check_is_empty(TextCtrl7, &IsError);
//        check_is_empty(TextCtrl8, &IsError);
//        check_is_empty(TextCtrl9, &IsError);
//        check_is_empty(TextCtrl10, &IsError);
//        check_is_empty(TextCtrl11, &IsError);
    }

    else
    {
        check_is_empty(TextCtrl12, &IsError);
    }

    if ( IsError == 1)
    {
        wxMessageBox("Please fix highlighted errors" , _("Non numeric entry"));
        return;
    }

    WriteCableFile (fileName);

    Button1->Disable();
}

void FLEXCABLE::OnCheckBox1Click(wxCommandEvent& event)
{

    TextCtrl12->Disable();

    FD_ESR *winFD_ESR = new FD_ESR(this);

    winFD_ESR->formtype = 1;

    if ( winFD_ESR->ShowModal() == wxID_OK )
    {
        FD_ESR6 = winFD_ESR->TextCtrl6->GetValue();
        FD_ESR7 = winFD_ESR->TextCtrl7->GetValue();
        FD_ESR8 = winFD_ESR->TextCtrl8->GetValue();
        FD_ESR9 = winFD_ESR->TextCtrl9->GetValue();
        FD_ESR10= winFD_ESR->TextCtrl10->GetValue();
    }
    winFD_ESR->Destroy();


//    TextCtrl7->Enable();
//    TextCtrl8->Enable();
//    TextCtrl9->Enable();
//    TextCtrl10->Enable();
//    TextCtrl11->Enable();

}

void FLEXCABLE::OnButton2Click(wxCommandEvent& event)
{
    //CABLE BUIDER
    wxString fileName = TextCtrl1->GetValue();
    wxString fineName = fileName;

    std::stringstream stream;
    stream  << "\"..\\BIN\\cable_model_builder.exe\""
            << " "
            << "..\\" + MODname + "\\CABLE\\"
            << fileName ;

    system(stream.str().c_str());

    // Display Run Status
    RUN_STATUS *run_status = new RUN_STATUS(this);
    run_status->ShowModal();

    Button1->Enable();
}

void FLEXCABLE::OnButton3Click(wxCommandEvent& event)
{
    //clean up directory
    clean_temp_files();
    Close();
}

void FLEXCABLE::OnTextCtrl2Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl2, &IsError);
    check_is_positive(TextCtrl2, &IsError);
}

void FLEXCABLE::OnTextCtrl3Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl3, &IsError);
    check_is_positive(TextCtrl3, &IsError);
}

void FLEXCABLE::OnTextCtrl4Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl4, &IsError);
    check_is_positive(TextCtrl4, &IsError);
}

void FLEXCABLE::OnTextCtrl5Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl5, &IsError);
    check_is_positive(TextCtrl5, &IsError);
}

void FLEXCABLE::OnTextCtrl6Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl6, &IsError);
    check_is_positive(TextCtrl6, &IsError);
}

void FLEXCABLE::OnTextCtrl12Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl12, &IsError);
    check_is_positive(TextCtrl2, &IsError);
}

//void FLEXCABLE::OnTextCtrl7Text(wxCommandEvent& event)
//{
//    int IsError = 0;
//    check_is_numeric(TextCtrl7, &IsError);
//    check_is_positive(TextCtrl7, &IsError);
//}
//
//void FLEXCABLE::OnTextCtrl8Text(wxCommandEvent& event)
//{
//    int IsError = 0;
//    check_is_numeric(TextCtrl8, &IsError);
//    check_is_positive(TextCtrl8, &IsError);
//}
//
//void FLEXCABLE::OnTextCtrl9Text(wxCommandEvent& event)
//{
//    int IsError = 0;
//    check_is_numeric_list(TextCtrl9, &IsError);
//}
//
//void FLEXCABLE::OnTextCtrl10Text(wxCommandEvent& event)
//{
//    int IsError = 0;
//    check_is_numeric(TextCtrl10, &IsError);
//    check_is_positive(TextCtrl10, &IsError);
//}

//void FLEXCABLE::OnTextCtrl11Text(wxCommandEvent& event)
//{
//    int IsError = 0;
//    check_is_numeric_list(TextCtrl11, &IsError);
//}

void FLEXCABLE::OnTextCtrl13Text(wxCommandEvent& event)
{
    int IsError = 0;
    check_is_numeric(TextCtrl13, &IsError);
    check_is_positive(TextCtrl3, &IsError);

    double value_tb13;
    wxString contents_tb13;

    contents_tb13 = TextCtrl13->GetValue();

    contents_tb13.ToDouble(&value_tb13);

    if (floor(value_tb13) != ceil(value_tb13) || value_tb13 < 1)
    {
        TextCtrl13->SetBackgroundColour(wxColor(255,153,153));
        TextCtrl13->SetFocus();
        IsError = 1;
    }
}

void FLEXCABLE::WriteCableFile (wxString fileName)
{
    ofstream cableFile;

    wxString fullFileName = pathToMOD + "//CABLE//" + fileName + ".cable_spec";

    cableFile.open (fullFileName);

    if (cableFile.is_open())
    {
        // minimum requirements for type Cylindrical
        cableFile <<"# MOD_cable_lib_dir \n";
        cableFile <<"../"<< MODname <<"/CABLE/"<<"\n";
        cableFile <<"Flex_cable\n";
        cableFile << TextCtrl13->GetValue() <<" #number of conductors \n";
        cableFile <<"6 #number of parameters \n";
        cableFile << TextCtrl2->GetValue() <<" # parameter 1: conductor width (x dimension)\n";
        cableFile << TextCtrl3->GetValue() <<" # parameter 2: conductor height (y dimension)\n";
        cableFile << TextCtrl4->GetValue() <<" # parameter 3: conductor separation (x dimension)\n";
        cableFile << TextCtrl5->GetValue() <<" # parameter 4: dielectric offset x\n";
        cableFile << TextCtrl6->GetValue() <<" #  parameter 5: dielectric offset y\n";
        cableFile << TextCtrl14->GetValue() <<" # parameter 6: conductivity\n";
        cableFile <<"1 # number of frequency dependent parameters\n";

        if (CheckBox1->GetValue()){
            cableFile << "# Dielectric relative permittivity model follows\n";
            cableFile << FD_ESR6 <<" # w normalisation constant\n";
            cableFile << FD_ESR7 <<" # a order, a coefficients follow below\n";
            cableFile << FD_ESR8 <<" \n";
            cableFile << FD_ESR9 <<" # b order, b coefficients follow below\n";
            cableFile << FD_ESR10 <<" \n";
        }

        else{
            cableFile << "# Dielectric relative permittivity model follows\n";
            cableFile << "1.0 # w normalisation constant\n";
            cableFile << "0 # a order, a coefficients follow below\n";
            cableFile << TextCtrl12->GetValue()<<"\n";
            cableFile << "0 # b order, b coefficients follow below\n";
            cableFile << "1.0\n";
        }

        cableFile.close();
    }
}

void FLEXCABLE::OnPanel1Paint(wxPaintEvent& event)
{
    flexcableSchematic();
}

void FLEXCABLE::flexcableSchematic()
{
    int size_h, size_v;
    Panel1->GetSize(&size_h, &size_v );

    wxClientDC dc(Panel1);
    dc.Clear();

    // Draw centre lines
    dc.SetPen( wxPen( wxColor(224,224,224),1 , wxDOT_DASH) );
    dc.DrawLine( 0, size_v/2, size_h,size_v/2 );
    dc.DrawLine( size_h/2, 0, size_h/2, size_v);

    dc.SetTextForeground(*wxBLUE);
    dc.DrawText("General Flexcable", 10, 15);

    float conductor_width, conductor_height, conductor_separation, totalHeight, totalWidth;

    conductor_width = 0.175 * size_h;
    conductor_height = 0.06 * size_v;
    conductor_separation = 0.05 * size_h;
    totalHeight = 0.15 * size_v;
    totalWidth = 0.98 * size_h;

    dc.SetPen( wxPen( wxColor(192,192,192),1 ) );
    dc.SetBrush(wxBrush(wxColor(224,224,224), wxBDIAGONAL_HATCH));
    dc.DrawRectangle(wxPoint(size_h/2-totalWidth/2, size_v/2-totalHeight/2), wxSize(totalWidth,totalHeight));

    dc.SetBrush(wxBrush(wxColor(224,224,224), wxSOLID));
    dc.DrawRectangle(wxPoint(size_h/2-conductor_width-conductor_separation, size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
    dc.DrawRectangle(wxPoint(size_h/2-(2* conductor_width)-(2*conductor_separation), size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
    dc.DrawRectangle(wxPoint(size_h/2 + conductor_separation, size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
    dc.DrawRectangle(wxPoint(size_h/2 + conductor_width +(2*conductor_separation), size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));

    dc.SetPen( wxPen( wxColor(0,0,0),4, wxDOT ) );
    dc.DrawLine( size_h/2 - conductor_separation, size_v/2, size_h/2 + conductor_separation, size_v/2);
}

void FLEXCABLE::OnHighlightSchematic(wxCommandEvent& event)
{
    flexcableSchematic();

    int ID = event.GetId();
    event.Skip(true);

    int size_h, size_v;
    Panel1->GetSize(&size_h, &size_v );

    float conductor_width, conductor_height, conductor_separation, totalHeight, totalWidth;

    conductor_width = 0.175 * size_h;
    conductor_height = 0.06 * size_v;
    conductor_separation = 0.05 * size_h;
    totalHeight = 0.15 * size_v;
    totalWidth = 0.98 * size_h;

    wxClientDC dc(Panel1);

    wxPoint arrowHead[3];

    if ( ID == ID_TEXTCTRL2)
    {
        dc.SetPen(wxPen(wxColor(51,51,255), 3));
        dc.SetBrush(wxBrush(wxColor(51,51,255), wxSOLID));
        dc.DrawLine( size_h/2 - conductor_width - conductor_separation,size_v/2, size_h/2 - conductor_separation,size_v/2 );

        arrowHead[0] = wxPoint(size_h/2 - conductor_width - conductor_separation,size_v/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_width - conductor_separation+5,size_v/2-3);
        arrowHead[2] = wxPoint(size_h/2 - conductor_width - conductor_separation+5,size_v/2+3);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);

        arrowHead[0] = wxPoint(size_h/2 - conductor_separation,size_v/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_separation-5,size_v/2-3);
        arrowHead[2] = wxPoint(size_h/2 - conductor_separation-5,size_v/2+3);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);
    }

    if ( ID == ID_TEXTCTRL3)
    {
        dc.SetPen(wxPen(wxColor(51,51,255), 3));
        dc.SetBrush(wxBrush(wxColor(51,51,255), wxSOLID));
        dc.DrawLine( size_h/2 - conductor_separation,size_v/2-conductor_height/2, size_h/2 - conductor_separation,size_v/2 + conductor_height/2);

        arrowHead[0] = wxPoint(size_h/2 - conductor_separation,size_v/2-conductor_height/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_separation - 3,size_v/2-conductor_height/2 + 5);
        arrowHead[2] = wxPoint(size_h/2 - conductor_separation + 3,size_v/2-conductor_height/2 + 5);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);

        arrowHead[0] = wxPoint(size_h/2 - conductor_separation,size_v/2 + conductor_height/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_separation - 3,size_v/2 + conductor_height/2 - 5);
        arrowHead[2] = wxPoint(size_h/2 - conductor_separation + 3,size_v/2 + conductor_height/2 - 5);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);
    }

    if ( ID == ID_TEXTCTRL4)
    {
        dc.SetPen(wxPen(wxColor(51,51,255), 3));
        dc.SetBrush(wxBrush(wxColor(51,51,255), wxSOLID));
        dc.DrawLine( size_h/2 - conductor_width - 2 * conductor_separation,size_v/2, size_h/2 - conductor_width - conductor_separation,size_v/2);

        arrowHead[0] = wxPoint(size_h/2 - conductor_width - 2 * conductor_separation,size_v/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_width - 2 * conductor_separation + 5,size_v/2 - 3);
        arrowHead[2] = wxPoint(size_h/2 - conductor_width - 2 * conductor_separation + 5,size_v/2 + 3);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);

        arrowHead[0] = wxPoint(size_h/2 - conductor_width - conductor_separation,size_v/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_width - conductor_separation - 5,size_v/2 - 3);
        arrowHead[2] = wxPoint(size_h/2 - conductor_width - conductor_separation - 5,size_v/2 + 3);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);
    }

    if ( ID == ID_TEXTCTRL5)
    {
        dc.SetPen(wxPen(wxColor(51,51,255), 3));
        dc.SetBrush(wxBrush(wxColor(51,51,255), wxSOLID));
        dc.DrawLine( size_h/2 - totalWidth/2, size_v/2, size_h/2 - 2 * conductor_width - 2 * conductor_separation,size_v/2);

        arrowHead[0] = wxPoint(size_h/2 - totalWidth/2, size_v/2);
        arrowHead[1] = wxPoint(size_h/2 - totalWidth/2 + 5, size_v/2 - 3);
        arrowHead[2] = wxPoint(size_h/2 - totalWidth/2 + 5, size_v/2 + 3);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);

        arrowHead[0] = wxPoint(size_h/2 - 2 * conductor_width - 2 * conductor_separation,size_v/2);
        arrowHead[1] = wxPoint(size_h/2 - 2 * conductor_width - 2 * conductor_separation - 5,size_v/2 - 3);
        arrowHead[2] = wxPoint(size_h/2 - 2 * conductor_width - 2 * conductor_separation - 5,size_v/2 + 3);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);

    }

    if ( ID == ID_TEXTCTRL6)
    {
        dc.SetPen(wxPen(wxColor(51,51,255), 3));
        dc.SetBrush(wxBrush(wxColor(51,51,255), wxSOLID));
        dc.DrawLine( size_h/2 - conductor_separation,size_v/2 - conductor_height/2, size_h/2 - conductor_separation,size_v/2 - totalHeight/2);

        arrowHead[0] = wxPoint(size_h/2 - conductor_separation,size_v/2 - conductor_height/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_separation - 3,size_v/2 - conductor_height/2 - 5);
        arrowHead[2] = wxPoint(size_h/2 - conductor_separation + 3,size_v/2 - conductor_height/2 - 5);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);

        arrowHead[0] = wxPoint(size_h/2 - conductor_separation,size_v/2 - totalHeight/2);
        arrowHead[1] = wxPoint(size_h/2 - conductor_separation - 3,size_v/2 - totalHeight/2 +5);
        arrowHead[2] = wxPoint(size_h/2 - conductor_separation + 3,size_v/2 - totalHeight/2 + 5);
        dc.DrawPolygon(WXSIZEOF(arrowHead), arrowHead);
    }
    if ( ID == ID_TEXTCTRL14)
    {
        dc.SetPen(wxPen(wxColor(51,51,255), 3));
        dc.SetBrush(wxBrush(wxColor(51,51,255), wxSOLID));
        dc.DrawRectangle(wxPoint(size_h/2-conductor_width-conductor_separation, size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
        dc.DrawRectangle(wxPoint(size_h/2-(2* conductor_width)-(2*conductor_separation), size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
        dc.DrawRectangle(wxPoint(size_h/2 + conductor_separation, size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
        dc.DrawRectangle(wxPoint(size_h/2 + conductor_width +(2*conductor_separation), size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));

    }

    if ( ID == ID_TEXTCTRL12 || ID == ID_CHECKBOX1)
    {
    dc.SetPen( wxPen( wxColor(51,51,255),1 ) );
    dc.SetBrush(wxBrush(wxColor(51,51,255), wxBDIAGONAL_HATCH));
    dc.DrawRectangle(wxPoint(size_h/2-totalWidth/2, size_v/2-totalHeight/2), wxSize(totalWidth,totalHeight));

    dc.SetPen( wxPen( wxColor(192,192,192),1 ) );
    dc.SetBrush(wxBrush(wxColor(224,224,224), wxSOLID));
    dc.DrawRectangle(wxPoint(size_h/2-conductor_width-conductor_separation, size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
    dc.DrawRectangle(wxPoint(size_h/2-(2* conductor_width)-(2*conductor_separation), size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
    dc.DrawRectangle(wxPoint(size_h/2 + conductor_separation, size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));
    dc.DrawRectangle(wxPoint(size_h/2 + conductor_width +(2*conductor_separation), size_v/2-conductor_height/2), wxSize(conductor_width,conductor_height));

    dc.SetPen( wxPen( wxColor(0,0,0),4, wxDOT ) );
    dc.DrawLine( size_h/2 - conductor_separation, size_v/2, size_h/2 + conductor_separation, size_v/2);

    }
}

void FLEXCABLE::OnNotebook1PageChanging(wxNotebookEvent& event)
{
    flexcableSchematic();
}

void FLEXCABLE::OnPanel1LeftDClick(wxMouseEvent& event)
{
    flexcableSchematic();
}


