#include <TGClient.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>
#include <TGButton.h>
#include <TRootEmbeddedCanvas.h>
#include <TApplication.h>
#include "Frame.h"

Frame::Frame(const TGWindow *p,UInt_t w,UInt_t h)
    : TGMainFrame(p,w,h)
{
    // Creates widgets of the example
    fEcanvas = new TRootEmbeddedCanvas ("Ecanvas",this,1024,1024);
    AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
                                         10,10,10,1));
    TGHorizontalFrame *hframe=new TGHorizontalFrame(this, 200,40);
    TGTextButton *draw = new TGTextButton(hframe,"&Draw");
    draw->Connect("Clicked()","Frame",this,"DoDraw()");
    hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
    TGTextButton *exit = new TGTextButton(hframe,"&Exit ",
                                          "gApplication->Terminate()");
    hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
    AddFrame(hframe,new TGLayoutHints(kLHintsCenterX,2,2,2,2));
    // Sets window name and shows the main frame
    SetWindowName("JADE Display Frame");
    MapSubwindows();
    Resize(GetDefaultSize());

    MapWindow();

}
void Frame::DoDraw()
{
    // Draws function graphics in randomly choosen interval
    TF1 *f1 = new TF1("f1","sin(x)/x",0,gRandom->Rndm()*10);
    f1->SetFillColor(19);
    f1->SetFillStyle(1);
    f1->SetLineWidth(3);
    f1->Draw();
    TCanvas *fCanvas = fEcanvas->GetCanvas();
    fCanvas->cd();
    fCanvas->Update();
}
void Frame::CloseWindow()
{
    // Got close message for this MainFrame. Calls parent CloseWindow()
    // (which destroys the window) and terminate the application.
    // The close message is generated by the window manager when its close
    // window menu item is selected.
    TGMainFrame::CloseWindow();
    gApplication->Terminate(0);
}
Frame::~Frame()
{
    // Clean up used widgets: frames, buttons, layouthints
    //fMain->Cleanup();
    //delete fMain;
}
