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
21using namespace ROOT::Experimental;
22
23//////////////////////////////////////////////////////////
24/// destructor - pin vtable
25
26RMenuItems::~RMenuItems() = default;
27
28//////////////////////////////////////////////////////////
29/// Fill menu for provided object, using *MENU* as indicator in method comments
30
31void RMenuItems::PopulateObjectMenu(void *obj, TClass *cl)
32{
33 fItems.clear();
34
35 TList lst;
36 cl->GetMenuItems(&lst);
37
38 TIter iter(&lst);
39 TMethod *m = nullptr;
40
41 while ((m = (TMethod *)iter()) != nullptr) {
42
43 if (m->IsMenuItem() == kMenuToggle) {
44 TString getter;
45 if (m->Getter() && strlen(m->Getter()) > 0) {
46 getter = m->Getter();
47 } else if (strncmp(m->GetName(), "Set", 3) == 0) {
48 getter = TString(m->GetName())(3, strlen(m->GetName()) - 3);
49 if (cl->GetMethodAllAny(TString("Has") + getter))
50 getter = TString("Has") + getter;
51 else if (cl->GetMethodAllAny(TString("Get") + getter))
52 getter = TString("Get") + getter;
53 else if (cl->GetMethodAllAny(TString("Is") + getter))
54 getter = TString("Is") + getter;
55 else
56 getter = "";
57 }
58
59 if ((getter.Length() > 0) && cl->GetMethodAllAny(getter)) {
60 // execute getter method to get current state of toggle item
61
62 auto call = std::make_unique<TMethodCall>(cl, getter, "");
63
64 if (call->ReturnType() == TMethodCall::kLong) {
65 Long_t l(0);
66 call->Execute(obj, l);
67
68 AddChkMenuItem(m->GetName(), m->GetTitle(), l != 0, Form("%s(%s)", m->GetName(), (l != 0) ? "0" : "1"));
69
70 } else {
71 // Error("CheckModifiedFlag", "Cannot get toggle value with getter %s", getter.Data());
72 }
73 }
74 } else {
75 TList *args = m->GetListOfMethodArgs();
76
77 if (!args || (args->GetSize() == 0)) {
78 AddMenuItem(m->GetName(), m->GetTitle(), Form("%s()", m->GetName()));
79 } else {
80 auto item = std::make_unique<Detail::RArgsMenuItem>(m->GetName(), m->GetTitle());
81 item->SetExec(Form("%s()", m->GetName()));
82
83 TIter args_iter(args);
84 TMethodArg *arg = nullptr;
85
86 while ((arg = dynamic_cast<TMethodArg *>(args_iter())) != nullptr) {
87 Detail::RMenuArgument menu_arg(arg->GetName(), arg->GetTitle(), arg->GetFullTypeName());
88 if (arg->GetDefault()) menu_arg.SetDefault(arg->GetDefault());
89 item->AddArg(menu_arg);
90 }
91
92 Add(std::move(item));
93 }
94 }
95 }
96}
97
98//////////////////////////////////////////////////////////
99/// fill menu items for the drawable
100
101std::unique_ptr<RDrawableReply> RDrawableMenuRequest::Process()
102{
103 auto drawable = GetContext().GetDrawable();
104 if (!drawable) return nullptr;
105
106 auto items = std::make_unique<RMenuItems>(menureqid, menukind);
107
108 drawable->PopulateMenu(*items);
109
110 return items;
111}
long Long_t
Definition: RtypesCore.h:52
@ kMenuToggle
Definition: TMethod.h:34
char * Form(const char *fmt,...)
Argument description for menu item which should invoke class method.
Definition: RMenuItems.hxx:98
void SetDefault(const std::string &dflt)
Definition: RMenuItems.hxx:114
std::unique_ptr< RDrawableReply > Process() override
fill menu items for the drawable
Definition: RMenuItems.cxx:101
const RDrawable::RDisplayContext & GetContext() const
void AddMenuItem(const std::string &name, const std::string &title, const std::string &exec)
Definition: RMenuItems.hxx:176
void AddChkMenuItem(const std::string &name, const std::string &title, bool checked, const std::string &toggle)
Definition: RMenuItems.hxx:183
std::vector< std::unique_ptr< Detail::RMenuItem > > fItems
list of items in the menu
Definition: RMenuItems.hxx:157
void Add(std::unique_ptr< Detail::RMenuItem > &&item)
Definition: RMenuItems.hxx:174
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
void GetMenuItems(TList *listitems)
Returns list of methods accessible by context menu.
Definition: TClass.cxx:3842
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition: TClass.cxx:4347
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
A doubly linked list.
Definition: TList.h:44
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:31
const char * GetFullTypeName() const
Get full type description of method argument, e.g.: "class TDirectory*".
Definition: TMethodArg.cxx:75
const char * GetDefault() const
Get default value of method argument.
Definition: TMethodArg.cxx:58
static const EReturnType kLong
Definition: TMethodCall.h:43
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
auto * m
Definition: textangle.C:8
auto * l
Definition: textangle.C:4