Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TContextMenu.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Nenad Buncic 08/02/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/** \class TContextMenu
13\ingroup Base
14
15This class provides an interface to context sensitive popup menus.
16These menus pop up when the user hits the right mouse button, and
17are destroyed when the menu pops downs.
18
19Context Menus are automatically generated by ROOT using the
20following convention: if the string `// *MENU*` is found in the
21comment field of a member function. This function will be added to
22the list of items in the menu.
23
24The picture below shows a canvas with a pop-up menu.
25
26\image html base_hsummenu.png
27
28The picture below shows a canvas with a pop-up menu and a dialog box.
29
30\image html base_hsumdialog.png
31*/
32
33// silence warning about some cast operations
34#if defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ >= 4 && ((__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ >= 1) || (__GNUC_MINOR__ >= 3)))) && !__INTEL_COMPILER
35#pragma GCC diagnostic ignored "-Wstrict-aliasing"
36#endif
37
38#include "TROOT.h"
39#include "TContextMenu.h"
40#include "TContextMenuImp.h"
41#include "TVirtualPad.h"
42#include "TGuiFactory.h"
43#include "TMethod.h"
44#include "TMethodArg.h"
45#include "TObjArray.h"
46#include "TObjString.h"
47#include "TToggle.h"
48#include "TClassMenuItem.h"
49#include "TBrowser.h"
50#include "TClass.h"
51#include "TObjectSpy.h"
52
54
55
56////////////////////////////////////////////////////////////////////////////////
57/// Create a context menu.
58
59TContextMenu::TContextMenu(const char *name, const char *title)
60 : TNamed(name, title)
61{
62 fSelectedObject = nullptr;
63 fCalledObject = nullptr;
64 fSelectedMethod = nullptr;
65 fBrowser = nullptr;
66 fSelectedPad = nullptr;
67 fSelectedCanvas = nullptr;
68 fSelectedMenuItem = nullptr;
69
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// Destroy a context menu.
75
77{
78 delete fContextMenuImp;
79
80 fSelectedMethod = nullptr;
81 fCalledObject = nullptr;
82 fSelectedObject = nullptr;
83 fSelectedMenuItem = nullptr;
84 fContextMenuImp = nullptr;
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Action to be performed when this menu item is selected.
89/// If the selected method requires arguments we popup an
90/// automatically generated dialog, otherwise the method is
91/// directly executed.
92
93void TContextMenu::Action(TObject *object, TMethod *method)
94{
95 if (method) {
96 SetMethod( method );
97 SetSelectedMenuItem(nullptr);
98 SetCalledObject(object);
99
100 if (method->GetListOfMethodArgs()->First())
101 fContextMenuImp->Dialog(object, method);
102 else {
103 Execute(object, method, "");
104 }
105 }
106
107 if (fBrowser) fBrowser->Refresh();
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// Action to be performed when this menu item is selected.
112/// If the selected method requires arguments we popup an
113/// automatically generated dialog, otherwise the method is
114/// directly executed.
115
117{
119 TMethod* method = nullptr;
120
121 SetSelectedMenuItem( menuitem );
122
123 // Get the object to be called
124 if (menuitem->IsCallSelf()) object=fSelectedObject;
125 else object=menuitem->GetCalledObject();
126
127 if (object) {
128 // If object deleted, remove from popup and return
129 if (ROOT::Detail::HasBeenDeleted(object)) {
131 menuitem->SetCall(nullptr, "");
132 return;
133 }
134
135 method = object->IsA()->GetMethodWithPrototype(menuitem->GetFunctionName(),menuitem->GetArgs());
136
137 }
138
139 // calling object, call the method directly
140 if (object) {
141 if (method) {
142 SetMethod(method);
143 SetCalledObject(object);
144
145 if ((method->GetListOfMethodArgs()->First()
146 && menuitem->GetSelfObjectPos() < 0 ) ||
147 method->GetListOfMethodArgs()->GetSize() > 1)
148 fContextMenuImp->Dialog(object, method);
149 else {
150 if (menuitem->GetSelfObjectPos() < 0) {
151#ifndef WIN32
152 Execute(object, method, "");
153#else
154 // It is a workaround of the "Dead lock under Windows
155 TString cmd;
156 cmd.Form("((TContextMenu *)0x%zx)->Execute((TObject *)0x%zx,"
157 "(TMethod *)0x%zx,\"\");",
158 (size_t)this,(size_t)object,(size_t)method);
159 //Printf("%s", cmd.Data());
160 gROOT->ProcessLine(cmd.Data());
161 //Execute( object, method, (TObjArray *)NULL );
162#endif
163 } else {
164#ifndef WIN32
165 Execute(object, method, TString::Format("(TObject*)0x%zx",(size_t)fSelectedObject).Data());
166#else
167 // It is a workaround of the "Dead lock under Windows
168 TString cmd;
169 cmd.Form("((TContextMenu *)0x%zx)->Execute((TObject *)0x%zx,"
170 "(TMethod *)0x%zx,(TObject*)0x%zx);",
171 (size_t)this,(size_t)object,(size_t)method,
172 (size_t)fSelectedObject);
173 //Printf("%s", cmd.Data());
174 gROOT->ProcessLine(cmd.Data());
175 //Execute( object, method, (TObjArray *)NULL );
176#endif
177 }
178 }
179 }
180
181 } else {
182 // Calling a standalone global function
183 TFunction* function = gROOT->GetGlobalFunctionWithPrototype(
184 menuitem->GetFunctionName());
185 //menuitem->GetArgs());
186 if (function) {
187 SetMethod(function);
188 SetCalledObject(nullptr);
189 if ( (function->GetNargs() && menuitem->GetSelfObjectPos() < 0) ||
190 function->GetNargs() > 1) {
191 fContextMenuImp->Dialog(nullptr, function);
192 } else {
193 TString cmd;
194 if (menuitem->GetSelfObjectPos() < 0) {
195 cmd.Form("%s();", menuitem->GetFunctionName());
196 } else {
197 cmd.Form("%s((TObject*)0x%zx);",
198 menuitem->GetFunctionName(), (size_t)fSelectedObject);
199 }
200 gROOT->ProcessLine(cmd.Data());
201 }
202 }
203 }
204
205 if (fBrowser) fBrowser->Refresh();
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Action to be performed when this toggle menu item is selected.
210
212{
213 if (object && toggle) {
214 TObjectSpy savePad;
215
216 gROOT->SetSelectedPrimitive(object);
217 if (fSelectedPad && gPad) {
218 savePad.SetObject(gPad);
219 fSelectedPad->cd();
220 }
223
224 gROOT->SetFromPopUp(kTRUE);
225 toggle->Toggle();
228 if (fSelectedPad)
230 gROOT->SetFromPopUp(kFALSE);
231
232 if (savePad.GetObject())
233 ((TVirtualPad*)savePad.GetObject())->cd();
234
235 if (fSelectedCanvas) {
239 }
240 }
241
242 if (fBrowser) fBrowser->Refresh();
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// Create string describing argument (for use in dialog box).
247
249{
250 static TString argTitle;
251
252 if (argument) {
253 argTitle.Form("(%s) %s", argument->GetTitle(), argument->GetName());
254 if (argument->GetDefault() && *(argument->GetDefault())) {
255 argTitle += " [default: ";
256 argTitle += argument->GetDefault();
257 argTitle += "]";
258 }
259 } else
260 argTitle.Clear();
261
262 return argTitle.Data();
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// Create title for dialog box retrieving argument values.
267
269{
270 static TString methodTitle;
271
272 if (object && method)
273 methodTitle.Form("%s::%s", object->ClassName(), method->GetName());
274 else if (!object && method)
275 methodTitle.Form("%s", method->GetName());
276 else
277 methodTitle.Clear();
278
279 return methodTitle.Data();
280}
281
282////////////////////////////////////////////////////////////////////////////////
283/// Create title for popup menu.
284
286{
287 static TString popupTitle;
288
289 if (object) {
290 const char* clname = object->IsA()->GetContextMenuTitle();
291 if (!clname[0])
292 clname = object->ClassName();
293
294 if (!*(object->GetName()) || !strcmp(object->GetName(), object->ClassName())) {
295 popupTitle.Form(" %s ", clname);
296 } else {
297 popupTitle.Form(" %s::%s ", clname, object->GetName());
298 }
299 if (popupTitle.Length() > 60) {
300 popupTitle.Remove(60);
301 popupTitle += "...";
302 }
303 } else
304 popupTitle.Clear();
305
306 return popupTitle.Data();
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Display popup.
311
313{
314 if (fContextMenuImp)
316}
317
318
319////////////////////////////////////////////////////////////////////////////////
320/// Execute method with specified arguments for specified object.
321
322void TContextMenu::Execute(TObject *object, TFunction *method, const char *params)
323{
324 if (method) {
325 TObjectSpy savePad;
326
327 gROOT->SetSelectedPrimitive(object);
328 if (fSelectedPad && gPad) {
329 savePad.SetObject(gPad);
330 fSelectedPad->cd();
331 }
334
335 gROOT->SetFromPopUp(kTRUE);
336 if (object) {
337 object->Execute(method->GetName(), params);
338 } else {
339 gROOT->ProcessLine(TString::Format("%s(%s);", method->GetName(),params).Data());
340 }
343 if (fSelectedPad)
345 gROOT->SetFromPopUp(kFALSE);
346
347 if (savePad.GetObject())
348 ((TVirtualPad*)savePad.GetObject())->cd();
349
350 if (fSelectedCanvas) {
354 }
355 }
356
357 if (fBrowser) fBrowser->Refresh();
358}
359
360////////////////////////////////////////////////////////////////////////////////
361/// Execute method with specified arguments for specified object.
362
363void TContextMenu::Execute(TObject *object, TFunction *method, TObjArray *params)
364{
365 if (method) {
366 TObjectSpy savePad;
367
368 gROOT->SetSelectedPrimitive(object);
369 if (fSelectedPad && gPad) {
370 savePad.SetObject(gPad);
371 fSelectedPad->cd();
372 }
375
376 gROOT->SetFromPopUp(kTRUE);
377 if (object) {
378 object->Execute((TMethod*)method, params);
379 } else {
380 TString args;
381 TIter next(params);
382 TObjString *s;
383 while ((s = (TObjString*) next())) {
384 if (!args.IsNull()) args += ",";
385 args += s->String();
386 }
387 gROOT->ProcessLine(TString::Format("%s(%s);", method->GetName(), args.Data()).Data());
388 }
391 if (fSelectedPad)
393 gROOT->SetFromPopUp(kFALSE);
394
395 if (savePad.GetObject())
396 ((TVirtualPad*)savePad.GetObject())->cd();
397
398 if (fSelectedCanvas) {
402 }
403 }
404 if (fBrowser) fBrowser->Refresh();
405}
406
407////////////////////////////////////////////////////////////////////////////////
408/// Popup context menu at given location in canvas c and pad p for selected
409/// object.
410
412{
413 SetBrowser(nullptr);
414 SetObject(obj);
415 SetCanvas(c);
416 SetPad(p);
417
419}
420
421////////////////////////////////////////////////////////////////////////////////
422/// Popup context menu at given location in browser b for selected object.
423
425{
426 SetBrowser(b);
427 SetObject(obj);
428 SetCanvas(nullptr);
429 SetPad(nullptr);
430
432}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:382
winID h TVirtualViewer3D TVirtualGLPainter p
char name[80]
Definition TGX11.cxx:110
R__EXTERN TGuiFactory * gGuiFactory
Definition TGuiFactory.h:66
#define gROOT
Definition TROOT.h:406
#define gPad
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
void Refresh()
Refresh browser contents.
Definition TBrowser.cxx:419
Describes one element of the context menu associated to a class The menu item may describe.
virtual const char * GetArgs() const
virtual void SetType(Int_t type)
virtual Bool_t IsCallSelf() const
virtual TObject * GetCalledObject() const
virtual void SetCall(TObject *obj, const char *method, const char *args="", Int_t selfobjposition=0)
virtual const char * GetFunctionName() const
virtual Int_t GetSelfObjectPos() const
TMethod * GetMethodWithPrototype(const char *method, const char *proto, Bool_t objectIsConst=kFALSE, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Find the method with a given prototype.
Definition TClass.cxx:4525
const char * GetContextMenuTitle() const
Definition TClass.h:438
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
virtual void Dialog(TObject *object, TFunction *function)
virtual void DisplayPopup(Int_t x, Int_t y)
This class provides an interface to context sensitive popup menus.
virtual void SetCanvas(TVirtualPad *c)
TContextMenuImp * fContextMenuImp
virtual void SetCalledObject(TObject *o)
virtual ~TContextMenu()
Destroy a context menu.
virtual void Action(TObject *object, TMethod *method)
Action to be performed when this menu item is selected.
virtual void Popup(Int_t x, Int_t y, TObject *obj, TVirtualPad *c=nullptr, TVirtualPad *p=nullptr)
Popup context menu at given location in canvas c and pad p for selected object.
TBrowser * fBrowser
virtual void SetPad(TVirtualPad *p)
TFunction * fSelectedMethod
Context menu system specific implementation.
virtual void SetMethod(TFunction *m)
virtual void SetObject(TObject *o)
virtual const char * CreateArgumentTitle(TMethodArg *argument)
Create string describing argument (for use in dialog box).
virtual void DisplayPopUp(Int_t x, Int_t y)
Display popup.
TVirtualPad * fSelectedPad
TObject * fSelectedObject
TVirtualPad * fSelectedCanvas
virtual const char * CreateDialogTitle(TObject *object, TFunction *method)
Create title for dialog box retrieving argument values.
TObject * fCalledObject
virtual const char * CreatePopupTitle(TObject *object)
Create title for popup menu.
void Execute(const char *method, const char *params, Int_t *error=nullptr) override
Execute method on this object with the given parameter string, e.g.
virtual void SetSelectedMenuItem(TClassMenuItem *mi)
virtual void SetBrowser(TBrowser *b)
TClassMenuItem * fSelectedMenuItem
Global functions class (global functions are obtained from CINT).
Definition TFunction.h:30
virtual TContextMenuImp * CreateContextMenuImp(TContextMenu *c, const char *name, const char *title)
Create a batch version of TContextMenuImp.
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:657
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition TMethodArg.h:36
const char * GetDefault() const
Get default value of method argument.
Each ROOT class (see TClass) has a linked list of methods.
Definition TMethod.h:38
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();.
Definition TMethod.cxx:307
TClass * IsA() const override
Definition TMethod.h:68
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
An array of TObjects.
Definition TObjArray.h:31
Collectable string class.
Definition TObjString.h:28
TString & String()
Definition TObjString.h:48
Monitors objects for deletion and reflects the deletion by reverting the internal pointer to zero.
Definition TObjectSpy.h:30
TObject * GetObject() const
Definition TObjectSpy.h:45
void SetObject(TObject *obj, Bool_t fixMustCleanupBit=kTRUE)
Set obj as the spy target.
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1235
const char * Data() const
Definition TString.h:376
virtual TClass * IsA() const
Definition TString.h:473
Bool_t IsNull() const
Definition TString.h:414
TString & Remove(Ssiz_t pos)
Definition TString.h:685
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
This class defines toggling facility for both - object's method or variables.
Definition TToggle.h:47
virtual void Toggle()
Toggles the Values and State of this object and connected data!
Definition TToggle.cxx:112
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void Modified(Bool_t flag=1)=0
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
virtual void Update()=0
virtual TVirtualPad * GetPadSave() const =0
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
R__ALWAYS_INLINE bool HasBeenDeleted(const TObject *obj)
Check if the TObject's memory has been deleted.
Definition TObject.h:402