ROOT logo

From $ROOTSYS/tutorials/gui/buttongroupState.C

// A simple example that shows the enabled and disabled state
// of a button group with radio and check buttons. 
//
// Author: Roel Aaij   4/07/2007

#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TGLayout.h>
#include <TGWindow.h>
#include <TGLabel.h>
#include <TString.h>
#include <TGButtonGroup.h>

class IDList {

private:
   Int_t nID;   // creates unique widget's IDs

public:
   IDList() : nID(0) {}
   ~IDList() {}
   Int_t GetUnID(void) { return ++nID; }
};

class MyButtonTest : public TGMainFrame {

private:
   TGTextButton        *fExit;         // Exit text button
   TGVButtonGroup      *fButtonGroup;  // Button group
   TGCheckButton       *fCheckb[4];    // Check buttons
   TGRadioButton       *fRadiob[2];    // Radio buttons
   IDList               IDs;           // Widget IDs generator
   
public:
   MyButtonTest(const TGWindow *p, UInt_t w, UInt_t h);
   virtual ~MyButtonTest();

   void DoExit(void);
   void SetGroupEnabled(Bool_t);

   ClassDef(MyButtonTest, 0)
};
                          
MyButtonTest::MyButtonTest(const TGWindow *p, UInt_t w, UInt_t h) 
   : TGMainFrame(p, w, h)   
{
   SetCleanup(kDeepCleanup);
   
   Connect("CloseWindow()", "MyButtonTest", this, "DoExit()");
   DontCallClose();

   TGHorizontalFrame *fHL2 = new TGHorizontalFrame(this, 70, 100);
   fCheckb[0] = new TGCheckButton(fHL2, new TGHotString("Enable BG"), 
                                  IDs.GetUnID());
   fCheckb[0]->SetToolTipText("Enable/Disable the button group");
   fHL2->AddFrame(fCheckb[0], new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,
                                                1, 1, 1, 1));
   fButtonGroup = new TGVButtonGroup(fHL2, "My Button Group");
   fCheckb[1] = new TGCheckButton(fButtonGroup, new TGHotString("CB 2"),
                                  IDs.GetUnID());
   fCheckb[2] = new TGCheckButton(fButtonGroup, new TGHotString("CB 3"),
                                  IDs.GetUnID());
   fCheckb[3] = new TGCheckButton(fButtonGroup, new TGHotString("CB 4"),
                                  IDs.GetUnID());
   fRadiob[0] = new TGRadioButton(fButtonGroup, new TGHotString("RB 1"),
                                  IDs.GetUnID());
   fRadiob[1] = new TGRadioButton(fButtonGroup, new TGHotString("RB 2"),
                                  IDs.GetUnID());
   fButtonGroup->Show();

   fHL2->AddFrame(fButtonGroup, new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,
                                                  1, 1, 1, 1));
   AddFrame(fHL2);

   fCheckb[0]->Connect("Toggled(Bool_t)", "MyButtonTest", this,
                       "SetGroupEnabled(Bool_t)");

   TGHorizontalFrame *fHL3 = new TGHorizontalFrame(this, 70, 100, kFixedWidth);
   fExit = new TGTextButton(fHL3, "&Exit", IDs.GetUnID());
   fExit->Connect("Clicked()", "MyButtonTest", this, "DoExit()");
   fHL3->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX));
   AddFrame(fHL3, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY,1,1,1,1));

   //Default state
   fCheckb[0]->SetOn();
   fButtonGroup->SetState(kTRUE);
   
   SetWindowName("My Button Group");
   MapSubwindows();
   Resize(GetDefaultSize());
   MapWindow();

   fButtonGroup->SetRadioButtonExclusive(kTRUE);
   fRadiob[1]->SetOn();
};

MyButtonTest::~MyButtonTest()
{
   // Destructor.
   Cleanup();
}

void MyButtonTest::DoExit()
{
   // Exit this application via the Exit button or Window Manager.
   // Use one of the both lines according to your needs.
   // Please note to re-run this macro in the same ROOT session,
   // you have to compile it to get signals/slots 'on place'.
   
   //DeleteWindow();            // to stay in the ROOT session
   gApplication->Terminate();   // to exit and close the ROOT session
}

void MyButtonTest::SetGroupEnabled(Bool_t on)
{
   fButtonGroup->SetState(on);
}

void buttongroupState() 
{
   new MyButtonTest(gClient->GetRoot(),100,100);
}

 buttongroupState.C:1
 buttongroupState.C:2
 buttongroupState.C:3
 buttongroupState.C:4
 buttongroupState.C:5
 buttongroupState.C:6
 buttongroupState.C:7
 buttongroupState.C:8
 buttongroupState.C:9
 buttongroupState.C:10
 buttongroupState.C:11
 buttongroupState.C:12
 buttongroupState.C:13
 buttongroupState.C:14
 buttongroupState.C:15
 buttongroupState.C:16
 buttongroupState.C:17
 buttongroupState.C:18
 buttongroupState.C:19
 buttongroupState.C:20
 buttongroupState.C:21
 buttongroupState.C:22
 buttongroupState.C:23
 buttongroupState.C:24
 buttongroupState.C:25
 buttongroupState.C:26
 buttongroupState.C:27
 buttongroupState.C:28
 buttongroupState.C:29
 buttongroupState.C:30
 buttongroupState.C:31
 buttongroupState.C:32
 buttongroupState.C:33
 buttongroupState.C:34
 buttongroupState.C:35
 buttongroupState.C:36
 buttongroupState.C:37
 buttongroupState.C:38
 buttongroupState.C:39
 buttongroupState.C:40
 buttongroupState.C:41
 buttongroupState.C:42
 buttongroupState.C:43
 buttongroupState.C:44
 buttongroupState.C:45
 buttongroupState.C:46
 buttongroupState.C:47
 buttongroupState.C:48
 buttongroupState.C:49
 buttongroupState.C:50
 buttongroupState.C:51
 buttongroupState.C:52
 buttongroupState.C:53
 buttongroupState.C:54
 buttongroupState.C:55
 buttongroupState.C:56
 buttongroupState.C:57
 buttongroupState.C:58
 buttongroupState.C:59
 buttongroupState.C:60
 buttongroupState.C:61
 buttongroupState.C:62
 buttongroupState.C:63
 buttongroupState.C:64
 buttongroupState.C:65
 buttongroupState.C:66
 buttongroupState.C:67
 buttongroupState.C:68
 buttongroupState.C:69
 buttongroupState.C:70
 buttongroupState.C:71
 buttongroupState.C:72
 buttongroupState.C:73
 buttongroupState.C:74
 buttongroupState.C:75
 buttongroupState.C:76
 buttongroupState.C:77
 buttongroupState.C:78
 buttongroupState.C:79
 buttongroupState.C:80
 buttongroupState.C:81
 buttongroupState.C:82
 buttongroupState.C:83
 buttongroupState.C:84
 buttongroupState.C:85
 buttongroupState.C:86
 buttongroupState.C:87
 buttongroupState.C:88
 buttongroupState.C:89
 buttongroupState.C:90
 buttongroupState.C:91
 buttongroupState.C:92
 buttongroupState.C:93
 buttongroupState.C:94
 buttongroupState.C:95
 buttongroupState.C:96
 buttongroupState.C:97
 buttongroupState.C:98
 buttongroupState.C:99
 buttongroupState.C:100
 buttongroupState.C:101
 buttongroupState.C:102
 buttongroupState.C:103
 buttongroupState.C:104
 buttongroupState.C:105
 buttongroupState.C:106
 buttongroupState.C:107
 buttongroupState.C:108
 buttongroupState.C:109
 buttongroupState.C:110
 buttongroupState.C:111
 buttongroupState.C:112
 buttongroupState.C:113
 buttongroupState.C:114
 buttongroupState.C:115
 buttongroupState.C:116
 buttongroupState.C:117
 buttongroupState.C:118
 buttongroupState.C:119
 buttongroupState.C:120
 buttongroupState.C:121
 buttongroupState.C:122
 buttongroupState.C:123
 buttongroupState.C:124
 buttongroupState.C:125
 buttongroupState.C:126