Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Gtk4MethodDialog.cxx
Go to the documentation of this file.
1// Author: Sergey Linev, GSI 14/08/2026
2
3/*************************************************************************
4 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "Gtk4MethodDialog.h"
12
13#include "TString.h"
14#include "TROOT.h"
15#include "TVirtualPad.h"
16#include "TError.h"
17#include "TMethod.h"
18#include "TMethodArg.h"
19#include "TMethodCall.h"
20#include "TObjString.h"
21#include "TContextMenu.h"
22#include "TColor.h"
23
24#include <iostream>
25
26/** \class Gtk4MethodDialog
27 \ingroup gtk4canvas
28
29Specialized dialog to enter arguments for method exection
30*/
31
32////////////////////////////////////////////////////////////////////////////////
33/// Create method dialog
34
36{
37 set_title("method dialog");
38 set_default_size(width, height);
39
40 m_main_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 10);
41 m_main_box->set_margin(5);
42 set_child(*m_main_box);
43}
44
45////////////////////////////////////////////////////////////////////////////////
46/// clear all elements inside dialog main box
47
49{
50 while (auto* child = m_main_box->get_first_child())
51 m_main_box->remove(*child);
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// add line with color button
56
57Gtk::ColorDialogButton *Gtk4MethodDialog::addColorInput(const char *title, Int_t colindx)
58{
59 TColor *rootColor = gROOT->GetColor(colindx);
60 Gdk::RGBA initial_color;
61 initial_color.set_rgba(0.0, 0.0, 0.0, 1.0);
62
63 if (rootColor)
64 initial_color.set_rgba(rootColor->GetRed(), rootColor->GetGreen(), rootColor->GetBlue(), rootColor->GetAlpha());
65
66 auto color_dialog = Gtk::ColorDialog::create();
67 color_dialog->set_title("Select a Custom Color");
68 color_dialog->set_modal(true);
69
70 // Instantiate the specific ColorDialogButton using our dialog rules
71 auto m_color_button = Gtk::make_managed<Gtk::ColorDialogButton>(color_dialog);
72 m_color_button->set_halign(Gtk::Align::END);
73
74 // Set an initial default color (e.g., solid Red)
75 m_color_button->set_rgba(initial_color);
76
77 addLine(title, m_color_button);
78
79 return m_color_button;
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Add single dialog line
84
85void Gtk4MethodDialog::addLine(const char *txt, Gtk::Widget *widget)
86{
87 auto layout_grid = Gtk::make_managed<Gtk::Grid>();
88 layout_grid->set_column_spacing(15);
89 layout_grid->set_hexpand(true);
90 layout_grid->set_halign(Gtk::Align::FILL);
91 m_main_box->append(*layout_grid);
92
93 auto label = Gtk::make_managed<Gtk::Label>(txt);
94 label->set_halign(Gtk::Align::START);
95 label->set_hexpand(true); // Wants to expand
96 label->set_halign(Gtk::Align::FILL); // Stretch to fill its 50% allocation
97 label->set_xalign(0.0);
98
99
100 widget->set_hexpand(true); // Wants to expand equally with the label
101 widget->set_halign(Gtk::Align::FILL);
102
103 layout_grid->attach(*label, 0, 0, 3, 1);
104 layout_grid->attach(*widget, 3, 0, 7, 1);
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Add method argument
109
110void Gtk4MethodDialog::addArg(const char *argname, const char *value, const char *)
111{
112 auto entry = Gtk::make_managed<Gtk::Entry>();
113 entry->set_text(value);
114 addLine(TString::Format("%s:", argname).Data(), entry);
115 fArgs.push_back(entry);
116}
117
118
119////////////////////////////////////////////////////////////////////////////////
120/// Add Ok and Cancel buttons
121
123{
124 auto hbox = Gtk::make_managed<Gtk::Box>();
125 hbox->set_hexpand(true);
126 hbox->set_halign(Gtk::Align::END);
127 m_main_box->append(*hbox);
128
129 auto button1 = Gtk::make_managed<Gtk::Button>("Ok");
130
131 auto button2 = Gtk::make_managed<Gtk::Button>("Cancel");
132 button2->signal_clicked().connect([this]() { close(); });
133
134 hbox->append(*button1);
135 hbox->append(*button2);
136
137 return button1;
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Show method dialog
142
144{
145 if (!menu || !object || !func)
146 return;
147
148 set_title(menu->CreateDialogTitle(object, func));
149
150 // iterate through all arguments and create appropriate input-data objects:
151 // inputlines, option menus...
152 TIter next(func->GetListOfMethodArgs());
153
154 while (auto argument = (TMethodArg *) next()) {
155 TString argTitle = menu->CreateArgumentTitle(argument);
156 TString type = argument->GetTypeName();
157 TDataType *datatype = gROOT->GetType(type);
158 TString basictype;
159
160 if (datatype) {
161 basictype = datatype->GetTypeName();
162 } else {
163 if (type.CompareTo("enum") != 0)
164 std::cout << "*** Warning in Dialog(): data type is not basic type, assuming (int)\n";
165 basictype = "int";
166 }
167
168 if (TString(argument->GetTitle()).Index("*") != kNPOS) {
169 basictype += "*";
170 type = "char*";
171 }
172
173 TDataMember *m = argument->GetDataMember();
174 if (m && m->GetterMethod()) {
175
176 m->GetterMethod()->Init(object->IsA(), m->GetterMethod()->GetMethodName(), "");
177
178 // Get the current value and form it as a text:
179
180 TString val;
181
182 if (basictype == "char*") {
183 char *tdefval = nullptr;
184 m->GetterMethod()->Execute(object, "", &tdefval);
185 if (tdefval) val = tdefval;
186 } else
187 if ((basictype == "float") ||
188 (basictype == "double")) {
189 Double_t ddefval = 0.;
190 m->GetterMethod()->Execute(object, "", ddefval);
191 val = TString::Format("%g", ddefval);
192 } else
193 if ((basictype == "char") ||
194 (basictype == "int") ||
195 (basictype == "bool") ||
196 (basictype == "long") ||
197 (basictype == "short")) {
198 Longptr_t ldefval = 0;
199 m->GetterMethod()->Execute(object, "", ldefval);
200 val = TString::Format("%ld", (long) ldefval);
201 }
202
203 // Warn aout options ...
204 if (m->GetOptions())
205 ::Warning("Gtk4MethodDialog::Dialog", "option menu not yet implemented");
206
207 addArg(argTitle.Data(), val.Data(), type.Data());
208 } else { // if m not found ...
209 TString argDflt;
210 if (argument->GetDefault())
211 argDflt = argument->GetDefault();
212
213 if ((argDflt.Length() > 1) &&
214 (argDflt[0]=='\"') && (argDflt[argDflt.Length()-1]=='\"')) {
215 // cut "" from the string argument
216 argDflt.Remove(0,1);
217 argDflt.Remove(argDflt.Length()-1,1);
218 }
219
220 addArg(argTitle.Data(), argDflt.Data(), type.Data());
221 }
222 }
223
224 auto okBtn = addOkCancelButtons();
225
226 okBtn->signal_clicked().connect(sigc::mem_fun(*this, &Gtk4MethodDialog::on_ok_button_clicked));
227
228 present();
229
230 fMenu = menu;
231 fObject = object;
232 fFunc = func;
233}
234
235
237{
238 if (fMenu && fObject) {
239
240 TObjArray argslist(fArgs.size());
241
242 for (auto entry : fArgs) {
243 Glib::ustring v = entry->get_text();
244 argslist.AddLast(new TObjString(v.c_str()));
245 }
246
247 close();
248
249 fMenu->Execute(fObject, fFunc, &argslist);
250 } else {
251 close();
252 }
253}
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:90
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:132
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
#define gROOT
Definition TROOT.h:417
void clearAllElements()
clear all elements inside dialog main box
void addArg(const char *argname, const char *value, const char *type)
Add method argument.
Gtk::ColorDialogButton * addColorInput(const char *title, int colindx)
add line with color button
Gtk4MethodDialog(unsigned width=400, unsigned height=200)
Create method dialog.
void methodDialog(TContextMenu *menu, TObject *object, TFunction *func)
Show method dialog.
void addLine(const char *txt, Gtk::Widget *widget)
Add single dialog line.
std::vector< Gtk::Entry * > fArgs
TContextMenu * fMenu
Gtk::Button * addOkCancelButtons()
Add Ok and Cancel buttons.
The color creation and management class.
Definition TColor.h:22
Float_t GetRed() const
Definition TColor.h:61
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1939
Float_t GetAlpha() const
Definition TColor.h:67
Float_t GetBlue() const
Definition TColor.h:63
Float_t GetGreen() const
Definition TColor.h:62
This class provides an interface to context sensitive popup menus.
virtual const char * CreateArgumentTitle(TMethodArg *argument)
Create string describing argument (for use in dialog box).
virtual const char * CreateDialogTitle(TObject *object, TFunction *method)
Create title for dialog box retrieving argument values.
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.
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
Basic data type descriptor (datatype information is obtained from CINT).
Definition TDataType.h:44
Int_t GetType() const
Definition TDataType.h:71
TString GetTypeName()
Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
Global functions class (global functions are obtained from CINT).
Definition TFunction.h:30
TList * GetListOfMethodArgs()
Return list containing the TMethodArgs of a TFunction.
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition TMethodArg.h:36
An array of TObjects.
Definition TObjArray.h:31
void AddLast(TObject *obj) override
Add object in the next empty slot in the array.
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:42
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 TObject.cxx:377
Basic string class.
Definition TString.h:137
Ssiz_t Length() const
Definition TString.h:426
const char * Data() const
Definition TString.h:385
TString & Remove(Ssiz_t pos)
Definition TString.h:695
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:2460
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:661
TMarker m
Definition textangle.C:8