Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Slider3Demo.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// Simple macro showing capabilities of triple slider
4///
5/// \macro_code
6///
7/// \author Bertrand Bellenot, Ilka Antcheva
8
9#include "TGButton.h"
10#include "TRootEmbeddedCanvas.h"
11#include "TGLayout.h"
12#include "TF1.h"
13#include "TMath.h"
14#include "TCanvas.h"
15#include "TGTextEntry.h"
16#include "TGTripleSlider.h"
17
18enum ETestCommandIdentifiers {
19 HId1,
20 HId2,
21 HId3,
22 HCId1,
23 HCId2,
24
25 HSId1
26};
27
28class TTripleSliderDemo : public TGMainFrame {
29
30private:
31 TRootEmbeddedCanvas *fCanvas;
32 TGLayoutHints *fLcan;
33 TF1 *fFitFcn;
34 TGHorizontalFrame *fHframe0, *fHframe1, *fHframe2;
35 TGLayoutHints *fBly, *fBfly1, *fBfly2, *fBfly3;
36 TGTripleHSlider *fHslider1;
37 TGTextEntry *fTeh1, *fTeh2, *fTeh3;
38 TGTextBuffer *fTbh1, *fTbh2, *fTbh3;
39 TGCheckButton *fCheck1, *fCheck2;
40
41public:
42 TTripleSliderDemo();
43 virtual ~TTripleSliderDemo();
44
45 void CloseWindow();
46 void DoText(const char *text);
47 void DoSlider();
48 void HandleButtons();
49
50 ClassDef(TTripleSliderDemo, 0)
51};
52
53//______________________________________________________________________________
54TTripleSliderDemo::TTripleSliderDemo() : TGMainFrame(gClient->GetRoot(), 100, 100)
55{
56
57 char buf[32];
58 SetCleanup(kDeepCleanup);
59 // Create an embedded canvas and add to the main frame, centered in x and y
60 // and with 30 pixel margins all around
61 fCanvas = new TRootEmbeddedCanvas("Canvas", this, 600, 400);
62 fLcan = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 10, 10, 10, 10);
63 AddFrame(fCanvas, fLcan);
64 fCanvas->GetCanvas()->SetFillColor(33);
65 fCanvas->GetCanvas()->SetFrameFillColor(41);
66 fCanvas->GetCanvas()->SetBorderMode(0);
67 fCanvas->GetCanvas()->SetGrid();
68 fCanvas->GetCanvas()->SetLogy();
69
70 fHframe0 = new TGHorizontalFrame(this, 0, 0, 0);
71
72 fCheck1 = new TGCheckButton(fHframe0, "&Constrained", HCId1);
73 fCheck2 = new TGCheckButton(fHframe0, "&Relative", HCId2);
74 fCheck1->SetState(kButtonUp);
75 fCheck2->SetState(kButtonUp);
76 fCheck1->SetToolTipText("Pointer position constrained to slider sides");
77 fCheck2->SetToolTipText("Pointer position relative to slider position");
78
79 fHframe0->Resize(200, 50);
80
81 fHframe1 = new TGHorizontalFrame(this, 0, 0, 0);
82
83 fHslider1 = new TGTripleHSlider(fHframe1, 190, kDoubleScaleBoth, HSId1,
85 GetDefaultFrameBackground(),
87 fHslider1->Connect("PointerPositionChanged()", "TTripleSliderDemo",
88 this, "DoSlider()");
89 fHslider1->Connect("PositionChanged()", "TTripleSliderDemo",
90 this, "DoSlider()");
91 fHslider1->SetRange(0.05,5.0);
92
93 fHframe1->Resize(200, 25);
94
95 fHframe2 = new TGHorizontalFrame(this, 0, 0, 0);
96
97 fTeh1 = new TGTextEntry(fHframe2, fTbh1 = new TGTextBuffer(5), HId1);
98 fTeh2 = new TGTextEntry(fHframe2, fTbh2 = new TGTextBuffer(5), HId2);
99 fTeh3 = new TGTextEntry(fHframe2, fTbh3 = new TGTextBuffer(5), HId3);
100
101 fTeh1->SetToolTipText("Minimum (left) Value of Slider");
102 fTeh2->SetToolTipText("Pointer Position Value");
103 fTeh3->SetToolTipText("Maximum (right) Value of Slider");
104
105 fTbh1->AddText(0, "0.0");
106 fTbh2->AddText(0, "0.0");
107 fTbh3->AddText(0, "0.0");
108
109 fTeh1->Connect("TextChanged(char*)", "TTripleSliderDemo", this,
110 "DoText(char*)");
111 fTeh2->Connect("TextChanged(char*)", "TTripleSliderDemo", this,
112 "DoText(char*)");
113 fTeh3->Connect("TextChanged(char*)", "TTripleSliderDemo", this,
114 "DoText(char*)");
115
116 fCheck1->Connect("Clicked()", "TTripleSliderDemo", this,
117 "HandleButtons()");
118 fCheck2->Connect("Clicked()", "TTripleSliderDemo", this,
119 "HandleButtons()");
120
121 fHframe2->Resize(100, 25);
122
123 //--- layout for buttons: top align, equally expand horizontally
124 fBly = new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5);
125
126 //--- layout for the frame: place at bottom, right aligned
127 fBfly1 = new TGLayoutHints(kLHintsTop | kLHintsCenterX, 5, 5, 5, 5);
128 fBfly2 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5);
129 fBfly3 = new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5);
130
131 fHframe0->AddFrame(fCheck1, fBfly2);
132 fHframe0->AddFrame(fCheck2, fBfly2);
133 fHframe1->AddFrame(fHslider1, fBly);
134 fHframe2->AddFrame(fTeh1, fBfly2);
135 fHframe2->AddFrame(fTeh2, fBfly1);
136 fHframe2->AddFrame(fTeh3, fBfly3);
137
138 AddFrame(fHframe0, fBly);
139 AddFrame(fHframe1, fBly);
140 AddFrame(fHframe2, fBly);
141
142 // Set main frame name, map sub windows (buttons), initialize layout
143 // algorithm via Resize() and map main frame
144 SetWindowName("Triple Slider Demo");
145 MapSubwindows();
146 Resize(GetDefaultSize());
147 MapWindow();
148
149 fFitFcn = new TF1("fFitFcn", "TMath::LogNormal(x, [0], [1], [2])", 0, 5);
150 fFitFcn->SetRange(0.0, 2.5);
151 fFitFcn->SetParameters(1.0, 0, 1);
152 fFitFcn->SetMinimum(1.0e-3);
153 fFitFcn->SetMaximum(10.0);
154 fFitFcn->SetLineColor(kRed);
155 fFitFcn->SetLineWidth(1);
156 fFitFcn->Draw();
157
158 fHslider1->SetPosition(0.05,2.5);
159 fHslider1->SetPointerPosition(1.0);
160
161 sprintf(buf, "%.3f", fHslider1->GetMinPosition());
162 fTbh1->Clear();
163 fTbh1->AddText(0, buf);
164 sprintf(buf, "%.3f", fHslider1->GetPointerPosition());
165 fTbh2->Clear();
166 fTbh2->AddText(0, buf);
167 sprintf(buf, "%.3f", fHslider1->GetMaxPosition());
168 fTbh3->Clear();
169 fTbh3->AddText(0, buf);
170}
171
172//______________________________________________________________________________
173TTripleSliderDemo::~TTripleSliderDemo()
174{
175 // Clean up
176
177 Cleanup();
178}
179
180//______________________________________________________________________________
181void TTripleSliderDemo::CloseWindow()
182{
183 // Called when window is closed via the window manager.
184
185 delete this;
186}
187
188//______________________________________________________________________________
189void TTripleSliderDemo::DoText(const char * /*text*/)
190{
191 // Handle text entry widgets.
192
194 Int_t id = te->WidgetId();
195
196 switch (id) {
197 case HId1:
198 fHslider1->SetPosition(atof(fTbh1->GetString()),
199 fHslider1->GetMaxPosition());
200 break;
201 case HId2:
202 fHslider1->SetPointerPosition(atof(fTbh2->GetString()));
203 break;
204 case HId3:
205 fHslider1->SetPosition(fHslider1->GetMinPosition(),
206 atof(fTbh1->GetString()));
207 break;
208 default:
209 break;
210 }
211 fFitFcn->SetParameters(fHslider1->GetPointerPosition(), 0, 1);
212 fFitFcn->SetRange(fHslider1->GetMinPosition()-0.05,
213 fHslider1->GetMaxPosition());
214 fFitFcn->Draw();
215 fCanvas->GetCanvas()->Modified();
216 fCanvas->GetCanvas()->Update();
217}
218
219//______________________________________________________________________________
220void TTripleSliderDemo::DoSlider()
221{
222 // Handle slider widgets.
223
224 char buf[32];
225
226 sprintf(buf, "%.3f", fHslider1->GetMinPosition());
227 fTbh1->Clear();
228 fTbh1->AddText(0, buf);
229 fTeh1->SetCursorPosition(fTeh1->GetCursorPosition());
230 fTeh1->Deselect();
231 gClient->NeedRedraw(fTeh1);
232
233 sprintf(buf, "%.3f", fHslider1->GetPointerPosition());
234 fTbh2->Clear();
235 fTbh2->AddText(0, buf);
236 fTeh2->SetCursorPosition(fTeh2->GetCursorPosition());
237 fTeh2->Deselect();
238 gClient->NeedRedraw(fTeh2);
239
240 sprintf(buf, "%.3f", fHslider1->GetMaxPosition());
241 fTbh3->Clear();
242 fTbh3->AddText(0, buf);
243 fTeh3->SetCursorPosition(fTeh3->GetCursorPosition());
244 fTeh3->Deselect();
245 gClient->NeedRedraw(fTeh3);
246
247 fFitFcn->SetParameters(fHslider1->GetPointerPosition(), 0, 1);
248 fFitFcn->SetRange(fHslider1->GetMinPosition()-0.05,
249 fHslider1->GetMaxPosition());
250 fFitFcn->Draw();
251 fCanvas->GetCanvas()->Modified();
252 fCanvas->GetCanvas()->Update();
253}
254
255//______________________________________________________________________________
256void TTripleSliderDemo::HandleButtons()
257{
258 // Handle different buttons.
259
260 TGButton *btn = (TGButton *) gTQSender;
261 Int_t id = btn->WidgetId();
262
263 switch (id) {
264 case HCId1:
265 fHslider1->SetConstrained(fCheck1->GetState());
266 break;
267 case HCId2:
268 fHslider1->SetRelative(fCheck2->GetState());
269 break;
270 default:
271 break;
272 }
273}
274
275
276void Slider3Demo()
277{
278 new TTripleSliderDemo();
279}
280
@ kHorizontalFrame
Definition GuiTypes.h:382
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
#define ClassDef(name, id)
Definition Rtypes.h:325
@ kRed
Definition Rtypes.h:66
@ kButtonUp
Definition TGButton.h:53
#define gClient
Definition TGClient.h:166
@ kDoubleScaleBoth
@ kDeepCleanup
Definition TGFrame.h:50
@ kLHintsRight
Definition TGLayout.h:33
@ kLHintsExpandY
Definition TGLayout.h:38
@ kLHintsLeft
Definition TGLayout.h:31
@ kLHintsCenterX
Definition TGLayout.h:32
@ kLHintsTop
Definition TGLayout.h:34
@ kLHintsExpandX
Definition TGLayout.h:37
R__EXTERN void * gTQSender
Definition TQObject.h:46
1-Dim function class
Definition TF1.h:213
virtual void CloseWindow()
Close and delete main frame.
Definition TGFrame.cxx:1731
Int_t WidgetId() const
Definition TGWidget.h:78
TText * text