TCreatePrimitives.cxx

Go to the documentation of this file.
00001 // @(#)root/gpad:$Id: TCreatePrimitives.cxx,v 1.0
00002 
00003 /*************************************************************************
00004  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 //////////////////////////////////////////////////////////////////////////
00012 //                                                                      //
00013 // TCreatePrimitives                                                    //
00014 //                                                                      //
00015 // Creates new primitives.                                              //
00016 //                                                                      //
00017 // The functions in this static class are called by TPad::ExecuteEvent  //
00018 // to create new primitives in gPad from the TPad toolbar.              //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include "TCanvas.h"
00023 #include "TStyle.h"
00024 #include "TGraph.h"
00025 #include "TArrow.h"
00026 #include "TPavesText.h"
00027 #include "TPaveLabel.h"
00028 #include "TCurlyArc.h"
00029 #include "TArc.h"
00030 #include "TLatex.h"
00031 #include "TMarker.h"
00032 #include "TDiamond.h"
00033 #include "TGroupButton.h"
00034 #include "TVirtualPad.h"
00035 #include "TCreatePrimitives.h"
00036 #include "TROOT.h"
00037 #include "TSystem.h"
00038 #include "TMath.h"
00039 
00040 //______________________________________________________________________________
00041 TCreatePrimitives::TCreatePrimitives()
00042 {
00043    // TCreatePrimitives default constructor
00044 }
00045 
00046 
00047 //______________________________________________________________________________
00048 TCreatePrimitives::~TCreatePrimitives()
00049 {
00050    // TCreatePrimitives destructor
00051 }
00052 
00053 
00054 //______________________________________________________________________________
00055 void TCreatePrimitives::Ellipse(Int_t event, Int_t px, Int_t py, Int_t mode)
00056 {
00057    //  Create a new arc/ellipse in this gPad
00058    //
00059    //  Click left button to indicate arrow starting position.
00060    //  Release left button to terminate the arrow.
00061    //
00062 
00063    static Double_t x0, y0, x1, y1;
00064 
00065    static Int_t pxold, pyold;
00066    static Int_t px0, py0;
00067    static Int_t linedrawn;
00068    Double_t xc,yc,r1,r2;
00069    TEllipse *el = 0;
00070 
00071    switch (event) {
00072 
00073    case kButton1Down:
00074       gVirtualX->SetLineColor(-1);
00075       x0 = gPad->AbsPixeltoX(px);
00076       y0 = gPad->AbsPixeltoY(py);
00077       px0   = px; py0   = py;
00078       pxold = px; pyold = py;
00079       linedrawn = 0;
00080       break;
00081 
00082    case kButton1Motion:
00083       if (linedrawn) gVirtualX->DrawBox(px0, py0, pxold, pyold, TVirtualX::kHollow);
00084       pxold = px;
00085       pyold = py;
00086       linedrawn = 1;
00087       gVirtualX->DrawBox(px0, py0, pxold, pyold, TVirtualX::kHollow);
00088       break;
00089 
00090    case kButton1Up:
00091       x1 = gPad->AbsPixeltoX(px);
00092       y1 = gPad->AbsPixeltoY(py);
00093       if (gPad->GetLogx()) {
00094          x0 = TMath::Power(10,x0);
00095          x1 = TMath::Power(10,x1);
00096       }
00097       if (gPad->GetLogy()) {
00098          y0 = TMath::Power(10,y0);
00099          y1 = TMath::Power(10,y1);
00100       }
00101       xc = 0.5*(x0+x1);
00102       yc = 0.5*(y0+y1);
00103       if (mode == kArc) {
00104          r1 = 0.5*TMath::Abs(x1-x0);
00105          el = new TArc(xc, yc, r1);
00106       }
00107       if (mode == kEllipse) {
00108          r1 = 0.5*TMath::Abs(x1-x0);
00109          r2 = 0.5*TMath::Abs(y1-y0);
00110          el = new TEllipse(xc, yc, r1, r2);
00111       }
00112       gPad->GetCanvas()->FeedbackMode(kFALSE);
00113       gPad->Modified(kTRUE);
00114       if (el) el->Draw();
00115       gPad->GetCanvas()->Selected((TPad*)gPad, el, event);
00116       gROOT->SetEditorMode();
00117       break;
00118    }
00119 }
00120 
00121 
00122 //______________________________________________________________________________
00123 void TCreatePrimitives::Line(Int_t event, Int_t px, Int_t py, Int_t mode)
00124 {
00125    // Create a new line/arrow in this gPad
00126    //
00127    //  Click left button to indicate arrow starting position.
00128    //  Release left button to terminate the arrow.
00129    //
00130 
00131    static Double_t x0, y0, x1, y1;
00132 
00133    static Int_t pxold, pyold;
00134    static Int_t px0, py0;
00135    static Int_t linedrawn;
00136    TLine *line;
00137    TArrow *arrow;
00138    TCurlyLine *cline;
00139    Double_t radius, phimin,phimax;
00140 
00141    switch (event) {
00142 
00143    case kButton1Down:
00144       gVirtualX->SetLineColor(-1);
00145       x0 = gPad->AbsPixeltoX(px);
00146       y0 = gPad->AbsPixeltoY(py);
00147       px0   = px; py0   = py;
00148       pxold = px; pyold = py;
00149       linedrawn = 0;
00150       break;
00151 
00152    case kButton1Motion:
00153       if (linedrawn) gVirtualX->DrawLine(px0, py0, pxold, pyold);
00154       pxold = px;
00155       pyold = py;
00156       linedrawn = 1;
00157       gVirtualX->DrawLine(px0, py0, pxold, pyold);
00158       break;
00159 
00160    case kButton1Up:
00161       if (px == px0 && py == py0) break;
00162       x1 = gPad->AbsPixeltoX(px);
00163       y1 = gPad->AbsPixeltoY(py);
00164       gPad->Modified(kTRUE);
00165       if (gPad->GetLogx()) {
00166          x0 = TMath::Power(10,x0);
00167          x1 = TMath::Power(10,x1);
00168       }
00169       if (gPad->GetLogy()) {
00170          y0 = TMath::Power(10,y0);
00171          y1 = TMath::Power(10,y1);
00172       }
00173       if (mode == kLine) {
00174          line = new TLine(x0,y0,x1,y1);
00175          line->Draw();
00176          gPad->GetCanvas()->Selected((TPad*)gPad, line, event);
00177       }
00178       if (mode == kArrow) {
00179          arrow = new TArrow(x0,y0,x1,y1
00180                             , TArrow::GetDefaultArrowSize()
00181                             , TArrow::GetDefaultOption());
00182          arrow->Draw();
00183          gPad->GetCanvas()->Selected((TPad*)gPad, arrow, event);
00184       }
00185       if (mode == kCurlyLine) {
00186          cline = new TCurlyLine(x0,y0,x1,y1
00187                                 , TCurlyLine::GetDefaultWaveLength()
00188                                 , TCurlyLine::GetDefaultAmplitude());
00189          cline->Draw();
00190          gPad->GetCanvas()->Selected((TPad*)gPad, cline, event);
00191       }
00192       if (mode == kCurlyArc) {
00193          //calculate radius in pixels and convert to users x
00194          radius = gPad->PixeltoX((Int_t)(TMath::Sqrt((Double_t)((px-px0)*(px-px0) + (py-py0)*(py-py0)))))
00195                  - gPad->PixeltoX(0);
00196          phimin = 0;
00197          phimax = 360;
00198          cline = new TCurlyArc(x0,y0,radius,phimin,phimax
00199                                 , TCurlyArc::GetDefaultWaveLength()
00200                                 , TCurlyArc::GetDefaultAmplitude());
00201          cline->Draw();
00202          gPad->GetCanvas()->Selected((TPad*)gPad, cline, event);
00203       }
00204       gROOT->SetEditorMode();
00205       break;
00206    }
00207 }
00208 
00209 
00210 //______________________________________________________________________________
00211 void TCreatePrimitives::Pad(Int_t event, Int_t px, Int_t py, Int_t)
00212 {
00213    // Create a new pad in gPad
00214    //
00215    //  Click left button to indicate one corner of the pad
00216    //  Click left button to indicate the opposite corner
00217    //
00218    //  The new pad is inserted in the pad where the first point is selected.
00219    //
00220 
00221    static Int_t px1old, py1old, px2old, py2old;
00222    static Int_t px1, py1, px2, py2, pxl, pyl, pxt, pyt;
00223    static Bool_t boxdrawn;
00224    static TPad *padsav;
00225    Double_t xlow, ylow, xup, yup;
00226    TPad * newpad;
00227 
00228    Int_t  n = 0;
00229    TObject *obj;
00230    TIter next(gPad->GetListOfPrimitives());
00231 
00232    while ((obj = next())) {
00233       if (obj->InheritsFrom(TPad::Class())) {
00234          n++;
00235       }
00236    }
00237 
00238    switch (event) {
00239 
00240    case kButton1Down:
00241       padsav = (TPad*)gPad;
00242       gPad->cd();
00243       gVirtualX->SetLineColor(-1);
00244       px1 = gPad->XtoAbsPixel(gPad->GetX1());
00245       py1 = gPad->YtoAbsPixel(gPad->GetY1());
00246       px2 = gPad->XtoAbsPixel(gPad->GetX2());
00247       py2 = gPad->YtoAbsPixel(gPad->GetY2());
00248       px1old = px; py1old = py;
00249       boxdrawn = 0;
00250       break;
00251 
00252    case kButton1Motion:
00253       if (boxdrawn) gVirtualX->DrawBox(pxl, pyl, pxt, pyt, TVirtualX::kHollow);
00254       px2old = px;
00255       px2old = TMath::Max(px2old, px1);
00256       px2old = TMath::Min(px2old, px2);
00257       py2old = py;
00258       py2old = TMath::Max(py2old, py2);
00259       py2old = TMath::Min(py2old, py1);
00260       pxl = TMath::Min(px1old, px2old);
00261       pxt = TMath::Max(px1old, px2old);
00262       pyl = TMath::Max(py1old, py2old);
00263       pyt = TMath::Min(py1old, py2old);
00264       boxdrawn = 1;
00265       gVirtualX->DrawBox(pxl, pyl, pxt, pyt, TVirtualX::kHollow);
00266       break;
00267 
00268    case kButton1Up:
00269       gPad->Modified(kTRUE);
00270       gPad->SetDoubleBuffer(1);   // Turn on double buffer mode
00271       gVirtualX->SetDrawMode(TVirtualX::kCopy);       // set drawing mode back to normal (copy) mode
00272       xlow = (Double_t(pxl) - Double_t(px1))/(Double_t(px2) - Double_t(px1));
00273       ylow = (Double_t(py1) - Double_t(pyl))/(Double_t(py1) - Double_t(py2));
00274       xup  = (Double_t(pxt) - Double_t(px1))/(Double_t(px2) - Double_t(px1));
00275       yup  = (Double_t(py1) - Double_t(pyt))/(Double_t(py1) - Double_t(py2));
00276       gROOT->SetEditorMode();
00277       boxdrawn = 0;
00278       if (xup <= xlow || yup <= ylow) return;
00279       newpad = new TPad(Form("%s_%d",gPad->GetName(),n+1),"newpad",xlow, ylow, xup, yup);
00280       if (newpad->IsZombie()) break;
00281       newpad->SetFillColor(gStyle->GetPadColor());
00282       newpad->Draw();
00283       gPad->GetCanvas()->Selected((TPad*)gPad, newpad, event);
00284       padsav->cd();
00285       break;
00286    }
00287 }
00288 
00289 
00290 //______________________________________________________________________________
00291 void TCreatePrimitives::Pave(Int_t event, Int_t px, Int_t py, Int_t mode)
00292 {
00293    // Create a new pavetext in gPad
00294    //
00295    //  Click left button to indicate one corner of the pavelabel.
00296    //  Release left button at the opposite corner.
00297    //
00298 
00299    static Double_t x0, y0, x1, y1;
00300 
00301    static Int_t pxold, pyold;
00302    static Int_t px0, py0;
00303    static Int_t linedrawn;
00304    const Int_t kTMAX=100;
00305    Int_t i,pxl,pyl;
00306    Double_t temp;
00307    Double_t xp0,xp1,yp0,yp1;
00308    static char atext[kTMAX];
00309    TObject *pave = 0;
00310 
00311    if (mode == kPaveLabel)
00312       ((TPad *)gPad)->EventPave();
00313 
00314    switch (event) {
00315 
00316    case kButton1Down:
00317       gVirtualX->SetLineColor(-1);
00318       x0 = gPad->AbsPixeltoX(px);
00319       y0 = gPad->AbsPixeltoY(py);
00320       px0   = px; py0   = py;
00321       pxold = px; pyold = py;
00322       linedrawn = 0;
00323       break;
00324 
00325    case kButton1Motion:
00326       if (linedrawn) gVirtualX->DrawBox(px0, py0, pxold, pyold, TVirtualX::kHollow);
00327       pxold = px;
00328       pyold = py;
00329       linedrawn = 1;
00330       gVirtualX->DrawBox(px0, py0, pxold, pyold, TVirtualX::kHollow);
00331       break;
00332 
00333    case kButton1Up:
00334       if (px == px0) px = px0+10;
00335       if (py == py0) py = py0-10;
00336       x1 = gPad->AbsPixeltoX(px);
00337       y1 = gPad->AbsPixeltoY(py);
00338 
00339       if (x1 < x0) {temp = x0; x0 = x1; x1 = temp;}
00340       if (y1 < y0) {temp = y0; y0 = y1; y1 = temp;}
00341       xp0 = gPad->PadtoX(x0);
00342       xp1 = gPad->PadtoX(x1);
00343       yp0 = gPad->PadtoY(y0);
00344       yp1 = gPad->PadtoY(y1);
00345       if (mode == kPave)      pave = new TPave(xp0,yp0,xp1,yp1);
00346       if (mode == kPaveText ) pave = new TPaveText(xp0,yp0,xp1,yp1);
00347       if (mode == kPavesText) pave = new TPavesText(xp0,yp0,xp1,yp1);
00348       if (mode == kDiamond)   pave = new TDiamond(x0,y0,x1,y1);
00349       if (mode == kPaveLabel || mode == kButton) {
00350          ((TPad *)gPad)->StartEditing();
00351          gSystem->ProcessEvents();
00352          pxl = (px0 + px)/2;
00353          pyl = (py0 + py)/2;
00354          for (i=0;i<kTMAX;i++) atext[i] = ' ';
00355          atext[kTMAX-1] = 0;
00356          gVirtualX->RequestString(pxl, pyl, atext);
00357          for (i=kTMAX-2;i>=0;i--) {
00358             if ((i==0) || (atext[i] != ' ')) {
00359                atext[i+1] = 0;
00360                break;
00361             }
00362          }
00363          if (mode == kPaveLabel) { 
00364             pave = new TPaveLabel(xp0,yp0,xp1,yp1,atext);
00365             gSystem->ProcessEvents();
00366             ((TPad *)gPad)->RecordPave(pave);
00367          }
00368          if (mode == kButton) pave = new TButton(atext,"",
00369                               (x0-gPad->GetX1())/(gPad->GetX2() - gPad->GetX1()),
00370                               (y0-gPad->GetY1())/(gPad->GetY2() - gPad->GetY1()),
00371                               (x1-gPad->GetX1())/(gPad->GetX2() - gPad->GetX1()),
00372                               (y1-gPad->GetY1())/(gPad->GetY2() -
00373                               gPad->GetY1()));
00374       }
00375       gPad->GetCanvas()->FeedbackMode(kFALSE);
00376       gPad->Modified(kTRUE);
00377       if (pave) pave->Draw();
00378       gPad->GetCanvas()->Selected((TPad*)gPad, pave, event);
00379       gROOT->SetEditorMode();
00380       gPad->Update();
00381       break;
00382    }
00383 }
00384 
00385 
00386 //______________________________________________________________________________
00387 void TCreatePrimitives::PolyLine(Int_t event, Int_t px, Int_t py, Int_t mode)
00388 {
00389    // Create a new PolyLine in gPad
00390    //
00391    //  Click left button to indicate a new point
00392    //  Click left button at same place or double click to close the polyline
00393    //
00394 
00395    static Int_t pxold, pyold, px1old, py1old;
00396    static Int_t npoints = 0;
00397    static Int_t linedrawn = 0;
00398    Int_t dp;
00399    static TGraph *gr = 0;
00400 
00401    switch (event) {
00402 
00403    case kButton1Double:
00404    case kButton1Down:
00405       if (npoints == 0) {
00406          gVirtualX->SetLineColor(-1);
00407       } else {
00408          gPad->GetCanvas()->FeedbackMode(kFALSE);
00409          gVirtualX->DrawLine(px1old, py1old, pxold, pyold);
00410       }
00411       // stop collecting new points if new point is close ( < 5 pixels) of previous point
00412       if (event == kButton1Double) {
00413          px = px1old;
00414          py = py1old;
00415       }
00416       dp = TMath::Abs(px-px1old) +TMath::Abs(py-py1old);
00417       if (npoints && dp < 5) {
00418          gPad->Modified(kTRUE);
00419          if (mode == kCutG && gr) {
00420             gr->Set(gr->GetN() + 1);
00421             Double_t x0, y0;
00422             gr->GetPoint(0, x0, y0);
00423             gr->SetPoint(npoints, x0, y0);
00424          }
00425          npoints = 0;
00426          linedrawn = 0;
00427          gr = 0;
00428          gROOT->SetEditorMode();
00429          break;
00430       }
00431       if (npoints == 1 && gr == 0) {
00432          if (mode == kPolyLine) {
00433             gr = new TGraph(2);
00434             gr->ResetBit(TGraph::kClipFrame);
00435 
00436          } else {
00437             gr = (TGraph*)gROOT->ProcessLineFast(
00438                  Form("new %s(\"CUTG\",%d",
00439                       gROOT->GetCutClassName(),2));
00440          }
00441          gr->SetPoint(0, gPad->PadtoX(gPad->AbsPixeltoX(px1old)),
00442                          gPad->PadtoY(gPad->AbsPixeltoY(py1old)));
00443          gr->SetPoint(1, gPad->PadtoX(gPad->AbsPixeltoX(px)),
00444                          gPad->PadtoY(gPad->AbsPixeltoY(py)));
00445          npoints = 2;
00446          gr->Draw("L");
00447          gPad->GetCanvas()->Selected((TPad*)gPad, gr, event);
00448       } else if (npoints > 1) {
00449          gr->Set(gr->GetN() + 1);
00450          gr->SetPoint(npoints, gPad->PadtoX(gPad->AbsPixeltoX(px)),
00451                          gPad->PadtoY(gPad->AbsPixeltoY(py)));
00452          npoints ++;
00453          gPad->Modified();
00454          gPad->Update();
00455       } else {
00456          npoints = 1;
00457       }
00458       px1old = px; py1old = py;
00459       pxold  = px; pyold  = py;
00460       linedrawn = 0;
00461       break;
00462 
00463    case kMouseMotion:
00464    case kButton1Motion:
00465    case kButton1Up:
00466       if (npoints < 1) return;
00467       gPad->GetCanvas()->FeedbackMode(kTRUE);
00468       if (linedrawn) {
00469          gVirtualX->DrawLine(px1old, py1old, pxold, pyold);
00470       }
00471       pxold = px;
00472       pyold = py;
00473       linedrawn = 1;
00474       gVirtualX->DrawLine(px1old, py1old, pxold, pyold);
00475       break;
00476    }
00477 }
00478 
00479 
00480 //______________________________________________________________________________
00481 void TCreatePrimitives::Text(Int_t event, Int_t px, Int_t py, Int_t mode)
00482 {
00483    // Create a new TLatex at the cursor position in gPad
00484    //
00485    // Click left button to indicate the text position
00486    //
00487 
00488    const Int_t kTMAX=100;
00489    static char atext[kTMAX];
00490    Int_t i, lentext;
00491    TLatex *newtext;
00492    TMarker *marker;
00493    Double_t x, y;
00494 
00495    switch (event) {
00496 
00497    case kButton1Down:
00498       x = gPad->AbsPixeltoX(px);
00499       y = gPad->AbsPixeltoY(py);
00500       if (gPad->GetLogx()) x = TMath::Power(10,x);
00501       if (gPad->GetLogy()) y = TMath::Power(10,y);
00502       if (mode == kMarker) {
00503          marker = new TMarker(x,y,gStyle->GetMarkerStyle());
00504          marker->Draw();
00505          gPad->GetCanvas()->Selected((TPad*)gPad, marker, event);
00506          gROOT->SetEditorMode();
00507          break;
00508       }
00509       ((TPad *)gPad)->StartEditing();
00510       gSystem->ProcessEvents();
00511       for (i=0;i<kTMAX;i++) atext[i] = ' ';
00512       atext[kTMAX-1] = 0;
00513       lentext = kTMAX;
00514       newtext = new TLatex();
00515       gVirtualX->SetLineColor(-1);
00516       newtext->TAttText::Modify();
00517       gVirtualX->RequestString(px, py, atext);
00518       lentext = strlen(atext);
00519       for (i=lentext-1;i>=0;i--) {
00520          if (atext[i] != ' ') {
00521             atext[lentext] = 0;
00522             break;
00523          }
00524          lentext--;
00525       }
00526       if (!lentext) break;
00527       TLatex copytext(x, y, atext);
00528       gSystem->ProcessEvents();
00529       ((TPad *)gPad)->RecordLatex(&copytext);
00530       newtext->DrawLatex(x, y, atext);
00531       gPad->Modified(kTRUE);
00532       gPad->GetCanvas()->Selected((TPad*)gPad, newtext, event);
00533       gROOT->SetEditorMode();
00534       gPad->Update();
00535       break;
00536    }
00537 }

Generated on Tue Jul 5 14:14:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1