Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TButton.cxx
Go to the documentation of this file.
1// @(#)root/gpad:$Id$
2// Author: Rene Brun 01/07/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 "TButton.h"
14#include "TCanvas.h"
15#include "TLatex.h"
16
17#include <cstring>
18#include <iostream>
19
21
22/** \class TButton
23\ingroup gpad
24
25A TButton object is a user interface object.
26
27A TButton has a name and an associated action.
28When the button is clicked with the left mouse button, the corresponding
29action is executed.
30
31A TButton can be created by direct invocation of the constructors
32or via the graphics editor.
33
34The action can be set via TButton::SetMethod.
35The action can be any command. Examples of actions:
36 - "34+78" When the button is clicked, the result of addition is printed.
37 - ".x macro.C" . Clicking the button executes the macro macro.C
38The action can be modified at any time via TButton::SetMethod.
39
40To modify the layout/size/contents of one or several buttons
41in a canvas, you must set the canvas editable via TCanvas::SetEditable.
42By default a TCanvas is editable.
43By default a TDialogCanvas is not editable.
44TButtons are in general placed in a TDialogCanvas.
45
46A TButton being a TPad, one can draw graphics primitives in it
47when the TCanvas/TDialogCanvas is editable.
48
49Example of a macro creating a dialog canvas with buttons:
50~~~ {.cpp}
51void but() {
52// example of a dialog canvas with a few buttons
53
54 TDialogCanvas *dialog = new TDialogCanvas("dialog","",200,300);
55
56// Create first button. Clicking on this button will execute 34+56
57 TButton *but1 = new TButton("button1","34+56",.05,.8,.45,.88);
58 but1->Draw();
59
60// Create second button. Clicking on this button will create a new canvas
61 TButton *but2 = new TButton("canvas","c2 = new TCanvas(\"c2\")",.55,.8,.95,.88);
62 but2->Draw();
63
64// Create third button. Clicking on this button will invoke the browser
65 but3 = new TButton("Browser","br = new TBrowser(\"br\")",0.25,0.54,0.75,0.64);
66 but3->SetFillColor(42);
67 but3->Draw();
68
69// Create last button with no name. Instead a graph is draw inside the button
70// Clicking on this button will invoke the macro $ROOTSYS/tutorials/visualisation/graphs/graph.C
71 button = new TButton("",".x tutorials/visualisation/graphs/graph.C",0.15,0.15,0.85,0.38);
72 button->SetFillColor(42);
73 button->Draw();
74 button->SetEditable(kTRUE);
75 button->cd();
76
77 Double_t x[8] = {0.08,0.21,0.34,0.48,0.61,0.7,0.81,0.92};
78 Double_t y[8] = {0.2,0.65,0.4,0.34,0.24,0.43,0.75,0.52};
79 TGraph *graph = new TGraph(8,x,y);
80 graph->SetMarkerColor(4);
81 graph->SetMarkerStyle(21);
82 graph->Draw("lp");
83
84 dialog->cd();
85}
86~~~
87Executing the macro above produces the following dialog canvas:
88
89\image html gpad_dialogbuttons.png
90*/
91
92////////////////////////////////////////////////////////////////////////////////
93/// Button default constructor.
94
96{
98 fMethod = "";
99 fLogx = kFALSE;
100 fLogy = kFALSE;
103 for (UChar_t n = 0; n < 128; ++n)
104 fValidPattern[n] = n;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Button normal constructor.
109///
110/// Note that the button coordinates x1,y1,x2,y2 are always in the range [0,1]
111
112TButton::TButton(const char *title, const char *method, Double_t x1, Double_t y1,Double_t x2, Double_t y2)
113 :TPad("button",title,x1,y1,x2,y2,18,2,1), TAttText(22,0,1,61,0.65)
114{
118 fMethod = method;
119 if (title && strlen(title)) {
120 TLatex *text = new TLatex(0.5 * (fX1 + fX2), 0.5 * (fY1 + fY2), title);
122 }
123 fLogx = 0;
124 fLogy = 0;
127 for (UChar_t n = 0; n < 128; ++n)
128 fValidPattern[n] = n;
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Button default destructor.
133
135{
136 for (UChar_t n = 0; n < 128; ++n)
137 fValidPattern[n] = 255 - n;
138 if (fPrimitives)
140}
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Draw this button with its current attributes.
145
147{
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Execute action corresponding to one event.
153///
154/// This member function is called when a Button object is clicked.
155
157{
158 //check case where pressing a button deletes itself
159 if (ROOT::Detail::HasBeenDeleted(this)) return;
160
161 if (IsEditable()) {
162 TPad::ExecuteEvent(event,px,py);
163 return;
164 }
165
166 auto cdpad = gROOT->GetSelectedPad();
167 auto patt = fValidPattern;
168 HideToolTip(event);
169
170 switch (event) {
171
172 case kMouseEnter:
173 TPad::ExecuteEvent(event,px,py);
174 break;
175
176 case kButton1Down:
177 SetBorderMode(-1);
178 fFocused = kTRUE;
179 Modified();
180 Update();
181 break;
182
183 case kMouseMotion:
184
185 break;
186
187 case kButton1Motion:
188 if (px<XtoAbsPixel(1) && px>XtoAbsPixel(0) &&
189 py<YtoAbsPixel(0) && py>YtoAbsPixel(1)) {
190 if (!fFocused) {
191 SetBorderMode(-1);
192 fFocused = kTRUE;
193 Modified();
194 GetCanvas()->Modified();
195 Update();
196 }
197 } else if (fFocused) {
198 SetBorderMode(1);
200 Modified();
201 GetCanvas()->Modified();
202 Update();
203 }
204 break;
205
206 case kButton1Up:
208 if (fFocused) {
209 if (cdpad) cdpad->cd();
210 gROOT->ProcessLine(GetMethod());
211 }
212 //check case where pressing a button deletes itself
214 return;
215
216 // extra check when simple one does not work
217 for (UChar_t n = 0; n < 128; ++n)
218 if (patt[n] != n)
219 return;
220
221 SetBorderMode(1);
222 Modified();
223 Update();
225 break;
226 }
227}
228
229////////////////////////////////////////////////////////////////////////////////
230/// Paint this button with its current attributes.
231
233{
234 if (!fCanvas) return;
235 if (!fPrimitives) fPrimitives = new TList();
237 if (obj && obj->InheritsFrom(TLatex::Class())) {
238 TLatex *text = (TLatex*)obj;
239 text->SetTitle(GetTitle());
240 text->SetTextSize(GetTextSize());
241 text->SetTextFont(GetTextFont());
242 text->SetTextAlign(GetTextAlign());
243 text->SetTextColor(GetTextColor());
244 text->SetTextAngle(GetTextAngle());
245 }
246 SetLogx(0);
247 SetLogy(0);
248 TPad::Paint(option); //only called for Postscript print
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Paint is modified.
253
255{
256 if (!fCanvas) return;
257 if (!fPrimitives) fPrimitives = new TList();
259 if (obj && obj->InheritsFrom(TLatex::Class())) {
260 TLatex *text = (TLatex*)obj;
261 text->SetTitle(GetTitle());
262 text->SetTextSize(GetTextSize());
263 text->SetTextFont(GetTextFont());
264 text->SetTextAlign(GetTextAlign());
265 text->SetTextColor(GetTextColor());
266 text->SetTextAngle(GetTextAngle());
267 }
268 SetLogx(0);
269 SetLogy(0);
271}
272
273////////////////////////////////////////////////////////////////////////////////
274/// Set world coordinate system for the pad.
275
277{
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// Save primitive as a C++ statement(s) on output stream out
283
284void TButton::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
285{
286 char quote = '"';
287 if (gROOT->ClassSaved(TButton::Class()))
288 out<<" ";
289 else
290 out<<" TButton *";
291
292 TString cmethod = GetMethod();
293
294 out << "button = new TButton(" << quote << GetTitle() << quote << ","
295 << quote << cmethod.ReplaceSpecialCppChars() << quote << "," << fXlowNDC << "," << fYlowNDC
296 << "," << fXlowNDC + fWNDC << "," << fYlowNDC + fHNDC << ");"
297 << std::endl;
298
299 SaveFillAttributes(out,"button",0,1001);
300 SaveLineAttributes(out,"button",1,1,1);
301 SaveTextAttributes(out,"button",22,0,1,61,.65);
302
303 if (GetBorderSize() != 2)
304 out<<" button->SetBorderSize("<<GetBorderSize()<<");"<<std::endl;
305 if (GetBorderMode() != 1)
306 out<<" button->SetBorderMode("<<GetBorderMode()<<");"<<std::endl;
307
308 if (GetFraming()) out<<"button->SetFraming();"<<std::endl;
309 if (IsEditable()) out<<"button->SetEditable(kTRUE);"<<std::endl;
310
311 out<<" button->Draw();"<<std::endl;
312
314 next(); //do not save first primitive which should be text
315
316 Int_t nprim = 0;
317 while (auto obj = next()) {
318 if (nprim++ == 0)
319 out<<" button->cd();"<<std::endl;
320 obj->SavePrimitive(out, next.GetOption());
321 }
322
323 if ((nprim > 0) && gPad)
324 out<<" "<<gPad->GetName()<<"->cd();"<<std::endl;
325}
326
327////////////////////////////////////////////////////////////////////////////////
328/// if framing is set, button will be highlighted
329
331{
332 fFraming = f;
333 if (f) SetBit(kFraming);
334 else ResetBit(kFraming);
335}
@ kMouseMotion
Definition Buttons.h:23
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kButton1Down
Definition Buttons.h:17
@ kMouseEnter
Definition Buttons.h:23
@ kWatch
Definition GuiTypes.h:375
@ kCross
Definition GuiTypes.h:374
#define f(i)
Definition RSha256.hxx:104
unsigned char UChar_t
Definition RtypesCore.h:38
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t option
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void SetCursor
Option_t Option_t TPoint TPoint const char text
Option_t Option_t TPoint TPoint const char y1
#define gROOT
Definition TROOT.h:406
#define gPad
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:239
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:275
Text Attributes class.
Definition TAttText.h:18
virtual Float_t GetTextSize() const
Return the text size.
Definition TAttText.h:36
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition TAttText.h:32
virtual Font_t GetTextFont() const
Return the text font.
Definition TAttText.h:35
virtual Color_t GetTextColor() const
Return the text color.
Definition TAttText.h:34
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:33
virtual void SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef=12, Float_t angdef=0, Int_t coldef=1, Int_t fondef=61, Float_t sizdef=1)
Save text attributes as C++ statement(s) on output stream out.
Definition TAttText.cxx:373
A TButton object is a user interface object.
Definition TButton.h:18
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TButton.cxx:156
void SetLogy(Int_t=1) override
Definition TButton.h:48
virtual Bool_t GetFraming()
Definition TButton.h:45
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TButton.cxx:276
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TButton.cxx:284
virtual const char * GetMethod() const
Definition TButton.h:38
void SetLogx(Int_t=1) override
Definition TButton.h:47
void Draw(Option_t *option="") override
Draw this button with its current attributes.
Definition TButton.cxx:146
Bool_t fFraming
True if you want a frame to be painted when pressed.
Definition TButton.h:22
void Paint(Option_t *option="") override
Paint this button with its current attributes.
Definition TButton.cxx:232
static TClass * Class()
void PaintModified() override
Paint is modified.
Definition TButton.cxx:254
Bool_t fFocused
If cursor is in...
Definition TButton.h:21
~TButton() override
Button default destructor.
Definition TButton.cxx:134
void SetBorderMode(Short_t bordermode) override
Definition TButton.h:43
UChar_t fValidPattern[128]
! pattern in memory to detect button deletion
Definition TButton.h:23
virtual void SetFraming(Bool_t f=kTRUE)
if framing is set, button will be highlighted
Definition TButton.cxx:330
TButton()
Button default constructor.
Definition TButton.cxx:95
TString fMethod
Method to be executed by this button.
Definition TButton.h:29
Option_t * GetOption() const
To draw Mathematical Formula.
Definition TLatex.h:18
static TClass * Class()
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:657
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
Mother of all ROOT objects.
Definition TObject.h:41
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:202
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:798
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:542
void ResetBit(UInt_t f)
Definition TObject.h:198
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:62
The most important graphics class in the ROOT system.
Definition TPad.h:28
Short_t GetBorderMode() const override
Definition TPad.h:199
virtual void HideToolTip(Int_t event)
Hide tool tip depending on the event type.
Definition TPad.cxx:2847
Double_t fWNDC
Width of pad along X in Normalized Coordinates (NDC)
Definition TPad.h:66
Bool_t IsEditable() const override
Definition TPad.h:272
Double_t fX2
X of upper X coordinate.
Definition TPad.h:38
void PaintModified() override
Traverse pad hierarchy and (re)paint only modified pads.
Definition TPad.cxx:3812
void SetEditable(Bool_t mode=kTRUE) override
Set pad editable yes/no If a pad is not editable:
Definition TPad.cxx:6055
const char * GetTitle() const override
Returns title of object.
Definition TPad.h:261
Double_t fX1
X of lower X coordinate.
Definition TPad.h:36
TList * GetListOfPrimitives() const override
Definition TPad.h:245
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:5331
Double_t fY1
Y of lower Y coordinate.
Definition TPad.h:37
Double_t fYlowNDC
Y bottom left corner of pad in NDC [0,1].
Definition TPad.h:63
Bool_t fModified
Set to true when pad is modified.
Definition TPad.h:99
void Update() override
Update pad.
Definition TPad.cxx:2935
TCanvas * fCanvas
! Pointer to mother canvas
Definition TPad.h:106
void Modified(Bool_t flag=true) override
Mark pad modified Will be repainted when TCanvas::Update() will be called next time.
Definition TPad.cxx:7388
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TPad.cxx:1786
Short_t GetBorderSize() const override
Definition TPad.h:200
Int_t fLogx
(=0 if X linear scale, =1 if log scale)
Definition TPad.h:91
Int_t YtoAbsPixel(Double_t y) const override
Convert Y coordinate to absolute pixel.
Definition TPad.cxx:7518
TList * fPrimitives
->List of primitives (subpads)
Definition TPad.h:107
TCanvas * GetCanvas() const override
Definition TPad.h:262
void Paint(Option_t *option="") override
Paint all primitives in pad.
Definition TPad.cxx:3584
Int_t fLogy
(=0 if Y linear scale, =1 if log scale)
Definition TPad.h:92
Double_t fHNDC
Height of pad along Y in Normalized Coordinates (NDC)
Definition TPad.h:67
Double_t fXlowNDC
X bottom left corner of pad in NDC [0,1].
Definition TPad.h:62
Double_t fY2
Y of upper Y coordinate.
Definition TPad.h:39
@ kFraming
Frame is requested.
Definition TPad.h:154
Int_t XtoAbsPixel(Double_t x) const override
Convert X coordinate to absolute pixel.
Definition TPad.cxx:7494
Basic string class.
Definition TString.h:139
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1114
const Int_t n
Definition legend1.C:16
R__ALWAYS_INLINE bool HasBeenDeleted(const TObject *obj)
Check if the TObject's memory has been deleted.
Definition TObject.h:402