Logo ROOT   6.10/09
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 
TH1 * h
Definition: legend2.C:5
#define gClient
Definition: TGClient.h:166
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
#define ClassDef(name, id)
Definition: Rtypes.h:297
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:867
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
virtual void SetOn(Bool_t on=kTRUE, Bool_t emit=kFALSE)
Definition: TGButton.h:120
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void SetToolTipText(const char *text, Long_t delayms=400)
Set tool tip text associated with this button.
Definition: TGButton.cxx:395