Logo ROOT  
Reference Guide
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.
23 enum EMenuIds {
24  ID_1,
25  ID_2,
26  ID_3,
27  ID_4,
28  ID_5
29 };
30 
31 class IDList {
32 private:
33  Int_t nID ; // Generates unique widget IDs.
34 public:
35  IDList() : nID(0) {}
36  ~IDList() {}
37  Int_t GetUnID(void) { return ++nID ; }
38 } ;
39 
40 
41 class SplitButtonTest : public TGMainFrame {
42 
43 private:
44  TGSplitButton *fMButton; // Split Button
45  TGPopupMenu *fPopMenu; // TGpopupMenu that will be attached to
46  // the button.
47  IDList IDs ; // Generator for unique widget IDs.
48 
49 public:
50  SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h) ;
51  virtual ~SplitButtonTest() ;
52 
53  void DoExit() ;
54  void DoSplit(Bool_t split) ;
55  void DoEnable(Bool_t on) ;
56  void HandleMenu(Int_t id) ;
57 
58  ClassDef(SplitButtonTest, 0)
59 };
60 
61 SplitButtonTest::SplitButtonTest(const TGWindow *p, UInt_t w, UInt_t h)
62  : TGMainFrame(p, w, h)
63 {
64  SetCleanup(kDeepCleanup) ;
65 
66  Connect("CloseWindow()", "SplitButtonTest", this, "DoExit()") ;
67  DontCallClose() ;
68 
69  TGVerticalFrame *fVL = new TGVerticalFrame(this, 100, 100) ;
70  TGHorizontalFrame *fHL = new TGHorizontalFrame(fVL, 100, 40) ;
71 
72  // Create a popup menu.
73  fPopMenu = new TGPopupMenu(gClient->GetRoot());
74  fPopMenu->AddEntry("Button &1", ID_1);
75  fPopMenu->AddEntry("Button &2", ID_2);
76  fPopMenu->DisableEntry(ID_2);
77  fPopMenu->AddEntry("Button &3", ID_3);
78  fPopMenu->AddSeparator();
79 
80  // Create a split button, the menu is adopted.
81  fMButton = new TGSplitButton(fHL, new TGHotString("Button &Options"),
82  fPopMenu, IDs.GetUnID());
83 
84  // It is possible to add entries later
85  fPopMenu->AddEntry("En&try with really really long name", ID_4);
86  fPopMenu->AddEntry("&Exit", ID_5);
87 
88  // Connect the special signal for the activation of items in a menu
89  // that belongs to a split button to the slot.
90  fMButton->Connect("ItemClicked(Int_t)", "SplitButtonTest", this,
91  "HandleMenu(Int_t)");
92 
93  TGCheckButton *fCButton = new TGCheckButton(fHL, new TGHotString("Split"),
94  IDs.GetUnID());
95  fCButton->SetState(kButtonDown);
96  fCButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoSplit(Bool_t)");
97 
98  // Add frames to their parent for layout.
100  0, 10, 0, 0)) ;
101  TGCheckButton *fEButton = new TGCheckButton(fHL, new TGHotString("Enable"),
102  IDs.GetUnID());
103  fEButton->SetState(kButtonDown);
104  fEButton->Connect("Toggled(Bool_t)", "SplitButtonTest", this, "DoEnable(Bool_t)");
105 
106  // Add frames to their parent for layout.
108  0, 10, 0, 0)) ;
109  fHL->AddFrame(fMButton, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY));
111  AddFrame(fVL, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY)) ;
112 
113  SetWindowName("SplitButton Test") ;
114  MapSubwindows() ;
115  Resize(GetDefaultSize()) ;
116  MapWindow() ;
117 
118 } ;
119 
120 SplitButtonTest::~SplitButtonTest()
121 {
122  // Destructor
123  Cleanup() ;
124 }
125 
126 void SplitButtonTest::DoExit()
127 {
128  // Exit this application via the Exit button or Window Manager.
129  // Use one of the both lines according to your needs.
130  // Please note to re-run this macro in the same ROOT session,
131  // you have to compile it to get signals/slots 'on place'.
132 
133  //DeleteWindow(); // to stay in the ROOT session
134  gApplication->Terminate(); // to exit and close the ROOT session
135 }
136 
137 void SplitButtonTest::DoSplit(Bool_t split)
138 {
139  fMButton->SetSplit(split);
140 }
141 
142 void SplitButtonTest::DoEnable(Bool_t on)
143 {
144  if (on)
145  fMButton->SetState(kButtonUp);
146  else
147  fMButton->SetState(kButtonDisabled);
148 }
149 
150 void SplitButtonTest::HandleMenu(Int_t id)
151 {
152  // Activation of menu items in the popup menu are handled in a user
153  // defined slot to which the ItemClicked(Int_t) signal is
154  // connected.
155 
156  switch (id) {
157  case ID_1:
158  std::cout << "Button 1 was activated" << std::endl;
159  break;
160  case ID_2:
161  std::cout << "Button 2 was activated" << std::endl;
162  break;
163  case ID_3:
164  std::cout << "Button 3 was activated" << std::endl;
165  break;
166  case ID_4:
167  std::cout << "Button with a really really long name was activated"
168  << std::endl;
169  break;
170  case ID_5:
171  DoExit();
172  break;
173  }
174 }
175 void splitbuttonTest()
176 {
177  new SplitButtonTest(gClient->GetRoot(),100,100);
178 }
179 
TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:32
TGCheckButton::SetState
virtual void SetState(EButtonState state, Bool_t emit=kFALSE)
Set check button state.
Definition: TGButton.cxx:1203
TGMainFrame
Definition: TGFrame.h:443
kButtonDown
@ kButtonDown
Definition: TGButton.h:54
TGLayout.h
TGHotString
Definition: TGString.h:52
kButtonUp
@ kButtonUp
Definition: TGButton.h:53
Int_t
int Int_t
Definition: RtypesCore.h:45
TGHorizontalFrame
Definition: TGFrame.h:422
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TString.h
TGWindow.h
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
TGCheckButton
Definition: TGButton.h:264
kLHintsCenterY
@ kLHintsCenterY
Definition: TGLayout.h:35
h
#define h(i)
Definition: RSha256.hxx:106
TGVerticalFrame
Definition: TGFrame.h:411
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TApplication::Terminate
virtual void Terminate(Int_t status=0)
TGClient.h
TGLabel.h
TQObject::Connect
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:866
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGButton.h
TGPopupMenu
Definition: TGMenu.h:123
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:50
TGSplitButton
Definition: TGButton.h:379
TGCompositeFrame::AddFrame
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1102
TGMenu.h
kButtonDisabled
@ kButtonDisabled
Definition: TGButton.h:56