//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "About.h"
#include "glpk.h"
#include <cstring>
#include "newlsdlg.h"


//---------------------------------------------------------------------------
#pragma link "XPMenu"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------

__fastcall TMainForm::TMainForm(TComponent *Owner)
	: TForm(Owner)
{
        lib_set_print_hook(NULL,printmessage);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CreateMDIChild(String Name,int nVar, int nEq)
{
	TMDIChild *Child;

	//--- create a new MDI child window ----
	Child = new TMDIChild(Application);
	Child->Caption = Name;
	if (FileExists (Name))
		Child->LoadFromMPS(Name);
        else
                Child->NewLS(nVar,nEq);                
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileNew1Execute(TObject *Sender)
{
        NewLSDlg=new TNewLSDlg(this);
        if(NewLSDlg->ShowModal()==mrOk){
                int nEq;
                int nVar;
                try{
                    nEq=StrToInt(NewLSDlg->edEq->Text);
                    nVar=StrToInt(NewLSDlg->edVar->Text);
                }catch(EConvertError& e){
                        Application->MessageBoxA("Enter an integer between 1 and 1000!","",0);
                        delete NewLSDlg;
                        return;
                }
                if(nEq<1||nEq>1000||nVar<1||nVar>1000){
                                        Application->MessageBoxA("Enter an integer between 1 and 1000!","",0);
                                        delete NewLSDlg;
                                        return;
                }
                if(nEq>nVar){
                        Application->MessageBoxA("There can't be more equations than variables!","",0);
                        delete NewLSDlg;
                        return;
                }
        	CreateMDIChild("Linear System " + IntToStr(MDIChildCount + 1),nVar,nEq);
        }
        delete NewLSDlg;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileOpen1Execute(TObject *Sender)
{
	if (OpenDialog->Execute()){
		CreateMDIChild(OpenDialog->FileName,0,0);
        }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::HelpAbout1Execute(TObject *Sender)
{
	AboutBox->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileExit1Execute(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------

__fastcall TMainForm::~TMainForm()
{
        lib_free_env();
}

int printmessage(void* info, char* msg)
{
        if (strncmp(msg,"lpx_read_mps",12)!=0)
        Application->MessageBoxA(msg,"MPS reader",0);
        return 1;
}





