Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGroupButton.cxx
Go to the documentation of this file.
1 // @(#)root/gpad:$Id: faa839c41a9482cbcb403b991070b15d606c9137 $
2 // Author: Rene Brun 01/07/96
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "Riostream.h"
13 #include "TROOT.h"
14 #include "TBox.h"
15 #include "TGroupButton.h"
16 #include "TVirtualX.h"
17 #include "TDialogCanvas.h"
18 #include "TCanvas.h"
19 #include "TText.h"
20 #include "TInterpreter.h"
21 #include "TTimer.h"
22 #include <string.h>
23 
24 ClassImp(TGroupButton);
25 
26 
27 /** \class TGroupButton
28 \ingroup gpad
29 
30 A specialized TButton used in a group of Buttons.
31 When a button from a group of TGroupButtons is selected, all other buttons
32 from the group with the same name are disabled.
33 
34 For examples of use of TGroupButton objects, see:
35 AttFillCanvas, TAttLineCanvas, TAttTextCanvas and TAttMarkerCanvas.
36 
37 A TGroupButton object is a specialized TPad including possible list
38 of primitives used to build selections and options menus in a canvas.
39 */
40 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// GroupButton default constructor.
44 
45 TGroupButton::TGroupButton(): TButton()
46 {
47  SetFraming();
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// GroupButton normal constructor.
52 
53 TGroupButton::TGroupButton(const char *groupname, const char *title, const char *method, Double_t x1, Double_t y1,Double_t x2, Double_t y2)
54  :TButton(title,method,x1,y1,x2,y2)
55 {
56  SetName((char*)groupname);
57  SetFraming();
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// GroupButton default destructor.
62 
63 TGroupButton::~TGroupButton()
64 {
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Display Color Table in an attribute canvas.
69 
70 void TGroupButton::DisplayColorTable(const char *action, Double_t x0, Double_t y0, Double_t wc, Double_t hc)
71 {
72  TGroupButton *colorpad;
73  Int_t i, j;
74  Int_t color;
75  Double_t xlow, ylow, hs, ws;
76 
77  // draw color table buttons
78  hs = hc/5;
79  ws = wc/10;
80  char command[32];
81  for (i=0;i<10;i++) {
82  xlow = x0 + ws*i;
83  for (j=0;j<5;j++) {
84  ylow = y0 + hs*j;
85  color = 10*j + i + 1;
86  snprintf(command,32,"%s(%d)",action,10*j+i+1);
87  colorpad = new TGroupButton("Color","",command,xlow, ylow, xlow+0.9*ws, ylow+0.9*hs);
88  colorpad->SetFillColor(color);
89  colorpad->SetBorderSize(1);
90  if (i == 0 && j == 0) colorpad->SetBorderMode(-1);
91  colorpad->Draw();
92  }
93  }
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Execute action of this button.
98 ///
99 /// If an object has been selected before executing the APPLY button
100 /// in the control canvas, The member function and its parameters
101 /// for this object is executed via the interpreter.
102 
103 void TGroupButton::ExecuteAction()
104 {
105  TVirtualPad *pad;
106  char line[128];
107  strlcpy(line,GetMethod(),128);
108  char *method = line;
109  if(!line[0]) return;
110  char *params = strchr(method,'(');
111  if (params) {
112  *params = 0;
113  params++;
114  char *end = strrchr(params,')');
115  if (end) *end = 0;
116  }
117  TDialogCanvas *canvas = (TDialogCanvas*)GetMother();
118  TObject *obj = canvas->GetRefObject();
119  if (!obj) return;
120  if (strcmp(method,"PIXELS")) {
121  obj->Execute(method,params);
122  } else {
123  TText *text = (TText*)GetListOfPrimitives()->First();
124  Int_t npixels = Int_t((YtoPixel(0) - YtoPixel(1))*text->GetTextSize());
125  Double_t dy;
126  pad = gROOT->GetSelectedPad();
127  if (!params) return;
128  Int_t nmax = (Int_t)(params-method);
129  if (obj->InheritsFrom("TPaveLabel")) {
130  TBox *pl = (TBox*)obj;
131  dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
132  snprintf(params,nmax,"%f",dy/(pl->GetY2() - pl->GetY1()));
133  obj->Execute("SetTextSize",params);
134  } else {
135  if (obj->InheritsFrom("TPave")) {
136  dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
137  snprintf(params,nmax,"%f",dy/(pad->GetY2() - pad->GetY1()));
138  obj->Execute("SetTextSize",params);
139  } else {
140  snprintf(params,nmax,"%d",npixels);
141  obj->Execute("SetTextSizePixels",params);
142  }
143  }
144  }
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Execute action corresponding to one event.
149 ///
150 /// This member function is called when a Button object is clicked.
151 
152 void TGroupButton::ExecuteEvent(Int_t event, Int_t px, Int_t py)
153 {
154  if (fMother->IsEditable()) {
155  TPad::ExecuteEvent(event,px,py);
156  return;
157  }
158 
159  TCanvas *c = gPad->GetCanvas();
160  if (!c) return;
161  TIter next(c->GetListOfPrimitives());
162  TObject *obj;
163  TGroupButton *button;
164  TPad *pad;
165  TDialogCanvas *canvas;
166 
167  switch (event) {
168 
169  case kButton1Down:
170 
171  case kMouseMotion:
172 
173  break;
174 
175  case kButton1Motion:
176 
177  break;
178 
179  case kButton1Up:
180  //Clicked on APPLY button?
181  if (!strcasecmp(GetName(),"APPLY")) {
182  canvas = (TDialogCanvas*)GetMother();
183  if (!strcasecmp(GetTitle(),"CLOSE")) {
184  canvas->Close();
185  return;
186  }
187  pad = canvas->GetRefPad();
188  if (pad) pad->GetCanvas()->FeedbackMode(kFALSE);
189  canvas->Apply(GetTitle()); //just in case the apply button executes some code
190  if (pad) {
191  pad->Modified(kTRUE);
192  pad->Update();
193  }
194  break;
195  }
196  //Unset other buttons with same name
197  while ((obj = next())) {
198  if (obj == this) continue;
199  if (obj->InheritsFrom(TGroupButton::Class())) {
200  button = (TGroupButton*)obj;
201  if (!strcmp(button->GetName(),GetName())) {
202  if (button->GetBorderMode() < 0) {
203  button->SetBorderMode(1);
204  button->Modified();
205  }
206  }
207  }
208  }
209  //Set button on
210  SetBorderMode(-1);
211  Modified();
212  c->Modified();
213  gPad->Update();
214  break;
215  }
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Save primitive as a C++ statement(s) on output stream out
220 
221 void TGroupButton::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
222 {
223  TPad *padsav = (TPad*)gPad;
224  char quote = '"';
225  if (gROOT->ClassSaved(TGroupButton::Class())) {
226  out<<" ";
227  } else {
228  out<<" TGroupButton *";
229  }
230  out<<"button = new TGroupButton("<<quote<<GetName()<<quote<<", "<<quote<<GetTitle()
231  <<quote<<","<<quote<<GetMethod()<<quote
232  <<","<<fXlowNDC
233  <<","<<fYlowNDC
234  <<","<<fXlowNDC+fWNDC
235  <<","<<fYlowNDC+fHNDC
236  <<");"<<std::endl;
237 
238  SaveFillAttributes(out,"button",0,1001);
239  SaveLineAttributes(out,"button",1,1,1);
240  SaveTextAttributes(out,"button",22,0,1,62,.75);
241 
242  if (GetBorderSize() != 2) {
243  out<<" button->SetBorderSize("<<GetBorderSize()<<");"<<std::endl;
244  }
245  if (GetBorderMode() != 1) {
246  out<<" button->SetBorderMode("<<GetBorderMode()<<");"<<std::endl;
247  }
248 
249  out<<" button->Draw();"<<std::endl;
250  out<<" button->cd();"<<std::endl;
251 
252  TIter next(GetListOfPrimitives());
253  TObject *obj = next(); //do not save first primitive
254 
255  while ((obj = next()))
256  obj->SavePrimitive(out, (Option_t *)next.GetOption());
257 
258  out<<" "<<padsav->GetName()<<"->cd();"<<std::endl;
259  padsav->cd();
260 }