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
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Create a context menu.
57
58TContextMenu::TContextMenu(const char *name, const char *title)
59 : TNamed(name, title)
60{
61 fSelectedObject = nullptr;
62 fCalledObject = nullptr;
63 fSelectedMethod = nullptr;
64 fBrowser = nullptr;
65 fSelectedPad = nullptr;
66 fSelectedCanvas = nullptr;
67 fSelectedMenuItem = nullptr;
68
69 fContextMenuImp = gGuiFactory->CreateContextMenuImp(this, name, title);
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Destroy a context menu.
74
76{
77 delete fContextMenuImp;
78
79 fSelectedMethod = nullptr;
80 fCalledObject = nullptr;
81 fSelectedObject = nullptr;
82 fSelectedMenuItem = nullptr;
83 fContextMenuImp = nullptr;
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
92void TContextMenu::Action(TObject *object, TMethod *method)
93{
94 if (method) {
95 SetMethod( method );
96 SetSelectedMenuItem(nullptr);
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{
118 TMethod* method = nullptr;
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 (ROOT::Detail::HasBeenDeleted(object)) {
130 menuitem->SetCall(nullptr, "");
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 TString cmd;
155 cmd.Form("((TContextMenu *)0x%zx)->Execute((TObject *)0x%zx,"
156 "(TMethod *)0x%zx,\"\");",
157 (size_t)this,(size_t)object,(size_t)method);
158 //Printf("%s", cmd.Data());
159 gROOT->ProcessLine(cmd.Data());
160 //Execute( object, method, (TObjArray *)NULL );
161#endif
162 } else {
163#ifndef WIN32
164 Execute(object, method, TString::Format("(TObject*)0x%zx",(size_t)fSelectedObject).Data());
165#else
166 // It is a workaround of the "Dead lock under Windows
167 TString cmd;
168 cmd.Form("((TContextMenu *)0x%zx)->Execute((TObject *)0x%zx,"
169 "(TMethod *)0x%zx,(TObject*)0x%zx);",
170 (size_t)this,(size_t)object,(size_t)method,
171 (size_t)fSelectedObject);
172 //Printf("%s", cmd.Data());
173 gROOT->ProcessLine(cmd.Data());
174 //Execute( object, method, (TObjArray *)NULL );
175#endif
176 }
177 }
178 }
179
180 } else {
181 // Calling a standalone global function
182 TFunction* function = gROOT->GetGlobalFunctionWithPrototype(
183 menuitem->GetFunctionName());
184 //menuitem->GetArgs());
185 if (function) {
186 SetMethod(function);
187 SetCalledObject(nullptr);
188 if ( (function->GetNargs() && menuitem->GetSelfObjectPos() < 0) ||
189 function->GetNargs() > 1) {
190 fContextMenuImp->Dialog(nullptr, function);
191 } else {
192 TString cmd;
193 if (menuitem->GetSelfObjectPos() < 0) {
194 cmd.Form("%s();", menuitem->GetFunctionName());
195 } else {
196 cmd.Form("%s((TObject*)0x%zx);",
197 menuitem->GetFunctionName(), (size_t)fSelectedObject);
198 }
199 gROOT->ProcessLine(cmd.Data());
200 }
201 }
202 }
203
204 if (fBrowser) fBrowser->Refresh();
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// Action to be performed when this toggle menu item is selected.
209
211{
212 if (object && toggle) {
213 TObjectSpy savePad;
214
215 gROOT->SetSelectedPrimitive(object);
216 if (fSelectedPad && gPad) {
217 savePad.SetObject(gPad);
218 fSelectedPad->cd();
219 }
222
223 gROOT->SetFromPopUp(kTRUE);
224 toggle->Toggle();
225 if (fSelectedCanvas && fSelectedCanvas->GetPadSave())
226 fSelectedCanvas->GetPadSave()->Modified();
227 if (fSelectedPad)
228 fSelectedPad->Modified();
229 gROOT->SetFromPopUp(kFALSE);
230
231 if (savePad.GetObject())
232 ((TVirtualPad*)savePad.GetObject())->cd();
233
234 if (fSelectedCanvas) {
235 fSelectedCanvas->Update();
236 if (fSelectedCanvas->GetPadSave())
237 fSelectedCanvas->GetPadSave()->Update();
238 }
239 }
240
241 if (fBrowser) fBrowser->Refresh();
242}
243
244////////////////////////////////////////////////////////////////////////////////
245/// Create string describing argument (for use in dialog box).
246
248{
249 static TString argTitle;
250
251 if (argument) {
252 argTitle.Form("(%s) %s", argument->GetTitle(), argument->GetName());
253 if (argument->GetDefault() && *(argument->GetDefault())) {
254 argTitle += " [default: ";
255 argTitle += argument->GetDefault();
256 argTitle += "]";
257 }
258 } else
259 argTitle.Clear();
260
261 return argTitle.Data();
262}
263
264////////////////////////////////////////////////////////////////////////////////
265/// Create title for dialog box retrieving argument values.
266
268{
269 static TString methodTitle;
270
271 if (object && method)
272 methodTitle.Form("%s::%s", object->ClassName(), method->GetName());
273 else if (!object && method)
274 methodTitle.Form("%s", method->GetName());
275 else
276 methodTitle.Clear();
277
278 return methodTitle.Data();
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// Create title for popup menu.
283
285{
286 static TString popupTitle;
287
288 if (object) {
289 const char* clname = object->IsA()->GetContextMenuTitle();
290 if (!clname[0])
291 clname = object->ClassName();
292
293 if (!*(object->GetName()) || !strcmp(object->GetName(), object->ClassName())) {
294 popupTitle.Form(" %s ", clname);
295 } else {
296 popupTitle.Form(" %s::%s ", clname, object->GetName());
297 }
298 if (popupTitle.Length() > 60) {
299 popupTitle.Remove(60);
300 popupTitle += "...";
301 }
302 } else
303 popupTitle.Clear();
304
305 return popupTitle.Data();
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// Display popup.
310
312{
313 if (fContextMenuImp)
314 fContextMenuImp->DisplayPopup(x, y);
315}
316
317
318////////////////////////////////////////////////////////////////////////////////
319/// Execute method with specified arguments for specified object.
320
321void TContextMenu::Execute(TObject *object, TFunction *method, const char *params)
322{
323 if (method) {
324 TObjectSpy savePad;
325
326 gROOT->SetSelectedPrimitive(object);
327 if (fSelectedPad && gPad) {
328 savePad.SetObject(gPad);
329 fSelectedPad->cd();
330 }
333
334 gROOT->SetFromPopUp(kTRUE);
335 if (object) {
336 object->Execute(method->GetName(), params);
337 } else {
338 gROOT->ProcessLine(TString::Format("%s(%s);", method->GetName(),params).Data());
339 }
340 if (fSelectedCanvas && fSelectedCanvas->GetPadSave())
341 fSelectedCanvas->GetPadSave()->Modified();
342 if (fSelectedPad)
343 fSelectedPad->Modified();
344 gROOT->SetFromPopUp(kFALSE);
345
346 if (savePad.GetObject())
347 ((TVirtualPad*)savePad.GetObject())->cd();
348
349 if (fSelectedCanvas) {
350 fSelectedCanvas->Update();
351 if (fSelectedCanvas->GetPadSave())
352 fSelectedCanvas->GetPadSave()->Update();
353 }
354 }
355
356 if (fBrowser) fBrowser->Refresh();
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Execute method with specified arguments for specified object.
361
362void 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 gROOT->ProcessLine(TString::Format("%s(%s);", method->GetName(), args.Data()).Data());
387 }
388 if (fSelectedCanvas && fSelectedCanvas->GetPadSave())
389 fSelectedCanvas->GetPadSave()->Modified();
390 if (fSelectedPad)
391 fSelectedPad->Modified();
392 gROOT->SetFromPopUp(kFALSE);
393
394 if (savePad.GetObject())
395 ((TVirtualPad*)savePad.GetObject())->cd();
396
397 if (fSelectedCanvas) {
398 fSelectedCanvas->Update();
399 if (fSelectedCanvas->GetPadSave())
400 fSelectedCanvas->GetPadSave()->Update();
401 }
402 }
403 if (fBrowser) fBrowser->Refresh();
404}
405
406////////////////////////////////////////////////////////////////////////////////
407/// Popup context menu at given location in canvas c and pad p for selected
408/// object.
409
411{
412 SetBrowser(nullptr);
413 SetObject(obj);
414 SetCanvas(c);
415 SetPad(p);
416
418}
419
420////////////////////////////////////////////////////////////////////////////////
421/// Popup context menu at given location in browser b for selected object.
422
424{
425 SetBrowser(b);
426 SetObject(obj);
427 SetCanvas(nullptr);
428 SetPad(nullptr);
429
431}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
char name[80]
Definition TGX11.cxx:148
externTGuiFactory * gGuiFactory
Definition TGuiFactory.h:66
#define gROOT
Definition TROOT.h:417
#define gPad
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
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:4514
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
virtual void SetCanvas(TVirtualPad *c)
TContextMenuImp * fContextMenuImp
!Context menu system specific implementation
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
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
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:789
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:306
TClass * IsA() const override
Definition TMethod.h:68
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TNamed()
Definition TNamed.h:38
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.
TObject()
TObject constructor.
Definition TObject.h:259
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1241
const char * Data() const
Definition TString.h:384
Bool_t IsNull() const
Definition TString.h:422
TString & Remove(Ssiz_t pos)
Definition TString.h:694
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:2385
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
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:111
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
bool HasBeenDeleted(const TObject *obj)
Check if the TObject's memory has been deleted.
Definition TObject.h:409