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