Logo ROOT   6.14/05
Reference Guide
buttonTest.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gui
3 /// This macro gives an example of how to set/change text button attributes.
4 /// To run it do either:
5 /// ~~~
6 /// .x buttonTest.C
7 /// .x buttonTest.C++
8 /// ~~~
9 ///
10 /// \macro_code
11 ///
12 /// \author Valeriy Onuchin 17/07/2007
13 
14 #include <TGButton.h>
15 #include <TGButtonGroup.h>
16 #include <TGLabel.h>
17 #include <TGNumberEntry.h>
18 #include <TG3DLine.h>
19 #include <TApplication.h>
20 
21 
22 
23 //////////// auxilary class ///////////////////////////////////////////////////
24 class TextMargin : public TGHorizontalFrame {
25 
26 protected:
27  TGNumberEntry *fEntry;
28 
29 public:
30  TextMargin(const TGWindow *p, const char *name) : TGHorizontalFrame(p)
31  {
32  fEntry = new TGNumberEntry(this, 0, 6, -1, TGNumberFormat::kNESInteger);
33  AddFrame(fEntry, new TGLayoutHints(kLHintsLeft));
34  TGLabel *label = new TGLabel(this, name);
35  AddFrame(label, new TGLayoutHints(kLHintsLeft, 10));
36  }
37  TGTextEntry *GetEntry() const { return fEntry->GetNumberEntry(); }
38 
39  ClassDef(TextMargin, 0)
40 };
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 class ButtonWindow : public TGMainFrame {
44 
45 protected:
46  TGTextButton *fButton; // button being tested
47 
48 public:
49  ButtonWindow();
50  void DoHPosition(Int_t);
51  void DoVPosition(Int_t);
52  void DoLeftMargin(char*);
53  void DoRightMargin(char*);
54  void DoTopMargin(char*);
55  void DoBottomMargin(char*);
56 
57  ClassDef(ButtonWindow, 0)
58 };
59 
60 
61 //______________________________________________________________________________
62 ButtonWindow::ButtonWindow() : TGMainFrame(gClient->GetRoot(), 10, 10, kHorizontalFrame)
63 {
64  // Main test window.
65 
66  SetCleanup(kDeepCleanup);
67 
68  // Controls on right
69  TGVerticalFrame *controls = new TGVerticalFrame(this);
70  AddFrame(controls, new TGLayoutHints(kLHintsRight | kLHintsExpandY,
71  5, 5, 5, 5));
72 
73  // Separator
74  TGVertical3DLine *separator = new TGVertical3DLine(this);
75  AddFrame(separator, new TGLayoutHints(kLHintsRight | kLHintsExpandY));
76 
77  // Contents
78  TGHorizontalFrame *contents = new TGHorizontalFrame(this);
79  AddFrame(contents, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,5,5));
80 
81  // The button for test
82  fButton = new TGTextButton(contents,
83  "&This button has a multi-line label\nand shows features\n"
84  "available in the button classes");
85  fButton->Resize(300, 200);
86  fButton->ChangeOptions(fButton->GetOptions() | kFixedSize);
87  fButton->SetToolTipText("The assigned tooltip\ncan be multi-line also",200);
88  contents->AddFrame(fButton, new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,
89  20, 20, 20, 20));
90 
91  TGGroupFrame *group = new TGGroupFrame(controls, "Enable/Disable");
93  TGCheckButton *disable = new TGCheckButton(group, "Switch state\nEnable/Disable");
94  disable->SetOn();
95  disable->Connect("Toggled(Bool_t)", "TGButton", fButton, "SetEnabled(Bool_t)");
96  group->AddFrame(disable, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
97  controls->AddFrame(group, new TGLayoutHints(kLHintsExpandX));
98 
99 
100  // control horizontal position of the text
101  TGButtonGroup *horizontal = new TGButtonGroup(controls, "Horizontal Position");
102  horizontal->SetTitlePos(TGGroupFrame::kCenter);
103  new TGRadioButton(horizontal, "Center", kTextCenterX);
104  new TGRadioButton(horizontal, "Left", kTextLeft);
105  new TGRadioButton(horizontal, "Right", kTextRight);
106  horizontal->SetButton(kTextCenterX);
107  horizontal->Connect("Pressed(Int_t)", "ButtonWindow", this,
108  "DoHPosition(Int_t)");
109  controls->AddFrame(horizontal, new TGLayoutHints(kLHintsExpandX));
110 
111 
112  // control vertical position of the text
113  TGButtonGroup *vertical = new TGButtonGroup(controls, "Vertical Position");
115  new TGRadioButton(vertical, "Center", kTextCenterY);
116  new TGRadioButton(vertical, "Top", kTextTop);
117  new TGRadioButton(vertical, "Bottom", kTextBottom);
118  vertical->SetButton(kTextCenterY);
119  vertical->Connect("Pressed(Int_t)", "ButtonWindow", this,
120  "DoVPosition(Int_t)");
121  controls->AddFrame(vertical, new TGLayoutHints(kLHintsExpandX));
122 
123 
124  // control margins of the text
125  TGGroupFrame *margins = new TGGroupFrame(controls, "Text Margins");
127 
128  TextMargin *left = new TextMargin(margins, "Left");
129  margins->AddFrame(left, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 2));
130  left->GetEntry()->Connect("TextChanged(char*)", "ButtonWindow",
131  this, "DoLeftMargin(char*)");
132 
133  TextMargin *right = new TextMargin(margins, "Right");
134  margins->AddFrame(right, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 2));
135  right->GetEntry()->Connect("TextChanged(char*)", "ButtonWindow",
136  this, "DoRightMargin(char*)");
137 
138  TextMargin *top = new TextMargin(margins, "Top");
139  margins->AddFrame(top, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 2));
140  top->GetEntry()->Connect("TextChanged(char*)", "ButtonWindow",
141  this, "DoTopMargin(char*)");
142 
143  TextMargin *bottom = new TextMargin(margins, "Bottom");
144  margins->AddFrame(bottom, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 2));
145  bottom->GetEntry()->Connect("TextChanged(char*)", "ButtonWindow",
146  this, "DoBottomMargin(char*)");
147 
148  controls->AddFrame(margins, new TGLayoutHints(kLHintsExpandX));
149 
150  TGTextButton *quit = new TGTextButton(controls, "Quit");
151  controls->AddFrame(quit, new TGLayoutHints(kLHintsBottom | kLHintsExpandX,
152  0, 0, 0, 5));
153  quit->Connect("Pressed()", "TApplication", gApplication, "Terminate()");
154 
155  Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
156  DontCallClose();
157 
158  MapSubwindows();
159  Resize();
160 
161  SetWMSizeHints(GetDefaultWidth(), GetDefaultHeight(), 1000, 1000, 0 ,0);
162  SetWindowName("Button Test");
163  MapRaised();
164 }
165 
166 //______________________________________________________________________________
167 void ButtonWindow::DoHPosition(Int_t id)
168 {
169  // Horizontal position handler.
170 
171  Int_t tj = fButton->GetTextJustify();
172  tj &= ~kTextCenterX;
173  tj &= ~kTextLeft;
174  tj &= ~kTextRight;
175  tj |= id;
176  fButton->SetTextJustify(tj);
177 }
178 
179 //______________________________________________________________________________
180 void ButtonWindow::DoVPosition(Int_t id)
181 {
182  // Vertical position handler.
183 
184  Int_t tj = fButton->GetTextJustify();
185 
186  tj &= ~kTextCenterY;
187  tj &= ~kTextTop;
188  tj &= ~kTextBottom;
189  tj |= id;
190  fButton->SetTextJustify(tj);
191 }
192 
193 //______________________________________________________________________________
194 void ButtonWindow::DoLeftMargin(char *val)
195 {
196  // Set left text margin.
197 
198  fButton->SetLeftMargin(atoi(val));
199  gClient->NeedRedraw(fButton);
200 }
201 
202 //______________________________________________________________________________
203 void ButtonWindow::DoRightMargin(char *val)
204 {
205  // Set right text margin.
206 
207  fButton->SetRightMargin(atoi(val));
208  gClient->NeedRedraw(fButton);
209 }
210 
211 //______________________________________________________________________________
212 void ButtonWindow::DoTopMargin(char *val)
213 {
214  // Set top text margin.
215 
216  fButton->SetTopMargin(atoi(val));
217  gClient->NeedRedraw(fButton);
218 }
219 
220 //______________________________________________________________________________
221 void ButtonWindow::DoBottomMargin(char *val)
222 {
223  // Set bottom text margin.
224 
225  fButton->SetBottomMargin(atoi(val));
226  gClient->NeedRedraw(fButton);
227 }
228 
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 void buttonTest()
232 {
233  // Main program.
234 
235  new ButtonWindow();
236 }
virtual void SetButton(Int_t id, Bool_t down=kTRUE)
Sets the button with id to be on/down, and if this is an exclusive group, all other button in the gro...
#define gClient
Definition: TGClient.h:166
int Int_t
Definition: RtypesCore.h:41
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
virtual void SetTitlePos(ETitlePos pos=kLeft)
Definition: TGFrame.h:651
#define ClassDef(name, id)
Definition: Rtypes.h:320
XFontStruct * id
Definition: TGX11.cxx:108
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
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
TGNumberEntryField * GetNumberEntry() const
char name[80]
Definition: TGX11.cxx:109