Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TToggle.cxx
Go to the documentation of this file.
1 // @(#)root/meta:$Id$
2 // Author: Piotr Golonka 30/07/97
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 /** \class TToggle
13 
14 This class defines toggling facility for both - object's method or
15 variables.
16 Assume that user provides an object with a two-state field , and
17 methods to Get/Set value of this field. This object enables to switch
18 values via this method when the only thing you know about the field
19 is the name of the method (or method itself) which sets the field.
20 This facility is required in context Pop-Up menu, when the only
21 information about how to toggle a field is a name of methhod which
22 sets it.
23 This class may be also used for toggling an integer variable,
24 which may be important while building universal objects...
25 When user provides a "set-method" of name SetXXX this object tries
26 automaticaly find a matching "get-method" by lookin for a method
27 with name GetXXX, IsXXX or HasXXX for given object.
28 */
29 
30 #include "TMethod.h"
31 #include "TToggle.h"
32 #include "TDataMember.h"
33 
34 
35 ClassImp(TToggle);
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// TToggle default constructor. You have to initialize it before using
39 /// by making a call to SetToggledVariable() or SetToggledObject().
40 
41 TToggle::TToggle()
42 {
43  fState = kFALSE;
44  fValue = -1;
45  fOnValue = 1;
46  fOffValue = 0;
47  fInitialized = 0;
48  fObject = 0;
49  fGetter = 0;
50  fSetter = 0;
51  fTglVariable = 0;
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Initializes object for use with a variable - you pass it via reference
56 /// so it will be modified by Toggle.
57 
58 void TToggle::SetToggledVariable(Int_t &var)
59 {
60  fTglVariable=&var;
61  fValue=var;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Returns the state of Toggle according to its current value and
66 /// fOnValue, returns true if they match.
67 
68 Bool_t TToggle::GetState()
69 {
70  if (fInitialized)
71  if (fGetter) fGetter->Execute(fObject, fValue);
72  return (fState = (fValue == fOnValue));
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Sets the value of toggle to fOnValue or fOffValue according to passed
77 /// argument.
78 
79 void TToggle::SetState(Bool_t state)
80 {
81  if (fInitialized) {
82  char stringon[20];
83  char stringoff[20];
84  snprintf(stringon,sizeof(stringon),"%li",fOnValue);
85  snprintf(stringoff,sizeof(stringoff),"%li",fOffValue);
86 
87  fSetter->Execute(fObject, state ? stringon:stringoff);
88  fState=state;
89  fValue= ( state ? fOnValue : fOffValue);
90  }
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Sets the value of toggle and modifies its state according to whether
95 /// the value is equal to fOnValue.
96 
97 void TToggle::SetValue(Long_t val)
98 {
99  if (fInitialized) {
100  char stringval[20];
101  snprintf(stringval,sizeof(stringval),"%li",val);
102  fSetter->Execute(fObject, stringval);
103  fState=(val==fOnValue);
104  fValue= val;
105  }
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Toggles the Values and State of this object and connected data!
110 
111 void TToggle::Toggle()
112 {
113  if (fInitialized){
114  if (fTglVariable){
115  *fTglVariable = !(*fTglVariable);
116  fValue=(*fTglVariable);
117  fState=*fTglVariable;
118  }
119  if (fGetter && fSetter){
120  fGetter->Execute(fObject,fValue);
121  fValue=( (fValue==fOnValue) ? fOffValue:fOnValue);
122  fState=(!(fValue!=fOnValue));
123  char stringon[20];
124  snprintf(stringon,sizeof(stringon),"%li",fValue);
125  fSetter->Execute(fObject, stringon);
126  }
127  }
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Initializes it to toggle an object's datamember using this object's
132 /// method.
133 
134 void TToggle::SetToggledObject(TObject *obj, TMethod *anymethod)
135 {
136  fObject = obj;
137  TDataMember *m = anymethod->FindDataMember();
138  if (!m) {
139  // try to see if the TMethod has a getter associated via the *GETTER=
140  // comment string
141  if (anymethod->GetterMethod()) {
142  fGetter = anymethod->GetterMethod();
143  fSetter = anymethod->SetterMethod();
144  fInitialized = 1;
145  } else
146  Error("SetToggledObject", "cannot determine getter method for %s", anymethod->GetName());
147  } else {
148  fGetter = m->GetterMethod(obj->IsA());
149  fSetter = m->SetterMethod(obj->IsA());
150  fInitialized = 1;
151  }
152 }