#include "Riostream.h" #include #include #include #include #include #include #include #include class myClass{ public: myClass(); //buttons static TGCheckButton *fTextButton_thread; static TGCheckButton *fTextButton_series; // canvas static TCanvas *c123; // members for the box static TBox *box; static float box_size; float box_x; float box_y; static int loopCount; //thread and functions TThread *thread; void threadFunc(); static void* innerLoop(void* ptr=0); }; //declaration of static variables TBox* myClass::box=NULL; TCanvas* myClass::c123=NULL; TGCheckButton* myClass::fTextButton_thread=NULL; TGCheckButton* myClass::fTextButton_series=NULL; float myClass::box_size=0.2; int myClass::loopCount=0; myClass::myClass(){ // main frame TGMainFrame *fMainFrame = new TGMainFrame(gClient->GetRoot(),10,10,kMainFrame | kVerticalFrame); fMainFrame->SetLayoutBroken(kTRUE); // composite frame TGCompositeFrame *fMainFrame_composite = new TGCompositeFrame(fMainFrame,900,626,kVerticalFrame); fMainFrame_composite->SetLayoutBroken(kTRUE); // embedded canvas TRootEmbeddedCanvas *fRootEmbeddedCanvas674 = new TRootEmbeddedCanvas(0,fMainFrame_composite,592,376); Int_t wfRootEmbeddedCanvas674 = fRootEmbeddedCanvas674->GetCanvasWindowId(); c123 = new TCanvas("c123", 10, 10, wfRootEmbeddedCanvas674); fRootEmbeddedCanvas674->AdoptCanvas(c123); fMainFrame_composite->AddFrame(fRootEmbeddedCanvas674, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2)); fRootEmbeddedCanvas674->MoveResize(40,112,592,376); // // make and connect button for the threaded version // fTextButton_thread = new TGCheckButton(fMainFrame_composite,"Run in thread"); fTextButton_thread->SetTextJustify(36); fTextButton_thread->SetMargins(0,0,0,0); fTextButton_thread->SetWrapLength(-1); fMainFrame_composite->AddFrame(fTextButton_thread, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2)); fTextButton_thread->MoveResize(664,128,106,50); fTextButton_thread->Connect("Clicked()", "myClass", this, "threadFunc()"); // //make and connect button for the series version // fTextButton_series = new TGCheckButton(fMainFrame_composite,"Run in series"); fTextButton_series->SetTextJustify(36); fTextButton_series->SetMargins(0,0,0,0); fTextButton_series->SetWrapLength(-1); fMainFrame_composite->AddFrame(fTextButton_series, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2)); fTextButton_series->MoveResize(664,160,106,50); fTextButton_series->Connect("Clicked()", "myClass", this, "innerLoop()"); // // extra stuff auto generated by gui builder // fMainFrame->AddFrame(fMainFrame_composite, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY)); fMainFrame_composite->MoveResize(0,0,900,626); fMainFrame->SetMWMHints(kMWMDecorAll, kMWMFuncAll, kMWMInputModeless); fMainFrame->MapSubwindows(); fMainFrame->Resize(fMainFrame->GetDefaultSize()); fMainFrame->MapWindow(); fMainFrame->Resize(1032,689); // //set up and draw the box // box_x = 0.2; box_y = 0.2; box = new TBox(box_x, box_y, box_x+box_size, box_y+box_size); box->SetFillColor(2); box->SetFillStyle(1001); c123->cd(); box->Draw(); // //set up the thread // int a=1; thread = new TThread("my_thread", innerLoop, (void*)a); } // this loop is called by both threaded and series buttons and just moves the // box along so long as the buttons are in valid positions void* myClass::innerLoop(void* ptr){ while(true){ //stop looping if we switch off the thread button if(!fTextButton_thread->IsOn() && !fTextButton_series->IsOn()) break; // don't want to loop for ever for the serial case loopCount++; if(loopCount>200 && !fTextButton_thread->IsOn()) break; //reset coordinates box->SetX1(box->GetX1()+0.01); box->SetX2(box->GetX2()+0.01); //wrap around if(box->GetX2()>=1){ box->SetX1(0); box->SetX2(box_size); } // redraw c123->Modified(); c123->Update(); //short pause gSystem->Sleep(10); } loopCount=0; fTextButton_series->SetOn(false); return ptr; } // called by pressing on the run in thread button void myClass::threadFunc(){ loopCount=0; if(fTextButton_thread->IsOn()) thread->Run();// calls innerLoop }