00001 // @(#)root/gpad:$Id: TSlider.cxx 31709 2009-12-09 09:24:43Z couet $ 00002 // Author: Rene Brun 23/11/96 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #include "Riostream.h" 00013 #include "TROOT.h" 00014 #include "TSlider.h" 00015 #include "TSliderBox.h" 00016 00017 #include <string.h> 00018 00019 ClassImp(TSlider) 00020 00021 //______________________________________________________________________________ 00022 ////////////////////////////////////////////////////////////////////////// 00023 // // 00024 // TSlider // 00025 // // 00026 // A TSlider object is a specialized TPad including a TSliderBox object// 00027 // The TSliderBox can be moved in the pad. // 00028 // Slider drawing options include the possibility to change the slider // 00029 // starting and ending positions or only one of them. // 00030 // // 00031 // The current slider position can be retrieved via the functions // 00032 // TSlider::GetMinimum and TSlider::GetMaximum // 00033 // These two functions return numbers in the range [0,1]. // 00034 // // 00035 // if a method has been set (via SetMethod), the expression is // 00036 // executed via the interpreter when the button 1 is released. // 00037 // // 00038 // if no method has been set, and an object is referenced (SetObject // 00039 // has been called), while the slider is being moved/resized, // 00040 // the object ExecuteEvent function is called. // // 00041 // // 00042 // Example 1 using SetMethod macro xyslider.C // 00043 // ========================= // 00044 //{ // 00045 // //Example of macro featuring two sliders // 00046 // TFile *f = new TFile("hsimple.root"); // 00047 // TH2F *hpxpy = (TH2F*)f->Get("hpxpy"); // 00048 // TCanvas *c1 = new TCanvas("c1"); // 00049 // TPad *pad = new TPad("pad","lego pad",0.1,0.1,0.98,0.98); // 00050 // pad->SetFillColor(33); // 00051 // pad->Draw(); // 00052 // pad->cd(); // 00053 // gStyle->SetFrameFillColor(42); // 00054 // hpxpy->SetFillColor(46); // 00055 // hpxpy->Draw("lego1"); // 00056 // c1->cd(); // 00057 // // 00058 // //Create two sliders in main canvas. When button1 // 00059 // //of the mouse will be released, action.C will be called // 00060 // TSlider *xslider = new TSlider("xslider","x",0.1,0.02,0.98,0.08); // 00061 // xslider->SetMethod(".x action.C"); // 00062 // TSlider *yslider = new TSlider("yslider","y",0.02,0.1,0.06,0.98); // 00063 // yslider->SetMethod(".x action.C"); // 00064 //} // 00065 // // 00066 // macro action.C // 00067 //{ // 00068 // Int_t nx = hpxpy->GetXaxis()->GetNbins(); // 00069 // Int_t ny = hpxpy->GetYaxis()->GetNbins(); // 00070 // Int_t binxmin = nx*xslider->GetMinimum(); // 00071 // Int_t binxmax = nx*xslider->GetMaximum(); // 00072 // hpxpy->GetXaxis()->SetRange(binxmin,binxmax); // 00073 // Int_t binymin = ny*yslider->GetMinimum(); // 00074 // Int_t binymax = ny*yslider->GetMaximum(); // 00075 // hpxpy->GetYaxis()->SetRange(binymin,binymax); // 00076 // pad->cd(); // 00077 // hpxpy->Draw("lego1"); // 00078 // c1->Update(); // 00079 //} // 00080 // The canvas and the sliders created in the above macro are shown // 00081 // in the picture below. // 00082 //Begin_Html // 00083 /* 00084 <img src="gif/xyslider.gif"> 00085 */ 00086 //End_Html 00087 // // 00088 // Example 2 using SetObject macro xyslider.C // 00089 // ========================= // 00090 // // 00091 // Same example as above. Instead of SetMethod: // 00092 // Myclass *obj = new Myclass(); // Myclass derived from TObject // 00093 // xslider->SetObject(obj); // 00094 // yslider->SetObject(obj); // 00095 // // 00096 // When the slider will be changed, MyClass::ExecuteEvent will be // 00097 // called with px=0 and py = 0 // 00098 ////////////////////////////////////////////////////////////////////////// 00099 00100 00101 //______________________________________________________________________________ 00102 TSlider::TSlider(): TPad() 00103 { 00104 // slider default constructor. 00105 00106 fObject = 0; 00107 fMethod = ""; 00108 fMinimum = 0; 00109 fMaximum = 1; 00110 } 00111 00112 00113 //______________________________________________________________________________ 00114 TSlider::TSlider(const char *name, const char *title, Double_t x1, Double_t y1,Double_t x2, Double_t y2, Color_t color, Short_t bordersize, Short_t bordermode) 00115 :TPad(name,title,0.1,0.1,0.9,0.9,color,bordersize,bordermode) 00116 { 00117 // Slider normal constructor. 00118 // 00119 // x1,y1,x2,y2 are in pad user coordinates 00120 00121 Double_t x1pad = gPad->GetX1(); 00122 Double_t x2pad = gPad->GetX2(); 00123 Double_t y1pad = gPad->GetY1(); 00124 Double_t y2pad = gPad->GetY2(); 00125 Double_t xmin = (x1-x1pad)/(x2pad-x1pad); 00126 Double_t ymin = (y1-y1pad)/(y2pad-y1pad); 00127 Double_t xmax = (x2-x1pad)/(x2pad-x1pad); 00128 Double_t ymax = (y2-y1pad)/(y2pad-y1pad); 00129 SetPad(xmin,ymin,xmax,ymax); 00130 Range(0,0,1,1); 00131 00132 SetBit(kCanDelete); 00133 Modified(kTRUE); 00134 00135 fMinimum = 0; 00136 fMaximum = 1; 00137 fObject = 0; 00138 fMethod = ""; 00139 Double_t dx = PixeltoX(bordersize); 00140 Double_t dy = PixeltoY(-bordersize); 00141 TSliderBox *sbox = new TSliderBox(dx,dy,1-dx,1-dy,color,bordersize,-bordermode); 00142 sbox->SetSlider(this); 00143 fPrimitives->Add(sbox); 00144 AppendPad(); 00145 } 00146 00147 00148 //______________________________________________________________________________ 00149 TSlider::~TSlider() 00150 { 00151 // slider default destructor. 00152 } 00153 00154 00155 //______________________________________________________________________________ 00156 void TSlider::Paint(Option_t *option) 00157 { 00158 // Paint this slider with its current attributes. 00159 00160 TPad::Paint(option); 00161 } 00162 00163 00164 //______________________________________________________________________________ 00165 void TSlider::SavePrimitive(ostream &out, Option_t * /*= ""*/) 00166 { 00167 // Save primitive as a C++ statement(s) on output stream out 00168 00169 TPad *padsav = (TPad*)gPad; 00170 char quote = '"'; 00171 if (gROOT->ClassSaved(TSlider::Class())) { 00172 out<<" "; 00173 } else { 00174 out<<" TSlider *"; 00175 } 00176 out<<"slider = new TSlider("<<quote<<GetName()<<quote<<", "<<quote<<GetTitle() 00177 <<quote 00178 <<","<<fXlowNDC 00179 <<","<<fYlowNDC 00180 <<","<<fXlowNDC+fWNDC 00181 <<","<<fYlowNDC+fHNDC 00182 <<");"<<endl; 00183 00184 SaveFillAttributes(out,"slider",0,1001); 00185 SaveLineAttributes(out,"slider",1,1,1); 00186 00187 if (GetBorderSize() != 2) { 00188 out<<" slider->SetBorderSize("<<GetBorderSize()<<");"<<endl; 00189 } 00190 if (GetBorderMode() != -1) { 00191 out<<" slider->SetBorderMode("<<GetBorderMode()<<");"<<endl; 00192 } 00193 Int_t lenMethod = strlen(GetMethod()); 00194 if (lenMethod > 0) { 00195 out<<" slider->SetMethod("<<quote<<GetMethod()<<quote<<");"<<endl; 00196 } 00197 00198 out<<" "<<padsav->GetName()<<"->cd();"<<endl; 00199 padsav->cd(); 00200 } 00201 00202 00203 //______________________________________________________________________________ 00204 void TSlider::SetRange(Double_t xmin, Double_t xmax) 00205 { 00206 //*-*-*-*-*-*-*-*-*-*-*Set Slider range in [0,1]*-*-*-*-* 00207 //*-* ========================= 00208 00209 TSliderBox *sbox = (TSliderBox*)fPrimitives->FindObject("TSliderBox"); 00210 if (sbox) { 00211 if (fAbsWNDC > fAbsHNDC) { 00212 sbox->SetX1(xmin); 00213 sbox->SetX2(xmax); 00214 } else { 00215 sbox->SetY1(xmin); 00216 sbox->SetY2(xmax); 00217 } 00218 } 00219 fMinimum = xmin; 00220 fMaximum = xmax; 00221 Modified(); 00222 }