// @(#)root/gpad:$Id$
// Author: Rene Brun   23/11/96

/*************************************************************************
 * 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.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TSlider.h"
#include "TSliderBox.h"

#include <string.h>

ClassImp(TSlider)

//______________________________________________________________________________
//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TSlider                                                              //
//                                                                      //
//  A TSlider object is a specialized TPad including a TSliderBox object//
//  The TSliderBox can be moved in the pad.                             //
//  Slider drawing options include the possibility to change the slider //
//  starting and ending positions or only one of them.                  //
//                                                                      //
//  The current slider position can be retrieved via the functions      //
//     TSlider::GetMinimum and TSlider::GetMaximum                      //
//  These two functions return numbers in the range [0,1].              //
//                                                                      //
//  if a method has been set (via SetMethod), the expression  is        //
//  executed via the interpreter when the button 1 is released.         //
//                                                                      //
//  if no method has been set, and an object is referenced (SetObject   //
//  has been called), while the slider is being moved/resized,          //
//  the object ExecuteEvent function is called.                         //                                                //
//                                                                      //
//  Example 1 using SetMethod    macro xyslider.C                       //
//  =========================                                           //
//{                                                                     //
//   //Example of macro featuring two sliders                           //
//   TFile *f = new TFile("hsimple.root");                              //
//   TH2F *hpxpy = (TH2F*)f->Get("hpxpy");                              //
//   TCanvas *c1 = new TCanvas("c1");                                   //
//   TPad *pad = new TPad("pad","lego pad",0.1,0.1,0.98,0.98);          //
//   pad->SetFillColor(33);                                             //
//   pad->Draw();                                                       //
//   pad->cd();                                                         //
//   gStyle->SetFrameFillColor(42);                                     //
//   hpxpy->SetFillColor(46);                                           //
//   hpxpy->Draw("lego1");                                              //
//   c1->cd();                                                          //
//                                                                      //
//   //Create two sliders in main canvas. When button1                  //
//   //of the mouse will be released, action.C will be called           //
//   TSlider *xslider = new TSlider("xslider","x",0.1,0.02,0.98,0.08);  //
//   xslider->SetMethod(".x action.C");                                 //
//   TSlider *yslider = new TSlider("yslider","y",0.02,0.1,0.06,0.98);  //
//   yslider->SetMethod(".x action.C");                                 //
//}                                                                     //
//                                                                      //
//            macro action.C                                            //
//{                                                                     //
//   Int_t nx = hpxpy->GetXaxis()->GetNbins();                          //
//   Int_t ny = hpxpy->GetYaxis()->GetNbins();                          //
//   Int_t binxmin = nx*xslider->GetMinimum();                          //
//   Int_t binxmax = nx*xslider->GetMaximum();                          //
//   hpxpy->GetXaxis()->SetRange(binxmin,binxmax);                      //
//   Int_t binymin = ny*yslider->GetMinimum();                          //
//   Int_t binymax = ny*yslider->GetMaximum();                          //
//   hpxpy->GetYaxis()->SetRange(binymin,binymax);                      //
//   pad->cd();                                                         //
//   hpxpy->Draw("lego1");                                              //
//   c1->Update();                                                      //
//}                                                                     //
//    The canvas and the sliders created in the above macro are shown   //
//    in the picture below.                                             //
//Begin_Html                                                            //
/*
<img src="gif/xyslider.gif">
*/
//End_Html
//                                                                      //
//  Example 2 using SetObject    macro xyslider.C                       //
//  =========================                                           //
//                                                                      //
//  Same example as above. Instead of SetMethod:                        //
//    Myclass *obj = new Myclass(); // Myclass derived from TObject     //
//    xslider->SetObject(obj);                                          //
//    yslider->SetObject(obj);                                          //
//                                                                      //
//    When the slider will be changed, MyClass::ExecuteEvent will be    //
//    called with px=0 and py = 0                                       //
//////////////////////////////////////////////////////////////////////////


//______________________________________________________________________________
TSlider::TSlider(): TPad()
{
   // slider default constructor.

   fObject  = 0;
   fMethod  = "";
   fMinimum = 0;
   fMaximum = 1;
}


//______________________________________________________________________________
TSlider::TSlider(const char *name, const char *title, Double_t x1, Double_t y1,Double_t x2, Double_t  y2, Color_t color, Short_t bordersize, Short_t bordermode)
           :TPad(name,title,0.1,0.1,0.9,0.9,color,bordersize,bordermode)
{
   // Slider normal constructor.
   //
   //   x1,y1,x2,y2 are in pad user coordinates

   Double_t x1pad = gPad->GetX1();
   Double_t x2pad = gPad->GetX2();
   Double_t y1pad = gPad->GetY1();
   Double_t y2pad = gPad->GetY2();
   Double_t xmin  = (x1-x1pad)/(x2pad-x1pad);
   Double_t ymin  = (y1-y1pad)/(y2pad-y1pad);
   Double_t xmax  = (x2-x1pad)/(x2pad-x1pad);
   Double_t ymax  = (y2-y1pad)/(y2pad-y1pad);
   SetPad(xmin,ymin,xmax,ymax);
   Range(0,0,1,1);

   SetBit(kCanDelete);
   Modified(kTRUE);

   fMinimum = 0;
   fMaximum = 1;
   fObject  = 0;
   fMethod  = "";
   Double_t dx = PixeltoX(bordersize);
   Double_t dy = PixeltoY(-bordersize);
   TSliderBox *sbox = new TSliderBox(dx,dy,1-dx,1-dy,color,bordersize,-bordermode);
   sbox->SetSlider(this);
   fPrimitives->Add(sbox);
   AppendPad();
}


//______________________________________________________________________________
TSlider::~TSlider()
{
   // slider default destructor.
}


//______________________________________________________________________________
void TSlider::Paint(Option_t *option)
{
   // Paint this slider with its current attributes.

   TPad::Paint(option);
}


//______________________________________________________________________________
void TSlider::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
{
   // Save primitive as a C++ statement(s) on output stream out

   TPad *padsav = (TPad*)gPad;
   char quote = '"';
   if (gROOT->ClassSaved(TSlider::Class())) {
      out<<"   ";
   } else {
      out<<"   TSlider *";
   }
   out<<"slider = new TSlider("<<quote<<GetName()<<quote<<", "<<quote<<GetTitle()
      <<quote
      <<","<<fXlowNDC
      <<","<<fYlowNDC
      <<","<<fXlowNDC+fWNDC
      <<","<<fYlowNDC+fHNDC
      <<");"<<std::endl;

   SaveFillAttributes(out,"slider",0,1001);
   SaveLineAttributes(out,"slider",1,1,1);

   if (GetBorderSize() != 2) {
      out<<"   slider->SetBorderSize("<<GetBorderSize()<<");"<<std::endl;
   }
   if (GetBorderMode() != -1) {
      out<<"   slider->SetBorderMode("<<GetBorderMode()<<");"<<std::endl;
   }
   Int_t lenMethod = strlen(GetMethod());
   if (lenMethod > 0) {
      out<<"   slider->SetMethod("<<quote<<GetMethod()<<quote<<");"<<std::endl;
   }

   out<<"   "<<padsav->GetName()<<"->cd();"<<std::endl;
   padsav->cd();
}


//______________________________________________________________________________
void TSlider::SetRange(Double_t xmin, Double_t xmax)
{
//*-*-*-*-*-*-*-*-*-*-*Set Slider range in [0,1]*-*-*-*-*
//*-*                  =========================

   TSliderBox *sbox = (TSliderBox*)fPrimitives->FindObject("TSliderBox");
   if (sbox) {
      if (fAbsWNDC > fAbsHNDC) {
         sbox->SetX1(xmin);
         sbox->SetX2(xmax);
      } else {
         sbox->SetY1(xmin);
         sbox->SetY2(xmax);
      }
   }
   fMinimum = xmin;
   fMaximum = xmax;
   Modified();
}
 TSlider.cxx:1
 TSlider.cxx:2
 TSlider.cxx:3
 TSlider.cxx:4
 TSlider.cxx:5
 TSlider.cxx:6
 TSlider.cxx:7
 TSlider.cxx:8
 TSlider.cxx:9
 TSlider.cxx:10
 TSlider.cxx:11
 TSlider.cxx:12
 TSlider.cxx:13
 TSlider.cxx:14
 TSlider.cxx:15
 TSlider.cxx:16
 TSlider.cxx:17
 TSlider.cxx:18
 TSlider.cxx:19
 TSlider.cxx:20
 TSlider.cxx:21
 TSlider.cxx:22
 TSlider.cxx:23
 TSlider.cxx:24
 TSlider.cxx:25
 TSlider.cxx:26
 TSlider.cxx:27
 TSlider.cxx:28
 TSlider.cxx:29
 TSlider.cxx:30
 TSlider.cxx:31
 TSlider.cxx:32
 TSlider.cxx:33
 TSlider.cxx:34
 TSlider.cxx:35
 TSlider.cxx:36
 TSlider.cxx:37
 TSlider.cxx:38
 TSlider.cxx:39
 TSlider.cxx:40
 TSlider.cxx:41
 TSlider.cxx:42
 TSlider.cxx:43
 TSlider.cxx:44
 TSlider.cxx:45
 TSlider.cxx:46
 TSlider.cxx:47
 TSlider.cxx:48
 TSlider.cxx:49
 TSlider.cxx:50
 TSlider.cxx:51
 TSlider.cxx:52
 TSlider.cxx:53
 TSlider.cxx:54
 TSlider.cxx:55
 TSlider.cxx:56
 TSlider.cxx:57
 TSlider.cxx:58
 TSlider.cxx:59
 TSlider.cxx:60
 TSlider.cxx:61
 TSlider.cxx:62
 TSlider.cxx:63
 TSlider.cxx:64
 TSlider.cxx:65
 TSlider.cxx:66
 TSlider.cxx:67
 TSlider.cxx:68
 TSlider.cxx:69
 TSlider.cxx:70
 TSlider.cxx:71
 TSlider.cxx:72
 TSlider.cxx:73
 TSlider.cxx:74
 TSlider.cxx:75
 TSlider.cxx:76
 TSlider.cxx:77
 TSlider.cxx:78
 TSlider.cxx:79
 TSlider.cxx:80
 TSlider.cxx:81
 TSlider.cxx:82
 TSlider.cxx:83
 TSlider.cxx:84
 TSlider.cxx:85
 TSlider.cxx:86
 TSlider.cxx:87
 TSlider.cxx:88
 TSlider.cxx:89
 TSlider.cxx:90
 TSlider.cxx:91
 TSlider.cxx:92
 TSlider.cxx:93
 TSlider.cxx:94
 TSlider.cxx:95
 TSlider.cxx:96
 TSlider.cxx:97
 TSlider.cxx:98
 TSlider.cxx:99
 TSlider.cxx:100
 TSlider.cxx:101
 TSlider.cxx:102
 TSlider.cxx:103
 TSlider.cxx:104
 TSlider.cxx:105
 TSlider.cxx:106
 TSlider.cxx:107
 TSlider.cxx:108
 TSlider.cxx:109
 TSlider.cxx:110
 TSlider.cxx:111
 TSlider.cxx:112
 TSlider.cxx:113
 TSlider.cxx:114
 TSlider.cxx:115
 TSlider.cxx:116
 TSlider.cxx:117
 TSlider.cxx:118
 TSlider.cxx:119
 TSlider.cxx:120
 TSlider.cxx:121
 TSlider.cxx:122
 TSlider.cxx:123
 TSlider.cxx:124
 TSlider.cxx:125
 TSlider.cxx:126
 TSlider.cxx:127
 TSlider.cxx:128
 TSlider.cxx:129
 TSlider.cxx:130
 TSlider.cxx:131
 TSlider.cxx:132
 TSlider.cxx:133
 TSlider.cxx:134
 TSlider.cxx:135
 TSlider.cxx:136
 TSlider.cxx:137
 TSlider.cxx:138
 TSlider.cxx:139
 TSlider.cxx:140
 TSlider.cxx:141
 TSlider.cxx:142
 TSlider.cxx:143
 TSlider.cxx:144
 TSlider.cxx:145
 TSlider.cxx:146
 TSlider.cxx:147
 TSlider.cxx:148
 TSlider.cxx:149
 TSlider.cxx:150
 TSlider.cxx:151
 TSlider.cxx:152
 TSlider.cxx:153
 TSlider.cxx:154
 TSlider.cxx:155
 TSlider.cxx:156
 TSlider.cxx:157
 TSlider.cxx:158
 TSlider.cxx:159
 TSlider.cxx:160
 TSlider.cxx:161
 TSlider.cxx:162
 TSlider.cxx:163
 TSlider.cxx:164
 TSlider.cxx:165
 TSlider.cxx:166
 TSlider.cxx:167
 TSlider.cxx:168
 TSlider.cxx:169
 TSlider.cxx:170
 TSlider.cxx:171
 TSlider.cxx:172
 TSlider.cxx:173
 TSlider.cxx:174
 TSlider.cxx:175
 TSlider.cxx:176
 TSlider.cxx:177
 TSlider.cxx:178
 TSlider.cxx:179
 TSlider.cxx:180
 TSlider.cxx:181
 TSlider.cxx:182
 TSlider.cxx:183
 TSlider.cxx:184
 TSlider.cxx:185
 TSlider.cxx:186
 TSlider.cxx:187
 TSlider.cxx:188
 TSlider.cxx:189
 TSlider.cxx:190
 TSlider.cxx:191
 TSlider.cxx:192
 TSlider.cxx:193
 TSlider.cxx:194
 TSlider.cxx:195
 TSlider.cxx:196
 TSlider.cxx:197
 TSlider.cxx:198
 TSlider.cxx:199
 TSlider.cxx:200
 TSlider.cxx:201
 TSlider.cxx:202
 TSlider.cxx:203
 TSlider.cxx:204
 TSlider.cxx:205
 TSlider.cxx:206
 TSlider.cxx:207
 TSlider.cxx:208
 TSlider.cxx:209
 TSlider.cxx:210
 TSlider.cxx:211
 TSlider.cxx:212
 TSlider.cxx:213
 TSlider.cxx:214
 TSlider.cxx:215
 TSlider.cxx:216
 TSlider.cxx:217
 TSlider.cxx:218
 TSlider.cxx:219
 TSlider.cxx:220
 TSlider.cxx:221
 TSlider.cxx:222