Logo ROOT  
Reference Guide
RMenuItems.cxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
3  * All rights reserved. *
4  * *
5  * For the licensing terms see $ROOTSYS/LICENSE. *
6  * For the list of contributors see $ROOTSYS/README/CREDITS. *
7  *************************************************************************/
8 
9 #include "ROOT/RMenuItems.hxx"
10 
11 #include "ROOT/RDrawable.hxx"
12 
13 #include "TROOT.h"
14 #include "TString.h"
15 #include "TClass.h"
16 #include "TList.h"
17 #include "TMethod.h"
18 #include "TMethodArg.h"
19 #include "TMethodCall.h"
20 
21 using namespace ROOT::Experimental;
22 
23 //////////////////////////////////////////////////////////
24 /// destructor - pin vtable
25 
26 RMenuItems::~RMenuItems() = default;
27 
28 //////////////////////////////////////////////////////////
29 /// Fill menu for provided object, using *MENU* as indicator in method comments
30 
32 {
33  fItems.clear();
34 
35  TList lst;
36  cl->GetMenuItems(&lst);
37 
38  TIter iter(&lst);
39  TMethod *m = nullptr;
40 
41  TClass *last_class = nullptr;
42  bool has_editor = false;
43 
44  auto match = [](const TObject *v, const char *name) { return !strcmp(v->GetName(), name); };
45 
46  while ((m = (TMethod *)iter()) != nullptr) {
47 
48  TClass *m_cl = m->GetClass();
49  bool is_editor = false;
50 
51  if (match(m_cl, "TH1")) {
52  if (match(m, "SetHighlight")) continue;
53  if (match(m, "DrawPanel")) is_editor = true;
54  } else if (match(m_cl, "TGraph")) {
55  if (match(m, "SetHighlight")) continue;
56  if (match(m, "DrawPanel")) is_editor = true;
57  } else if (match(m_cl, "TAttFill")) {
58  if (match(m, "SetFillAttributes")) is_editor = true;
59  } else if (match(m_cl, "TAttLine")) {
60  if (match(m, "SetLineAttributes")) is_editor = true;
61  } else if (match(m_cl, "TAttMarker")) {
62  if (match(m, "SetMarkerAttributes")) is_editor = true;
63  } else if (match(m_cl, "TAttText")) {
64  if (match(m, "SetTextAttributes")) is_editor = true;
65  }
66 
67  if (is_editor) {
68  if (!has_editor) {
69  AddMenuItem("Editor", "Attributes editor", "Show:Editor", last_class ? last_class : m_cl);
70  has_editor = true;
71  }
72  continue;
73  }
74 
75  last_class = m_cl;
76 
77  if (m->IsMenuItem() == kMenuToggle) {
78  TString getter;
79  if (m->Getter() && strlen(m->Getter()) > 0) {
80  getter = m->Getter();
81  } else if (strncmp(m->GetName(), "Set", 3) == 0) {
82  getter = TString(m->GetName())(3, strlen(m->GetName()) - 3);
83  if (cl->GetMethodAllAny(TString("Has") + getter))
84  getter = TString("Has") + getter;
85  else if (cl->GetMethodAllAny(TString("Get") + getter))
86  getter = TString("Get") + getter;
87  else if (cl->GetMethodAllAny(TString("Is") + getter))
88  getter = TString("Is") + getter;
89  else
90  getter = "";
91  }
92 
93  if ((getter.Length() > 0) && cl->GetMethodAllAny(getter)) {
94  // execute getter method to get current state of toggle item
95 
96  auto call = std::make_unique<TMethodCall>(cl, getter, "");
97 
98  if (call->ReturnType() == TMethodCall::kLong) {
99  Long_t l(0);
100  call->Execute(obj, l);
101 
102  AddChkMenuItem(m->GetName(), m->GetTitle(), l != 0, Form("%s(%s)", m->GetName(), (l != 0) ? "0" : "1"), m_cl);
103 
104  } else {
105  // Error("CheckModifiedFlag", "Cannot get toggle value with getter %s", getter.Data());
106  }
107  }
108  } else {
109  TList *args = m->GetListOfMethodArgs();
110 
111  if (!args || (args->GetSize() == 0)) {
112  AddMenuItem(m->GetName(), m->GetTitle(), Form("%s()", m->GetName()), m_cl);
113  } else {
114  auto item = std::make_unique<Detail::RArgsMenuItem>(m->GetName(), m->GetTitle());
115  item->SetExec(Form("%s()", m->GetName()));
116  item->SetClassName(m_cl->GetName());
117 
118  TIter args_iter(args);
119  TMethodArg *arg = nullptr;
120 
121  while ((arg = dynamic_cast<TMethodArg *>(args_iter())) != nullptr) {
122  Detail::RMenuArgument menu_arg(arg->GetName(), arg->GetTitle(), arg->GetFullTypeName());
123  if (arg->GetDefault()) menu_arg.SetDefault(arg->GetDefault());
124  item->AddArg(menu_arg);
125  }
126 
127  Add(std::move(item));
128  }
129  }
130  }
131 }
132 
133 //////////////////////////////////////////////////////////
134 /// fill menu items for the drawable
135 
136 std::unique_ptr<RDrawableReply> RDrawableMenuRequest::Process()
137 {
138  auto drawable = GetContext().GetDrawable();
139  if (!drawable) return nullptr;
140 
141  auto items = std::make_unique<RMenuItems>(menureqid, menukind);
142 
143  drawable->PopulateMenu(*items);
144 
145  return items;
146 }
l
auto * l
Definition: textangle.C:4
m
auto * m
Definition: textangle.C:8
TClass::GetMethodAllAny
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition: TClass.cxx:4360
ROOT::Experimental::RMenuItems::fItems
std::vector< std::unique_ptr< Detail::RMenuItem > > fItems
list of items in the menu
Definition: RMenuItems.hxx:161
ROOT::Experimental::Detail::RMenuArgument::SetDefault
void SetDefault(const std::string &dflt)
Definition: RMenuItems.hxx:118
Form
char * Form(const char *fmt,...)
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
TMethodArg
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:36
TMethod.h
ROOT::Experimental::RMenuItems::PopulateObjectMenu
void PopulateObjectMenu(void *obj, TClass *cl)
Fill menu for provided object, using MENU as indicator in method comments.
Definition: RMenuItems.cxx:31
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
TClass.h
ROOT::Experimental::RMenuItems::~RMenuItems
virtual ~RMenuItems()
destructor - pin vtable
TList.h
TString
Basic string class.
Definition: TString.h:136
TClass::GetMenuItems
void GetMenuItems(TList *listitems)
Returns list of methods accessible by context menu.
Definition: TClass.cxx:3850
v
@ v
Definition: rootcling_impl.cxx:3635
ROOT::Experimental::Detail::RMenuArgument
Argument description for menu item which should invoke class method.
Definition: RMenuItems.hxx:102
TString.h
TMethodArg::GetFullTypeName
const char * GetFullTypeName() const
Get full type description of method argument, e.g.: "class TDirectory*".
Definition: TMethodArg.cxx:74
TMethodCall::kLong
static const EReturnType kLong
Definition: TMethodCall.h:43
TROOT.h
kMenuToggle
@ kMenuToggle
Definition: TMethod.h:34
ROOT::Experimental::RDrawableMenuRequest::menureqid
std::string menureqid
Definition: RMenuItems.hxx:210
TMethod
Each ROOT class (see TClass) has a linked list of methods.
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
ROOT::Experimental::RDrawableRequest::GetContext
const RDrawable::RDisplayContext & GetContext() const
Definition: RDrawableRequest.hxx:64
ROOT::Experimental::RDrawableMenuRequest::menukind
std::string menukind
Definition: RMenuItems.hxx:209
ROOT::Experimental::RDrawable::RDisplayContext::GetDrawable
RDrawable * GetDrawable() const
Definition: RDrawable.hxx:162
RDrawable.hxx
TCollection::GetSize
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
TClass
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Experimental::RMenuItems::Add
void Add(std::unique_ptr< Detail::RMenuItem > &&item)
Definition: RMenuItems.hxx:178
TIter
Definition: TCollection.h:233
ROOT::Experimental
Definition: RDirectory.hxx:30
TMethodArg.h
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RMenuItems.hxx
ROOT::Experimental::RDrawableMenuRequest::Process
std::unique_ptr< RDrawableReply > Process() override
fill menu items for the drawable
Definition: RMenuItems.cxx:136
ROOT::Experimental::RMenuItems::AddChkMenuItem
void AddChkMenuItem(const std::string &name, const std::string &title, bool checked, const std::string &toggle, const TClass *cl=nullptr)
Definition: RMenuItems.hxx:188
TList
A doubly linked list.
Definition: TList.h:44
ROOT::Experimental::RMenuItems::AddMenuItem
void AddMenuItem(const std::string &name, const std::string &title, const std::string &exec, const TClass *cl=nullptr)
Definition: RMenuItems.hxx:180
TMethodCall.h