Logo ROOT  
Reference Guide
buttonChangelabel.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gui
3 /// This macro gives an example for changing text button labels anytime the Start or Pause buttons are clicked.
4 /// To run it do either:
5 /// ~~~
6 /// .x buttonChangelabel.C
7 /// .x buttonChangelabel.C++
8 /// ~~~
9 ///
10 /// \macro_code
11 ///
12 /// \author Ilka Antcheva 1/12/2006
13 
14 #include <TGClient.h>
15 #include <TGButton.h>
16 #include <TGFrame.h>
17 
18 class MyMainFrame : public TGMainFrame {
19 
20 private:
21  TGCompositeFrame *fCframe;
22  TGTextButton *fStart, *fPause, *fExit;
23  Bool_t start, pause;
24 
25 public:
26  MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
27  virtual ~MyMainFrame();
28  // slots
29  void ChangeStartLabel();
30  void ChangePauseLabel();
31 
32  ClassDef(MyMainFrame, 0)
33 };
34 
35 void MyMainFrame::ChangeStartLabel()
36 {
37  // Slot connected to the Clicked() signal.
38  // It will toggle labels "Start" and "Stop".
39 
40  fStart->SetState(kButtonDown);
41  if (!start) {
42  fStart->SetText("&Stop");
43  start = kTRUE;
44  } else {
45  fStart->SetText("&Start");
46  start = kFALSE;
47  }
48  fStart->SetState(kButtonUp);
49 }
50 
51 void MyMainFrame::ChangePauseLabel()
52 {
53  // Slot connected to the Clicked() signal.
54  // It will toggle labels "Resume" and "Pause".
55 
56  fPause->SetState(kButtonDown);
57  if (!pause) {
58  fPause->SetText("&Resume");
59  pause = kTRUE;
60  } else {
61  fPause->SetText("&Pause");
62  pause = kFALSE;
63  }
64  fPause->SetState(kButtonUp);
65 }
66 
67 MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
68  TGMainFrame(p, w, h)
69 {
70  // Create a horizontal frame containing buttons
71  fCframe = new TGCompositeFrame(this, 170, 20, kHorizontalFrame|kFixedWidth);
72 
73  fStart = new TGTextButton(fCframe, "&Start");
74  fStart->Connect("Clicked()", "MyMainFrame", this, "ChangeStartLabel()");
75  fCframe->AddFrame(fStart, new TGLayoutHints(kLHintsTop | kLHintsExpandX,
76  3, 2, 2, 2));
77  fStart->SetToolTipText("Click to toggle the button label (Start/Stop)");
78  start = kFALSE;
79 
80  fPause = new TGTextButton(fCframe, "&Pause");
81  fPause->Connect("Clicked()", "MyMainFrame", this, "ChangePauseLabel()");
82  fPause->SetToolTipText("Click to toggle the button label (Pause/Resume)");
83  fCframe->AddFrame(fPause, new TGLayoutHints(kLHintsTop | kLHintsExpandX,
84  3, 2, 2, 2));
85  pause = kFALSE;
86 
87  AddFrame(fCframe, new TGLayoutHints(kLHintsCenterX, 2, 2, 5, 1));
88 
89  fExit = new TGTextButton(this, "&Exit ","gApplication->Terminate(0)");
90  AddFrame(fExit, new TGLayoutHints(kLHintsTop | kLHintsExpandX,5,5,2,2));
91 
92  SetWindowName("Change Labels");
93 
94  MapSubwindows();
95  Resize(GetDefaultSize());
96  MapWindow();
97 }
98 
99 
100 MyMainFrame::~MyMainFrame()
101 {
102  // Clean up all widgets, frames and layouthints that were used
103  fCframe->Cleanup();
104  Cleanup();
105 }
106 
107 
108 void buttonChangelabel()
109 {
110  // Popup the GUI...
111  new MyMainFrame(gClient->GetRoot(), 350, 80);
112 }
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
kButtonDown
@ kButtonDown
Definition: TGButton.h:54
kLHintsTop
@ kLHintsTop
Definition: TGLayout.h:34
kButtonUp
@ kButtonUp
Definition: TGButton.h:53
TGTextButton
Definition: TGButton.h:142
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
gClient
#define gClient
Definition: TGClient.h:166
h
#define h(i)
Definition: RSha256.hxx:106
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TGClient.h
kHorizontalFrame
@ kHorizontalFrame
Definition: GuiTypes.h:382
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGButton.h
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
TGCompositeFrame
Definition: TGFrame.h:323