Logo ROOT   6.16/01
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 "Riostream.h"
13#include "TROOT.h"
14#include "TSlider.h"
15#include "TSliderBox.h"
16
17#include <string.h>
18
20
21/** \class TSlider
22\ingroup gpad
23
24A specialized TPad including a TSliderBox object.
25The TSliderBox can be moved in the pad.
26
27Slider drawing options include the possibility to change the slider
28starting and ending positions or only one of them.
29
30The current slider position can be retrieved via the functions
31TSlider::GetMinimum and TSlider::GetMaximum
32 These two functions return numbers in the range [0,1].
33
34If a method has been set (via TSlider::SetMethod), the expression is
35executed 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}
46void 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~~~ {.cpp}
69void action()
70{
71 Int_t nx = hpxpy->GetXaxis()->GetNbins();
72 Int_t ny = hpxpy->GetYaxis()->GetNbins();
73 Int_t binxmin = nx*xslider->GetMinimum();
74 Int_t binxmax = nx*xslider->GetMaximum();
75 hpxpy->GetXaxis()->SetRange(binxmin,binxmax);
76 Int_t binymin = ny*yslider->GetMinimum();
77 Int_t binymax = ny*yslider->GetMaximum();
78 hpxpy->GetYaxis()->SetRange(binymin,binymax);
79 pad->cd();
80 hpxpy->Draw("COLZ");
81 c1->Update();
82}
83~~~
84
85The canvas and the sliders created in the above macro are shown in the picture
86below.
87
88\image html gpad_slider.png
89
90### Example 2 using TSlider::SetObject macro xyslider.C
91
92 Same example as above. Instead of TSlider::SetMethod:
93~~~ {.cpp}
94 Myclass *obj = new Myclass(); // Myclass derived from TObject
95 xslider->SetObject(obj);
96 yslider->SetObject(obj);
97~~~
98
99When the slider will be changed, MyClass::ExecuteEvent will be called with px=0
100and py = 0
101*/
102
103////////////////////////////////////////////////////////////////////////////////
104/// slider default constructor.
105
107{
108 fObject = 0;
109 fMethod = "";
110 fMinimum = 0;
111 fMaximum = 1;
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Slider normal constructor.
116///
117/// x1,y1,x2,y2 are in pad user coordinates
118
119TSlider::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)
120 :TPad(name,title,0.1,0.1,0.9,0.9,color,bordersize,bordermode)
121{
122 Double_t x1pad = gPad->GetX1();
123 Double_t x2pad = gPad->GetX2();
124 Double_t y1pad = gPad->GetY1();
125 Double_t y2pad = gPad->GetY2();
126 Double_t xmin = (x1-x1pad)/(x2pad-x1pad);
127 Double_t ymin = (y1-y1pad)/(y2pad-y1pad);
128 Double_t xmax = (x2-x1pad)/(x2pad-x1pad);
129 Double_t ymax = (y2-y1pad)/(y2pad-y1pad);
131 Range(0,0,1,1);
132
135
136 fMinimum = 0;
137 fMaximum = 1;
138 fObject = 0;
139 fMethod = "";
140 Double_t dx = PixeltoX(bordersize);
141 Double_t dy = PixeltoY(-bordersize);
142 TSliderBox *sbox = new TSliderBox(dx,dy,1-dx,1-dy,color,bordersize,-bordermode);
143 sbox->SetSlider(this);
144 fPrimitives->Add(sbox);
145 AppendPad();
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// slider default destructor.
150
152{
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// Paint this slider with its current attributes.
157
159{
160 TPad::Paint(option);
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Save primitive as a C++ statement(s) on output stream out
165
166void TSlider::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
167{
168 TPad *padsav = (TPad*)gPad;
169 char quote = '"';
170 if (gROOT->ClassSaved(TSlider::Class())) {
171 out<<" ";
172 } else {
173 out<<" TSlider *";
174 }
175 out<<"slider = new TSlider("<<quote<<GetName()<<quote<<", "<<quote<<GetTitle()
176 <<quote
177 <<","<<fXlowNDC
178 <<","<<fYlowNDC
179 <<","<<fXlowNDC+fWNDC
180 <<","<<fYlowNDC+fHNDC
181 <<");"<<std::endl;
182
183 SaveFillAttributes(out,"slider",0,1001);
184 SaveLineAttributes(out,"slider",1,1,1);
185
186 if (GetBorderSize() != 2) {
187 out<<" slider->SetBorderSize("<<GetBorderSize()<<");"<<std::endl;
188 }
189 if (GetBorderMode() != -1) {
190 out<<" slider->SetBorderMode("<<GetBorderMode()<<");"<<std::endl;
191 }
192 Int_t lenMethod = strlen(GetMethod());
193 if (lenMethod > 0) {
194 out<<" slider->SetMethod("<<quote<<GetMethod()<<quote<<");"<<std::endl;
195 }
196
197 out<<" "<<padsav->GetName()<<"->cd();"<<std::endl;
198 padsav->cd();
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Set Slider range in [0,1]
203
205{
206 TSliderBox *sbox = (TSliderBox*)fPrimitives->FindObject("TSliderBox");
207 if (sbox) {
208 if (fAbsWNDC > fAbsHNDC) {
209 sbox->SetX1(xmin);
210 sbox->SetX2(xmax);
211 } else {
212 sbox->SetY1(xmin);
213 sbox->SetY2(xmax);
214 }
215 }
216 fMinimum = xmin;
217 fMaximum = xmax;
218 Modified();
219}
void Class()
Definition: Class.C:29
static const double x2[5]
static const double x1[5]
int Int_t
Definition: RtypesCore.h:41
short Short_t
Definition: RtypesCore.h:35
double Double_t
Definition: RtypesCore.h:55
short Color_t
Definition: RtypesCore.h:79
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:363
float xmin
Definition: THbookFile.cxx:93
float ymin
Definition: THbookFile.cxx:93
float xmax
Definition: THbookFile.cxx:93
float ymax
Definition: THbookFile.cxx:93
#define gROOT
Definition: TROOT.h:410
#define gPad
Definition: TVirtualPad.h:286
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:233
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:262
virtual void SetY2(Double_t y2)
Definition: TBox.h:66
virtual void SetX1(Double_t x1)
Definition: TBox.h:63
virtual void SetX2(Double_t x2)
Definition: TBox.h:64
virtual void SetY1(Double_t y1)
Definition: TBox.h:65
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * FindObject(const char *name) const
Delete a TObjLink object.
Definition: TList.cxx:574
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:105
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
The most important graphics class in the ROOT system.
Definition: TPad.h:29
Double_t fWNDC
Width of pad along X in NDC.
Definition: TPad.h:65
Double_t fAbsWNDC
Absolute Width of pad along X in NDC.
Definition: TPad.h:70
const char * GetTitle() const
Returns title of object.
Definition: TPad.h:256
Double_t fYlowNDC
Y bottom left corner of pad in NDC [0,1].
Definition: TPad.h:62
const char * GetName() const
Returns name of object.
Definition: TPad.h:255
virtual 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)
Set all pad parameters.
Definition: TPad.cxx:5902
Double_t PixeltoY(Int_t py)
Definition: TPad.h:439
Double_t fAbsHNDC
Absolute Height of pad along Y in NDC.
Definition: TPad.h:71
void Modified(Bool_t flag=1)
Definition: TPad.h:415
virtual void Paint(Option_t *option="")
Paint all primitives in pad.
Definition: TPad.cxx:3389
TList * fPrimitives
->List of primitives (subpads)
Definition: TPad.h:106
virtual Short_t GetBorderMode() const
Definition: TPad.h:196
Double_t PixeltoX(Int_t px)
Definition: TPad.h:431
virtual Short_t GetBorderSize() const
Definition: TPad.h:197
Double_t fHNDC
Height of pad along Y in NDC.
Definition: TPad.h:66
Double_t fXlowNDC
X bottom left corner of pad in NDC [0,1].
Definition: TPad.h:61
virtual void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Set world coordinate system for the pad.
Definition: TPad.cxx:5150
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
The moving box in a TSlider.
Definition: TSliderBox.h:20
void SetSlider(TSlider *slider)
Definition: TSliderBox.h:38
A specialized TPad including a TSliderBox object.
Definition: TSlider.h:18
TSlider()
slider default constructor.
Definition: TSlider.cxx:106
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TSlider.cxx:166
TObject * fObject
!Pointer to associated object
Definition: TSlider.h:23
virtual const char * GetMethod() const
Definition: TSlider.h:37
Double_t fMaximum
Slider maximum value in [0,1].
Definition: TSlider.h:22
virtual void Paint(Option_t *option="")
Paint this slider with its current attributes.
Definition: TSlider.cxx:158
virtual ~TSlider()
slider default destructor.
Definition: TSlider.cxx:151
virtual void SetRange(Double_t xmin=0, Double_t xmax=1)
Set Slider range in [0,1].
Definition: TSlider.cxx:204
Double_t fMinimum
Slider minimum value in [0,1].
Definition: TSlider.h:21
TString fMethod
command to be executed when slider is changed
Definition: TSlider.h:24