Logo ROOT  
Reference Guide
buttongroupState.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gui
3 /// A simple example that shows the enabled and disabled state of a button group with radio and check buttons.
4 ///
5 /// \macro_code
6 ///
7 /// \author Roel Aaij 4/07/2007
8 
9 #include <TApplication.h>
10 #include <TGClient.h>
11 #include <TGButton.h>
12 #include <TGFrame.h>
13 #include <TGLayout.h>
14 #include <TGWindow.h>
15 #include <TGLabel.h>
16 #include <TString.h>
17 #include <TGButtonGroup.h>
18 
19 class IDList {
20 
21 private:
22  Int_t nID; // creates unique widget's IDs
23 
24 public:
25  IDList() : nID(0) {}
26  ~IDList() {}
27  Int_t GetUnID(void) { return ++nID; }
28 };
29 
30 class MyButtonTest : public TGMainFrame {
31 
32 private:
33  TGTextButton *fExit; // Exit text button
34  TGVButtonGroup *fButtonGroup; // Button group
35  TGCheckButton *fCheckb[4]; // Check buttons
36  TGRadioButton *fRadiob[2]; // Radio buttons
37  IDList IDs; // Widget IDs generator
38 
39 public:
40  MyButtonTest(const TGWindow *p, UInt_t w, UInt_t h);
41  virtual ~MyButtonTest();
42 
43  void DoExit(void);
44  void SetGroupEnabled(Bool_t);
45 
46  ClassDef(MyButtonTest, 0)
47 };
48 
49 MyButtonTest::MyButtonTest(const TGWindow *p, UInt_t w, UInt_t h)
50  : TGMainFrame(p, w, h)
51 {
52  SetCleanup(kDeepCleanup);
53 
54  Connect("CloseWindow()", "MyButtonTest", this, "DoExit()");
55  DontCallClose();
56 
57  TGHorizontalFrame *fHL2 = new TGHorizontalFrame(this, 70, 100);
58  fCheckb[0] = new TGCheckButton(fHL2, new TGHotString("Enable BG"),
59  IDs.GetUnID());
60  fCheckb[0]->SetToolTipText("Enable/Disable the button group");
62  1, 1, 1, 1));
63  fButtonGroup = new TGVButtonGroup(fHL2, "My Button Group");
64  fCheckb[1] = new TGCheckButton(fButtonGroup, new TGHotString("CB 2"),
65  IDs.GetUnID());
66  fCheckb[2] = new TGCheckButton(fButtonGroup, new TGHotString("CB 3"),
67  IDs.GetUnID());
68  fCheckb[3] = new TGCheckButton(fButtonGroup, new TGHotString("CB 4"),
69  IDs.GetUnID());
70  fRadiob[0] = new TGRadioButton(fButtonGroup, new TGHotString("RB 1"),
71  IDs.GetUnID());
72  fRadiob[1] = new TGRadioButton(fButtonGroup, new TGHotString("RB 2"),
73  IDs.GetUnID());
74  fButtonGroup->Show();
75 
76  fHL2->AddFrame(fButtonGroup, new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,
77  1, 1, 1, 1));
78  AddFrame(fHL2);
79 
80  fCheckb[0]->Connect("Toggled(Bool_t)", "MyButtonTest", this,
81  "SetGroupEnabled(Bool_t)");
82 
83  TGHorizontalFrame *fHL3 = new TGHorizontalFrame(this, 70, 100, kFixedWidth);
84  fExit = new TGTextButton(fHL3, "&Exit", IDs.GetUnID());
85  fExit->Connect("Clicked()", "MyButtonTest", this, "DoExit()");
86  fHL3->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX));
87  AddFrame(fHL3, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY,1,1,1,1));
88 
89  //Default state
90  fCheckb[0]->SetOn();
91  fButtonGroup->SetState(kTRUE);
92 
93  SetWindowName("My Button Group");
94  MapSubwindows();
95  Resize(GetDefaultSize());
96  MapWindow();
97 
98  fButtonGroup->SetRadioButtonExclusive(kTRUE);
99  fRadiob[1]->SetOn();
100 };
101 
102 MyButtonTest::~MyButtonTest()
103 {
104  // Destructor.
105  Cleanup();
106 }
107 
108 void MyButtonTest::DoExit()
109 {
110  // Exit this application via the Exit button or Window Manager.
111  // Use one of the both lines according to your needs.
112  // Please note to re-run this macro in the same ROOT session,
113  // you have to compile it to get signals/slots 'on place'.
114 
115  //DeleteWindow(); // to stay in the ROOT session
116  gApplication->Terminate(); // to exit and close the ROOT session
117 }
118 
119 void MyButtonTest::SetGroupEnabled(Bool_t on)
120 {
121  fButtonGroup->SetState(on);
122 }
123 
124 void buttongroupState()
125 {
126  new MyButtonTest(gClient->GetRoot(),100,100);
127 }
128 
TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:32
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
kFixedWidth
@ kFixedWidth
Definition: GuiTypes.h:387
TGMainFrame
Definition: TGFrame.h:443
TGLayout.h
TGHotString
Definition: TGString.h:52
TGTextButton
Definition: TGButton.h:142
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
TGVButtonGroup
Definition: TGButtonGroup.h:101
kLHintsCenterY
@ kLHintsCenterY
Definition: TGLayout.h:35
TGRadioButton
Definition: TGButton.h:322
h
#define h(i)
Definition: RSha256.hxx:106
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
TGButtonGroup.h
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TApplication::Terminate
virtual void Terminate(Int_t status=0)
TGClient.h
TGLabel.h
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGButton.h
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:50
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
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