Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
splitbuttonTest.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// A simple example that shows the usage of a TGSplitButton.
4/// The checkbutton is used to change the split state of the button.
5///
6/// \macro_code
7///
8/// \author Roel Aaij 13/07/2007
9
10#include <iostream>
11#include <TApplication.h>
12#include <TGClient.h>
13#include <TGButton.h>
14#include <TGFrame.h>
15#include <TGLayout.h>
16#include <TGWindow.h>
17#include <TGLabel.h>
18#include <TString.h>
19#include <TGMenu.h>
20
21// A little class to automatically handle the generation of unique
22// widget ids.
23enum EMenuIds {
24 ID_1,
25 ID_2,
26 ID_3,
27 ID_4,
28 ID_5
29};
30
31class IDList {
32private:
33 Int_t nID; // Generates unique widget IDs.
34public:
35 IDList() : nID(0) {}
36 ~IDList() {}
37 Int_t GetUnID(void) { return ++nID; }
38};
39
40class SplitButtonTest : public TGMainFrame {
41
42private:
43 TGSplitButton *fMButton; // Split Button
44 TGPopupMenu *fPopMenu; // TGpopupMenu that will be attached to
45 // the button.
46 IDList IDs; // Generator for unique widget IDs.
47
48public:
49 SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h);
50 ~SplitButtonTest() override;
51
52 void DoExit();
53 void DoSplit(Bool_t split);
54 void DoEnable(Bool_t on);
55 void HandleMenu(Int_t id);
56
57 ClassDefOverride(SplitButtonTest, 0)
58};
59
60SplitButtonTest::SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h) : TGMainFrame(p, w, h)
61{
62 SetCleanup(kDeepCleanup);
63
64 Connect("CloseWindow()", "SplitButtonTest", this, "DoExit()");
65 DontCallClose();
66
67 TGVerticalFrame *fVL = new TGVerticalFrame(this, 100, 100);
68 TGHorizontalFrame *fHL = new TGHorizontalFrame(fVL, 100, 40);
69
70 // Create a popup menu.
71 fPopMenu = new TGPopupMenu(gClient->GetRoot());
72 fPopMenu->AddEntry("Button &1", ID_1);
73 fPopMenu->AddEntry("Button &2", ID_2);
74 fPopMenu->DisableEntry(ID_2);
75 fPopMenu->AddEntry("Button &3", ID_3);
76 fPopMenu->AddSeparator();
77
78 // Create a split button, the menu is adopted.
79 fMButton = new TGSplitButton(fHL, new TGHotString("Button &Options"), fPopMenu, IDs.GetUnID());
80
81 // It is possible to add entries later
82 fPopMenu->AddEntry("En&try with really really long name", ID_4);
83 fPopMenu->AddEntry("&Exit", ID_5);
84
85 // Connect the special signal for the activation of items in a menu
86 // that belongs to a split button to the slot.
87 fMButton->Connect("ItemClicked(Int_t)", "SplitButtonTest", this, "HandleMenu(Int_t)");
88
89 TGCheckButton *fCButton = new TGCheckButton(fHL, new TGHotString("Split"), IDs.GetUnID());
90 fCButton->SetState(kButtonDown);
91 fCButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoSplit(Bool_t)");
92
93 // Add frames to their parent for layout.
94 fHL->AddFrame(fCButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 0, 10, 0, 0));
95 TGCheckButton *fEButton = new TGCheckButton(fHL, new TGHotString("Enable"), IDs.GetUnID());
96 fEButton->SetState(kButtonDown);
97 fEButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoEnable(Bool_t)");
98
99 // Add frames to their parent for layout.
100 fHL->AddFrame(fEButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 0, 10, 0, 0));
103 AddFrame(fVL, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY));
104
105 SetWindowName("SplitButton Test");
107 Resize(GetDefaultSize());
108 MapWindow();
109};
110
111SplitButtonTest::~SplitButtonTest()
112{
113 // Destructor
114 Cleanup();
115}
116
117void SplitButtonTest::DoExit()
118{
119 // Exit this application via the Exit button or Window Manager.
120 // Use one of the both lines according to your needs.
121 // Please note to re-run this macro in the same ROOT session,
122 // you have to compile it to get signals/slots 'on place'.
123
124 // DeleteWindow(); // to stay in the ROOT session
125 gApplication->Terminate(); // to exit and close the ROOT session
126}
127
128void SplitButtonTest::DoSplit(Bool_t split)
129{
130 fMButton->SetSplit(split);
131}
132
133void SplitButtonTest::DoEnable(Bool_t on)
134{
135 if (on)
136 fMButton->SetState(kButtonUp);
137 else
138 fMButton->SetState(kButtonDisabled);
139}
140
141void SplitButtonTest::HandleMenu(Int_t id)
142{
143 // Activation of menu items in the popup menu are handled in a user
144 // defined slot to which the ItemClicked(Int_t) signal is
145 // connected.
146
147 switch (id) {
148 case ID_1: std::cout << "Button 1 was activated" << std::endl; break;
149 case ID_2: std::cout << "Button 2 was activated" << std::endl; break;
150 case ID_3: std::cout << "Button 3 was activated" << std::endl; break;
151 case ID_4: std::cout << "Button with a really really long name was activated" << std::endl; break;
152 case ID_5: DoExit(); break;
153 }
154}
155void splitbuttonTest()
156{
157 new SplitButtonTest(gClient->GetRoot(), 100, 100);
158}
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
R__EXTERN TApplication * gApplication
@ kButtonDown
Definition TGButton.h:54
@ kButtonDisabled
Definition TGButton.h:56
@ kButtonUp
Definition TGButton.h:53
#define gClient
Definition TGClient.h:157
@ kDeepCleanup
Definition TGFrame.h:42
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsCenterX
Definition TGLayout.h:25
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize MapSubwindows
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
Selects different options.
Definition TGButton.h:264
void SetState(EButtonState state, Bool_t emit=kFALSE) override
Set check button state.
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:385
TGHotString is a string with a "hot" character underlined.
Definition TGString.h:42
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
Defines top level windows that interact with the system Window Manager.
Definition TGFrame.h:397
This class creates a popup menu object.
Definition TGMenu.h:110
Implements a button with added menu functionality.
Definition TGButton.h:378
A composite frame that layout their children in vertical way.
Definition TGFrame.h:374
ROOT GUI Window base class.
Definition TGWindow.h:23
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition TQObject.cxx:869