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