Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <gtkmm.h>
2#include <cmath>
3
4#include "TGtk4Canvas.h"
5
6#include "TCanvas.h"
7#include "TCanvasImp.h"
8#include "TRandom.h"
9#include "TMath.h"
10#include "TLatex.h"
11#include "TH1I.h"
12#include "TH2I.h"
13#include "TGraph.h"
14
16
17class MainWindow : public Gtk::Window
18{
19private:
20 Gtk::Box m_root;
21 Gtk::Box m_buttons;
22 Gtk::CheckButton m_check;
23 sigc::connection m_timer_conn;
24 Gtk::Box m_flexarea;
26 TCanvas *m_canvas = nullptr;
27 int m_counter = 0;
29
30public:
31 MainWindow();
32
33private:
34 void fill_canvas_object();
35 void button_clicked(int index);
36};
37
39{
40 set_title("GTK4 drawing demo");
41 set_default_size(640, 400);
42
43 m_root.set_orientation(Gtk::Orientation::HORIZONTAL);
44 m_root.set_spacing(6);
45 m_root.set_margin(6);
46 set_child(m_root);
47
48 // Left panel with buttons
49 m_buttons.set_orientation(Gtk::Orientation::VERTICAL);
50 m_buttons.set_spacing(6);
51 m_buttons.set_vexpand(true);
52 m_root.append(m_buttons);
53
54 const char *btns[7] = {"TLatex", "TH1", "TH2", "TGraph", "Toggle menu", "Toggle toobar", "Toggle statusbar"};
55
56 for (int i = 0; i < 7; ++i) {
57 auto b = Gtk::make_managed<Gtk::Button>(btns[i]);
58 m_buttons.append(*b);
59 b->signal_clicked().connect([this, i]() {
61 });
62 }
63
64 m_check.set_label("Update drawing");
65 m_check.set_active(false); // Checked by default
66
67 m_check.signal_toggled().connect([this]() {
68 bool is_checked = m_check.get_active();
69 if (is_checked && !m_timer_conn.connected()) {
70 m_timer_conn = Glib::signal_timeout().connect([this]() -> bool {
71 m_counter++;
73 return true; // Keep repeating while checked
74 }, 200);
75 } else if (!is_checked && m_timer_conn.connected())
76 m_timer_conn.disconnect();
77 });
78 m_buttons.append(m_check);
79
80 // Spacer pushes Quit to the bottom (optional, remove if not wanted)
81 auto spacer = Gtk::make_managed<Gtk::Box>();
82 spacer->set_vexpand(true);
83 m_buttons.append(*spacer);
84
85 auto quit = Gtk::make_managed<Gtk::Button>("Quit");
86 quit->signal_clicked().connect([this]() { close(); });
87 m_buttons.append(*quit);
88
89 m_flexarea.set_hexpand(true);
90 m_flexarea.set_vexpand(true);
91 m_root.append(m_flexarea);
92
93 // Embed special ROOT widget and create TCanvas instance with the name "gtk4demo"
94 // One separately provides `Gtk::Window` and `Gtk::Box` pointer to specify place for the canvas
95 // After one can add primitives, call Update() and all other ROOT methods - but not Close()
97
98 // create TLatex
100}
101
102
104{
105 if (!m_canvas)
106 return;
107
108 TObject *obj = nullptr;
110 while (obj = iter()) {
111 // skip TFrame object
112 if (!obj->InheritsFrom("TFrame"))
113 break;
114 }
115
116 switch (m_clicked_button) {
117 case 0:
118 if (auto ltx = dynamic_cast<TLatex *>(obj))
119 if (m_counter > 0) {
120 ltx->SetTextAlign(12);
121 ltx->SetX(0.2);
122 ltx->SetTitle(TString::Format("TLatex : #color[%d]{%d}", 2 + m_counter % 7, m_counter));
123 ltx->SetY(0.5 + 0.2*TMath::Sin(m_counter * 0.15));
124 }
125 break;
126 case 1:
127 if (auto h1 = dynamic_cast<TH1 *>(obj)) {
128 h1->Reset();
129 auto meanx = TMath::Sin(m_counter * 0.4);
130 for (int n = 0; n < 300; ++n) {
131 auto x = gRandom->Gaus(meanx);
132 h1->Fill(x);
133 }
134 if (m_counter > 0)
135 h1->SetTitle(TString::Format("TH1 example cnt: %d", m_counter));
136 }
137 break;
138 case 2:
139 if (auto h2 = dynamic_cast<TH2 *>(obj)) {
140 h2->Reset();
141 auto meanx = TMath::Sin(m_counter * 0.15);
142 auto meany = -TMath::Sin(m_counter * 0.37);
143 for (int n = 0; n < 300; ++n) {
144 auto x = gRandom->Gaus(meanx);
145 auto y = gRandom->Gaus(meany);
146 h2->Fill(x, y);
147 }
148 if (m_counter > 0)
149 h2->SetTitle(TString::Format("TH2 example cnt: %d", m_counter));
150 }
151 break;
152 case 3:
153 if (auto gr = dynamic_cast<TGraph *>(obj)) {
154 for (int i = 0; i < 100; ++i)
155 gr->SetPoint(i, i + 10, TMath::Sin((i + m_counter) * 0.2));
156 if (m_counter > 0)
157 gr->SetTitle(TString::Format("TGraph example cnt: %d", m_counter));
158 }
159 break;
160 }
161 m_canvas->Update();
162}
163
164
166{
167 if (!m_canvas)
168 return;
169
170 if (index == 4) {
171 // there is no toggle method in TCanvas class, use TCanvasImp
175 return;
176 }
177 if (index == 5) {
179 return;
180 }
181 if (index == 6) {
183 return;
184 }
185
187 m_counter = 0;
188
189 m_canvas->Clear();
190 m_canvas->Resize();
191
192 switch (index) {
193 case 0: {
194 auto ltx = new TLatex(0.5, 0.5, "TLatex");
195 ltx->SetTextAlign(22);
196 ltx->SetTextSize(0.2);
197 ltx->SetBit(kCanDelete);
198 m_canvas->Range(0, 0, 1, 1); // set default ranges
199 m_canvas->Add(ltx);
200 break;
201 }
202 case 1: {
203 auto h1 = new TH1I("h1", "Example of TH1I", 100, -5, 5);
204 h1->SetDirectory(nullptr);
206 m_canvas->Add(h1);
207 break;
208 }
209 case 2: {
210 auto h2 = new TH2I("h2", "Example of TH2I", 40, -5, 5, 40, -5, 5);
211 h2->SetDirectory(nullptr);
212 h2->SetBit(kCanDelete);
213 m_canvas->Add(h2, "colz");
214 break;
215 }
216 case 3: {
217 auto gr = new TGraph(100);
218 gr->SetName("gr1");
219 gr->SetTitle("Example of TGraph");
221 gr->SetLineWidth(2);
223 m_canvas->Add(gr, "al");
224 break;
225 }
226 default: break;
227 }
228
230}
231
232
233int main(int argc, char* argv[])
234{
235 auto app = Gtk::Application::create("org.example.root_gtk4canv_demo");
236 return app->make_window_and_run<MainWindow>(argc, argv);
237}
#define b(i)
Definition RSha256.hxx:100
@ kGreen
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
@ kCanDelete
Definition TObject.h:375
R__EXTERN TRandom * gRandom
Definition TRandom.h:73
Gtk::CheckButton m_check
Definition main.cpp:22
MainWindow()
Definition main.cpp:38
Shape m_shape
Definition main.cpp:25
int m_counter
Definition main.cpp:27
sigc::connection m_timer_conn
Definition main.cpp:23
void fill_canvas_object()
Definition main.cpp:103
Gtk::Box m_buttons
Definition main.cpp:21
Gtk::Box m_root
Definition main.cpp:20
int m_clicked_button
Definition main.cpp:28
TCanvas * m_canvas
Definition main.cpp:26
Gtk::Box m_flexarea
Definition main.cpp:24
void button_clicked(int index)
Definition main.cpp:165
static TCanvas * EmbedGtk4Canvas(Gtk::Window *window, Gtk::Box &place, const char *name)
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual void ShowMenuBar(Bool_t show=kTRUE)
Definition TCanvasImp.h:79
The Canvas class.
Definition TCanvas.h:23
void Clear(Option_t *option="") override
Remove all primitives from the canvas.
Definition TCanvas.cxx:763
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition TCanvas.cxx:2483
TCanvasImp * GetCanvasImp() const override
Definition TCanvas.h:163
virtual void Resize(Option_t *option="")
Recompute canvas parameters following a X11 Resize.
Definition TCanvas.cxx:1701
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2543
@ kMenuBar
Definition TCanvas.h:96
virtual void ToggleToolBar()
Toggle toolbar.
Definition TCanvas.cxx:2494
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2386
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2425
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2441
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:10528
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:826
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:126
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:9170
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6932
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3489
2-D histogram with an int per channel (see TH1 documentation)
Definition TH2.h:245
Service class for 2-D histogram classes.
Definition TH2.h:30
To draw Mathematical Formula.
Definition TLatex.h:20
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:548
TList * GetListOfPrimitives() const override
Definition TPad.h:259
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TPad.cxx:5278
void Add(TObject *obj, Option_t *opt="", Bool_t modified=kTRUE) override
Add an object to list of primitives with specified draw option When.
Definition TPad.cxx:485
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
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
int main()
Definition main.cpp:18
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TGraphErrors * gr
Definition legend1.C:25
TH1F * h1
Definition legend1.C:5
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:601
Shape
Definition main.cpp:15
@ Triangle
@ Rectangle