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 "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 
59 TContextMenu::TContextMenu(const char *name, const char *title)
60  : TNamed(name, title)
61 {
62  fSelectedObject = 0;
63  fCalledObject = 0;
64  fSelectedMethod = 0;
65  fBrowser = 0;
66  fSelectedPad = 0;
67  fSelectedCanvas = 0;
69 
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Destroy a context menu.
75 
77 {
78  delete fContextMenuImp;
79 
80  fSelectedMethod = 0;
81  fCalledObject = 0;
82  fSelectedObject = 0;
84  fContextMenuImp = 0;
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 
93 void TContextMenu::Action(TObject *object, TMethod *method)
94 {
95  if (method) {
96  SetMethod( method );
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 {
118  TObject* object;
119  TMethod* method = 0;
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 (!(object->TestBit(kNotDeleted))) {
131  menuitem->SetCall(0,"");
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  char *cmd = Form("((TContextMenu *)0x%lx)->Execute((TObject *)0x%lx,"
156  "(TMethod *)0x%lx,\"\");",
157  (Long_t)this,(Long_t)object,(Long_t)method);
158  //Printf("%s", cmd);
159  gROOT->ProcessLine(cmd);
160  //Execute( object, method, (TObjArray *)NULL );
161 #endif
162  } else {
163 #ifndef WIN32
164  Execute(object, method, Form("(TObject*)0x%lx",(Long_t)fSelectedObject));
165 #else
166  // It is a workaround of the "Dead lock under Windows
167  char *cmd = Form("((TContextMenu *)0x%lx)->Execute((TObject *)0x%lx,"
168  "(TMethod *)0x%lx,(TObject*)0x%lx);",
169  (Long_t)this,(Long_t)object,(Long_t)method,
171  //Printf("%s", cmd);
172  gROOT->ProcessLine(cmd);
173  //Execute( object, method, (TObjArray *)NULL );
174 #endif
175  }
176  }
177  }
178 
179  } else {
180  // Calling a standalone global function
181  TFunction* function = gROOT->GetGlobalFunctionWithPrototype(
182  menuitem->GetFunctionName());
183  //menuitem->GetArgs());
184  if (function) {
185  SetMethod(function);
186  SetCalledObject(0);
187  if ( (function->GetNargs() && menuitem->GetSelfObjectPos() < 0) ||
188  function->GetNargs() > 1) {
189  fContextMenuImp->Dialog(0,function);
190  } else {
191  char* cmd;
192  if (menuitem->GetSelfObjectPos() < 0) {
193  cmd = Form("%s();", menuitem->GetFunctionName());
194  } else {
195  cmd = Form("%s((TObject*)0x%lx);",
196  menuitem->GetFunctionName(), (Long_t)fSelectedObject);
197  }
198  gROOT->ProcessLine(cmd);
199  }
200  }
201  }
202 
203  if (fBrowser) fBrowser->Refresh();
204 }
205 
206 ////////////////////////////////////////////////////////////////////////////////
207 /// Action to be performed when this toggle menu item is selected.
208 
209 void TContextMenu::Action(TObject *object, TToggle *toggle)
210 {
211  if (object && toggle) {
212  TObjectSpy savePad;
213 
214  gROOT->SetSelectedPrimitive(object);
215  if (fSelectedPad && gPad) {
216  savePad.SetObject(gPad);
217  fSelectedPad->cd();
218  }
221 
222  gROOT->SetFromPopUp(kTRUE);
223  toggle->Toggle();
226  if (fSelectedPad)
228  gROOT->SetFromPopUp(kFALSE);
229 
230  if (savePad.GetObject())
231  ((TVirtualPad*)savePad.GetObject())->cd();
232 
233  if (fSelectedCanvas) {
237  }
238  }
239 
240  if (fBrowser) fBrowser->Refresh();
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Create string describing argument (for use in dialog box).
245 
247 {
248  static TString argTitle;
249 
250  if (argument) {
251  argTitle.Form("(%s) %s", argument->GetTitle(), argument->GetName());
252  if (argument->GetDefault() && *(argument->GetDefault())) {
253  argTitle += " [default: ";
254  argTitle += argument->GetDefault();
255  argTitle += "]";
256  }
257  } else
258  argTitle.Clear();
259 
260  return argTitle.Data();
261 }
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// Create title for dialog box retrieving argument values.
265 
266 const char *TContextMenu::CreateDialogTitle(TObject *object, TFunction *method)
267 {
268  static TString methodTitle;
269 
270  if (object && method)
271  methodTitle.Form("%s::%s", object->ClassName(), method->GetName());
272  else if (!object && method)
273  methodTitle.Form("%s", method->GetName());
274  else
275  methodTitle.Clear();
276 
277  return methodTitle.Data();
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// Create title for popup menu.
282 
284 {
285  static TString popupTitle;
286 
287  if (object) {
288  const char* clname = object->IsA()->GetContextMenuTitle();
289  if (!clname[0])
290  clname = object->ClassName();
291 
292  if (!*(object->GetName()) || !strcmp(object->GetName(), object->ClassName())) {
293  popupTitle.Form(" %s ", clname);
294  } else {
295  popupTitle.Form(" %s::%s ", clname, object->GetName());
296  }
297  if (popupTitle.Length() > 60) {
298  popupTitle.Remove(60);
299  popupTitle += "...";
300  }
301  } else
302  popupTitle.Clear();
303 
304  return popupTitle.Data();
305 }
306 
307 ////////////////////////////////////////////////////////////////////////////////
308 /// Display popup.
309 
311 {
312  if (fContextMenuImp)
314 }
315 
316 
317 ////////////////////////////////////////////////////////////////////////////////
318 /// Execute method with specified arguments for specified object.
319 
320 void TContextMenu::Execute(TObject *object, TFunction *method, const char *params)
321 {
322  if (method) {
323  TObjectSpy savePad;
324 
325  gROOT->SetSelectedPrimitive(object);
326  if (fSelectedPad && gPad) {
327  savePad.SetObject(gPad);
328  fSelectedPad->cd();
329  }
332 
333  gROOT->SetFromPopUp(kTRUE);
334  if (object) {
335  object->Execute((char *) method->GetName(), params);
336  } else {
337  char *cmd = Form("%s(%s);", method->GetName(),params);
338  gROOT->ProcessLine(cmd);
339  }
342  if (fSelectedPad)
344  gROOT->SetFromPopUp(kFALSE);
345 
346  if (savePad.GetObject())
347  ((TVirtualPad*)savePad.GetObject())->cd();
348 
349  if (fSelectedCanvas) {
353  }
354  }
355 
356  if (fBrowser) fBrowser->Refresh();
357 }
358 
359 ////////////////////////////////////////////////////////////////////////////////
360 /// Execute method with specified arguments for specified object.
361 
362 void TContextMenu::Execute(TObject *object, TFunction *method, TObjArray *params)
363 {
364  if (method) {
365  TObjectSpy savePad;
366 
367  gROOT->SetSelectedPrimitive(object);
368  if (fSelectedPad && gPad) {
369  savePad.SetObject(gPad);
370  fSelectedPad->cd();
371  }
374 
375  gROOT->SetFromPopUp(kTRUE);
376  if (object) {
377  object->Execute((TMethod*)method, params);
378  } else {
379  TString args;
380  TIter next(params);
381  TObjString *s;
382  while ((s = (TObjString*) next())) {
383  if (!args.IsNull()) args += ",";
384  args += s->String();
385  }
386  char *cmd = Form("%s(%s);", method->GetName(), args.Data());
387  gROOT->ProcessLine(cmd);
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(0);
414  SetObject(obj);
415  SetCanvas(c);
416  SetPad(p);
417 
418  DisplayPopUp(x,y);
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(0);
429  SetPad(0);
430 
431  DisplayPopUp(x,y);
432 }
c
#define c(i)
Definition: RSha256.hxx:119
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:308
TContextMenu::fSelectedMenuItem
TClassMenuItem * fSelectedMenuItem
Definition: TContextMenu.h:57
TContextMenu::fBrowser
TBrowser * fBrowser
Definition: TContextMenu.h:60
TBrowser
Definition: TBrowser.h:37
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TClassMenuItem::kPopupSeparator
@ kPopupSeparator
Definition: TClassMenuItem.h:35
TContextMenu::fSelectedPad
TVirtualPad * fSelectedPad
Definition: TContextMenu.h:59
TObjArray
Definition: TObjArray.h:37
TVirtualPad::Update
virtual void Update()=0
TContextMenu::~TContextMenu
virtual ~TContextMenu()
Destroy a context menu.
Definition: TContextMenu.cxx:76
TClassMenuItem::IsCallSelf
virtual Bool_t IsCallSelf() const
Definition: TClassMenuItem.h:68
TString::Data
const char * Data() const
Definition: TString.h:369
TObjectSpy.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
Form
char * Form(const char *fmt,...)
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
TContextMenu::SetCalledObject
virtual void SetCalledObject(TObject *o)
Definition: TContextMenu.h:98
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:79
TMethodArg
Definition: TMethodArg.h:36
TMethod.h
TContextMenu::SetCanvas
virtual void SetCanvas(TVirtualPad *c)
Definition: TContextMenu.h:95
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:411
TContextMenu::SetBrowser
virtual void SetBrowser(TBrowser *b)
Definition: TContextMenu.h:96
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
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:410
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:79
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
Definition: TString.h:136
TClassMenuItem.h
TContextMenu::fSelectedCanvas
TVirtualPad * fSelectedCanvas
Definition: TContextMenu.h:58
TString::Clear
void Clear()
Clear string without changing its capacity.
Definition: TString.cxx:1176
b
#define b(i)
Definition: RSha256.hxx:118
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
Definition: TToggle.h:47
TROOT.h
TObjString
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
Definition: TContextMenu.h:44
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
Definition: TObjectSpy.h:30
TClassMenuItem
Definition: TClassMenuItem.h:31
TString::Remove
TString & Remove(Ssiz_t pos)
Definition: TString.h:673
TNamed
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TContextMenu::SetMethod
virtual void SetMethod(TFunction *m)
Definition: TContextMenu.h:97
TContextMenu::fContextMenuImp
TContextMenuImp * fContextMenuImp
Definition: TContextMenu.h:53
ROOT::R::function
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
TMethod
Definition: TMethod.h:38
Long_t
long Long_t
Definition: RtypesCore.h:54
TMethodArg::GetDefault
const char * GetDefault() const
Get default value of method argument.
Definition: TMethodArg.cxx:57
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:246
y
Double_t y[n]
Definition: legend1.C:17
TContextMenu::SetObject
virtual void SetObject(TObject *o)
Definition: TContextMenu.h:101
TToggle::Toggle
virtual void Toggle()
Toggles the Values and State of this object and connected data!
Definition: TToggle.cxx:112
TToggle.h
TContextMenu::CreatePopupTitle
virtual const char * CreatePopupTitle(TObject *object)
Create title for popup menu.
Definition: TContextMenu.cxx:283
TBrowser::Refresh
void Refresh()
Refresh browser contents.
Definition: TBrowser.cxx:377
TContextMenu::DisplayPopUp
virtual void DisplayPopUp(Int_t x, Int_t y)
Display popup.
Definition: TContextMenu.cxx:310
TContextMenu::SetSelectedMenuItem
virtual void SetSelectedMenuItem(TClassMenuItem *mi)
Definition: TContextMenu.h:99
TString::IsNull
Bool_t IsNull() const
Definition: TString.h:407
TVirtualPad
Definition: TVirtualPad.h:50
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
Definition: TObject.h:37
TContextMenuImp.h
TContextMenu::Action
virtual void Action(TObject *object, TMethod *method)
Action to be performed when this menu item is selected.
Definition: TContextMenu.cxx:93
name
char name[80]
Definition: TGX11.cxx:110
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:54
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:266
TMethodArg.h
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TObjectSpy::GetObject
TObject * GetObject() const
Definition: TObjectSpy.h:51
TContextMenu::SetPad
virtual void SetPad(TVirtualPad *p)
Definition: TContextMenu.h:102
TContextMenu::fSelectedObject
TObject * fSelectedObject
Definition: TContextMenu.h:55
TGuiFactory.h
TContextMenuImp::DisplayPopup
virtual void DisplayPopup(Int_t x, Int_t y)
Definition: TContextMenuImp.h:59
TContextMenu::fCalledObject
TObject * fCalledObject
Definition: TContextMenu.h:56
TFunction
Definition: TFunction.h:30
gROOT
#define gROOT
Definition: TROOT.h:406
int