Logo ROOT  
Reference Guide
TSlider.cxx
Go to the documentation of this file.
1 // @(#)root/gpad:$Id$
2 // Author: Rene Brun 23/11/96
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TROOT.h"
13 #include "TSlider.h"
14 #include "TSliderBox.h"
15 
16 #include <cstring>
17 #include <iostream>
18 
20 
21 /** \class TSlider
22 \ingroup gpad
23 
24 A specialized TPad including a TSliderBox object.
25 The TSliderBox can be moved in the pad.
26 
27 Slider drawing options include the possibility to change the slider
28 starting and ending positions or only one of them.
29 
30 The current slider position can be retrieved via the functions
31 TSlider::GetMinimum and TSlider::GetMaximum
32  These two functions return numbers in the range [0,1].
33 
34 If a method has been set (via TSlider::SetMethod), the expression is
35 executed via the interpreter when the button 1 is released.
36 
37  if no method has been set, and an object is referenced (TSlider::SetObject
38  has been called), while the slider is being moved/resized,
39  the object ExecuteEvent function is called.
40 
41 ### Example 1 using TSlider::SetMethod
42 
43 #### macro xyslider.C
44 
45 ~~~ {.cpp}
46 void xyslider()
47 {
48  // Example of macro featuring two sliders
49  TFile *f = new TFile("hsimple.root");
50  TH2F *hpxpy = (TH2F*)f->Get("hpxpy");
51  TCanvas *c1 = new TCanvas("c1");
52  TPad *pad = new TPad("pad","Color plot pad",0.1,0.1,0.98,0.98);
53  pad->Draw();
54  pad->cd();
55  hpxpy->Draw("COLZ");
56  c1->cd();
57 
58  // Create two sliders in main canvas. When button1 will be released
59  // the macro action.C will be called.
60  TSlider *xslider = new TSlider("xslider","x",0.1,0.02,0.98,0.08);
61  xslider->SetMethod(".x action.C");
62  TSlider *yslider = new TSlider("yslider","y",0.02,0.1,0.06,0.98);
63  yslider->SetMethod(".x action.C");
64 }
65 ~~~
66 
67 #### macro action.C
68 
69 ~~~ {.cpp}
70 void action()
71 {
72  Int_t nx = hpxpy->GetXaxis()->GetNbins();
73  Int_t ny = hpxpy->GetYaxis()->GetNbins();
74  Int_t binxmin = nx*xslider->GetMinimum();
75  Int_t binxmax = nx*xslider->GetMaximum();
76  hpxpy->GetXaxis()->SetRange(binxmin,binxmax);
77  Int_t binymin = ny*yslider->GetMinimum();
78  Int_t binymax = ny*yslider->GetMaximum();
79  hpxpy->GetYaxis()->SetRange(binymin,binymax);
80  pad->cd();
81  hpxpy->Draw("COLZ");
82  c1->Update();
83 }
84 ~~~
85 
86 The canvas and the sliders created in the above macro are shown in the picture
87 below.
88 
89 \image html gpad_slider.png
90 
91 ### Example 2 using TSlider::SetObject macro xyslider.C
92 
93  Same example as above. Instead of TSlider::SetMethod:
94 ~~~ {.cpp}
95  Myclass *obj = new Myclass(); // Myclass derived from TObject
96  xslider->SetObject(obj);
97  yslider->SetObject(obj);
98 ~~~
99 
100 When the slider will be changed, MyClass::ExecuteEvent will be called with px=0
101 and py = 0
102 */
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// slider default constructor.
106 
108 {
109  fObject = 0;
110  fMethod = "";
111  fMinimum = 0;
112  fMaximum = 1;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Slider normal constructor.
117 ///
118 /// x1,y1,x2,y2 are in pad user coordinates
119 
120 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)
121  :TPad(name,title,0.1,0.1,0.9,0.9,color,bordersize,bordermode)
122 {
123  if (!gPad) return;
124 
125  Double_t x1pad = gPad->GetX1();
126  Double_t x2pad = gPad->GetX2();
127  Double_t y1pad = gPad->GetY1();
128  Double_t y2pad = gPad->GetY2();
129  Double_t xmin = (x1-x1pad)/(x2pad-x1pad);
130  Double_t ymin = (y1-y1pad)/(y2pad-y1pad);
131  Double_t xmax = (x2-x1pad)/(x2pad-x1pad);
132  Double_t ymax = (y2-y1pad)/(y2pad-y1pad);
134  Range(0,0,1,1);
135 
137  Modified(kTRUE);
138 
139  fMinimum = 0;
140  fMaximum = 1;
141  fObject = 0;
142  fMethod = "";
143  Double_t dx = PixeltoX(bordersize);
144  Double_t dy = PixeltoY(-bordersize);
145  TSliderBox *sbox = new TSliderBox(dx,dy,1-dx,1-dy,color,bordersize,-bordermode);
146  sbox->SetSlider(this);
147  fPrimitives->Add(sbox);
148  AppendPad();
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// slider default destructor.
153 
155 {
156 }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 /// Paint this slider with its current attributes.
160 
162 {
163  TPad::Paint(option);
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Save primitive as a C++ statement(s) on output stream out
168 
169 void TSlider::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
170 {
171  TPad *padsav = (TPad*)gPad;
172  char quote = '"';
173  if (gROOT->ClassSaved(TSlider::Class())) {
174  out<<" ";
175  } else {
176  out<<" TSlider *";
177  }
178  out<<"slider = new TSlider("<<quote<<GetName()<<quote<<", "<<quote<<GetTitle()
179  <<quote
180  <<","<<fXlowNDC
181  <<","<<fYlowNDC
182  <<","<<fXlowNDC+fWNDC
183  <<","<<fYlowNDC+fHNDC
184  <<");"<<std::endl;
185 
186  SaveFillAttributes(out,"slider",0,1001);
187  SaveLineAttributes(out,"slider",1,1,1);
188 
189  if (GetBorderSize() != 2) {
190  out<<" slider->SetBorderSize("<<GetBorderSize()<<");"<<std::endl;
191  }
192  if (GetBorderMode() != -1) {
193  out<<" slider->SetBorderMode("<<GetBorderMode()<<");"<<std::endl;
194  }
195  Int_t lenMethod = strlen(GetMethod());
196  if (lenMethod > 0) {
197  out<<" slider->SetMethod("<<quote<<GetMethod()<<quote<<");"<<std::endl;
198  }
199 
200  out<<" "<<padsav->GetName()<<"->cd();"<<std::endl;
201  padsav->cd();
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Set Slider range in [0,1]
206 
208 {
209  TSliderBox *sbox = (TSliderBox*)fPrimitives->FindObject("TSliderBox");
210  if (sbox) {
211  if (fAbsWNDC > fAbsHNDC) {
212  sbox->SetX1(xmin);
213  sbox->SetX2(xmax);
214  } else {
215  sbox->SetY1(xmin);
216  sbox->SetY2(xmax);
217  }
218  }
219  fMinimum = xmin;
220  fMaximum = xmax;
221  Modified();
222 }
TSlider::SavePrimitive
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TSlider.cxx:169
TSlider::fMinimum
Double_t fMinimum
Slider minimum value in [0,1].
Definition: TSlider.h:20
TBox::SetY2
virtual void SetY2(Double_t y2)
Definition: TBox.h:64
ymax
float ymax
Definition: THbookFile.cxx:95
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TSliderBox
The moving box in a TSlider.
Definition: TSliderBox.h:20
TSlider.h
Option_t
const char Option_t
Definition: RtypesCore.h:66
TPad
The most important graphics class in the ROOT system.
Definition: TPad.h:26
TPad::Modified
void Modified(Bool_t flag=1) override
Definition: TPad.h:414
TList::FindObject
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:578
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
xmax
float xmax
Definition: THbookFile.cxx:95
TObject::AppendPad
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:107
TAttLine::SaveLineAttributes
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttLine.cxx:270
TAttFill::SaveFillAttributes
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition: TAttFill.cxx:234
TSlider::Paint
virtual void Paint(Option_t *option="")
Paint this slider with its current attributes.
Definition: TSlider.cxx:161
Color_t
short Color_t
Definition: RtypesCore.h:83
TPad::fXlowNDC
Double_t fXlowNDC
X bottom left corner of pad in NDC [0,1].
Definition: TPad.h:58
TBox::SetY1
virtual void SetY1(Double_t y1)
Definition: TBox.h:63
x1
static const double x1[5]
Definition: RooGaussKronrodIntegrator1D.cxx:346
TSlider::fObject
TObject * fObject
!Pointer to associated object
Definition: TSlider.h:22
TROOT.h
TPad::fHNDC
Double_t fHNDC
Height of pad along Y in Normalized Coordinates (NDC)
Definition: TPad.h:63
TSlider::TSlider
TSlider()
slider default constructor.
Definition: TSlider.cxx:107
TPad::fWNDC
Double_t fWNDC
Width of pad along X in Normalized Coordinates (NDC)
Definition: TPad.h:62
TPad::cd
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition: TPad.cxx:603
TPad::Range
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition: TPad.cxx:5196
TPad::GetBorderSize
Short_t GetBorderSize() const override
Definition: TPad.h:194
xmin
float xmin
Definition: THbookFile.cxx:95
TPad::fPrimitives
TList * fPrimitives
->List of primitives (subpads)
Definition: TPad.h:103
TSlider::~TSlider
virtual ~TSlider()
slider default destructor.
Definition: TSlider.cxx:154
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TPad::Paint
void Paint(Option_t *option="") override
Paint all primitives in pad.
Definition: TPad.cxx:3446
TPad::GetBorderMode
Short_t GetBorderMode() const override
Definition: TPad.h:193
TPad::PixeltoY
Double_t PixeltoY(Int_t py) override
Definition: TPad.h:438
TSliderBox.h
Short_t
short Short_t
Definition: RtypesCore.h:39
TObject::kCanDelete
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
ymin
float ymin
Definition: THbookFile.cxx:95
TBox::SetX2
virtual void SetX2(Double_t x2)
Definition: TBox.h:62
TPad::fAbsWNDC
Double_t fAbsWNDC
Absolute Width of pad along X in NDC.
Definition: TPad.h:67
Double_t
double Double_t
Definition: RtypesCore.h:59
TPad::PixeltoX
Double_t PixeltoX(Int_t px) override
Definition: TPad.h:430
TPad::GetTitle
const char * GetTitle() const override
Returns title of object.
Definition: TPad.h:255
TSlider::fMethod
TString fMethod
command to be executed when slider is changed
Definition: TSlider.h:23
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TSlider::fMaximum
Double_t fMaximum
Slider maximum value in [0,1].
Definition: TSlider.h:21
name
char name[80]
Definition: TGX11.cxx:110
TSliderBox::SetSlider
void SetSlider(TSlider *slider)
Definition: TSliderBox.h:38
x2
static const double x2[5]
Definition: RooGaussKronrodIntegrator1D.cxx:364
gPad
#define gPad
Definition: TVirtualPad.h:287
TPad::GetName
const char * GetName() const override
Returns name of object.
Definition: TPad.h:254
TSlider::SetRange
virtual void SetRange(Double_t xmin=0, Double_t xmax=1)
Set Slider range in [0,1].
Definition: TSlider.cxx:207
TBox::SetX1
virtual void SetX1(Double_t x1)
Definition: TBox.h:61
Class
void Class()
Definition: Class.C:29
TPad::SetPad
void SetPad(const char *name, const char *title, Double_t xlow, Double_t ylow, Double_t xup, Double_t yup, Color_t color=35, Short_t bordersize=5, Short_t bordermode=-1) override
Set all pad parameters.
Definition: TPad.cxx:6014
gROOT
#define gROOT
Definition: TROOT.h:406
TPad::fAbsHNDC
Double_t fAbsHNDC
Absolute Height of pad along Y in NDC.
Definition: TPad.h:68
int
TSlider
A specialized TPad including a TSliderBox object.
Definition: TSlider.h:17
TPad::fYlowNDC
Double_t fYlowNDC
Y bottom left corner of pad in NDC [0,1].
Definition: TPad.h:59
TSlider::GetMethod
virtual const char * GetMethod() const
Definition: TSlider.h:36