ROOT logo
// @(#)root/meta:$Id: TToggle.cxx 20882 2007-11-19 11:31:26Z rdm $
// Author: Piotr Golonka   30/07/97

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TToggle                                                              //
//                                                                      //
// This class defines toggling facility for both - object's method or   //
// variables.                                                           //
// Assume that user provides an object with a two-state field , and     //
// methods to Get/Set value of this field. This object enables to switch//
// values via this method when the only thing you know about the field  //
// is the name of the method (or method itself) which sets the field.   //
// This facility is required in context Pop-Up menu, when the only      //
// information about how to toggle a field is a name of methhod which   //
// sets it.                                                             //
// This class may be also used for toggling an integer variable,        //
// which may be important while building universal objects...           //
// When user provides a "set-method" of name SetXXX this object tries   //
// automaticaly find a matching "get-method" by lookin for a method     //
// with name GetXXX, IsXXX or HasXXX for given object.                  //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TMethod.h"
#include "TToggle.h"
#include "TDataMember.h"


ClassImp(TToggle)

//______________________________________________________________________________
TToggle::TToggle()
{
   // TToggle default constructor. You have to initialize it before using
   // by making a call to SetToggledVariable() or SetToggledObject().

   fState       =  kFALSE;
   fValue       = -1;
   fOnValue     =  1;
   fOffValue    =  0;
   fInitialized =  0;
   fObject      =  0;
   fGetter      =  0;
   fSetter      =  0;
   fTglVariable =  0;
}

//______________________________________________________________________________
void TToggle::SetToggledVariable(Int_t &var)
{
   // Initializes object for use with a variable - you pass it via reference
   // so it will be modified by Toggle.

   fTglVariable=&var;
   fValue=var;
}

//______________________________________________________________________________
Bool_t TToggle::GetState()
{
   // Returns the state of Toggle according to its current value and
   // fOnValue, returns true if they match.

   if (fInitialized)
      if (fGetter) fGetter->Execute(fObject, fValue);
   return (fState = (fValue == fOnValue));
}

//______________________________________________________________________________
void TToggle::SetState(Bool_t state)
{
   // Sets the value of toggle to fOnValue or fOffValue according to passed
   // argument.

   if (fInitialized) {
      char stringon[7];
      char stringoff[7];
      sprintf(stringon,"%li",fOnValue);
      sprintf(stringoff,"%li",fOffValue);

      fSetter->Execute(fObject, state ? stringon:stringoff);
      fState=state;
      fValue= ( state ? fOnValue : fOffValue);
   }
}

//______________________________________________________________________________
void TToggle::SetValue(Long_t val)
{
   // Sets the value of toggle and modifies its state according to whether
   // the value is equal to fOnValue.

   if (fInitialized) {
      char stringval[7];
      sprintf(stringval,"%li",val);
      fSetter->Execute(fObject, stringval);
      fState=(val==fOnValue);
      fValue= val;
   }
}

//______________________________________________________________________________
void TToggle::Toggle()
{
   // Toggles the Values and State of this object and connected data!

   if (fInitialized){
      if (fTglVariable){
         *fTglVariable = !(*fTglVariable);
         fValue=(*fTglVariable);
         fState=*fTglVariable;
      }
      if (fGetter && fSetter){
         fGetter->Execute(fObject,fValue);
         fValue=( (fValue==fOnValue) ? fOffValue:fOnValue);
         fState=(!(fValue!=fOnValue));
         char stringon[7];
         sprintf(stringon,"%li",fValue);
         fSetter->Execute(fObject, stringon);
      }
   }
}

//______________________________________________________________________________
void TToggle::SetToggledObject(TObject *obj, TMethod *anymethod)
{
   // Initializes it to toggle an object's datamember using this object's
   // method.

   fObject = obj;
   TDataMember *m = anymethod->FindDataMember();
   if (!m) {
      // try to see if the TMethod has a getter associated via the *GETTER=
      // comment string
      if (anymethod->GetterMethod()) {
         fGetter = anymethod->GetterMethod();
         fSetter = anymethod->SetterMethod();
         fInitialized = 1;
      } else
         Error("SetToggledObject", "cannot determine getter method for %s", anymethod->GetName());
   } else {
      fGetter = m->GetterMethod(obj->IsA());
      fSetter = m->SetterMethod(obj->IsA());
      fInitialized = 1;
   }
}
 TToggle.cxx:1
 TToggle.cxx:2
 TToggle.cxx:3
 TToggle.cxx:4
 TToggle.cxx:5
 TToggle.cxx:6
 TToggle.cxx:7
 TToggle.cxx:8
 TToggle.cxx:9
 TToggle.cxx:10
 TToggle.cxx:11
 TToggle.cxx:12
 TToggle.cxx:13
 TToggle.cxx:14
 TToggle.cxx:15
 TToggle.cxx:16
 TToggle.cxx:17
 TToggle.cxx:18
 TToggle.cxx:19
 TToggle.cxx:20
 TToggle.cxx:21
 TToggle.cxx:22
 TToggle.cxx:23
 TToggle.cxx:24
 TToggle.cxx:25
 TToggle.cxx:26
 TToggle.cxx:27
 TToggle.cxx:28
 TToggle.cxx:29
 TToggle.cxx:30
 TToggle.cxx:31
 TToggle.cxx:32
 TToggle.cxx:33
 TToggle.cxx:34
 TToggle.cxx:35
 TToggle.cxx:36
 TToggle.cxx:37
 TToggle.cxx:38
 TToggle.cxx:39
 TToggle.cxx:40
 TToggle.cxx:41
 TToggle.cxx:42
 TToggle.cxx:43
 TToggle.cxx:44
 TToggle.cxx:45
 TToggle.cxx:46
 TToggle.cxx:47
 TToggle.cxx:48
 TToggle.cxx:49
 TToggle.cxx:50
 TToggle.cxx:51
 TToggle.cxx:52
 TToggle.cxx:53
 TToggle.cxx:54
 TToggle.cxx:55
 TToggle.cxx:56
 TToggle.cxx:57
 TToggle.cxx:58
 TToggle.cxx:59
 TToggle.cxx:60
 TToggle.cxx:61
 TToggle.cxx:62
 TToggle.cxx:63
 TToggle.cxx:64
 TToggle.cxx:65
 TToggle.cxx:66
 TToggle.cxx:67
 TToggle.cxx:68
 TToggle.cxx:69
 TToggle.cxx:70
 TToggle.cxx:71
 TToggle.cxx:72
 TToggle.cxx:73
 TToggle.cxx:74
 TToggle.cxx:75
 TToggle.cxx:76
 TToggle.cxx:77
 TToggle.cxx:78
 TToggle.cxx:79
 TToggle.cxx:80
 TToggle.cxx:81
 TToggle.cxx:82
 TToggle.cxx:83
 TToggle.cxx:84
 TToggle.cxx:85
 TToggle.cxx:86
 TToggle.cxx:87
 TToggle.cxx:88
 TToggle.cxx:89
 TToggle.cxx:90
 TToggle.cxx:91
 TToggle.cxx:92
 TToggle.cxx:93
 TToggle.cxx:94
 TToggle.cxx:95
 TToggle.cxx:96
 TToggle.cxx:97
 TToggle.cxx:98
 TToggle.cxx:99
 TToggle.cxx:100
 TToggle.cxx:101
 TToggle.cxx:102
 TToggle.cxx:103
 TToggle.cxx:104
 TToggle.cxx:105
 TToggle.cxx:106
 TToggle.cxx:107
 TToggle.cxx:108
 TToggle.cxx:109
 TToggle.cxx:110
 TToggle.cxx:111
 TToggle.cxx:112
 TToggle.cxx:113
 TToggle.cxx:114
 TToggle.cxx:115
 TToggle.cxx:116
 TToggle.cxx:117
 TToggle.cxx:118
 TToggle.cxx:119
 TToggle.cxx:120
 TToggle.cxx:121
 TToggle.cxx:122
 TToggle.cxx:123
 TToggle.cxx:124
 TToggle.cxx:125
 TToggle.cxx:126
 TToggle.cxx:127
 TToggle.cxx:128
 TToggle.cxx:129
 TToggle.cxx:130
 TToggle.cxx:131
 TToggle.cxx:132
 TToggle.cxx:133
 TToggle.cxx:134
 TToggle.cxx:135
 TToggle.cxx:136
 TToggle.cxx:137
 TToggle.cxx:138
 TToggle.cxx:139
 TToggle.cxx:140
 TToggle.cxx:141
 TToggle.cxx:142
 TToggle.cxx:143
 TToggle.cxx:144
 TToggle.cxx:145
 TToggle.cxx:146
 TToggle.cxx:147
 TToggle.cxx:148
 TToggle.cxx:149
 TToggle.cxx:150
 TToggle.cxx:151
 TToggle.cxx:152
 TToggle.cxx:153
 TToggle.cxx:154
 TToggle.cxx:155